webpack 5.36.2 → 5.38.1

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 (113) hide show
  1. package/README.md +5 -8
  2. package/bin/webpack.js +0 -0
  3. package/lib/CacheFacade.js +3 -3
  4. package/lib/Chunk.js +10 -5
  5. package/lib/Compilation.js +45 -57
  6. package/lib/Compiler.js +9 -6
  7. package/lib/ConcatenationScope.js +2 -1
  8. package/lib/ContextModule.js +3 -3
  9. package/lib/ContextReplacementPlugin.js +4 -3
  10. package/lib/DefinePlugin.js +16 -12
  11. package/lib/EntryPlugin.js +4 -4
  12. package/lib/EvalSourceMapDevToolPlugin.js +3 -1
  13. package/lib/ExportsInfo.js +20 -13
  14. package/lib/FileSystemInfo.js +25 -31
  15. package/lib/FlagDependencyExportsPlugin.js +8 -7
  16. package/lib/FlagDependencyUsagePlugin.js +2 -4
  17. package/lib/HotModuleReplacementPlugin.js +27 -32
  18. package/lib/JavascriptMetaInfoPlugin.js +2 -1
  19. package/lib/MainTemplate.js +2 -3
  20. package/lib/ModuleFilenameHelpers.js +4 -2
  21. package/lib/ModuleGraphConnection.js +6 -2
  22. package/lib/ModuleInfoHeaderPlugin.js +2 -3
  23. package/lib/MultiCompiler.js +31 -27
  24. package/lib/NormalModule.js +42 -8
  25. package/lib/NormalModuleFactory.js +2 -0
  26. package/lib/RecordIdsPlugin.js +5 -4
  27. package/lib/ResolverFactory.js +10 -7
  28. package/lib/RuntimeGlobals.js +7 -0
  29. package/lib/RuntimePlugin.js +19 -1
  30. package/lib/SourceMapDevToolPlugin.js +14 -15
  31. package/lib/Template.js +4 -2
  32. package/lib/Watching.js +91 -47
  33. package/lib/asset/AssetGenerator.js +46 -29
  34. package/lib/asset/AssetModulesPlugin.js +1 -1
  35. package/lib/buildChunkGraph.js +21 -21
  36. package/lib/cache/PackFileCacheStrategy.js +12 -9
  37. package/lib/config/defaults.js +31 -10
  38. package/lib/config/normalization.js +16 -7
  39. package/lib/config/target.js +7 -2
  40. package/lib/debug/ProfilingPlugin.js +4 -3
  41. package/lib/dependencies/AMDRequireDependency.js +3 -3
  42. package/lib/dependencies/CommonJsExportRequireDependency.js +2 -3
  43. package/lib/dependencies/CommonJsExportsParserPlugin.js +3 -1
  44. package/lib/dependencies/CommonJsImportsParserPlugin.js +2 -4
  45. package/lib/dependencies/CommonJsPlugin.js +8 -7
  46. package/lib/dependencies/CommonJsRequireContextDependency.js +2 -1
  47. package/lib/dependencies/ContextDependencyHelpers.js +10 -8
  48. package/lib/dependencies/CreateScriptUrlDependency.js +54 -0
  49. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +11 -13
  50. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +2 -4
  51. package/lib/dependencies/HarmonyImportSpecifierDependency.js +4 -11
  52. package/lib/dependencies/ImportDependency.js +3 -3
  53. package/lib/dependencies/ImportParserPlugin.js +2 -4
  54. package/lib/dependencies/RequireEnsureDependenciesBlockParserPlugin.js +2 -3
  55. package/lib/dependencies/RequireEnsureDependency.js +3 -3
  56. package/lib/dependencies/WorkerDependency.js +6 -6
  57. package/lib/dependencies/WorkerPlugin.js +32 -20
  58. package/lib/hmr/LazyCompilationPlugin.js +6 -4
  59. package/lib/ids/HashedModuleIdsPlugin.js +3 -3
  60. package/lib/ids/OccurrenceModuleIdsPlugin.js +2 -3
  61. package/lib/index.js +5 -3
  62. package/lib/javascript/ArrayPushCallbackChunkFormatPlugin.js +4 -6
  63. package/lib/javascript/CommonJsChunkFormatPlugin.js +2 -3
  64. package/lib/javascript/JavascriptGenerator.js +3 -1
  65. package/lib/javascript/JavascriptModulesPlugin.js +15 -23
  66. package/lib/javascript/JavascriptParser.js +10 -8
  67. package/lib/library/AssignLibraryPlugin.js +4 -2
  68. package/lib/node/NodeEnvironmentPlugin.js +1 -0
  69. package/lib/node/NodeWatchFileSystem.js +19 -4
  70. package/lib/optimize/AggressiveSplittingPlugin.js +5 -4
  71. package/lib/optimize/ConcatenatedModule.js +31 -27
  72. package/lib/optimize/FlagIncludedChunksPlugin.js +4 -6
  73. package/lib/optimize/InnerGraph.js +9 -11
  74. package/lib/optimize/InnerGraphPlugin.js +3 -1
  75. package/lib/optimize/ModuleConcatenationPlugin.js +7 -10
  76. package/lib/optimize/RealContentHashPlugin.js +14 -16
  77. package/lib/optimize/SideEffectsFlagPlugin.js +6 -5
  78. package/lib/optimize/SplitChunksPlugin.js +13 -15
  79. package/lib/runtime/CreateScriptUrlRuntimeModule.js +61 -0
  80. package/lib/runtime/EnsureChunkRuntimeModule.js +9 -8
  81. package/lib/runtime/LoadScriptRuntimeModule.js +12 -5
  82. package/lib/runtime/OnChunksLoadedRuntimeModule.js +38 -37
  83. package/lib/schemes/DataUriPlugin.js +21 -2
  84. package/lib/serialization/BinaryMiddleware.js +5 -3
  85. package/lib/serialization/FileMiddleware.js +3 -1
  86. package/lib/serialization/ObjectMiddleware.js +11 -7
  87. package/lib/stats/DefaultStatsFactoryPlugin.js +7 -7
  88. package/lib/stats/DefaultStatsPrinterPlugin.js +11 -9
  89. package/lib/stats/StatsFactory.js +2 -1
  90. package/lib/stats/StatsPrinter.js +3 -3
  91. package/lib/util/LazyBucketSortedSet.js +3 -3
  92. package/lib/util/cleverMerge.js +3 -1
  93. package/lib/util/comparators.js +13 -13
  94. package/lib/util/createHash.js +5 -4
  95. package/lib/util/fs.js +8 -8
  96. package/lib/util/identifier.js +2 -1
  97. package/lib/util/internalSerializables.js +2 -0
  98. package/lib/util/serialization.js +108 -59
  99. package/lib/validateSchema.js +5 -3
  100. package/lib/wasm-async/AsyncWebAssemblyModulesPlugin.js +4 -4
  101. package/lib/wasm-sync/WasmFinalizeExportsPlugin.js +5 -4
  102. package/lib/wasm-sync/WebAssemblyGenerator.js +89 -83
  103. package/lib/wasm-sync/WebAssemblyModulesPlugin.js +2 -1
  104. package/lib/wasm-sync/WebAssemblyParser.js +6 -5
  105. package/lib/web/JsonpChunkLoadingRuntimeModule.js +2 -4
  106. package/lib/webpack.js +11 -7
  107. package/lib/webworker/ImportScriptsChunkLoadingPlugin.js +13 -1
  108. package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +14 -4
  109. package/package.json +10 -7
  110. package/schemas/WebpackOptions.check.js +1 -1
  111. package/schemas/WebpackOptions.json +107 -60
  112. package/types.d.ts +480 -285
  113. package/lib/util/DataURI.js +0 -32
@@ -225,7 +225,8 @@ class FlagDependencyExportsPlugin {
225
225
  }
226
226
 
227
227
  if (exports) {
228
- const nestedExportsInfo = exportInfo.createNestedExportsInfo();
228
+ const nestedExportsInfo =
229
+ exportInfo.createNestedExportsInfo();
229
230
  mergeExports(nestedExportsInfo, exports);
230
231
  }
231
232
 
@@ -247,12 +248,12 @@ class FlagDependencyExportsPlugin {
247
248
  const target = exportInfo.getTarget(moduleGraph);
248
249
  let targetExportsInfo = undefined;
249
250
  if (target) {
250
- const targetModuleExportsInfo = moduleGraph.getExportsInfo(
251
- target.module
252
- );
253
- targetExportsInfo = targetModuleExportsInfo.getNestedExportsInfo(
254
- target.export
255
- );
251
+ const targetModuleExportsInfo =
252
+ moduleGraph.getExportsInfo(target.module);
253
+ targetExportsInfo =
254
+ targetModuleExportsInfo.getNestedExportsInfo(
255
+ target.export
256
+ );
256
257
  // add dependency for this module
257
258
  const set = dependencies.get(target.module);
258
259
  if (set === undefined) {
@@ -201,10 +201,8 @@ class FlagDependencyUsagePlugin {
201
201
  if (oldReferencedExports === EXPORTS_OBJECT_REFERENCED) {
202
202
  continue;
203
203
  }
204
- const referencedExports = compilation.getDependencyReferencedExports(
205
- dep,
206
- runtime
207
- );
204
+ const referencedExports =
205
+ compilation.getDependencyReferencedExports(dep, runtime);
208
206
  if (
209
207
  oldReferencedExports === undefined ||
210
208
  oldReferencedExports === NO_EXPORTS_REFERENCED ||
@@ -31,7 +31,8 @@ const {
31
31
  keyToRuntime,
32
32
  forEachRuntime,
33
33
  mergeRuntimeOwned,
34
- subtractRuntime
34
+ subtractRuntime,
35
+ intersectRuntime
35
36
  } = require("./util/runtime");
36
37
 
37
38
  /** @typedef {import("./Chunk")} Chunk */
@@ -87,10 +88,8 @@ class HotModuleReplacementPlugin {
87
88
  const runtimeRequirements = [RuntimeGlobals.module];
88
89
 
89
90
  const createAcceptHandler = (parser, ParamDependency) => {
90
- const {
91
- hotAcceptCallback,
92
- hotAcceptWithoutCallback
93
- } = HotModuleReplacementPlugin.getParserHooks(parser);
91
+ const { hotAcceptCallback, hotAcceptWithoutCallback } =
92
+ HotModuleReplacementPlugin.getParserHooks(parser);
94
93
 
95
94
  return expr => {
96
95
  const module = parser.state.module;
@@ -306,9 +305,7 @@ class HotModuleReplacementPlugin {
306
305
  }
307
306
  records.chunkModuleIds = {};
308
307
  for (const chunk of compilation.chunks) {
309
- records.chunkModuleIds[
310
- chunk.id
311
- ] = Array.from(
308
+ records.chunkModuleIds[chunk.id] = Array.from(
312
309
  chunkGraph.getOrderedChunkModulesIterable(
313
310
  chunk,
314
311
  compareModulesById(chunkGraph)
@@ -341,9 +338,8 @@ class HotModuleReplacementPlugin {
341
338
  return chunkGraph.getModuleHash(module, chunk.runtime);
342
339
  }
343
340
  };
344
- const fullHashModulesInThisChunk = chunkGraph.getChunkFullHashModulesSet(
345
- chunk
346
- );
341
+ const fullHashModulesInThisChunk =
342
+ chunkGraph.getChunkFullHashModulesSet(chunk);
347
343
  if (fullHashModulesInThisChunk !== undefined) {
348
344
  for (const module of fullHashModulesInThisChunk) {
349
345
  fullHashModules.add(module, chunk);
@@ -451,16 +447,14 @@ class HotModuleReplacementPlugin {
451
447
  allOldRuntime = mergeRuntimeOwned(allOldRuntime, runtime);
452
448
  }
453
449
  forEachRuntime(allOldRuntime, runtime => {
454
- const {
455
- path: filename,
456
- info: assetInfo
457
- } = compilation.getPathWithInfo(
458
- compilation.outputOptions.hotUpdateMainFilename,
459
- {
460
- hash: records.hash,
461
- runtime
462
- }
463
- );
450
+ const { path: filename, info: assetInfo } =
451
+ compilation.getPathWithInfo(
452
+ compilation.outputOptions.hotUpdateMainFilename,
453
+ {
454
+ hash: records.hash,
455
+ runtime
456
+ }
457
+ );
464
458
  hotUpdateMainContentByRuntime.set(runtime, {
465
459
  updatedChunkIds: new Set(),
466
460
  removedChunkIds: new Set(),
@@ -509,16 +503,19 @@ class HotModuleReplacementPlugin {
509
503
  );
510
504
  if (currentChunk) {
511
505
  chunkId = currentChunk.id;
512
- newRuntime = currentChunk.runtime;
506
+ newRuntime = intersectRuntime(
507
+ currentChunk.runtime,
508
+ allOldRuntime
509
+ );
510
+ if (newRuntime === undefined) continue;
513
511
  newModules = chunkGraph
514
512
  .getChunkModules(currentChunk)
515
513
  .filter(module => updatedModules.has(module, currentChunk));
516
514
  newRuntimeModules = Array.from(
517
515
  chunkGraph.getChunkRuntimeModulesIterable(currentChunk)
518
516
  ).filter(module => updatedModules.has(module, currentChunk));
519
- const fullHashModules = chunkGraph.getChunkFullHashModulesIterable(
520
- currentChunk
521
- );
517
+ const fullHashModules =
518
+ chunkGraph.getChunkFullHashModulesIterable(currentChunk);
522
519
  newFullHashModules =
523
520
  fullHashModules &&
524
521
  Array.from(fullHashModules).filter(module =>
@@ -631,13 +628,11 @@ class HotModuleReplacementPlugin {
631
628
  filename = entry.filename;
632
629
  assetInfo = entry.info;
633
630
  } else {
634
- ({
635
- path: filename,
636
- info: assetInfo
637
- } = compilation.getPathWithInfo(
638
- entry.filenameTemplate,
639
- entry.pathOptions
640
- ));
631
+ ({ path: filename, info: assetInfo } =
632
+ compilation.getPathWithInfo(
633
+ entry.filenameTemplate,
634
+ entry.pathOptions
635
+ ));
641
636
  }
642
637
  const source = entry.render();
643
638
  compilation.additionalChunkAssets.push(filename);
@@ -34,7 +34,8 @@ class JavascriptMetaInfoPlugin {
34
34
  let topLevelDeclarations =
35
35
  parser.state.module.buildInfo.topLevelDeclarations;
36
36
  if (topLevelDeclarations === undefined) {
37
- topLevelDeclarations = parser.state.module.buildInfo.topLevelDeclarations = new Set();
37
+ topLevelDeclarations =
38
+ parser.state.module.buildInfo.topLevelDeclarations = new Set();
38
39
  }
39
40
  for (const name of parser.scope.definitions.asSet()) {
40
41
  const freeInfo = parser.getFreeInfoFromVariable(name);
@@ -239,9 +239,8 @@ class MainTemplate {
239
239
  "chunkIdExpression"
240
240
  ]),
241
241
  get jsonpScript() {
242
- const hooks = getLoadScriptRuntimeModule().getCompilationHooks(
243
- compilation
244
- );
242
+ const hooks =
243
+ getLoadScriptRuntimeModule().getCompilationHooks(compilation);
245
244
  return hooks.createScript;
246
245
  },
247
246
  get linkPrefetch() {
@@ -12,14 +12,16 @@ const ModuleFilenameHelpers = exports;
12
12
 
13
13
  // TODO webpack 6: consider removing these
14
14
  ModuleFilenameHelpers.ALL_LOADERS_RESOURCE = "[all-loaders][resource]";
15
- ModuleFilenameHelpers.REGEXP_ALL_LOADERS_RESOURCE = /\[all-?loaders\]\[resource\]/gi;
15
+ ModuleFilenameHelpers.REGEXP_ALL_LOADERS_RESOURCE =
16
+ /\[all-?loaders\]\[resource\]/gi;
16
17
  ModuleFilenameHelpers.LOADERS_RESOURCE = "[loaders][resource]";
17
18
  ModuleFilenameHelpers.REGEXP_LOADERS_RESOURCE = /\[loaders\]\[resource\]/gi;
18
19
  ModuleFilenameHelpers.RESOURCE = "[resource]";
19
20
  ModuleFilenameHelpers.REGEXP_RESOURCE = /\[resource\]/gi;
20
21
  ModuleFilenameHelpers.ABSOLUTE_RESOURCE_PATH = "[absolute-resource-path]";
21
22
  // cSpell:words olute
22
- ModuleFilenameHelpers.REGEXP_ABSOLUTE_RESOURCE_PATH = /\[abs(olute)?-?resource-?path\]/gi;
23
+ ModuleFilenameHelpers.REGEXP_ABSOLUTE_RESOURCE_PATH =
24
+ /\[abs(olute)?-?resource-?path\]/gi;
23
25
  ModuleFilenameHelpers.RESOURCE_PATH = "[resource-path]";
24
26
  ModuleFilenameHelpers.REGEXP_RESOURCE_PATH = /\[resource-?path\]/gi;
25
27
  ModuleFilenameHelpers.ALL_LOADERS = "[all-loaders]";
@@ -183,5 +183,9 @@ class ModuleGraphConnection {
183
183
 
184
184
  module.exports = ModuleGraphConnection;
185
185
  module.exports.addConnectionStates = addConnectionStates;
186
- module.exports.TRANSITIVE_ONLY = /** @type {typeof TRANSITIVE_ONLY} */ (TRANSITIVE_ONLY);
187
- module.exports.CIRCULAR_CONNECTION = /** @type {typeof CIRCULAR_CONNECTION} */ (CIRCULAR_CONNECTION);
186
+ module.exports.TRANSITIVE_ONLY = /** @type {typeof TRANSITIVE_ONLY} */ (
187
+ TRANSITIVE_ONLY
188
+ );
189
+ module.exports.CIRCULAR_CONNECTION = /** @type {typeof CIRCULAR_CONNECTION} */ (
190
+ CIRCULAR_CONNECTION
191
+ );
@@ -222,9 +222,8 @@ class ModuleInfoHeaderPlugin {
222
222
  )}`
223
223
  ) + "\n"
224
224
  );
225
- const optimizationBailout = moduleGraph.getOptimizationBailout(
226
- module
227
- );
225
+ const optimizationBailout =
226
+ moduleGraph.getOptimizationBailout(module);
228
227
  if (optimizationBailout) {
229
228
  for (const text of optimizationBailout) {
230
229
  let code;
@@ -318,16 +318,17 @@ module.exports = class MultiCompiler {
318
318
  * @returns {SetupResult[]} result of setup
319
319
  */
320
320
  _runGraph(setup, run, callback) {
321
- /** @typedef {{ compiler: Compiler, result: Stats, state: "pending" | "blocked" | "queued" | "running" | "running-outdated" | "done", children: Node[], parents: Node[] }} Node */
321
+ /** @typedef {{ compiler: Compiler, result: Stats, state: "pending" | "blocked" | "queued" | "starting" | "running" | "running-outdated" | "done", children: Node[], parents: Node[] }} Node */
322
322
 
323
323
  // State transitions for nodes:
324
324
  // -> blocked (initial)
325
- // blocked -> queued [add to queue] (when all parents done)
326
- // queued -> running [running++] (when processing the queue)
325
+ // blocked -> starting [running++] (when all parents done)
326
+ // queued -> starting [running++] (when processing the queue)
327
+ // starting -> running (when run has been called)
327
328
  // running -> done [running--] (when compilation is done)
328
329
  // done -> pending (when invalidated from file change)
329
- // pending -> blocked (when invalidated from aggregated changes)
330
- // done -> blocked (when invalidated, from parent invalidation)
330
+ // pending -> blocked [add to queue] (when invalidated from aggregated changes)
331
+ // done -> blocked [add to queue] (when invalidated, from parent invalidation)
331
332
  // running -> running-outdated (when invalidated, either from change or parent invalidation)
332
333
  // running-outdated -> blocked [running--] (when compilation is done)
333
334
 
@@ -351,6 +352,7 @@ module.exports = class MultiCompiler {
351
352
  parent.children.push(node);
352
353
  }
353
354
  }
355
+ /** @type {ArrayQueue<Node>} */
354
356
  const queue = new ArrayQueue();
355
357
  for (const node of nodes) {
356
358
  if (node.parents.length === 0) {
@@ -388,13 +390,13 @@ module.exports = class MultiCompiler {
388
390
  if (node.state === "running") {
389
391
  node.state = "done";
390
392
  for (const child of node.children) {
391
- checkUnblocked(child);
393
+ if (child.state === "blocked") queue.enqueue(child);
392
394
  }
393
395
  } else if (node.state === "running-outdated") {
394
396
  node.state = "blocked";
395
- checkUnblocked(node);
397
+ queue.enqueue(node);
396
398
  }
397
- process.nextTick(processQueue);
399
+ processQueue();
398
400
  };
399
401
  /**
400
402
  * @param {Node} node node
@@ -433,20 +435,9 @@ module.exports = class MultiCompiler {
433
435
  if (node.state === "pending") {
434
436
  node.state = "blocked";
435
437
  }
436
- checkUnblocked(node);
437
- processQueue();
438
- };
439
- /**
440
- * @param {Node} node node
441
- * @returns {void}
442
- */
443
- const checkUnblocked = node => {
444
- if (
445
- node.state === "blocked" &&
446
- node.parents.every(p => p.state === "done")
447
- ) {
448
- node.state = "queued";
438
+ if (node.state === "blocked") {
449
439
  queue.enqueue(node);
440
+ processQueue();
450
441
  }
451
442
  };
452
443
 
@@ -457,20 +448,33 @@ module.exports = class MultiCompiler {
457
448
  node.compiler,
458
449
  i,
459
450
  nodeDone.bind(null, node),
460
- () => node.state !== "done" && node.state !== "running",
451
+ () => node.state !== "starting" && node.state !== "running",
461
452
  () => nodeChange(node),
462
453
  () => nodeInvalid(node)
463
454
  )
464
455
  );
465
456
  });
457
+ let processing = true;
466
458
  const processQueue = () => {
459
+ if (processing) return;
460
+ processing = true;
461
+ process.nextTick(processQueueWorker);
462
+ };
463
+ const processQueueWorker = () => {
467
464
  while (running < parallelism && queue.length > 0 && !errored) {
468
465
  const node = queue.dequeue();
469
- if (node.state !== "queued") continue;
470
- running++;
471
- node.state = "running";
472
- run(node.compiler, nodeDone.bind(null, node));
466
+ if (
467
+ node.state === "queued" ||
468
+ (node.state === "blocked" &&
469
+ node.parents.every(p => p.state === "done"))
470
+ ) {
471
+ running++;
472
+ node.state = "starting";
473
+ run(node.compiler, nodeDone.bind(null, node));
474
+ node.state = "running";
475
+ }
473
476
  }
477
+ processing = false;
474
478
  if (
475
479
  !errored &&
476
480
  running === 0 &&
@@ -489,7 +493,7 @@ module.exports = class MultiCompiler {
489
493
  }
490
494
  }
491
495
  };
492
- processQueue();
496
+ processQueueWorker();
493
497
  return setupResults;
494
498
  }
495
499
 
@@ -41,10 +41,12 @@ const { contextify, absolutify } = require("./util/identifier");
41
41
  const makeSerializable = require("./util/makeSerializable");
42
42
  const memoize = require("./util/memoize");
43
43
 
44
- /** @typedef {import("source-map").RawSourceMap} SourceMap */
45
44
  /** @typedef {import("webpack-sources").Source} Source */
45
+ /** @typedef {import("../declarations/LoaderContext").NormalModuleLoaderContext} NormalModuleLoaderContext */
46
+ /** @typedef {import("../declarations/WebpackOptions").Mode} Mode */
46
47
  /** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
47
48
  /** @typedef {import("./ChunkGraph")} ChunkGraph */
49
+ /** @typedef {import("./Compiler")} Compiler */
48
50
  /** @typedef {import("./Dependency").UpdateHashContext} UpdateHashContext */
49
51
  /** @typedef {import("./DependencyTemplates")} DependencyTemplates */
50
52
  /** @typedef {import("./Generator")} Generator */
@@ -60,10 +62,22 @@ const memoize = require("./util/memoize");
60
62
  /** @typedef {import("./RequestShortener")} RequestShortener */
61
63
  /** @typedef {import("./ResolverFactory").ResolverWithOptions} ResolverWithOptions */
62
64
  /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
65
+ /** @typedef {import("./logging/Logger").Logger} WebpackLogger */
63
66
  /** @typedef {import("./util/Hash")} Hash */
64
67
  /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
65
68
  /** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
66
69
 
70
+ /**
71
+ * @typedef {Object} SourceMap
72
+ * @property {number} version
73
+ * @property {string[]} sources
74
+ * @property {string} mappings
75
+ * @property {string=} file
76
+ * @property {string=} sourceRoot
77
+ * @property {string[]=} sourcesContent
78
+ * @property {string[]=} names
79
+ */
80
+
67
81
  const getInvalidDependenciesModuleWarning = memoize(() =>
68
82
  require("./InvalidDependenciesModuleWarning")
69
83
  );
@@ -205,6 +219,7 @@ class NormalModule extends Module {
205
219
  * @param {string} options.rawRequest request without resolving
206
220
  * @param {LoaderItem[]} options.loaders list of loaders
207
221
  * @param {string} options.resource path + query of the real resource
222
+ * @param {Record<string, any>=} options.resourceResolveData resource resolve data
208
223
  * @param {string | undefined} options.matchResource path + query of the matched resource (virtual)
209
224
  * @param {Parser} options.parser the parser used
210
225
  * @param {object} options.parserOptions the options of the parser used
@@ -220,6 +235,7 @@ class NormalModule extends Module {
220
235
  rawRequest,
221
236
  loaders,
222
237
  resource,
238
+ resourceResolveData,
223
239
  matchResource,
224
240
  parser,
225
241
  parserOptions,
@@ -246,6 +262,7 @@ class NormalModule extends Module {
246
262
  this.generatorOptions = generatorOptions;
247
263
  /** @type {string} */
248
264
  this.resource = resource;
265
+ this.resourceResolveData = resourceResolveData;
249
266
  /** @type {string | undefined} */
250
267
  this.matchResource = matchResource;
251
268
  /** @type {LoaderItem[]} */
@@ -262,6 +279,8 @@ class NormalModule extends Module {
262
279
  this._source = null;
263
280
  /** @private @type {Map<string, number> | undefined} **/
264
281
  this._sourceSizes = undefined;
282
+ /** @private @type {Set<string>} */
283
+ this._sourceTypes = undefined;
265
284
 
266
285
  // Cache
267
286
  this._lastSuccessfulBuildMeta = {};
@@ -333,12 +352,19 @@ class NormalModule extends Module {
333
352
  this.resource = m.resource;
334
353
  this.matchResource = m.matchResource;
335
354
  this.loaders = m.loaders;
355
+ this._sourceTypes = m._sourceTypes;
356
+ this._sourceSizes = m._sourceSizes;
336
357
  }
337
358
 
338
359
  /**
339
360
  * Assuming this module is in the cache. Remove internal references to allow freeing some memory.
340
361
  */
341
362
  cleanupForCache() {
363
+ // Make sure to cache types and sizes before cleanup
364
+ if (this._sourceTypes === undefined) this.getSourceTypes();
365
+ for (const type of this._sourceTypes) {
366
+ this.size(type);
367
+ }
342
368
  super.cleanupForCache();
343
369
  this.parser = undefined;
344
370
  this.parserOptions = undefined;
@@ -376,6 +402,7 @@ class NormalModule extends Module {
376
402
  this.type,
377
403
  this.generatorOptions
378
404
  );
405
+ // we assume the generator behaves identically and keep cached sourceTypes/Sizes
379
406
  }
380
407
 
381
408
  /**
@@ -421,7 +448,7 @@ class NormalModule extends Module {
421
448
  * @param {WebpackOptions} options webpack options
422
449
  * @param {Compilation} compilation the compilation
423
450
  * @param {InputFileSystem} fs file system from reading
424
- * @returns {any} loader context
451
+ * @returns {NormalModuleLoaderContext} loader context
425
452
  */
426
453
  createLoaderContext(resolver, options, compilation, fs) {
427
454
  const { requestShortener } = compilation.runtimeTemplate;
@@ -751,7 +778,11 @@ class NormalModule extends Module {
751
778
  },
752
779
  (err, result) => {
753
780
  // Cleanup loaderContext to avoid leaking memory in ICs
754
- loaderContext._compilation = loaderContext._compiler = loaderContext._module = loaderContext.fs = undefined;
781
+ loaderContext._compilation =
782
+ loaderContext._compiler =
783
+ loaderContext._module =
784
+ loaderContext.fs =
785
+ undefined;
755
786
 
756
787
  if (!result) {
757
788
  return processResult(
@@ -855,6 +886,7 @@ class NormalModule extends Module {
855
886
  this._forceBuild = false;
856
887
  this._source = null;
857
888
  if (this._sourceSizes !== undefined) this._sourceSizes.clear();
889
+ this._sourceTypes = undefined;
858
890
  this._ast = null;
859
891
  this.error = null;
860
892
  this.clearWarningsAndErrors();
@@ -873,7 +905,7 @@ class NormalModule extends Module {
873
905
  assetsInfo: undefined
874
906
  };
875
907
 
876
- const startTime = Date.now();
908
+ const startTime = compilation.compiler.fsStartTime || Date.now();
877
909
 
878
910
  return this.doBuild(options, compilation, resolver, fs, err => {
879
911
  // if we have an error mark module as failed and exit
@@ -944,7 +976,8 @@ class NormalModule extends Module {
944
976
  checkDependencies(this.buildInfo.missingDependencies);
945
977
  checkDependencies(this.buildInfo.contextDependencies);
946
978
  if (nonAbsoluteDependencies !== undefined) {
947
- const InvalidDependenciesModuleWarning = getInvalidDependenciesModuleWarning();
979
+ const InvalidDependenciesModuleWarning =
980
+ getInvalidDependenciesModuleWarning();
948
981
  this.addWarning(
949
982
  new InvalidDependenciesModuleWarning(this, nonAbsoluteDependencies)
950
983
  );
@@ -1056,7 +1089,10 @@ class NormalModule extends Module {
1056
1089
  * @returns {Set<string>} types available (do not mutate)
1057
1090
  */
1058
1091
  getSourceTypes() {
1059
- return this.generator.getTypes(this);
1092
+ if (this._sourceTypes === undefined) {
1093
+ this._sourceTypes = this.generator.getTypes(this);
1094
+ }
1095
+ return this._sourceTypes;
1060
1096
  }
1061
1097
 
1062
1098
  /**
@@ -1245,7 +1281,6 @@ class NormalModule extends Module {
1245
1281
  const { write } = context;
1246
1282
  // deserialize
1247
1283
  write(this._source);
1248
- write(this._sourceSizes);
1249
1284
  write(this.error);
1250
1285
  write(this._lastSuccessfulBuildMeta);
1251
1286
  write(this._forceBuild);
@@ -1277,7 +1312,6 @@ class NormalModule extends Module {
1277
1312
  deserialize(context) {
1278
1313
  const { read } = context;
1279
1314
  this._source = read();
1280
- this._sourceSizes = read();
1281
1315
  this.error = read();
1282
1316
  this._lastSuccessfulBuildMeta = read();
1283
1317
  this._forceBuild = read();
@@ -168,6 +168,7 @@ const unsafeCacheData = new WeakMap();
168
168
 
169
169
  const ruleSetCompiler = new RuleSetCompiler([
170
170
  new BasicMatcherRulePlugin("test", "resource"),
171
+ new BasicMatcherRulePlugin("scheme"),
171
172
  new BasicMatcherRulePlugin("mimetype"),
172
173
  new BasicMatcherRulePlugin("dependency"),
173
174
  new BasicMatcherRulePlugin("include", "resource"),
@@ -443,6 +444,7 @@ class NormalModuleFactory extends ModuleFactory {
443
444
  realResource: resourceData.path,
444
445
  resourceQuery: resourceDataForRules.query,
445
446
  resourceFragment: resourceDataForRules.fragment,
447
+ scheme,
446
448
  mimetype: matchResourceData ? "" : resourceData.data.mimetype || "",
447
449
  dependency: dependencyType,
448
450
  descriptionData: matchResourceData
@@ -48,10 +48,11 @@ class RecordIdsPlugin {
48
48
  apply(compiler) {
49
49
  const portableIds = this.options.portableIds;
50
50
 
51
- const makePathsRelative = identifierUtils.makePathsRelative.bindContextCache(
52
- compiler.context,
53
- compiler.root
54
- );
51
+ const makePathsRelative =
52
+ identifierUtils.makePathsRelative.bindContextCache(
53
+ compiler.context,
54
+ compiler.root
55
+ );
55
56
 
56
57
  /**
57
58
  * @param {Module} module the module
@@ -42,9 +42,9 @@ const convertToResolveOptions = resolveOptionsWithDepType => {
42
42
  ...remaining,
43
43
  plugins:
44
44
  plugins &&
45
- /** @type {ResolvePluginInstance[]} */ (plugins.filter(
46
- item => item !== "..."
47
- ))
45
+ /** @type {ResolvePluginInstance[]} */ (
46
+ plugins.filter(item => item !== "...")
47
+ )
48
48
  };
49
49
 
50
50
  if (!partialOptions.fileSystem) {
@@ -53,7 +53,10 @@ const convertToResolveOptions = resolveOptionsWithDepType => {
53
53
  );
54
54
  }
55
55
  // These weird types validate that we checked all non-optional properties
56
- const options = /** @type {Partial<ResolveOptions> & Pick<ResolveOptions, "fileSystem">} */ (partialOptions);
56
+ const options =
57
+ /** @type {Partial<ResolveOptions> & Pick<ResolveOptions, "fileSystem">} */ (
58
+ partialOptions
59
+ );
57
60
 
58
61
  return removeOperations(
59
62
  resolveByProperty(options, "byDependency", dependencyType)
@@ -124,9 +127,9 @@ module.exports = class ResolverFactory {
124
127
  const resolveOptions = convertToResolveOptions(
125
128
  this.hooks.resolveOptions.for(type).call(resolveOptionsWithDepType)
126
129
  );
127
- const resolver = /** @type {ResolverWithOptions} */ (Factory.createResolver(
128
- resolveOptions
129
- ));
130
+ const resolver = /** @type {ResolverWithOptions} */ (
131
+ Factory.createResolver(resolveOptions)
132
+ );
130
133
  if (!resolver) {
131
134
  throw new Error("No resolver created");
132
135
  }
@@ -168,6 +168,13 @@ exports.scriptNonce = "__webpack_require__.nc";
168
168
  */
169
169
  exports.loadScript = "__webpack_require__.l";
170
170
 
171
+ /**
172
+ * function to promote a string to a TrustedScriptURL using webpack's Trusted
173
+ * Types policy
174
+ * Arguments: (url: string) => TrustedScriptURL
175
+ */
176
+ exports.createScriptUrl = "__webpack_require__.tu";
177
+
171
178
  /**
172
179
  * the chunk name of the chunk with the runtime
173
180
  */
@@ -13,6 +13,7 @@ const AutoPublicPathRuntimeModule = require("./runtime/AutoPublicPathRuntimeModu
13
13
  const CompatGetDefaultExportRuntimeModule = require("./runtime/CompatGetDefaultExportRuntimeModule");
14
14
  const CompatRuntimeModule = require("./runtime/CompatRuntimeModule");
15
15
  const CreateFakeNamespaceObjectRuntimeModule = require("./runtime/CreateFakeNamespaceObjectRuntimeModule");
16
+ const CreateScriptUrlRuntimeModule = require("./runtime/CreateScriptUrlRuntimeModule");
16
17
  const DefinePropertyGettersRuntimeModule = require("./runtime/DefinePropertyGettersRuntimeModule");
17
18
  const EnsureChunkRuntimeModule = require("./runtime/EnsureChunkRuntimeModule");
18
19
  const GetChunkFilenameRuntimeModule = require("./runtime/GetChunkFilenameRuntimeModule");
@@ -38,6 +39,7 @@ const GLOBALS_ON_REQUIRE = [
38
39
  RuntimeGlobals.runtimeId,
39
40
  RuntimeGlobals.compatGetDefaultExport,
40
41
  RuntimeGlobals.createFakeNamespaceObject,
42
+ RuntimeGlobals.createScriptUrl,
41
43
  RuntimeGlobals.definePropertyGetters,
42
44
  RuntimeGlobals.ensureChunk,
43
45
  RuntimeGlobals.entryModuleId,
@@ -319,7 +321,23 @@ class RuntimePlugin {
319
321
  compilation.hooks.runtimeRequirementInTree
320
322
  .for(RuntimeGlobals.loadScript)
321
323
  .tap("RuntimePlugin", (chunk, set) => {
322
- compilation.addRuntimeModule(chunk, new LoadScriptRuntimeModule());
324
+ const withCreateScriptUrl = !!compilation.outputOptions.trustedTypes;
325
+ if (withCreateScriptUrl) {
326
+ set.add(RuntimeGlobals.createScriptUrl);
327
+ }
328
+ compilation.addRuntimeModule(
329
+ chunk,
330
+ new LoadScriptRuntimeModule(withCreateScriptUrl)
331
+ );
332
+ return true;
333
+ });
334
+ compilation.hooks.runtimeRequirementInTree
335
+ .for(RuntimeGlobals.createScriptUrl)
336
+ .tap("RuntimePlugin", (chunk, set) => {
337
+ compilation.addRuntimeModule(
338
+ chunk,
339
+ new CreateScriptUrlRuntimeModule()
340
+ );
323
341
  return true;
324
342
  });
325
343
  compilation.hooks.runtimeRequirementInTree