webpack 5.108.0 → 5.108.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/lib/CleanPlugin.js +8 -25
  2. package/lib/Compilation.js +0 -1
  3. package/lib/DefinePlugin.js +8 -19
  4. package/lib/ExternalModule.js +64 -19
  5. package/lib/ExternalModuleFactoryPlugin.js +37 -1
  6. package/lib/LazyBarrel.js +4 -3
  7. package/lib/NormalModule.js +49 -59
  8. package/lib/RuntimeTemplate.js +3 -3
  9. package/lib/bun/BunTargetPlugin.js +1 -7
  10. package/lib/container/ModuleFederationPlugin.js +9 -25
  11. package/lib/css/CssInjectStyleRuntimeModule.js +9 -24
  12. package/lib/css/CssLoadingRuntimeModule.js +12 -27
  13. package/lib/css/CssModulesPlugin.js +15 -31
  14. package/lib/debug/ProfilingPlugin.js +12 -1
  15. package/lib/deno/DenoTargetPlugin.js +1 -7
  16. package/lib/dependencies/CommonJsExportRequireDependency.js +3 -3
  17. package/lib/dependencies/CommonJsExportsDependency.js +2 -2
  18. package/lib/dependencies/CommonJsFullRequireDependency.js +1 -1
  19. package/lib/dependencies/CommonJsSelfReferenceDependency.js +1 -1
  20. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +22 -0
  21. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +1 -1
  22. package/lib/dependencies/HarmonyImportDependency.js +2 -0
  23. package/lib/dependencies/ImportMetaPlugin.js +9 -25
  24. package/lib/dependencies/ProvidedDependency.js +1 -1
  25. package/lib/esm/ModuleChunkLoadingRuntimeModule.js +10 -26
  26. package/lib/javascript/JavascriptModulesPlugin.js +38 -54
  27. package/lib/node/NodeTargetPlugin.js +1 -62
  28. package/lib/node/nodeBuiltins.js +80 -0
  29. package/lib/optimize/ConcatenatedModule.js +20 -30
  30. package/lib/optimize/RealContentHashPlugin.js +8 -24
  31. package/lib/runtime/LoadScriptRuntimeModule.js +9 -24
  32. package/lib/util/createHooksRegistry.js +35 -0
  33. package/lib/util/property.js +1 -1
  34. package/lib/wasm-async/AsyncWebAssemblyModulesPlugin.js +12 -29
  35. package/lib/web/JsonpChunkLoadingRuntimeModule.js +10 -25
  36. package/package.json +2 -1
  37. package/types.d.ts +84 -71
@@ -6,12 +6,13 @@
6
6
  "use strict";
7
7
 
8
8
  const { SyncWaterfallHook } = require("tapable");
9
- const Compilation = require("../Compilation");
9
+ /** @typedef {import("../Compilation")} Compilation */
10
10
  const { CSS_TYPE } = require("../ModuleSourceTypeConstants");
11
11
  const RuntimeGlobals = require("../RuntimeGlobals");
12
12
  const RuntimeModule = require("../RuntimeModule");
13
13
  const Template = require("../Template");
14
14
  const compileBooleanMatcher = require("../util/compileBooleanMatcher");
15
+ const createHooksRegistry = require("../util/createHooksRegistry");
15
16
  const { chunkHasCss } = require("./CssModulesPlugin");
16
17
 
17
18
  /** @typedef {import("../Chunk")} Chunk */
@@ -27,33 +28,7 @@ const { chunkHasCss } = require("./CssModulesPlugin");
27
28
  * @property {SyncWaterfallHook<[string, Chunk]>} linkInsert
28
29
  */
29
30
 
30
- /** @type {WeakMap<Compilation, CssLoadingRuntimeModulePluginHooks>} */
31
- const compilationHooksMap = new WeakMap();
32
-
33
31
  class CssLoadingRuntimeModule extends RuntimeModule {
34
- /**
35
- * @param {Compilation} compilation the compilation
36
- * @returns {CssLoadingRuntimeModulePluginHooks} hooks
37
- */
38
- static getCompilationHooks(compilation) {
39
- if (!(compilation instanceof Compilation)) {
40
- throw new TypeError(
41
- "The 'compilation' argument must be an instance of Compilation"
42
- );
43
- }
44
- let hooks = compilationHooksMap.get(compilation);
45
- if (hooks === undefined) {
46
- hooks = {
47
- createStylesheet: new SyncWaterfallHook(["source", "chunk"]),
48
- linkPreload: new SyncWaterfallHook(["source", "chunk"]),
49
- linkPrefetch: new SyncWaterfallHook(["source", "chunk"]),
50
- linkInsert: new SyncWaterfallHook(["source", "chunk"])
51
- };
52
- compilationHooksMap.set(compilation, hooks);
53
- }
54
- return hooks;
55
- }
56
-
57
32
  /**
58
33
  * @param {ReadOnlyRuntimeRequirements} runtimeRequirements runtime requirements
59
34
  */
@@ -597,4 +572,14 @@ class CssLoadingRuntimeModule extends RuntimeModule {
597
572
  }
598
573
  }
599
574
 
575
+ CssLoadingRuntimeModule.getCompilationHooks = createHooksRegistry(
576
+ () =>
577
+ /** @type {CssLoadingRuntimeModulePluginHooks} */ ({
578
+ createStylesheet: new SyncWaterfallHook(["source", "chunk"]),
579
+ linkPreload: new SyncWaterfallHook(["source", "chunk"]),
580
+ linkPrefetch: new SyncWaterfallHook(["source", "chunk"]),
581
+ linkInsert: new SyncWaterfallHook(["source", "chunk"])
582
+ })
583
+ );
584
+
600
585
  module.exports = CssLoadingRuntimeModule;
@@ -13,7 +13,7 @@ const {
13
13
  RawSource,
14
14
  ReplaceSource
15
15
  } = require("webpack-sources");
16
- const Compilation = require("../Compilation");
16
+ /** @typedef {import("../Compilation")} Compilation */
17
17
  const HotUpdateChunk = require("../HotUpdateChunk");
18
18
  const { CSS_IMPORT_TYPE, CSS_TYPE } = require("../ModuleSourceTypeConstants");
19
19
  const {
@@ -37,6 +37,7 @@ const JavascriptModulesPlugin = require("../javascript/JavascriptModulesPlugin")
37
37
  const ConcatenatedModule = require("../optimize/ConcatenatedModule");
38
38
  const { compareModulesByFullName } = require("../util/comparators");
39
39
  const createHash = require("../util/createHash");
40
+ const createHooksRegistry = require("../util/createHooksRegistry");
40
41
  const { getUndoPath } = require("../util/identifier");
41
42
  const memoize = require("../util/memoize");
42
43
  const { digestNonNumericOnlyWithFull } = require("../util/nonNumericOnlyHash");
@@ -180,39 +181,9 @@ const generatorValidationOptions = {
180
181
  baseDataPath: "generator"
181
182
  };
182
183
 
183
- /** @type {WeakMap<Compilation, CompilationHooks>} */
184
- const compilationHooksMap = new WeakMap();
185
-
186
184
  const PLUGIN_NAME = "CssModulesPlugin";
187
185
 
188
186
  class CssModulesPlugin {
189
- /**
190
- * Returns the attached hooks.
191
- * @param {Compilation} compilation the compilation
192
- * @returns {CompilationHooks} the attached hooks
193
- */
194
- static getCompilationHooks(compilation) {
195
- if (!(compilation instanceof Compilation)) {
196
- throw new TypeError(
197
- "The 'compilation' argument must be an instance of Compilation"
198
- );
199
- }
200
- let hooks = compilationHooksMap.get(compilation);
201
- if (hooks === undefined) {
202
- hooks = {
203
- renderModulePackage: new SyncWaterfallHook([
204
- "source",
205
- "module",
206
- "renderContext"
207
- ]),
208
- chunkHash: new SyncHook(["chunk", "hash", "context"]),
209
- orderModules: new SyncBailHook(["chunk", "modules", "compilation"])
210
- };
211
- compilationHooksMap.set(compilation, hooks);
212
- }
213
- return hooks;
214
- }
215
-
216
187
  constructor() {
217
188
  /** @type {WeakMap<Source, ModuleFactoryCacheEntry>} */
218
189
  this._moduleFactoryCache = new WeakMap();
@@ -1164,4 +1135,17 @@ class CssModulesPlugin {
1164
1135
  }
1165
1136
  }
1166
1137
 
1138
+ CssModulesPlugin.getCompilationHooks = createHooksRegistry(
1139
+ () =>
1140
+ /** @type {CompilationHooks} */ ({
1141
+ renderModulePackage: new SyncWaterfallHook([
1142
+ "source",
1143
+ "module",
1144
+ "renderContext"
1145
+ ]),
1146
+ chunkHash: new SyncHook(["chunk", "hash", "context"]),
1147
+ orderModules: new SyncBailHook(["chunk", "modules", "compilation"])
1148
+ })
1149
+ );
1150
+
1167
1151
  module.exports = CssModulesPlugin;
@@ -208,13 +208,24 @@ const createTrace = (fs, outputPath) => {
208
208
  }
209
209
  });
210
210
 
211
+ // Chrome DevTools treats this as the primary trace-bootstrap event and
212
+ // iterates `args.data.frames`; it must be present or the trace fails to load.
211
213
  trace.instantEvent({
212
214
  name: "TracingStartedInBrowser",
213
215
  id: ++counter,
214
216
  cat: ["disabled-by-default-devtools.timeline"],
215
217
  args: {
216
218
  data: {
217
- sessionId: "-1"
219
+ sessionId: "-1",
220
+ frameTreeNodeId: 1,
221
+ persistentIds: true,
222
+ frames: [
223
+ {
224
+ frame: "0xfff",
225
+ url: "webpack",
226
+ name: ""
227
+ }
228
+ ]
218
229
  }
219
230
  }
220
231
  });
@@ -6,18 +6,12 @@
6
6
  "use strict";
7
7
 
8
8
  const ExternalsPlugin = require("../ExternalsPlugin");
9
- const NodeTargetPlugin = require("../node/NodeTargetPlugin");
9
+ const { coreModules } = require("../node/nodeBuiltins");
10
10
 
11
11
  /** @typedef {import("../Compiler")} Compiler */
12
12
 
13
13
  // Deno only resolves node.js core modules through the `node:` specifier, so the
14
14
  // prefix is baked into the external request for both `import` and `require`.
15
- // cspell:word pnpapi
16
- const coreModules = new Set(
17
- NodeTargetPlugin.builtins.filter(
18
- (builtin) => typeof builtin === "string" && builtin !== "pnpapi"
19
- )
20
- );
21
15
 
22
16
  // Deno's own import protocols are resolved by the runtime, never bundled.
23
17
  const DENO_PROTOCOLS = /^(?:npm|jsr|https?):/;
@@ -462,7 +462,7 @@ CommonJsExportRequireDependency.Template = class CommonJsExportRequireDependency
462
462
  const comment = equals(usedImported, ids)
463
463
  ? ""
464
464
  : `${Template.toNormalComment(propertyAccess(ids))} `;
465
- requireExpr += `${comment}${propertyAccess(usedImported)}`;
465
+ requireExpr += `${comment}${propertyAccess(/** @type {string[]} */ (usedImported))}`;
466
466
  }
467
467
  }
468
468
  }
@@ -473,7 +473,7 @@ CommonJsExportRequireDependency.Template = class CommonJsExportRequireDependency
473
473
  dep.range[0],
474
474
  dep.range[1] - 1,
475
475
  used
476
- ? `${base}${propertyAccess(used)} = ${requireExpr}`
476
+ ? `${base}${propertyAccess(/** @type {string[]} */ (used))} = ${requireExpr}`
477
477
  : `/* unused reexport */ ${requireExpr}`
478
478
  );
479
479
  return;
@@ -501,7 +501,7 @@ CommonJsExportRequireDependency.Template = class CommonJsExportRequireDependency
501
501
  dep.range[0],
502
502
  valueRange[0] - 1,
503
503
  `Object.defineProperty(${base}${propertyAccess(
504
- used.slice(0, -1)
504
+ /** @type {string[]} */ (used).slice(0, -1)
505
505
  )}, ${JSON.stringify(used[used.length - 1])}, { ${descriptor}`
506
506
  );
507
507
  source.replace(valueRange[0], valueRange[1] - 1, requireExpr);
@@ -149,7 +149,7 @@ CommonJsExportsDependency.Template = class CommonJsExportsDependencyTemplate ext
149
149
  source.replace(
150
150
  dep.range[0],
151
151
  dep.range[1] - 1,
152
- `${base}${propertyAccess(used)}`
152
+ `${base}${propertyAccess(/** @type {string[]} */ (used))}`
153
153
  );
154
154
  return;
155
155
  case "Object.defineProperty":
@@ -178,7 +178,7 @@ CommonJsExportsDependency.Template = class CommonJsExportsDependencyTemplate ext
178
178
  dep.range[0],
179
179
  /** @type {Range} */ (dep.valueRange)[0] - 1,
180
180
  `Object.defineProperty(${base}${propertyAccess(
181
- used.slice(0, -1)
181
+ /** @type {string[]} */ (used).slice(0, -1)
182
182
  )}, ${JSON.stringify(used[used.length - 1])}, (`
183
183
  );
184
184
  source.replace(
@@ -178,7 +178,7 @@ CommonJsFullRequireDependency.Template = class CommonJsFullRequireDependencyTemp
178
178
  const comment = equals(usedImported, trimmedIds)
179
179
  ? ""
180
180
  : `${Template.toNormalComment(propertyAccess(trimmedIds))} `;
181
- const access = `${comment}${propertyAccess(usedImported)}`;
181
+ const access = `${comment}${propertyAccess(/** @type {string[]} */ (usedImported))}`;
182
182
  requireExpr =
183
183
  dep.asiSafe === true
184
184
  ? `(${requireExpr}${access})`
@@ -161,7 +161,7 @@ CommonJsSelfReferenceDependency.Template = class CommonJsSelfReferenceDependency
161
161
  source.replace(
162
162
  dep.range[0],
163
163
  dep.range[1] - 1,
164
- `${base}${propertyAccess(used)}`
164
+ `${base}${propertyAccess(/** @type {string[]} */ (used))}`
165
165
  );
166
166
  }
167
167
  };
@@ -108,6 +108,11 @@ module.exports = class HarmonyExportDependencyParserPlugin {
108
108
  /** @type {DependencyLocation} */ (statement.loc),
109
109
  -1
110
110
  );
111
+ // lazy barrel: defer at parse time when the importing module is side-effect-free
112
+ const moduleFactoryMeta = parser.state.module.factoryMeta;
113
+ if (moduleFactoryMeta !== undefined && moduleFactoryMeta.sideEffectFree) {
114
+ sideEffectDep.setLazy(true);
115
+ }
111
116
  parser.state.current.addDependency(sideEffectDep);
112
117
  return true;
113
118
  });
@@ -226,6 +231,15 @@ module.exports = class HarmonyExportDependencyParserPlugin {
226
231
  name,
227
232
  tagData ? tagData.value || null : undefined
228
233
  );
234
+ // lazy barrel: defer the re-export at parse time when the importing module is side-effect-free
235
+ const moduleFactoryMeta = parser.state.module.factoryMeta;
236
+ if (
237
+ settings &&
238
+ moduleFactoryMeta !== undefined &&
239
+ moduleFactoryMeta.sideEffectFree
240
+ ) {
241
+ dep.setLazy(true);
242
+ }
229
243
  dep.setLocWithIndex(
230
244
  /** @type {DependencyLocation} */ (statement.loc),
231
245
  /** @type {number} */ (idx)
@@ -273,6 +287,14 @@ module.exports = class HarmonyExportDependencyParserPlugin {
273
287
  if (harmonyStarExports) {
274
288
  harmonyStarExports.push(dep);
275
289
  }
290
+ // lazy barrel: defer the re-export at parse time when the importing module is side-effect-free
291
+ const moduleFactoryMeta = parser.state.module.factoryMeta;
292
+ if (
293
+ moduleFactoryMeta !== undefined &&
294
+ moduleFactoryMeta.sideEffectFree
295
+ ) {
296
+ dep.setLazy(true);
297
+ }
276
298
  dep.setLocWithIndex(
277
299
  /** @type {DependencyLocation} */ (statement.loc),
278
300
  /** @type {number} */ (idx)
@@ -1667,7 +1667,7 @@ HarmonyExportImportedSpecifierDependency.Template = class HarmonyExportImportedS
1667
1667
  );
1668
1668
  }
1669
1669
 
1670
- return `${name}${propertyAccess(valueKey)}`;
1670
+ return `${name}${propertyAccess(Array.isArray(valueKey) ? valueKey : [valueKey])}`;
1671
1671
  }
1672
1672
  };
1673
1673
 
@@ -366,6 +366,7 @@ class HarmonyImportDependency extends ModuleDependency {
366
366
  const { write } = context;
367
367
  write(this.attributes);
368
368
  write(this.phase);
369
+ write(this._lazyMake);
369
370
  super.serialize(context);
370
371
  }
371
372
 
@@ -377,6 +378,7 @@ class HarmonyImportDependency extends ModuleDependency {
377
378
  const { read } = context;
378
379
  this.attributes = read();
379
380
  this.phase = read();
381
+ this._lazyMake = read();
380
382
  super.deserialize(context);
381
383
  }
382
384
  }
@@ -7,7 +7,7 @@
7
7
 
8
8
  const { pathToFileURL } = require("url");
9
9
  const { SyncBailHook } = require("tapable");
10
- const Compilation = require("../Compilation");
10
+ /** @typedef {import("../Compilation")} Compilation */
11
11
  const DefinePlugin = require("../DefinePlugin");
12
12
  const {
13
13
  JAVASCRIPT_MODULE_TYPE_AUTO,
@@ -22,6 +22,7 @@ const {
22
22
  evaluateToString,
23
23
  toConstantDependency
24
24
  } = require("../javascript/JavascriptParserHelpers");
25
+ const createHooksRegistry = require("../util/createHooksRegistry");
25
26
  const { propertyAccess } = require("../util/property");
26
27
  const ConstDependency = require("./ConstDependency");
27
28
  const ModuleInitFragmentDependency = require("./ModuleInitFragmentDependency");
@@ -109,31 +110,7 @@ const collectImportMetaEnvDefinitions = (compilation) => {
109
110
  * @property {SyncBailHook<[DestructuringAssignmentProperty], string | void>} propertyInDestructuring
110
111
  */
111
112
 
112
- /** @type {WeakMap<Compilation, ImportMetaPluginHooks>} */
113
- const compilationHooksMap = new WeakMap();
114
-
115
113
  class ImportMetaPlugin {
116
- /**
117
- * Returns the attached hooks.
118
- * @param {Compilation} compilation the compilation
119
- * @returns {ImportMetaPluginHooks} the attached hooks
120
- */
121
- static getCompilationHooks(compilation) {
122
- if (!(compilation instanceof Compilation)) {
123
- throw new TypeError(
124
- "The 'compilation' argument must be an instance of Compilation"
125
- );
126
- }
127
- let hooks = compilationHooksMap.get(compilation);
128
- if (hooks === undefined) {
129
- hooks = {
130
- propertyInDestructuring: new SyncBailHook(["property"])
131
- };
132
- compilationHooksMap.set(compilation, hooks);
133
- }
134
- return hooks;
135
- }
136
-
137
114
  /**
138
115
  * Applies the plugin by registering its hooks on the compiler.
139
116
  * @param {Compiler} compiler compiler
@@ -501,6 +478,13 @@ class ImportMetaPlugin {
501
478
  }
502
479
  }
503
480
 
481
+ ImportMetaPlugin.getCompilationHooks = createHooksRegistry(
482
+ () =>
483
+ /** @type {ImportMetaPluginHooks} */ ({
484
+ propertyInDestructuring: new SyncBailHook(["property"])
485
+ })
486
+ );
487
+
504
488
  module.exports = ImportMetaPlugin;
505
489
  module.exports.IMPORT_META_DIRNAME = IMPORT_META_DIRNAME;
506
490
  module.exports.IMPORT_META_FILENAME = IMPORT_META_FILENAME;
@@ -146,7 +146,7 @@ class ProvidedDependencyTemplate extends ModuleDependency.Template {
146
146
  `inlined export ${propertyAccess(dep.ids)}`
147
147
  )
148
148
  )})`
149
- : `${moduleRaw}${propertyAccess(usedName, 0)}`;
149
+ : `${moduleRaw}${propertyAccess(/** @type {string[]} */ (usedName), 0)}`;
150
150
 
151
151
  initFragments.push(
152
152
  new InitFragment(
@@ -5,7 +5,7 @@
5
5
  "use strict";
6
6
 
7
7
  const { SyncWaterfallHook } = require("tapable");
8
- const Compilation = require("../Compilation");
8
+ /** @typedef {import("../Compilation")} Compilation */
9
9
  const RuntimeGlobals = require("../RuntimeGlobals");
10
10
  const RuntimeModule = require("../RuntimeModule");
11
11
  const Template = require("../Template");
@@ -18,6 +18,7 @@ const {
18
18
  } = require("../javascript/JavascriptModulesPlugin");
19
19
  const { getInitialChunkIds } = require("../javascript/StartupHelpers");
20
20
  const compileBooleanMatcher = require("../util/compileBooleanMatcher");
21
+ const createHooksRegistry = require("../util/createHooksRegistry");
21
22
  const { getUndoPath } = require("../util/identifier");
22
23
 
23
24
  /** @typedef {import("../Chunk")} Chunk */
@@ -31,32 +32,7 @@ const { getUndoPath } = require("../util/identifier");
31
32
  * @property {SyncWaterfallHook<[string, Chunk]>} linkPrefetch
32
33
  */
33
34
 
34
- /** @type {WeakMap<Compilation, JsonpCompilationPluginHooks>} */
35
- const compilationHooksMap = new WeakMap();
36
-
37
35
  class ModuleChunkLoadingRuntimeModule extends RuntimeModule {
38
- /**
39
- * Returns hooks.
40
- * @param {Compilation} compilation the compilation
41
- * @returns {JsonpCompilationPluginHooks} hooks
42
- */
43
- static getCompilationHooks(compilation) {
44
- if (!(compilation instanceof Compilation)) {
45
- throw new TypeError(
46
- "The 'compilation' argument must be an instance of Compilation"
47
- );
48
- }
49
- let hooks = compilationHooksMap.get(compilation);
50
- if (hooks === undefined) {
51
- hooks = {
52
- linkPreload: new SyncWaterfallHook(["source", "chunk"]),
53
- linkPrefetch: new SyncWaterfallHook(["source", "chunk"])
54
- };
55
- compilationHooksMap.set(compilation, hooks);
56
- }
57
- return hooks;
58
- }
59
-
60
36
  /**
61
37
  * Creates an instance of ModuleChunkLoadingRuntimeModule.
62
38
  * @param {ReadOnlyRuntimeRequirements} runtimeRequirements runtime requirements
@@ -433,4 +409,12 @@ class ModuleChunkLoadingRuntimeModule extends RuntimeModule {
433
409
  }
434
410
  }
435
411
 
412
+ ModuleChunkLoadingRuntimeModule.getCompilationHooks = createHooksRegistry(
413
+ () =>
414
+ /** @type {JsonpCompilationPluginHooks} */ ({
415
+ linkPreload: new SyncWaterfallHook(["source", "chunk"]),
416
+ linkPrefetch: new SyncWaterfallHook(["source", "chunk"])
417
+ })
418
+ );
419
+
436
420
  module.exports = ModuleChunkLoadingRuntimeModule;
@@ -16,7 +16,7 @@ const {
16
16
  RawSource,
17
17
  ReplaceSource
18
18
  } = require("webpack-sources");
19
- const Compilation = require("../Compilation");
19
+ /** @typedef {import("../Compilation")} Compilation */
20
20
  const HotUpdateChunk = require("../HotUpdateChunk");
21
21
  const InitFragment = require("../InitFragment");
22
22
  const { JAVASCRIPT_TYPE } = require("../ModuleSourceTypeConstants");
@@ -42,6 +42,7 @@ const {
42
42
  getUsedNamesInScopeInfo
43
43
  } = require("../util/concatenate");
44
44
  const createHash = require("../util/createHash");
45
+ const createHooksRegistry = require("../util/createHooksRegistry");
45
46
  const { digestNonNumericOnlyWithFull } = require("../util/nonNumericOnlyHash");
46
47
  const removeBOM = require("../util/removeBOM");
47
48
  const { intersectRuntime } = require("../util/runtime");
@@ -345,9 +346,6 @@ const printGeneratedCodeForStack = (module, code) => {
345
346
  * @property {SyncBailHook<[Chunk, RenderContext], boolean | void>} useSourceMap
346
347
  */
347
348
 
348
- /** @type {WeakMap<Compilation, CompilationHooks>} */
349
- const compilationHooksMap = new WeakMap();
350
-
351
349
  /**
352
350
  * Renames an inlined module's top-level declaration (and all its references) when
353
351
  * its name is already taken in the shared startup scope, recording the chosen name
@@ -422,56 +420,6 @@ const PLUGIN_NAME = "JavascriptModulesPlugin";
422
420
  /** @typedef {{ header: string[], beforeStartup: string[], startup: string[], afterStartup: string[], allowInlineStartup: boolean }} Bootstrap */
423
421
 
424
422
  class JavascriptModulesPlugin {
425
- /**
426
- * Returns the attached hooks.
427
- * @param {Compilation} compilation the compilation
428
- * @returns {CompilationHooks} the attached hooks
429
- */
430
- static getCompilationHooks(compilation) {
431
- if (!(compilation instanceof Compilation)) {
432
- throw new TypeError(
433
- "The 'compilation' argument must be an instance of Compilation"
434
- );
435
- }
436
- let hooks = compilationHooksMap.get(compilation);
437
- if (hooks === undefined) {
438
- hooks = {
439
- renderModuleContent: new SyncWaterfallHook([
440
- "source",
441
- "module",
442
- "moduleRenderContext"
443
- ]),
444
- renderModuleContainer: new SyncWaterfallHook([
445
- "source",
446
- "module",
447
- "moduleRenderContext"
448
- ]),
449
- renderModulePackage: new SyncWaterfallHook([
450
- "source",
451
- "module",
452
- "moduleRenderContext"
453
- ]),
454
- render: new SyncWaterfallHook(["source", "renderContext"]),
455
- renderContent: new SyncWaterfallHook(["source", "renderContext"]),
456
- renderStartup: new SyncWaterfallHook([
457
- "source",
458
- "module",
459
- "startupRenderContext"
460
- ]),
461
- renderChunk: new SyncWaterfallHook(["source", "renderContext"]),
462
- renderMain: new SyncWaterfallHook(["source", "renderContext"]),
463
- renderRequire: new SyncWaterfallHook(["code", "renderContext"]),
464
- inlineInRuntimeBailout: new SyncBailHook(["module", "renderContext"]),
465
- embedInRuntimeBailout: new SyncBailHook(["module", "renderContext"]),
466
- strictRuntimeBailout: new SyncBailHook(["renderContext"]),
467
- chunkHash: new SyncHook(["chunk", "hash", "context"]),
468
- useSourceMap: new SyncBailHook(["chunk", "renderContext"])
469
- };
470
- compilationHooksMap.set(compilation, hooks);
471
- }
472
- return hooks;
473
- }
474
-
475
423
  constructor(options = {}) {
476
424
  this.options = options;
477
425
  /** @type {WeakMap<Source, { source: Source, needModule: boolean, needExports: boolean, needRequire: boolean, needThisAsExports: boolean, needStrict: boolean | undefined, renderShorthand: boolean }>} */
@@ -2185,5 +2133,41 @@ class JavascriptModulesPlugin {
2185
2133
  }
2186
2134
  }
2187
2135
 
2136
+ JavascriptModulesPlugin.getCompilationHooks = createHooksRegistry(
2137
+ () =>
2138
+ /** @type {CompilationHooks} */ ({
2139
+ renderModuleContent: new SyncWaterfallHook([
2140
+ "source",
2141
+ "module",
2142
+ "moduleRenderContext"
2143
+ ]),
2144
+ renderModuleContainer: new SyncWaterfallHook([
2145
+ "source",
2146
+ "module",
2147
+ "moduleRenderContext"
2148
+ ]),
2149
+ renderModulePackage: new SyncWaterfallHook([
2150
+ "source",
2151
+ "module",
2152
+ "moduleRenderContext"
2153
+ ]),
2154
+ render: new SyncWaterfallHook(["source", "renderContext"]),
2155
+ renderContent: new SyncWaterfallHook(["source", "renderContext"]),
2156
+ renderStartup: new SyncWaterfallHook([
2157
+ "source",
2158
+ "module",
2159
+ "startupRenderContext"
2160
+ ]),
2161
+ renderChunk: new SyncWaterfallHook(["source", "renderContext"]),
2162
+ renderMain: new SyncWaterfallHook(["source", "renderContext"]),
2163
+ renderRequire: new SyncWaterfallHook(["code", "renderContext"]),
2164
+ inlineInRuntimeBailout: new SyncBailHook(["module", "renderContext"]),
2165
+ embedInRuntimeBailout: new SyncBailHook(["module", "renderContext"]),
2166
+ strictRuntimeBailout: new SyncBailHook(["renderContext"]),
2167
+ chunkHash: new SyncHook(["chunk", "hash", "context"]),
2168
+ useSourceMap: new SyncBailHook(["chunk", "renderContext"])
2169
+ })
2170
+ );
2171
+
2188
2172
  module.exports = JavascriptModulesPlugin;
2189
2173
  module.exports.chunkHasJs = chunkHasJs;
@@ -6,72 +6,11 @@
6
6
  "use strict";
7
7
 
8
8
  const ExternalsPlugin = require("../ExternalsPlugin");
9
+ const { builtins } = require("./nodeBuiltins");
9
10
 
10
11
  /** @typedef {import("../../declarations/WebpackOptions").ExternalsType} ExternalsType */
11
12
  /** @typedef {import("../Compiler")} Compiler */
12
13
 
13
- const builtins = [
14
- "assert",
15
- "assert/strict",
16
- "async_hooks",
17
- "buffer",
18
- "child_process",
19
- "cluster",
20
- "console",
21
- "constants",
22
- "crypto",
23
- "dgram",
24
- "diagnostics_channel",
25
- "dns",
26
- "dns/promises",
27
- "domain",
28
- "events",
29
- "fs",
30
- "fs/promises",
31
- "http",
32
- "http2",
33
- "https",
34
- "inspector",
35
- "inspector/promises",
36
- "module",
37
- "net",
38
- "os",
39
- "path",
40
- "path/posix",
41
- "path/win32",
42
- "perf_hooks",
43
- "process",
44
- "punycode",
45
- "querystring",
46
- "readline",
47
- "readline/promises",
48
- "repl",
49
- "stream",
50
- "stream/consumers",
51
- "stream/promises",
52
- "stream/web",
53
- "string_decoder",
54
- "sys",
55
- "timers",
56
- "timers/promises",
57
- "tls",
58
- "trace_events",
59
- "tty",
60
- "url",
61
- "util",
62
- "util/types",
63
- "v8",
64
- "vm",
65
- "wasi",
66
- "worker_threads",
67
- "zlib",
68
- /^node:/,
69
-
70
- // cspell:word pnpapi
71
- // Yarn PnP adds pnpapi as "builtin"
72
- "pnpapi"
73
- ];
74
-
75
14
  class NodeTargetPlugin {
76
15
  /**
77
16
  * Creates an instance of NodeTargetPlugin.