-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
184 lines (171 loc) · 3.88 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
180
181
182
183
184
import { dirname, join, resolve } from "path";
import { fileURLToPath } from "url";
import webpack from "webpack";
import MiniCssExtractPlugin from "mini-css-extract-plugin";
import HtmlPlugin from "html-webpack-plugin";
import ReactRefreshPlugin from "@pmmmwh/react-refresh-webpack-plugin";
import TerserPlugin from "terser-webpack-plugin";
import { BundleAnalyzerPlugin } from "webpack-bundle-analyzer";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// css-minimizer-webpack-plugin is ineffective (index.css 27898 bytes -> 27540 bytes), so we not use it.
export default function (env) {
const shouldReport = Boolean(env?.report);
const isProd = Boolean(env?.production);
const isDevelopment = !isProd;
function cssLoaderChain(cssModules) {
const outputLoader = isProd ? MiniCssExtractPlugin.loader : "style-loader";
const cssLoader = {
loader: "css-loader",
options: {
importLoaders: 1,
},
};
if (cssModules) {
cssLoader.options.modules = {
hashStrategy: "minimal-subset",
namedExport: false,
localIdentName: isProd ? "[hash:base64:5]" : "[local]_[hash:base64:5]",
};
}
return [outputLoader, cssLoader, "sass-loader"];
}
const loaders = [
{
test: /\.tsx?$/,
use: "swc-loader",
},
{
test: /\.wasm$/,
type: "asset/resource",
},
{
test: /\.svg$/,
use: {
loader: "@svgr/webpack",
options: {
svgoConfig: {
plugins: [
{
name: "preset-default",
params: {
overrides: {
removeViewBox: false,
},
},
},
],
},
svgProps: {
fill: "currentColor",
width: "1em",
height: "1em",
},
},
},
},
{
test: /\.(?:jpg|png|gif)$/,
type: "asset/resource",
},
{
test: /\.s?css$/,
oneOf: [
{
include: [
join(__dirname, "web", "app"),
join(__dirname, "web", "ui"),
join(__dirname, "web", "form"),
],
use: cssLoaderChain(true),
},
{
test: /\.module\.s?css/,
use: cssLoaderChain(true),
},
{
use: cssLoaderChain(false),
},
],
},
];
const plugins = [
new webpack.EnvironmentPlugin({ SENTRY_DSN: null }),
new HtmlPlugin({ template: "web/index.html" }),
shouldReport && new BundleAnalyzerPlugin({
openAnalyzer: false,
analyzerMode: "static",
}),
isProd && new MiniCssExtractPlugin({
filename: "s/[name].css",
}),
isDevelopment && new ReactRefreshPlugin(),
];
return {
mode: isProd ? "production" : "development",
context: __dirname,
entry: {
check: "./web/support-check.ts",
index: "./web/index.tsx",
},
output: {
filename: "s/[name].js",
assetModuleFilename: "s/[name][ext]",
clean: true,
hashFunction: "xxhash64",
},
devtool: isProd ? "source-map" : "cheap-module-source-map",
resolve: {
extensions: [".tsx", ".ts", ".js", ".json"],
alias: {
squoosh: join(__dirname, "deps/squoosh"),
},
fallback: {
path: false,
fs: false,
crypto: false,
module: false,
// Required by wasm-feature-detect
worker_threads: false,
},
},
module: {
rules: loaders,
},
plugins: plugins.filter(Boolean),
optimization: {
minimizer: [
new TerserPlugin({ minify: TerserPlugin.swcMinify }),
],
splitChunks: {
chunks: "all",
},
// Set it to single fixes hot reloading with multiple entries.
// https://github.com/webpack/webpack-dev-server/issues/2792
runtimeChunk: "single",
},
cache: {
type: "filesystem",
buildDependencies: {
config: [
__filename,
resolve(__dirname, "tsconfig.json"),
],
},
},
performance: false,
stats: "minimal",
devServer: {
static: false,
hot: true,
client: {
logging: "none",
},
// Required by SharedArrayBuffer
headers: {
"Cross-Origin-Opener-Policy": "same-origin",
"Cross-Origin-Embedder-Policy": "require-corp",
},
},
};
}