webpack 5.77.0 → 5.79.0

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.

Potentially problematic release.


This version of webpack might be problematic. Click here for more details.

Files changed (82) hide show
  1. package/bin/webpack.js +0 -0
  2. package/lib/APIPlugin.js +25 -18
  3. package/lib/CompatibilityPlugin.js +80 -62
  4. package/lib/Compiler.js +7 -4
  5. package/lib/ConstPlugin.js +22 -15
  6. package/lib/ContextModule.js +3 -2
  7. package/lib/DefinePlugin.js +62 -42
  8. package/lib/DelegatedModule.js +2 -1
  9. package/lib/DllModule.js +2 -1
  10. package/lib/ErrorHelpers.js +61 -22
  11. package/lib/ExportsInfoApiPlugin.js +16 -9
  12. package/lib/ExternalModule.js +2 -1
  13. package/lib/FlagAllModulesAsUsedPlugin.js +22 -27
  14. package/lib/FlagDependencyExportsPlugin.js +336 -348
  15. package/lib/FlagDependencyUsagePlugin.js +6 -8
  16. package/lib/FlagEntryExportAsUsedPlugin.js +22 -23
  17. package/lib/HotModuleReplacementPlugin.js +50 -45
  18. package/lib/JavascriptMetaInfoPlugin.js +16 -9
  19. package/lib/LibManifestPlugin.js +2 -1
  20. package/lib/ModuleTypeConstants.js +50 -0
  21. package/lib/NodeStuffPlugin.js +35 -31
  22. package/lib/NormalModule.js +2 -1
  23. package/lib/NormalModuleFactory.js +7 -1
  24. package/lib/NormalModuleReplacementPlugin.js +1 -1
  25. package/lib/ProvidePlugin.js +17 -10
  26. package/lib/RawModule.js +2 -1
  27. package/lib/RequireJsStuffPlugin.js +15 -15
  28. package/lib/UseStrictPlugin.js +15 -8
  29. package/lib/WebpackIsIncludedPlugin.js +16 -9
  30. package/lib/config/defaults.js +16 -8
  31. package/lib/config/normalization.js +4 -0
  32. package/lib/container/ContainerEntryModule.js +2 -1
  33. package/lib/css/CssLoadingRuntimeModule.js +1 -1
  34. package/lib/css/CssParser.js +28 -8
  35. package/lib/css/walkCssTokens.js +6 -1
  36. package/lib/debug/ProfilingPlugin.js +20 -12
  37. package/lib/dependencies/AMDPlugin.js +26 -20
  38. package/lib/dependencies/CommonJsImportsParserPlugin.js +5 -4
  39. package/lib/dependencies/CommonJsPlugin.js +29 -25
  40. package/lib/dependencies/HarmonyDetectionParserPlugin.js +3 -1
  41. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +4 -0
  42. package/lib/dependencies/HarmonyImportSpecifierDependency.js +28 -3
  43. package/lib/dependencies/HarmonyModulesPlugin.js +11 -5
  44. package/lib/dependencies/ImportMetaContextPlugin.js +11 -5
  45. package/lib/dependencies/ImportMetaPlugin.js +26 -20
  46. package/lib/dependencies/ImportPlugin.js +14 -7
  47. package/lib/dependencies/RequireContextPlugin.js +12 -6
  48. package/lib/dependencies/RequireEnsurePlugin.js +13 -7
  49. package/lib/dependencies/RequireIncludePlugin.js +11 -5
  50. package/lib/dependencies/SystemPlugin.js +21 -15
  51. package/lib/dependencies/URLPlugin.js +15 -9
  52. package/lib/dependencies/WorkerPlugin.js +14 -8
  53. package/lib/index.js +5 -0
  54. package/lib/javascript/JavascriptModulesPlugin.js +157 -164
  55. package/lib/javascript/JavascriptParser.js +88 -0
  56. package/lib/json/JsonModulesPlugin.js +13 -5
  57. package/lib/library/AmdLibraryPlugin.js +22 -6
  58. package/lib/node/ReadFileCompileAsyncWasmPlugin.js +2 -1
  59. package/lib/node/ReadFileCompileWasmPlugin.js +2 -1
  60. package/lib/optimize/ConcatenatedModule.js +2 -1
  61. package/lib/optimize/InnerGraphPlugin.js +47 -46
  62. package/lib/optimize/SideEffectsFlagPlugin.js +43 -43
  63. package/lib/sharing/ConsumeSharedPlugin.js +4 -0
  64. package/lib/stats/DefaultStatsPrinterPlugin.js +14 -0
  65. package/lib/util/hash/md4.js +2 -2
  66. package/lib/util/hash/xxhash64.js +1 -1
  67. package/lib/wasm-async/AsyncWebAssemblyModulesPlugin.js +9 -6
  68. package/lib/wasm-sync/WebAssemblyModulesPlugin.js +42 -43
  69. package/lib/web/FetchCompileAsyncWasmPlugin.js +2 -1
  70. package/lib/web/FetchCompileWasmPlugin.js +40 -40
  71. package/lib/webpack.js +1 -1
  72. package/package.json +41 -36
  73. package/schemas/WebpackOptions.check.js +1 -1
  74. package/schemas/WebpackOptions.json +18 -0
  75. package/schemas/plugins/ProgressPlugin.check.js +1 -1
  76. package/schemas/plugins/SourceMapDevToolPlugin.check.js +1 -1
  77. package/schemas/plugins/container/ContainerPlugin.check.js +1 -1
  78. package/schemas/plugins/container/ContainerPlugin.json +8 -0
  79. package/schemas/plugins/container/ModuleFederationPlugin.check.js +1 -1
  80. package/schemas/plugins/container/ModuleFederationPlugin.json +8 -0
  81. package/schemas/plugins/sharing/SharePlugin.check.js +1 -1
  82. package/types.d.ts +152 -122
@@ -24,6 +24,9 @@ const { getEntryRuntime, mergeRuntimeOwned } = require("./util/runtime");
24
24
 
25
25
  const { NO_EXPORTS_REFERENCED, EXPORTS_OBJECT_REFERENCED } = Dependency;
26
26
 
27
+ const PLUGIN_NAME = "FlagDependencyUsagePlugin";
28
+ const PLUGIN_LOGGER_NAME = `webpack.${PLUGIN_NAME}`;
29
+
27
30
  class FlagDependencyUsagePlugin {
28
31
  /**
29
32
  * @param {boolean} global do a global analysis instead of per runtime
@@ -38,13 +41,10 @@ class FlagDependencyUsagePlugin {
38
41
  * @returns {void}
39
42
  */
40
43
  apply(compiler) {
41
- compiler.hooks.compilation.tap("FlagDependencyUsagePlugin", compilation => {
44
+ compiler.hooks.compilation.tap(PLUGIN_NAME, compilation => {
42
45
  const moduleGraph = compilation.moduleGraph;
43
46
  compilation.hooks.optimizeDependencies.tap(
44
- {
45
- name: "FlagDependencyUsagePlugin",
46
- stage: STAGE_DEFAULT
47
- },
47
+ { name: PLUGIN_NAME, stage: STAGE_DEFAULT },
48
48
  modules => {
49
49
  if (compilation.moduleMemCaches) {
50
50
  throw new Error(
@@ -52,9 +52,7 @@ class FlagDependencyUsagePlugin {
52
52
  );
53
53
  }
54
54
 
55
- const logger = compilation.getLogger(
56
- "webpack.FlagDependencyUsagePlugin"
57
- );
55
+ const logger = compilation.getLogger(PLUGIN_LOGGER_NAME);
58
56
  /** @type {Map<ExportsInfo, Module>} */
59
57
  const exportInfoToModuleMap = new Map();
60
58
 
@@ -9,6 +9,8 @@ const { getEntryRuntime } = require("./util/runtime");
9
9
 
10
10
  /** @typedef {import("./Compiler")} Compiler */
11
11
 
12
+ const PLUGIN_NAME = "FlagEntryExportAsUsedPlugin";
13
+
12
14
  class FlagEntryExportAsUsedPlugin {
13
15
  constructor(nsObjectUsed, explanation) {
14
16
  this.nsObjectUsed = nsObjectUsed;
@@ -21,32 +23,29 @@ class FlagEntryExportAsUsedPlugin {
21
23
  * @returns {void}
22
24
  */
23
25
  apply(compiler) {
24
- compiler.hooks.thisCompilation.tap(
25
- "FlagEntryExportAsUsedPlugin",
26
- compilation => {
27
- const moduleGraph = compilation.moduleGraph;
28
- compilation.hooks.seal.tap("FlagEntryExportAsUsedPlugin", () => {
29
- for (const [
30
- entryName,
31
- { dependencies: deps, options }
32
- ] of compilation.entries) {
33
- const runtime = getEntryRuntime(compilation, entryName, options);
34
- for (const dep of deps) {
35
- const module = moduleGraph.getModule(dep);
36
- if (module) {
37
- const exportsInfo = moduleGraph.getExportsInfo(module);
38
- if (this.nsObjectUsed) {
39
- exportsInfo.setUsedInUnknownWay(runtime);
40
- } else {
41
- exportsInfo.setAllKnownExportsUsed(runtime);
42
- }
43
- moduleGraph.addExtraReason(module, this.explanation);
26
+ compiler.hooks.thisCompilation.tap(PLUGIN_NAME, compilation => {
27
+ const moduleGraph = compilation.moduleGraph;
28
+ compilation.hooks.seal.tap(PLUGIN_NAME, () => {
29
+ for (const [
30
+ entryName,
31
+ { dependencies: deps, options }
32
+ ] of compilation.entries) {
33
+ const runtime = getEntryRuntime(compilation, entryName, options);
34
+ for (const dep of deps) {
35
+ const module = moduleGraph.getModule(dep);
36
+ if (module) {
37
+ const exportsInfo = moduleGraph.getExportsInfo(module);
38
+ if (this.nsObjectUsed) {
39
+ exportsInfo.setUsedInUnknownWay(runtime);
40
+ } else {
41
+ exportsInfo.setAllKnownExportsUsed(runtime);
44
42
  }
43
+ moduleGraph.addExtraReason(module, this.explanation);
45
44
  }
46
45
  }
47
- });
48
- }
49
- );
46
+ }
47
+ });
48
+ });
50
49
  }
51
50
  }
52
51
 
@@ -35,6 +35,12 @@ const {
35
35
  intersectRuntime
36
36
  } = require("./util/runtime");
37
37
 
38
+ const {
39
+ JAVASCRIPT_MODULE_TYPE_AUTO,
40
+ JAVASCRIPT_MODULE_TYPE_DYNAMIC,
41
+ JAVASCRIPT_MODULE_TYPE_ESM
42
+ } = require("./ModuleTypeConstants");
43
+
38
44
  /** @typedef {import("./Chunk")} Chunk */
39
45
  /** @typedef {import("./Compilation").AssetInfo} AssetInfo */
40
46
  /** @typedef {import("./Compiler")} Compiler */
@@ -51,6 +57,8 @@ const {
51
57
  /** @type {WeakMap<JavascriptParser, HMRJavascriptParserHooks>} */
52
58
  const parserHooksMap = new WeakMap();
53
59
 
60
+ const PLUGIN_NAME = "HotModuleReplacementPlugin";
61
+
54
62
  class HotModuleReplacementPlugin {
55
63
  /**
56
64
  * @param {JavascriptParser} parser the parser
@@ -183,7 +191,7 @@ class HotModuleReplacementPlugin {
183
191
  const applyModuleHot = parser => {
184
192
  parser.hooks.evaluateIdentifier.for("module.hot").tap(
185
193
  {
186
- name: "HotModuleReplacementPlugin",
194
+ name: PLUGIN_NAME,
187
195
  before: "NodeStuffPlugin"
188
196
  },
189
197
  expr => {
@@ -198,24 +206,24 @@ class HotModuleReplacementPlugin {
198
206
  parser.hooks.call
199
207
  .for("module.hot.accept")
200
208
  .tap(
201
- "HotModuleReplacementPlugin",
209
+ PLUGIN_NAME,
202
210
  createAcceptHandler(parser, ModuleHotAcceptDependency)
203
211
  );
204
212
  parser.hooks.call
205
213
  .for("module.hot.decline")
206
214
  .tap(
207
- "HotModuleReplacementPlugin",
215
+ PLUGIN_NAME,
208
216
  createDeclineHandler(parser, ModuleHotDeclineDependency)
209
217
  );
210
218
  parser.hooks.expression
211
219
  .for("module.hot")
212
- .tap("HotModuleReplacementPlugin", createHMRExpressionHandler(parser));
220
+ .tap(PLUGIN_NAME, createHMRExpressionHandler(parser));
213
221
  };
214
222
 
215
223
  const applyImportMetaHot = parser => {
216
224
  parser.hooks.evaluateIdentifier
217
225
  .for("import.meta.webpackHot")
218
- .tap("HotModuleReplacementPlugin", expr => {
226
+ .tap(PLUGIN_NAME, expr => {
219
227
  return evaluateToIdentifier(
220
228
  "import.meta.webpackHot",
221
229
  "import.meta",
@@ -226,22 +234,22 @@ class HotModuleReplacementPlugin {
226
234
  parser.hooks.call
227
235
  .for("import.meta.webpackHot.accept")
228
236
  .tap(
229
- "HotModuleReplacementPlugin",
237
+ PLUGIN_NAME,
230
238
  createAcceptHandler(parser, ImportMetaHotAcceptDependency)
231
239
  );
232
240
  parser.hooks.call
233
241
  .for("import.meta.webpackHot.decline")
234
242
  .tap(
235
- "HotModuleReplacementPlugin",
243
+ PLUGIN_NAME,
236
244
  createDeclineHandler(parser, ImportMetaHotDeclineDependency)
237
245
  );
238
246
  parser.hooks.expression
239
247
  .for("import.meta.webpackHot")
240
- .tap("HotModuleReplacementPlugin", createHMRExpressionHandler(parser));
248
+ .tap(PLUGIN_NAME, createHMRExpressionHandler(parser));
241
249
  };
242
250
 
243
251
  compiler.hooks.compilation.tap(
244
- "HotModuleReplacementPlugin",
252
+ PLUGIN_NAME,
245
253
  (compilation, { normalModuleFactory }) => {
246
254
  // This applies the HMR plugin only to the targeted compiler
247
255
  // It should not affect child compilations
@@ -289,40 +297,37 @@ class HotModuleReplacementPlugin {
289
297
  const fullHashChunkModuleHashes = {};
290
298
  const chunkModuleHashes = {};
291
299
 
292
- compilation.hooks.record.tap(
293
- "HotModuleReplacementPlugin",
294
- (compilation, records) => {
295
- if (records.hash === compilation.hash) return;
296
- const chunkGraph = compilation.chunkGraph;
297
- records.hash = compilation.hash;
298
- records.hotIndex = hotIndex;
299
- records.fullHashChunkModuleHashes = fullHashChunkModuleHashes;
300
- records.chunkModuleHashes = chunkModuleHashes;
301
- records.chunkHashes = {};
302
- records.chunkRuntime = {};
303
- for (const chunk of compilation.chunks) {
304
- records.chunkHashes[chunk.id] = chunk.hash;
305
- records.chunkRuntime[chunk.id] = getRuntimeKey(chunk.runtime);
306
- }
307
- records.chunkModuleIds = {};
308
- for (const chunk of compilation.chunks) {
309
- records.chunkModuleIds[chunk.id] = Array.from(
310
- chunkGraph.getOrderedChunkModulesIterable(
311
- chunk,
312
- compareModulesById(chunkGraph)
313
- ),
314
- m => chunkGraph.getModuleId(m)
315
- );
316
- }
300
+ compilation.hooks.record.tap(PLUGIN_NAME, (compilation, records) => {
301
+ if (records.hash === compilation.hash) return;
302
+ const chunkGraph = compilation.chunkGraph;
303
+ records.hash = compilation.hash;
304
+ records.hotIndex = hotIndex;
305
+ records.fullHashChunkModuleHashes = fullHashChunkModuleHashes;
306
+ records.chunkModuleHashes = chunkModuleHashes;
307
+ records.chunkHashes = {};
308
+ records.chunkRuntime = {};
309
+ for (const chunk of compilation.chunks) {
310
+ records.chunkHashes[chunk.id] = chunk.hash;
311
+ records.chunkRuntime[chunk.id] = getRuntimeKey(chunk.runtime);
317
312
  }
318
- );
313
+ records.chunkModuleIds = {};
314
+ for (const chunk of compilation.chunks) {
315
+ records.chunkModuleIds[chunk.id] = Array.from(
316
+ chunkGraph.getOrderedChunkModulesIterable(
317
+ chunk,
318
+ compareModulesById(chunkGraph)
319
+ ),
320
+ m => chunkGraph.getModuleId(m)
321
+ );
322
+ }
323
+ });
319
324
  /** @type {TupleSet<[Module, Chunk]>} */
320
325
  const updatedModules = new TupleSet();
321
326
  /** @type {TupleSet<[Module, Chunk]>} */
322
327
  const fullHashModules = new TupleSet();
323
328
  /** @type {TupleSet<[Module, RuntimeSpec]>} */
324
329
  const nonCodeGeneratedModules = new TupleSet();
325
- compilation.hooks.fullHash.tap("HotModuleReplacementPlugin", hash => {
330
+ compilation.hooks.fullHash.tap(PLUGIN_NAME, hash => {
326
331
  const chunkGraph = compilation.chunkGraph;
327
332
  const records = compilation.records;
328
333
  for (const chunk of compilation.chunks) {
@@ -412,7 +417,7 @@ class HotModuleReplacementPlugin {
412
417
  });
413
418
  compilation.hooks.processAssets.tap(
414
419
  {
415
- name: "HotModuleReplacementPlugin",
420
+ name: PLUGIN_NAME,
416
421
  stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL
417
422
  },
418
423
  () => {
@@ -734,7 +739,7 @@ To fix this, make sure to include [runtime] in the output.hotUpdateMainFilename
734
739
  );
735
740
 
736
741
  compilation.hooks.additionalTreeRuntimeRequirements.tap(
737
- "HotModuleReplacementPlugin",
742
+ PLUGIN_NAME,
738
743
  (chunk, runtimeRequirements) => {
739
744
  runtimeRequirements.add(RuntimeGlobals.hmrDownloadManifest);
740
745
  runtimeRequirements.add(RuntimeGlobals.hmrDownloadUpdateHandlers);
@@ -748,24 +753,24 @@ To fix this, make sure to include [runtime] in the output.hotUpdateMainFilename
748
753
  );
749
754
 
750
755
  normalModuleFactory.hooks.parser
751
- .for("javascript/auto")
752
- .tap("HotModuleReplacementPlugin", parser => {
756
+ .for(JAVASCRIPT_MODULE_TYPE_AUTO)
757
+ .tap(PLUGIN_NAME, parser => {
753
758
  applyModuleHot(parser);
754
759
  applyImportMetaHot(parser);
755
760
  });
756
761
  normalModuleFactory.hooks.parser
757
- .for("javascript/dynamic")
758
- .tap("HotModuleReplacementPlugin", parser => {
762
+ .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
763
+ .tap(PLUGIN_NAME, parser => {
759
764
  applyModuleHot(parser);
760
765
  });
761
766
  normalModuleFactory.hooks.parser
762
- .for("javascript/esm")
763
- .tap("HotModuleReplacementPlugin", parser => {
767
+ .for(JAVASCRIPT_MODULE_TYPE_ESM)
768
+ .tap(PLUGIN_NAME, parser => {
764
769
  applyImportMetaHot(parser);
765
770
  });
766
771
 
767
772
  NormalModule.getCompilationHooks(compilation).loader.tap(
768
- "HotModuleReplacementPlugin",
773
+ PLUGIN_NAME,
769
774
  context => {
770
775
  context.hot = true;
771
776
  }
@@ -5,11 +5,18 @@
5
5
 
6
6
  "use strict";
7
7
 
8
+ const {
9
+ JAVASCRIPT_MODULE_TYPE_AUTO,
10
+ JAVASCRIPT_MODULE_TYPE_DYNAMIC,
11
+ JAVASCRIPT_MODULE_TYPE_ESM
12
+ } = require("./ModuleTypeConstants");
8
13
  const InnerGraph = require("./optimize/InnerGraph");
9
14
 
10
15
  /** @typedef {import("./Compiler")} Compiler */
11
16
  /** @typedef {import("./javascript/JavascriptParser")} JavascriptParser */
12
17
 
18
+ const PLUGIN_NAME = "JavascriptMetaInfoPlugin";
19
+
13
20
  class JavascriptMetaInfoPlugin {
14
21
  /**
15
22
  * Apply the plugin
@@ -18,14 +25,14 @@ class JavascriptMetaInfoPlugin {
18
25
  */
19
26
  apply(compiler) {
20
27
  compiler.hooks.compilation.tap(
21
- "JavascriptMetaInfoPlugin",
28
+ PLUGIN_NAME,
22
29
  (compilation, { normalModuleFactory }) => {
23
30
  /**
24
31
  * @param {JavascriptParser} parser the parser
25
32
  * @returns {void}
26
33
  */
27
34
  const handler = parser => {
28
- parser.hooks.call.for("eval").tap("JavascriptMetaInfoPlugin", () => {
35
+ parser.hooks.call.for("eval").tap(PLUGIN_NAME, () => {
29
36
  parser.state.module.buildInfo.moduleConcatenationBailout = "eval()";
30
37
  parser.state.module.buildInfo.usingEval = true;
31
38
  const currentSymbol = InnerGraph.getTopLevelSymbol(parser.state);
@@ -35,7 +42,7 @@ class JavascriptMetaInfoPlugin {
35
42
  InnerGraph.bailout(parser.state);
36
43
  }
37
44
  });
38
- parser.hooks.finish.tap("JavascriptMetaInfoPlugin", () => {
45
+ parser.hooks.finish.tap(PLUGIN_NAME, () => {
39
46
  let topLevelDeclarations =
40
47
  parser.state.module.buildInfo.topLevelDeclarations;
41
48
  if (topLevelDeclarations === undefined) {
@@ -52,14 +59,14 @@ class JavascriptMetaInfoPlugin {
52
59
  };
53
60
 
54
61
  normalModuleFactory.hooks.parser
55
- .for("javascript/auto")
56
- .tap("JavascriptMetaInfoPlugin", handler);
62
+ .for(JAVASCRIPT_MODULE_TYPE_AUTO)
63
+ .tap(PLUGIN_NAME, handler);
57
64
  normalModuleFactory.hooks.parser
58
- .for("javascript/dynamic")
59
- .tap("JavascriptMetaInfoPlugin", handler);
65
+ .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
66
+ .tap(PLUGIN_NAME, handler);
60
67
  normalModuleFactory.hooks.parser
61
- .for("javascript/esm")
62
- .tap("JavascriptMetaInfoPlugin", handler);
68
+ .for(JAVASCRIPT_MODULE_TYPE_ESM)
69
+ .tap(PLUGIN_NAME, handler);
63
70
  }
64
71
  );
65
72
  }
@@ -49,7 +49,8 @@ class LibManifestPlugin {
49
49
  const name =
50
50
  this.options.name &&
51
51
  compilation.getPath(this.options.name, {
52
- chunk
52
+ chunk,
53
+ contentHashType: "javascript"
53
54
  });
54
55
  const content = Object.create(null);
55
56
  for (const module of chunkGraph.getOrderedChunkModulesIterable(
@@ -0,0 +1,50 @@
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ Author Sean Larkin @TheLarkInn
4
+ */
5
+
6
+ "use strict";
7
+
8
+ /**
9
+ * @type {Readonly<"javascript/auto">}
10
+ */
11
+ const JAVASCRIPT_MODULE_TYPE_AUTO = "javascript/auto";
12
+
13
+ /**
14
+ * @type {Readonly<"javascript/dynamic">}
15
+ */
16
+ const JAVASCRIPT_MODULE_TYPE_DYNAMIC = "javascript/dynamic";
17
+
18
+ /**
19
+ * @type {Readonly<"javascript/esm">}
20
+ * This is the module type used for _strict_ ES Module syntax. This means that all legacy formats
21
+ * that webpack supports (CommonJS, AMD, SystemJS) are not supported.
22
+ */
23
+ const JAVASCRIPT_MODULE_TYPE_ESM = "javascript/esm";
24
+
25
+ /**
26
+ * @type {Readonly<"json">}
27
+ * This is the module type used for JSON files. JSON files are always parsed as ES Module.
28
+ */
29
+ const JSON_MODULE_TYPE = "json";
30
+
31
+ /**
32
+ * @type {Readonly<"webassembly/async">}
33
+ * This is the module type used for WebAssembly modules. In webpack 5 they are always treated as async modules.
34
+ *
35
+ */
36
+ const WEBASSEMBLY_MODULE_TYPE_ASYNC = "webassembly/async";
37
+
38
+ /**
39
+ * @type {Readonly<"webassembly/sync">}
40
+ * This is the module type used for WebAssembly modules. In webpack 4 they are always treated as sync modules.
41
+ * There is a legacy option to support this usage in webpack 5 and up.
42
+ */
43
+ const WEBASSEMBLY_MODULE_TYPE_SYNC = "webassembly/sync";
44
+
45
+ exports.JAVASCRIPT_MODULE_TYPE_AUTO = JAVASCRIPT_MODULE_TYPE_AUTO;
46
+ exports.JAVASCRIPT_MODULE_TYPE_DYNAMIC = JAVASCRIPT_MODULE_TYPE_DYNAMIC;
47
+ exports.JAVASCRIPT_MODULE_TYPE_ESM = JAVASCRIPT_MODULE_TYPE_ESM;
48
+ exports.JSON_MODULE_TYPE = JSON_MODULE_TYPE;
49
+ exports.WEBASSEMBLY_MODULE_TYPE_ASYNC = WEBASSEMBLY_MODULE_TYPE_ASYNC;
50
+ exports.WEBASSEMBLY_MODULE_TYPE_SYNC = WEBASSEMBLY_MODULE_TYPE_SYNC;
@@ -5,6 +5,10 @@
5
5
 
6
6
  "use strict";
7
7
 
8
+ const {
9
+ JAVASCRIPT_MODULE_TYPE_AUTO,
10
+ JAVASCRIPT_MODULE_TYPE_DYNAMIC
11
+ } = require("./ModuleTypeConstants");
8
12
  const NodeStuffInWebError = require("./NodeStuffInWebError");
9
13
  const RuntimeGlobals = require("./RuntimeGlobals");
10
14
  const CachedConstDependency = require("./dependencies/CachedConstDependency");
@@ -22,6 +26,8 @@ const { parseResource } = require("./util/identifier");
22
26
  /** @typedef {import("./DependencyTemplates")} DependencyTemplates */
23
27
  /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
24
28
 
29
+ const PLUGIN_NAME = "NodeStuffPlugin";
30
+
25
31
  class NodeStuffPlugin {
26
32
  constructor(options) {
27
33
  this.options = options;
@@ -35,7 +41,7 @@ class NodeStuffPlugin {
35
41
  apply(compiler) {
36
42
  const options = this.options;
37
43
  compiler.hooks.compilation.tap(
38
- "NodeStuffPlugin",
44
+ PLUGIN_NAME,
39
45
  (compilation, { normalModuleFactory }) => {
40
46
  const handler = (parser, parserOptions) => {
41
47
  if (parserOptions.node === false) return;
@@ -47,29 +53,27 @@ class NodeStuffPlugin {
47
53
 
48
54
  if (localOptions.global !== false) {
49
55
  const withWarning = localOptions.global === "warn";
50
- parser.hooks.expression
51
- .for("global")
52
- .tap("NodeStuffPlugin", expr => {
53
- const dep = new ConstDependency(
54
- RuntimeGlobals.global,
55
- expr.range,
56
- [RuntimeGlobals.global]
57
- );
58
- dep.loc = expr.loc;
59
- parser.state.module.addPresentationalDependency(dep);
56
+ parser.hooks.expression.for("global").tap(PLUGIN_NAME, expr => {
57
+ const dep = new ConstDependency(
58
+ RuntimeGlobals.global,
59
+ expr.range,
60
+ [RuntimeGlobals.global]
61
+ );
62
+ dep.loc = expr.loc;
63
+ parser.state.module.addPresentationalDependency(dep);
60
64
 
61
- // TODO webpack 6 remove
62
- if (withWarning) {
63
- parser.state.module.addWarning(
64
- new NodeStuffInWebError(
65
- dep.loc,
66
- "global",
67
- "The global namespace object is a Node.js feature and isn't available in browsers."
68
- )
69
- );
70
- }
71
- });
72
- parser.hooks.rename.for("global").tap("NodeStuffPlugin", expr => {
65
+ // TODO webpack 6 remove
66
+ if (withWarning) {
67
+ parser.state.module.addWarning(
68
+ new NodeStuffInWebError(
69
+ dep.loc,
70
+ "global",
71
+ "The global namespace object is a Node.js feature and isn't available in browsers."
72
+ )
73
+ );
74
+ }
75
+ });
76
+ parser.hooks.rename.for("global").tap(PLUGIN_NAME, expr => {
73
77
  const dep = new ConstDependency(
74
78
  RuntimeGlobals.global,
75
79
  expr.range,
@@ -84,7 +88,7 @@ class NodeStuffPlugin {
84
88
  const setModuleConstant = (expressionName, fn, warning) => {
85
89
  parser.hooks.expression
86
90
  .for(expressionName)
87
- .tap("NodeStuffPlugin", expr => {
91
+ .tap(PLUGIN_NAME, expr => {
88
92
  const dep = new CachedConstDependency(
89
93
  JSON.stringify(fn(parser.state.module)),
90
94
  expr.range,
@@ -129,7 +133,7 @@ class NodeStuffPlugin {
129
133
 
130
134
  parser.hooks.evaluateIdentifier
131
135
  .for("__filename")
132
- .tap("NodeStuffPlugin", expr => {
136
+ .tap(PLUGIN_NAME, expr => {
133
137
  if (!parser.state.module) return;
134
138
  const resource = parseResource(parser.state.module.resource);
135
139
  return evaluateToString(resource.path)(expr);
@@ -156,7 +160,7 @@ class NodeStuffPlugin {
156
160
 
157
161
  parser.hooks.evaluateIdentifier
158
162
  .for("__dirname")
159
- .tap("NodeStuffPlugin", expr => {
163
+ .tap(PLUGIN_NAME, expr => {
160
164
  if (!parser.state.module) return;
161
165
  return evaluateToString(parser.state.module.context)(expr);
162
166
  });
@@ -164,7 +168,7 @@ class NodeStuffPlugin {
164
168
  parser.hooks.expression
165
169
  .for("require.extensions")
166
170
  .tap(
167
- "NodeStuffPlugin",
171
+ PLUGIN_NAME,
168
172
  expressionIsUnsupported(
169
173
  parser,
170
174
  "require.extensions is not supported by webpack. Use a loader instead."
@@ -173,11 +177,11 @@ class NodeStuffPlugin {
173
177
  };
174
178
 
175
179
  normalModuleFactory.hooks.parser
176
- .for("javascript/auto")
177
- .tap("NodeStuffPlugin", handler);
180
+ .for(JAVASCRIPT_MODULE_TYPE_AUTO)
181
+ .tap(PLUGIN_NAME, handler);
178
182
  normalModuleFactory.hooks.parser
179
- .for("javascript/dynamic")
180
- .tap("NodeStuffPlugin", handler);
183
+ .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
184
+ .tap(PLUGIN_NAME, handler);
181
185
  }
182
186
  );
183
187
  }
@@ -22,6 +22,7 @@ const ModuleBuildError = require("./ModuleBuildError");
22
22
  const ModuleError = require("./ModuleError");
23
23
  const ModuleGraphConnection = require("./ModuleGraphConnection");
24
24
  const ModuleParseError = require("./ModuleParseError");
25
+ const { JAVASCRIPT_MODULE_TYPE_AUTO } = require("./ModuleTypeConstants");
25
26
  const ModuleWarning = require("./ModuleWarning");
26
27
  const RuntimeGlobals = require("./RuntimeGlobals");
27
28
  const UnhandledSchemeError = require("./UnhandledSchemeError");
@@ -339,7 +340,7 @@ class NormalModule extends Module {
339
340
  */
340
341
  identifier() {
341
342
  if (this.layer === null) {
342
- if (this.type === "javascript/auto") {
343
+ if (this.type === JAVASCRIPT_MODULE_TYPE_AUTO) {
343
344
  return this.request;
344
345
  } else {
345
346
  return `${this.type}|${this.request}`;
@@ -18,6 +18,7 @@ const ChunkGraph = require("./ChunkGraph");
18
18
  const Module = require("./Module");
19
19
  const ModuleFactory = require("./ModuleFactory");
20
20
  const ModuleGraph = require("./ModuleGraph");
21
+ const { JAVASCRIPT_MODULE_TYPE_AUTO } = require("./ModuleTypeConstants");
21
22
  const NormalModule = require("./NormalModule");
22
23
  const BasicEffectRulePlugin = require("./rules/BasicEffectRulePlugin");
23
24
  const BasicMatcherRulePlugin = require("./rules/BasicMatcherRulePlugin");
@@ -489,7 +490,7 @@ class NormalModuleFactory extends ModuleFactory {
489
490
  -settings.type.length - 10
490
491
  );
491
492
  } else {
492
- settings.type = "javascript/auto";
493
+ settings.type = JAVASCRIPT_MODULE_TYPE_AUTO;
493
494
  const resourceDataForRules = matchResourceData || resourceData;
494
495
  const result = this.ruleSet.exec({
495
496
  resource: resourceDataForRules.path,
@@ -510,6 +511,11 @@ class NormalModuleFactory extends ModuleFactory {
510
511
  issuerLayer: contextInfo.issuerLayer || ""
511
512
  });
512
513
  for (const r of result) {
514
+ // https://github.com/webpack/webpack/issues/16466
515
+ // if a request exists PrePostAutoLoaders, should disable modifying Rule.type
516
+ if (r.type === "type" && noPrePostAutoLoaders) {
517
+ continue;
518
+ }
513
519
  if (r.type === "use") {
514
520
  if (!noAutoLoaders && !noPrePostAutoLoaders) {
515
521
  useLoaders.push(r.value);
@@ -8,7 +8,7 @@
8
8
  const { join, dirname } = require("./util/fs");
9
9
 
10
10
  /** @typedef {import("./Compiler")} Compiler */
11
- /** @typedef {function(TODO): void} ModuleReplacer */
11
+ /** @typedef {function(import("./NormalModuleFactory").ResolveData): void} ModuleReplacer */
12
12
 
13
13
  class NormalModuleReplacementPlugin {
14
14
  /**