-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
579 lines (519 loc) · 17.9 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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
import { posix as path } from "path";
import { statSync } from "fs";
import { cosmiconfigSync } from "cosmiconfig";
import chalk from "chalk";
import devserverProxy from "./lib/devserver-proxy.js";
import MiniCssExtractPlugin from "mini-css-extract-plugin";
import { EsbuildPlugin } from "esbuild-loader";
import CopyPlugin from "copy-webpack-plugin";
import { BundleAnalyzerPlugin } from "webpack-bundle-analyzer";
import ImageMinimizerPlugin from "image-minimizer-webpack-plugin";
import DependencyManifestPlugin from "./lib/DependencyManifestPlugin.js";
import AfterDoneReporterPlugin from "./lib/AfterDoneReporterPlugin.js";
import WatchRunReporterPlugin from "./lib/WatchRunReporterPlugin.js";
import autoprefixer from "autoprefixer";
import cssnano from "cssnano";
/**
* Sass flavors for conditional sass-loader implementations
*/
import * as nodeSass from "sass";
import * as dartSass from "sass-embedded";
// Experimenting with this
import DependencyExtractionWebpackPlugin from "@wordpress/dependency-extraction-webpack-plugin";
/**
* Force `mode: production` when running the analyzer
* TODO: webpack5 changed env in here, might need to change WEBPACK_BUNDLE_ANALYZER
*/
if (process.env.WEBPACK_BUNDLE_ANALYZER) process.env.NODE_ENV = "production";
const isProduction = process.env.NODE_ENV === "production";
const stats = {
preset: "normal",
cachedAssets: false,
assets: true,
// assetsSpace: 12,
// context: new URL( import.meta.url).pathname,
all: false,
// assets: true,
// builtAt: true,
// cachedModules: true,
// children: false, // Adds a bunch of blank lines to stats output
// chunkGroups: false,
// chunkModules: false,
// chunkOrigins: true,
// chunkRelations: true,
// chunks: false,
// colors: true,
// depth: false,
// env: true,
// orphanModules: false,
// dependentModules: true,
modules: false,
groupAssetsByChunk: true,
entrypoints: true,
// // errorDetails: "auto",
children: false,
// errorDetails: true,
// errors: true,
// errorStack: true,
// excludeAssets: [/hot-update/, /_sock-/],
// groupAssetsByChunk: true,
logging: "info",
// optimizationBailout: true,
// loggingTrace: false,
performance: true,
reasons: true,
relatedAssets: true,
timings: true,
version: true,
warnings: true,
};
const siteDir = new URL("../site", import.meta.url).pathname;
// const explorerSync = cosmiconfigSync("ideasonpurpose");
// const configFile = explorerSync.search(siteDir);
import buildConfig from "./lib/buildConfig.js";
import siteConfig from "../site/ideasonpurpose.config.js";
// console.log({configFile})
// const config = buildConfig(configFile ?? siteConfig);
const config = buildConfig({ config: siteConfig });
/**
* `usePolling` is a placeholder, try and detect native Windows Docker mounts
* since they don't support file-watching (no inotify events), if there's
* something clean, use that instead. For now, this will force-enable polling.
*
* TODO: Why so much dancing around defaults when this could just inherit from default.config?
*/
const usePolling = Boolean(config.usePolling);
const pollInterval = Math.max(
parseInt(config.pollInterval, 10) || parseInt(config.usePolling, 10) || 400,
400
);
const devtool = config.devtool || false;
config.esTarget = config.esTarget || "es2020"; // was "es2015"
// console.log({config})
export default async (env, argv) => {
return {
// stats,
module: {
rules: [
// {
// test: /\.(js|jsx|mjs)$/,
// include: [
// path.resolve(config.src),
// path.resolve("../tools/node_modules"),
// path.resolve("../site/node_modules"),
// ],
// exclude: function (module) {
// const moduleRegex = new RegExp(
// `node_modules/(${config.transpileDependencies.join("|")})`
// );
// return /node_modules/.test(module) && !moduleRegex.test(module);
// },
// /**
// * EXPERIMENTAL!!
// * If JS compilation breaks, try reverting this first.
// */
// loader: "esbuild-loader",
// options: {
// loader: "jsx",
// target: "es2015",
// },
/**
* Updated again 2024-05 even simpler
*/
{
test: /\.[jt]sx?$/,
// test: /\.js$/,
loader: "esbuild-loader",
options: {
loader: "jsx",
target: config.esTarget,
},
},
// use: {
// loader: "babel-loader",
// options: {
// cacheDirectory: !isProduction,
// sourceType: "unambiguous",
// plugins: [
// "@babel/plugin-syntax-dynamic-import",
// ...(isProduction
// ? []
// : ["@babel/plugin-transform-react-jsx-source"]),
// ],
// presets: [
// [
// "@babel/preset-env",
// {
// forceAllTransforms: true,
// useBuiltIns: "usage",
// configPath: config.src,
// corejs: 3,
// modules: false,
// debug: false,
// },
// ],
// "@babel/preset-react",
// ],
// },
// },
// },
{
test: /\.(scss|css)$/,
use: [
{ loader: MiniCssExtractPlugin.loader },
{
loader: "css-loader",
options: {
import: false, // imports already handled by Sass or PostCSS
// sourceMap: !isProduction,
},
},
{
loader: "postcss-loader",
options: {
postcssOptions: {
plugins: isProduction
? [
autoprefixer,
cssnano({ preset: ["default", { colormin: false }] }),
]
: [autoprefixer],
},
},
},
// {
// loader: "resolve-url-loader",
// options: {
// // sourceMap: true,
// // debug: true,
// },
// },
{
loader: "sass-loader",
options: {
implementation: config.sass === "sass" ? nodeSass : dartSass,
// implementation: await import(config.sass),
sourceMap: !isProduction,
warnRuleAsWarning: true,
webpackImporter: false,
sassOptions: {
includePaths: [
path.resolve(config.src, "sass"),
path.resolve("../site/node_modules"),
],
// loadPaths: [
// path.resolve(config.src, "sass"),
// path.resolve("../site/node_modules"),
// ],
style: "expanded",
verbose: true,
},
},
},
],
},
/**
* This image loader is specifically for images which are required or
* imported into a webpack processed entry file. Optimization is
* handled by image-minimizer-webpack-plugin. These assets will be
* renamed with a chunkhash fragment.
*
* All images under `config.src` will be optimized and copied by
* copy-webpack-plugin but will keep their original filenames and
* relative paths. Images included in SCSS files will be processed
* twice, once with a hashed name and again with its original name.
*/
{
// test: /\.(jpe?g|png|gif|tif|webp|svg|avif)$/i,
test: /\.(jpe?g|png|gif|tif|webp|avif)$/i,
type: "asset",
},
/**
* SVGs can be imported as asset urls or React components
*
* To import an SVG file as a src url, append ?url to the filename:
* import svg from './assets/file.svg?url'
*
* To import an SVG file as a React component
* @link https://react-svgr.com/docs/webpack/#use-svgr-and-asset-svg-in-the-same-project
*/
{
test: /\.svg$/i,
type: "asset",
resourceQuery: /url/, // *.svg?url
},
{
test: /\.svg$/i,
issuer: /\.[jt]sx?$/,
resourceQuery: { not: [/url/] }, // exclude react component if *.svg?url
use: ["@svgr/webpack"],
},
// {
// loader: "svgo-loader",
// options: {
// multipass: true,
// js2svg: {
// indent: 2,
// pretty: true,
// },
// },
// },
// ],
// },
{
test: /fonts\/.*\.(ttf|eot|woff2?)$/i,
type: "asset",
},
],
},
context: path.resolve(config.src),
resolve: {
modules: [
path.resolve("../tools/node_modules"),
path.resolve("../site/node_modules"),
path.resolve("./node_modules"),
],
},
resolveLoader: {
modules: [
path.resolve("../tools/node_modules"),
path.resolve("./node_modules"), // for local development when running outside of Docker
],
},
entry: config.entry,
output: {
path: new URL(config.dist, import.meta.url).pathname,
/**
* Primary output filenames SHOULD NOT include hashes in development because
* some files are written to disk from devServer middleware. Because those
* files are written outside standard webpack output, they aren't cleaned
* up by standard webpack cleaning functions.
*/
filename: isProduction ? "[name]-[contenthash:8].js" : "[name].js",
chunkFilename: "[id]-[chunkhash:8].js",
publicPath: config.publicPath,
/**
* Assets are not cleaned when writeToDisk is true in devServer
* Works correctly with builds.
* @link https://github.com/webpack/webpack-dev-middleware/issues/861
*/
clean: true,
},
devServer: {
host: "0.0.0.0",
allowedHosts: "all",
setupExitSignals: true,
// server: 'https', // https://webpack.js.org/configuration/dev-server/#devserverserver
compress: config.devServerCompress || false, // TODO: True by default in devServer v4, exposed via config.devServerCompress to test speed impact
port: 8080, // hardcoded because Docker bridges to this port
hot: true,
client: {
logging: "error", // TODO: New, is this ok?
overlay: { warnings: true, errors: true },
progress: true, // TODO: New, is this ok?
reconnect: 30,
webSocketURL: {
port: parseInt(process.env.PORT), // external port, so websockets hit the right endpoint
},
},
webSocketServer: "ws",
static: {
// TODO: Should contentBase be `false` when there's a proxy?
directory: path.join("/usr/src/site/", config.contentBase),
/*
* TODO: Poll options were enabled as a workaround for Docker-win volume inotify
* issues. Looking to make this conditional...
* Maybe defined `isWindows` or `hasiNotify` for assigning a value
* Placeholder defined at the top of the file.
* For now, `usePolling` is a boolean (set to true)
* ref: https://github.com/docker/for-win/issues/56
* https://www.npmjs.com/package/is-windows
* TODO: Safe to remove?
* TODO: Test on vanilla Windows (should now work in WSL)
*/
watch: {
poll: usePolling && pollInterval,
ignored: ["node_modules", "vendor"],
},
},
devMiddleware: {
index: false, // enable root proxying
writeToDisk: (filePath) => {
/**
* Note: If this is an async function, it will write everything to disk
*
* Never write hot-update files to disk.
*/
if (/.+hot-update\.(js|json|js\.map)$/.test(filePath)) {
return false;
}
// SHORT_CIRCUIT FOR TESTING
return true;
if (/.+\.(svg|json|php|jpg|png)$/.test(filePath)) {
const fileStat = statSync(filePath, { throwIfNoEntry: false });
/**
* Always write SVG, PHP & JSON files
*/
if (/.+\.(svg|json|php)$/.test(filePath)) {
return true;
} else {
/**
* Write any images under 100k and anything not yet on disk
*/
if (!fileStat || fileStat.size < 100 * 1024) {
return true;
}
/**
* TODO: This might all be unnecessary. Webpack seems to be doing a good job with its native caching
*/
// const randOffset = Math.random() * 300000; // 0-5 minutes
// const expired = new Date() - fileStat.mtime > randOffset;
// const relPath = filePath.replace(config.dist, "dist");
// if (expired) {
// console.log("DEBUG writeToDisk:", { replacing: relPath });
// return true;
// }
// console.log("DEBUG writeToDisk:", { cached: relPath });
}
}
return false;
},
},
// NOTE: trying to make injection conditional so wp-admin stops reloading
// injectClient: compilerConfig => {
// console.log(compilerConfig);
// return true;
// },
// onBeforeSetupMiddleware: function (devServer) {
setupMiddlewares: (middlewares, devServer) => {
if (!devServer) {
throw new Error("webpack-dev-server is not defined");
}
/**
* The `/inform` route is an annoying bit of code. Here's why:
* Ubiquity Wi-fi hardware frequently spams the shit out of their
* networks, specifically requesting the `/inform` route from
* every device. We still have some Ubiquity hardware on our
* networks, so dev servers were constantly responding to
* `/inform` requests with 404s, filling logs and cluttering
* terminals. So that's why this is here. I hate it.
*/
devServer.app.all("/inform", () => false);
/**
* The "/webpack/reload" endpoint will trigger a full devServer refresh
* Originally from our Browsersync implementation:
*
* https://github.com/ideasonpurpose/wp-theme-init/blob/ad8039c9757ffc3a0a0ed0adcc616a013fdc8604/src/ThemeInit.php#L202
*/
devServer.app.get("/webpack/reload", (req, res) => {
console.log(
chalk.yellow("Reload triggered by request to /webpack/reload")
);
devServer.sendMessage(
devServer.webSocketServer.clients,
"content-changed"
);
res.json({ status: "Reloading!" });
});
return middlewares;
},
watchFiles: {
paths: [
path.resolve(config.src, "../**/*.{php,json}"), // WordPress
// path.resolve(config.src, `../${config.contentBase}/*.html`), // Jekyll
],
options: {
ignored: ["**/.git/**", "**/vendor/**", "**/node_modules/**"],
ignoreInitial: true,
ignorePermissionErrors: true,
usePolling,
interval: pollInterval,
},
},
...(await devserverProxy(config)),
},
mode: isProduction ? "production" : "development",
stats,
performance: {
hints: isProduction ? "warning" : false,
},
devtool,
infrastructureLogging: {
colors: true,
level: isProduction ? "warn" : "error",
// level: "info",
},
plugins: [
new MiniCssExtractPlugin({
filename: isProduction ? "[name]-[contenthash:8].css" : "[name].css",
}),
new CopyPlugin({
patterns: [
{
from: "**/*",
globOptions: {
dot: true, // TODO: Dangerous? Why is this ever necessary?!
ignore: [
"**/{.gitignore,.DS_Store,*:Zone.Identifier}",
config.src + "/{block,blocks,fonts,js,sass}/**",
],
},
},
{
from: config.src + "/{block,blocks}/**/block.json",
noErrorOnMissing: true,
}, // re-add block.json files for loading from PHP
],
options: { concurrency: 50 },
}),
/**
* @link https://developer.wordpress.org/block-editor/reference-guides/packages/packages-dependency-extraction-webpack-plugin/
*/
new DependencyExtractionWebpackPlugin(),
new DependencyManifestPlugin({
writeManifestFile: true,
manifestFile: config.manifestFile,
}),
new WatchRunReporterPlugin(),
new AfterDoneReporterPlugin({
echo: env && env.WEBPACK_SERVE,
message:
"Dev site " + chalk.blue.bold(`http://localhost:${process.env.PORT}`),
}),
new BundleAnalyzerPlugin({
analyzerMode: isProduction ? "static" : "disabled",
openAnalyzer: false,
reportFilename: path.resolve(siteDir, "webpack/stats/index.html"),
}),
],
optimization: {
splitChunks: {
chunks: "all",
},
minimizer: [
new EsbuildPlugin({
target: config.esTarget,
css: true,
}),
new ImageMinimizerPlugin({
severityError: "error",
minimizer: {
implementation: ImageMinimizerPlugin.sharpMinify,
options: {
/**
* Sharp options
*/
encodeOptions: {
jpeg: {
quality: 70,
mozjpeg: true,
},
png: {},
},
},
},
}),
],
},
};
};