-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
179 lines (161 loc) · 5.83 KB
/
webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Core Node modules
import path from "path";
import { fileURLToPath } from "url";
// Webpack Plugins
import HtmlWebpackPlugin from "html-webpack-plugin";
import MiniCssExtractPlugin from "mini-css-extract-plugin";
import CssMinimizerPlugin from "css-minimizer-webpack-plugin";
import ReactRefreshPlugin from "@pmmmwh/react-refresh-webpack-plugin";
import ReactRefreshBabelPlugin from "react-refresh/babel";
import DotEnvPlugin from "dotenv-webpack";
// Workaround for using `__dirname` inside an ESM
// Source: https://flaviocopes.com/fix-dirname-not-defined-es-module-scope/
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const buildOutDirectory = path.join(__dirname, "build");
const isDevelopment = process.env.NODE_ENV !== "production";
const browserTargets = "> 0.25%, not dead";
// Use ESM import/export syntax for webpack.config.js
// Source: https://stackoverflow.com/questions/72318969/how-to-export-the-configuration-of-webpack-config-js-using-pure-esm
export default {
mode: isDevelopment ? "development" : "production",
entry: "./src/index.tsx",
module: {
rules: [
{
test: /\.(tsx|ts|jsx|js)$/,
exclude: "/node_modules/",
use: {
loader: "babel-loader",
options: {
presets: [
// Transpile JavaScript to be compatible with
// the least qualified target environment; polyfilling
// missing API functionality with `core-js`.
[
"@babel/preset-env",
{
targets: browserTargets,
useBuiltIns: "usage",
corejs: "3.30.0",
},
],
// Transpile JSX to JavaScript
[
"@babel/preset-react",
{
// Use the new transform so JSX/TSX files
// don't have to manually import the React library.
// Source: https://legacy.reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html
runtime: "automatic",
},
],
// Transpile TypeScript to JavaScript
"@babel/preset-typescript",
],
// Source: https://webpack.js.org/loaders/babel-loader/#exclude-libraries-that-should-not-be-transpiled
exclude: [
// \\ for Windows, / for macOS and Linux
/node_modules[\\/]core-js/,
/node_modules[\\/]webpack[\\/]buildin/,
],
// Source: https://stackoverflow.com/a/68352125
sourceType: "unambiguous",
// Source: https://github.com/pmmmwh/react-refresh-webpack-plugin#usage
plugins: [isDevelopment && ReactRefreshBabelPlugin].filter(Boolean),
},
},
},
{
test: /\.(css|scss)$/,
use: [
// It's recommended to use `style-loader` for development
// since it's quicker.
// Source: https://webpack.js.org/plugins/mini-css-extract-plugin/#recommended
isDevelopment ? "style-loader" : MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: {
// Source: https://webpack.js.org/loaders/css-loader/#importloaders
importLoaders: 1,
},
},
{
loader: "postcss-loader",
options: {
postcssOptions: {
plugins: [
[
"postcss-preset-env",
{
// Source: https://github.com/csstools/postcss-plugins/tree/main/plugin-packs/postcss-preset-env#browsers
browsers: browserTargets,
},
],
],
},
},
},
"sass-loader",
],
},
// Use `asset/resource` module type to load
// background pattern svgs.
// "emits a separate file and exports the URL"
// Source: https://webpack.js.org/guides/asset-modules/
{
test: /curve-line.*\.svg$/,
type: "asset/resource",
},
// Use `asset/source` module type to load
// flag svgs (need to inline flag svgs).
// "exports the source code of the asset"
// Source: https://webpack.js.org/guides/asset-modules/
{
test: /flags\/(\w{2})\.svg$/,
type: "asset/source",
},
],
},
optimization: {
// [Webpack 5] The "..." syntax extends OOTB
// included minimizers (e.g. `terser-webpack-plugin`)
minimizer: ["...", new CssMinimizerPlugin()],
},
plugins: [
new DotEnvPlugin(),
new HtmlWebpackPlugin({
// Pass in your own `index.html` where
// webpack will automatically inject the
// bundled assets into.
template: "index.html",
}),
!isDevelopment && new MiniCssExtractPlugin(),
// Source: https://github.com/pmmmwh/react-refresh-webpack-plugin#usage
isDevelopment &&
// Explicitly enable react-refresh's error overlay
// NOTE: "wds" is the default value for `overlay.sockIntegration`,
// but wanted to include to make it clear that this is the source
// of the error overlay.
// Source: https://github.com/pmmmwh/react-refresh-webpack-plugin/blob/main/docs/API.md#sockintegration
new ReactRefreshPlugin({ overlay: { sockIntegration: "wds" } }),
].filter(Boolean),
resolve: {
extensions: [".tsx", ".jsx", ".ts", ".js"],
},
output: {
filename: "main.bundle.js",
path: buildOutDirectory,
},
devServer: {
static: buildOutDirectory,
client: {
// Disable webpack-dev-server's default error overlay in favor of
// react-refresh's
// Source: https://webpack.js.org/configuration/dev-server/#overlay
overlay: false,
},
hot: true,
compress: true,
port: 8080,
},
};