-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathwebpack.config.js
More file actions
378 lines (344 loc) · 10.9 KB
/
webpack.config.js
File metadata and controls
378 lines (344 loc) · 10.9 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// const path = require('path');
const path = require("path");
const { composePlugins, withNx } = require("@nx/webpack");
const { withReact } = require("@nx/react");
const { merge } = require("webpack-merge");
require("dotenv").config({
// resolve the .env file in the root of the project ../
path: path.resolve(__dirname, "../.env"),
});
// Use the project's webpack so resolution works (e.g. when @nx/webpack does not bundle webpack under node_modules).
const webpack = require("webpack");
const { EnvironmentPlugin, DefinePlugin } = webpack;
const TerserPlugin = require("terser-webpack-plugin");
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const RELEASE = require("./release").getReleaseName();
const css_prefix = "lsf-";
const mode = process.env.BUILD_MODULE ? "production" : process.env.NODE_ENV || "development";
const isDevelopment = mode !== "production";
const devtool = process.env.NODE_ENV === "production" ? "source-map" : "cheap-module-source-map";
const FRONTEND_HMR = process.env.FRONTEND_HMR === "true";
const FRONTEND_HOSTNAME = FRONTEND_HMR ? process.env.FRONTEND_HOSTNAME || "http://localhost:8010" : "";
const DJANGO_HOSTNAME = process.env.DJANGO_HOSTNAME || "http://localhost:8080";
const HMR_PORT = FRONTEND_HMR ? +new URL(FRONTEND_HOSTNAME).port : 8010;
const LOCAL_ENV = {
NODE_ENV: mode,
CSS_PREFIX: css_prefix,
RELEASE_NAME: RELEASE,
};
const BUILD = {
NO_MINIMIZE: isDevelopment || !!process.env.BUILD_NO_MINIMIZATION,
};
const plugins = [
new DefinePlugin({
"process.env.CSS_PREFIX": JSON.stringify(css_prefix),
}),
new EnvironmentPlugin(LOCAL_ENV),
];
const optimizer = () => {
const result = {
minimize: true,
minimizer: [],
};
if (mode === "production") {
result.minimizer.push(
new TerserPlugin({
parallel: true,
}),
new CssMinimizerPlugin({
parallel: true,
}),
);
}
if (BUILD.NO_MINIMIZE) {
result.minimize = false;
result.minimizer = undefined;
}
if (process.env.MODE?.startsWith("standalone")) {
result.runtimeChunk = false;
result.splitChunks = { cacheGroups: { default: false } };
}
return result;
};
// Nx plugins for webpack.
module.exports = composePlugins(
withNx({
nx: {
svgr: false,
},
skipTypeChecking: true,
}),
withReact({ svgr: false }),
(config) => {
// Remove the extension alias as this conflicts with the nx/webpack v21 changes
delete config.resolve.extensionAlias;
// LS entrypoint
if (!process.env.MODE?.startsWith("standalone")) {
config.entry = {
main: {
import: path.resolve(__dirname, "apps/labelstudio/src/main.tsx"),
},
};
config.output = {
...config.output,
uniqueName: "labelstudio",
publicPath:
isDevelopment && FRONTEND_HOSTNAME
? `${FRONTEND_HOSTNAME}/react-app/`
: process.env.MODE === "standalone-playground"
? "/playground-assets/"
: "auto",
scriptType: "text/javascript",
};
config.optimization = {
runtimeChunk: "single",
sideEffects: true,
splitChunks: {
cacheGroups: {
commonVendor: {
test: /[\\/]node_modules[\\/](react|react-dom|react-router|react-router-dom|mobx|mobx-react|mobx-react-lite|mobx-state-tree)[\\/]/,
name: "vendor",
chunks: "all",
},
defaultVendors: {
test: /[\\/]node_modules[\\/]/,
priority: -10,
reuseExistingChunk: true,
chunks: "async",
},
default: {
minChunks: 2,
priority: -20,
reuseExistingChunk: true,
chunks: "async",
},
},
},
};
}
config.resolve.fallback = {
fs: false,
path: false,
crypto: false,
worker_threads: false,
};
config.experiments = {
cacheUnaffected: true,
syncWebAssembly: true,
asyncWebAssembly: true,
};
config.module.rules.forEach((rule) => {
if (!rule.oneOf || !rule.test?.toString().includes("css")) return;
rule.oneOf.forEach((oneOfRule) => {
if (!oneOfRule.use) return;
oneOfRule.use = oneOfRule.use.filter(
(use) => !(use.loader && /sass-loader|stylus-loader|less-loader/.test(use.loader)),
);
const innerTest = oneOfRule.test?.toString() ?? "";
const cssLoader = oneOfRule.use.find((use) => use.loader?.includes("/css-loader/"));
if (innerTest.includes("module") && cssLoader?.options) {
cssLoader.options.modules = {
mode: "local",
auto: true,
namedExport: false,
localIdentName: "[local]--[hash:base64:5]",
};
}
});
const insertions = [];
rule.oneOf.forEach((oneOfRule, idx) => {
if (!oneOfRule.test || !oneOfRule.use) return;
const t = oneOfRule.test.toString();
if (/^\/\\\.css\$\/$/.test(t) && oneOfRule.use.some((u) => u.loader?.includes("/css-loader/"))) {
insertions.push(idx);
}
});
for (let i = insertions.length - 1; i >= 0; i--) {
const idx = insertions[i];
const template = rule.oneOf[idx];
const prefixUse = template.use.map((u) => {
if (typeof u === "string") return u;
if (u.loader?.includes("/css-loader/")) {
return {
...u,
options: {
...(u.options ?? {}),
modules: {
localIdentName: `${css_prefix}[local]`,
getLocalIdent(_ctx, _ident, className) {
if (className.includes("ant")) return className;
},
},
},
};
}
return u;
});
rule.oneOf.splice(idx, 0, {
test: /\.prefix\.css$/,
include: template.include,
exclude: /node_modules/,
use: prefixUse,
});
}
rule.exclude = /tailwind\.css/;
});
// Force local @humansignal icon SVGs through svgr regardless of issuer.
const humansignalIconsSvgRule = {
test: /libs[\\/]ui[\\/]src[\\/]assets[\\/]icons[\\/].*\.svg(\?.*)?$/,
use: [
{
loader: "@svgr/webpack",
options: {
ref: true,
exportType: "named",
namedExport: "ReactComponent",
svgo: false,
},
},
path.resolve(__dirname, "tools/loaders/svg-source-loader.cjs"),
],
};
config.module.rules.unshift(humansignalIconsSvgRule);
const svgRule = {
test: /\.svg(\?.*)?$/,
exclude: /node_modules/,
oneOf: [
{
issuer: /\.[jt]sx?$/,
use: [
{
loader: "@svgr/webpack",
options: {
ref: true,
exportType: "named",
namedExport: "ReactComponent",
svgo: false, // avoid parse errors with resolved svgo >=3.3.3
},
},
path.resolve(__dirname, "tools/loaders/svg-source-loader.cjs"),
],
},
{
type: "asset/resource",
},
],
};
config.module.rules.unshift(svgRule);
// Ensure no other webpack rules process .svg and override svgr output
const isOurSvgRule = (rule) => rule === svgRule || rule === humansignalIconsSvgRule;
const addSvgExclude = (rule) => {
if (!rule || isOurSvgRule(rule)) return;
const testString = rule.test?.toString?.() ?? "";
if (!testString.includes("svg")) return;
const svgExclude = /\.svg(\?.*)?$/;
if (!rule.exclude) {
rule.exclude = svgExclude;
} else if (Array.isArray(rule.exclude)) {
rule.exclude = [...rule.exclude, svgExclude];
} else {
rule.exclude = [rule.exclude, svgExclude];
}
};
config.module.rules.forEach((rule) => {
addSvgExclude(rule);
if (Array.isArray(rule.oneOf)) {
rule.oneOf.forEach(addSvgExclude);
}
});
config.module.rules.push(
{
test: /\.xml$/,
exclude: /node_modules/,
type: "asset/resource",
},
{
test: /\.wasm$/,
type: "asset/resource",
generator: {
filename: "[name][ext]",
},
},
{
test: /\.(gif|png|jpe?g|webp)(\?.*)?$/,
type: "asset/resource",
generator: {
filename: "assets/[name]-[hash][ext]",
},
},
// tailwindcss
{
test: /tailwind\.css/,
exclude: /node_modules/,
use: [
"style-loader",
{
loader: "css-loader",
options: {
importLoaders: 1,
},
},
"postcss-loader",
],
},
);
if (isDevelopment) {
config.optimization = {
...config.optimization,
moduleIds: "named",
};
}
config.resolve.alias = {
...(config.resolve.alias ?? {}),
// Common dependencies across at least two sub-packages
react: path.resolve(__dirname, "node_modules/react"),
"react-dom": path.resolve(__dirname, "node_modules/react-dom"),
"react-joyride": path.resolve(__dirname, "node_modules/react-joyride"),
"@humansignal/ui": path.resolve(__dirname, "libs/ui"),
"@humansignal/core": path.resolve(__dirname, "libs/core"),
"@humansignal/icons$": path.resolve(__dirname, "libs/ui/src/assets/icons/index.ts"),
"@humansignal/shad": path.resolve(__dirname, "libs/ui/src/shad"),
"@humansignal/ui/lib": path.resolve(__dirname, "libs/ui/src/lib"),
};
return merge(config, {
devtool,
mode,
plugins,
optimization: optimizer(),
ignoreWarnings: [/Failed to parse source map/],
devServer: process.env.MODE?.startsWith("standalone")
? {}
: {
// Port for the Webpack dev server
port: HMR_PORT,
// Enable HMR
hot: true,
// Allow cross-origin requests from Django
headers: { "Access-Control-Allow-Origin": "*" },
static: {
directory: path.resolve(__dirname, "../label_studio/core/static/"),
publicPath: "/static/",
},
devMiddleware: {
publicPath: `${FRONTEND_HOSTNAME}/react-app/`,
},
allowedHosts: "all", // Allow access from Django's server
proxy: [
{
context: ["/api"],
target: `${DJANGO_HOSTNAME}/api`,
changeOrigin: true,
pathRewrite: { "^/api": "" },
secure: false,
},
{
context: ["/"],
target: `${DJANGO_HOSTNAME}`,
changeOrigin: true,
secure: false,
},
],
},
});
},
);