webpack 5.65.0 → 5.69.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 (134) hide show
  1. package/README.md +1 -1
  2. package/lib/APIPlugin.js +33 -0
  3. package/lib/Cache.js +1 -1
  4. package/lib/CacheFacade.js +4 -11
  5. package/lib/Chunk.js +2 -0
  6. package/lib/ChunkGraph.js +1 -2
  7. package/lib/CleanPlugin.js +1 -1
  8. package/lib/Compilation.js +93 -47
  9. package/lib/Compiler.js +57 -3
  10. package/lib/ContextModule.js +100 -41
  11. package/lib/ContextModuleFactory.js +60 -21
  12. package/lib/DelegatedModule.js +1 -1
  13. package/lib/Dependency.js +10 -0
  14. package/lib/DependencyTemplate.js +9 -0
  15. package/lib/DependencyTemplates.js +1 -1
  16. package/lib/DllModule.js +1 -1
  17. package/lib/EvalDevToolModulePlugin.js +16 -1
  18. package/lib/EvalSourceMapDevToolPlugin.js +18 -1
  19. package/lib/ExportsInfo.js +4 -4
  20. package/lib/ExternalModule.js +93 -53
  21. package/lib/ExternalModuleFactoryPlugin.js +1 -1
  22. package/lib/FileSystemInfo.js +29 -25
  23. package/lib/Generator.js +2 -0
  24. package/lib/HookWebpackError.js +1 -1
  25. package/lib/Module.js +25 -4
  26. package/lib/ModuleFilenameHelpers.js +5 -1
  27. package/lib/MultiCompiler.js +1 -1
  28. package/lib/MultiWatching.js +1 -1
  29. package/lib/NormalModule.js +9 -5
  30. package/lib/NormalModuleFactory.js +25 -27
  31. package/lib/ProgressPlugin.js +1 -1
  32. package/lib/RawModule.js +1 -1
  33. package/lib/RuntimeGlobals.js +29 -1
  34. package/lib/RuntimeModule.js +1 -1
  35. package/lib/RuntimePlugin.js +50 -0
  36. package/lib/RuntimeTemplate.js +21 -0
  37. package/lib/Template.js +2 -1
  38. package/lib/TemplatedPathPlugin.js +48 -23
  39. package/lib/Watching.js +2 -2
  40. package/lib/WebpackOptionsApply.js +43 -2
  41. package/lib/asset/AssetGenerator.js +67 -26
  42. package/lib/asset/AssetModulesPlugin.js +3 -0
  43. package/lib/asset/RawDataUrlModule.js +148 -0
  44. package/lib/async-modules/AwaitDependenciesInitFragment.js +4 -4
  45. package/lib/buildChunkGraph.js +1 -1
  46. package/lib/cache/ResolverCachePlugin.js +81 -29
  47. package/lib/cli.js +44 -3
  48. package/lib/config/defaults.js +86 -7
  49. package/lib/config/normalization.js +5 -0
  50. package/lib/container/ContainerEntryModule.js +4 -2
  51. package/lib/container/FallbackModule.js +4 -4
  52. package/lib/container/RemoteModule.js +4 -2
  53. package/lib/css/CssExportsGenerator.js +139 -0
  54. package/lib/css/CssGenerator.js +109 -0
  55. package/lib/css/CssLoadingRuntimeModule.js +440 -0
  56. package/lib/css/CssModulesPlugin.js +462 -0
  57. package/lib/css/CssParser.js +618 -0
  58. package/lib/css/walkCssTokens.js +659 -0
  59. package/lib/debug/ProfilingPlugin.js +15 -14
  60. package/lib/dependencies/ContextElementDependency.js +8 -2
  61. package/lib/dependencies/CreateScriptUrlDependency.js +12 -0
  62. package/lib/dependencies/CssExportDependency.js +85 -0
  63. package/lib/dependencies/CssImportDependency.js +75 -0
  64. package/lib/dependencies/CssLocalIdentifierDependency.js +119 -0
  65. package/lib/dependencies/CssSelfLocalIdentifierDependency.js +101 -0
  66. package/lib/dependencies/CssUrlDependency.js +132 -0
  67. package/lib/dependencies/ExportsInfoDependency.js +6 -0
  68. package/lib/dependencies/HarmonyCompatibilityDependency.js +5 -5
  69. package/lib/dependencies/ImportMetaPlugin.js +22 -3
  70. package/lib/dependencies/LoaderPlugin.js +2 -2
  71. package/lib/dependencies/URLDependency.js +3 -8
  72. package/lib/esm/ModuleChunkLoadingRuntimeModule.js +1 -1
  73. package/lib/hmr/LazyCompilationPlugin.js +45 -21
  74. package/lib/hmr/lazyCompilationBackend.js +4 -2
  75. package/lib/ids/DeterministicModuleIdsPlugin.js +55 -35
  76. package/lib/ids/HashedModuleIdsPlugin.js +9 -12
  77. package/lib/ids/IdHelpers.js +24 -10
  78. package/lib/ids/NamedModuleIdsPlugin.js +6 -9
  79. package/lib/ids/NaturalModuleIdsPlugin.js +10 -13
  80. package/lib/ids/OccurrenceModuleIdsPlugin.js +13 -10
  81. package/lib/ids/SyncModuleIdsPlugin.js +140 -0
  82. package/lib/index.js +10 -0
  83. package/lib/javascript/JavascriptGenerator.js +1 -0
  84. package/lib/javascript/JavascriptModulesPlugin.js +27 -2
  85. package/lib/javascript/StartupHelpers.js +6 -5
  86. package/lib/library/AssignLibraryPlugin.js +34 -5
  87. package/lib/library/EnableLibraryPlugin.js +11 -0
  88. package/lib/node/NodeTargetPlugin.js +1 -0
  89. package/lib/node/ReadFileChunkLoadingRuntimeModule.js +1 -1
  90. package/lib/node/RequireChunkLoadingRuntimeModule.js +1 -1
  91. package/lib/optimize/ConcatenatedModule.js +21 -9
  92. package/lib/runtime/AsyncModuleRuntimeModule.js +25 -15
  93. package/lib/runtime/CreateScriptRuntimeModule.js +36 -0
  94. package/lib/runtime/CreateScriptUrlRuntimeModule.js +9 -34
  95. package/lib/runtime/GetTrustedTypesPolicyRuntimeModule.js +76 -0
  96. package/lib/schemes/HttpUriPlugin.js +32 -11
  97. package/lib/serialization/FileMiddleware.js +44 -9
  98. package/lib/sharing/ConsumeSharedModule.js +4 -2
  99. package/lib/sharing/ProvideSharedModule.js +4 -2
  100. package/lib/sharing/utils.js +1 -1
  101. package/lib/stats/DefaultStatsFactoryPlugin.js +112 -67
  102. package/lib/stats/DefaultStatsPrinterPlugin.js +88 -23
  103. package/lib/util/ArrayHelpers.js +18 -4
  104. package/lib/util/AsyncQueue.js +1 -1
  105. package/lib/util/compileBooleanMatcher.js +1 -1
  106. package/lib/util/deterministicGrouping.js +1 -1
  107. package/lib/util/hash/xxhash64.js +2 -2
  108. package/lib/util/identifier.js +65 -44
  109. package/lib/util/internalSerializables.js +11 -0
  110. package/lib/util/nonNumericOnlyHash.js +22 -0
  111. package/lib/util/semver.js +17 -10
  112. package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +9 -3
  113. package/lib/web/JsonpChunkLoadingRuntimeModule.js +2 -2
  114. package/lib/webworker/ImportScriptsChunkLoadingPlugin.js +3 -11
  115. package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +1 -1
  116. package/package.json +16 -23
  117. package/schemas/WebpackOptions.check.js +1 -1
  118. package/schemas/WebpackOptions.json +96 -1
  119. package/schemas/plugins/asset/AssetGeneratorOptions.check.js +1 -1
  120. package/schemas/plugins/asset/AssetResourceGeneratorOptions.check.js +1 -1
  121. package/schemas/plugins/container/ContainerPlugin.check.js +1 -1
  122. package/schemas/plugins/container/ContainerPlugin.json +2 -1
  123. package/schemas/plugins/container/ContainerReferencePlugin.check.js +1 -1
  124. package/schemas/plugins/container/ContainerReferencePlugin.json +1 -0
  125. package/schemas/plugins/container/ExternalsType.check.js +1 -1
  126. package/schemas/plugins/container/ModuleFederationPlugin.check.js +1 -1
  127. package/schemas/plugins/container/ModuleFederationPlugin.json +3 -1
  128. package/schemas/plugins/css/CssGeneratorOptions.check.d.ts +7 -0
  129. package/schemas/plugins/css/CssGeneratorOptions.check.js +6 -0
  130. package/schemas/plugins/css/CssGeneratorOptions.json +3 -0
  131. package/schemas/plugins/css/CssParserOptions.check.d.ts +7 -0
  132. package/schemas/plugins/css/CssParserOptions.check.js +6 -0
  133. package/schemas/plugins/css/CssParserOptions.json +3 -0
  134. package/types.d.ts +295 -59
package/lib/index.js CHANGED
@@ -11,6 +11,7 @@ const memoize = require("./util/memoize");
11
11
  /** @typedef {import("../declarations/WebpackOptions").Entry} Entry */
12
12
  /** @typedef {import("../declarations/WebpackOptions").EntryNormalized} EntryNormalized */
13
13
  /** @typedef {import("../declarations/WebpackOptions").EntryObject} EntryObject */
14
+ /** @typedef {import("../declarations/WebpackOptions").FileCacheOptions} FileCacheOptions */
14
15
  /** @typedef {import("../declarations/WebpackOptions").LibraryOptions} LibraryOptions */
15
16
  /** @typedef {import("../declarations/WebpackOptions").ModuleOptions} ModuleOptions */
16
17
  /** @typedef {import("../declarations/WebpackOptions").ResolveOptions} ResolveOptions */
@@ -26,11 +27,15 @@ const memoize = require("./util/memoize");
26
27
  /** @typedef {import("../declarations/WebpackOptions").WebpackPluginInstance} WebpackPluginInstance */
27
28
  /** @typedef {import("./Compilation").Asset} Asset */
28
29
  /** @typedef {import("./Compilation").AssetInfo} AssetInfo */
30
+ /** @typedef {import("./Compilation").EntryOptions} EntryOptions */
31
+ /** @typedef {import("./Compiler").AssetEmittedInfo} AssetEmittedInfo */
29
32
  /** @typedef {import("./MultiStats")} MultiStats */
30
33
  /** @typedef {import("./Parser").ParserState} ParserState */
31
34
  /** @typedef {import("./ResolverFactory").ResolvePluginInstance} ResolvePluginInstance */
32
35
  /** @typedef {import("./ResolverFactory").Resolver} Resolver */
33
36
  /** @typedef {import("./Watching")} Watching */
37
+ /** @typedef {import("./cli").Argument} Argument */
38
+ /** @typedef {import("./cli").Problem} Problem */
34
39
  /** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsAsset} StatsAsset */
35
40
  /** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsChunk} StatsChunk */
36
41
  /** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsChunkGroup} StatsChunkGroup */
@@ -564,6 +569,11 @@ module.exports = mergeExports(fn, {
564
569
  get HttpUriPlugin() {
565
570
  return require("./schemes/HttpUriPlugin");
566
571
  }
572
+ },
573
+ ids: {
574
+ get SyncModuleIdsPlugin() {
575
+ return require("./ids/SyncModuleIdsPlugin");
576
+ }
567
577
  }
568
578
  }
569
579
  });
@@ -199,6 +199,7 @@ class JavascriptGenerator extends Generator {
199
199
  runtime: generateContext.runtime,
200
200
  runtimeRequirements: generateContext.runtimeRequirements,
201
201
  concatenationScope: generateContext.concatenationScope,
202
+ codeGenerationResults: generateContext.codeGenerationResults,
202
203
  initFragments
203
204
  };
204
205
 
@@ -24,6 +24,7 @@ const { last, someInIterable } = require("../util/IterableHelpers");
24
24
  const StringXor = require("../util/StringXor");
25
25
  const { compareModulesByIdentifier } = require("../util/comparators");
26
26
  const createHash = require("../util/createHash");
27
+ const nonNumericOnlyHash = require("../util/nonNumericOnlyHash");
27
28
  const { intersectRuntime } = require("../util/runtime");
28
29
  const JavascriptGenerator = require("./JavascriptGenerator");
29
30
  const JavascriptParser = require("./JavascriptParser");
@@ -102,6 +103,7 @@ const printGeneratedCodeForStack = (module, code) => {
102
103
  /**
103
104
  * @typedef {Object} RenderBootstrapContext
104
105
  * @property {Chunk} chunk the chunk
106
+ * @property {CodeGenerationResults} codeGenerationResults results of code generation
105
107
  * @property {RuntimeTemplate} runtimeTemplate the runtime template
106
108
  * @property {ModuleGraph} moduleGraph the module graph
107
109
  * @property {ChunkGraph} chunkGraph the chunk graph
@@ -331,6 +333,7 @@ class JavascriptModulesPlugin {
331
333
  {
332
334
  hash: "0000",
333
335
  chunk,
336
+ codeGenerationResults: context.codeGenerationResults,
334
337
  chunkGraph: context.chunkGraph,
335
338
  moduleGraph: context.moduleGraph,
336
339
  runtimeTemplate: context.runtimeTemplate
@@ -343,6 +346,7 @@ class JavascriptModulesPlugin {
343
346
  compilation.hooks.contentHash.tap("JavascriptModulesPlugin", chunk => {
344
347
  const {
345
348
  chunkGraph,
349
+ codeGenerationResults,
346
350
  moduleGraph,
347
351
  runtimeTemplate,
348
352
  outputOptions: {
@@ -360,6 +364,7 @@ class JavascriptModulesPlugin {
360
364
  {
361
365
  hash: "0000",
362
366
  chunk,
367
+ codeGenerationResults,
363
368
  chunkGraph: compilation.chunkGraph,
364
369
  moduleGraph: compilation.moduleGraph,
365
370
  runtimeTemplate: compilation.runtimeTemplate
@@ -372,6 +377,7 @@ class JavascriptModulesPlugin {
372
377
  }
373
378
  hooks.chunkHash.call(chunk, hash, {
374
379
  chunkGraph,
380
+ codeGenerationResults,
375
381
  moduleGraph,
376
382
  runtimeTemplate
377
383
  });
@@ -398,7 +404,10 @@ class JavascriptModulesPlugin {
398
404
  xor.updateHash(hash);
399
405
  }
400
406
  const digest = /** @type {string} */ (hash.digest(hashDigest));
401
- chunk.contentHash.javascript = digest.substr(0, hashDigestLength);
407
+ chunk.contentHash.javascript = nonNumericOnlyHash(
408
+ digest,
409
+ hashDigestLength
410
+ );
402
411
  });
403
412
  compilation.hooks.additionalTreeRuntimeRequirements.tap(
404
413
  "JavascriptModulesPlugin",
@@ -974,7 +983,13 @@ class JavascriptModulesPlugin {
974
983
  * @returns {{ header: string[], beforeStartup: string[], startup: string[], afterStartup: string[], allowInlineStartup: boolean }} the generated source of the bootstrap code
975
984
  */
976
985
  renderBootstrap(renderContext, hooks) {
977
- const { chunkGraph, moduleGraph, chunk, runtimeTemplate } = renderContext;
986
+ const {
987
+ chunkGraph,
988
+ codeGenerationResults,
989
+ moduleGraph,
990
+ chunk,
991
+ runtimeTemplate
992
+ } = renderContext;
978
993
 
979
994
  const runtimeRequirements = chunkGraph.getTreeRuntimeRequirements(chunk);
980
995
 
@@ -1098,8 +1113,18 @@ class JavascriptModulesPlugin {
1098
1113
  );
1099
1114
  result.allowInlineStartup = false;
1100
1115
  }
1116
+
1117
+ let data;
1118
+ if (codeGenerationResults.has(entryModule, chunk.runtime)) {
1119
+ const result = codeGenerationResults.get(
1120
+ entryModule,
1121
+ chunk.runtime
1122
+ );
1123
+ data = result.data;
1124
+ }
1101
1125
  if (
1102
1126
  result.allowInlineStartup &&
1127
+ (!data || !data.get("topLevelDeclarations")) &&
1103
1128
  (!entryModule.buildInfo ||
1104
1129
  !entryModule.buildInfo.topLevelDeclarations)
1105
1130
  ) {
@@ -9,12 +9,12 @@ const RuntimeGlobals = require("../RuntimeGlobals");
9
9
  const Template = require("../Template");
10
10
  const { isSubset } = require("../util/SetHelpers");
11
11
  const { getAllChunks } = require("./ChunkHelpers");
12
- const { chunkHasJs } = require("./JavascriptModulesPlugin");
13
12
 
14
13
  /** @typedef {import("../util/Hash")} Hash */
15
14
  /** @typedef {import("../Chunk")} Chunk */
16
15
  /** @typedef {import("../Compilation")} Compilation */
17
16
  /** @typedef {import("../ChunkGraph")} ChunkGraph */
17
+ /** @typedef {import("../ChunkGraph").EntryModuleWithChunkGroup} EntryModuleWithChunkGroup */
18
18
  /** @typedef {import("../ChunkGroup")} ChunkGroup */
19
19
  /** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
20
20
  /** @typedef {(string|number)[]} EntryItem */
@@ -24,7 +24,7 @@ const EXPORT_PREFIX = "var __webpack_exports__ = ";
24
24
  /**
25
25
  * @param {ChunkGraph} chunkGraph chunkGraph
26
26
  * @param {RuntimeTemplate} runtimeTemplate runtimeTemplate
27
- * @param {import("../ChunkGraph").EntryModuleWithChunkGroup[]} entries entries
27
+ * @param {EntryModuleWithChunkGroup[]} entries entries
28
28
  * @param {Chunk} chunk chunk
29
29
  * @param {boolean} passive true: passive startup with on chunks loaded
30
30
  * @returns {string} runtime code
@@ -102,7 +102,7 @@ exports.generateEntryStartup = (
102
102
  /**
103
103
  * @param {Hash} hash the hash to update
104
104
  * @param {ChunkGraph} chunkGraph chunkGraph
105
- * @param {import("../ChunkGraph").EntryModuleWithChunkGroup[]} entries entries
105
+ * @param {EntryModuleWithChunkGroup[]} entries entries
106
106
  * @param {Chunk} chunk chunk
107
107
  * @returns {void}
108
108
  */
@@ -119,12 +119,13 @@ exports.updateHashForEntryStartup = (hash, chunkGraph, entries, chunk) => {
119
119
  /**
120
120
  * @param {Chunk} chunk the chunk
121
121
  * @param {ChunkGraph} chunkGraph the chunk graph
122
+ * @param {function(Chunk, ChunkGraph): boolean} filterFn filter function
122
123
  * @returns {Set<number | string>} initially fulfilled chunk ids
123
124
  */
124
- exports.getInitialChunkIds = (chunk, chunkGraph) => {
125
+ exports.getInitialChunkIds = (chunk, chunkGraph, filterFn) => {
125
126
  const initialChunkIds = new Set(chunk.ids);
126
127
  for (const c of chunk.getAllInitialChunks()) {
127
- if (c === chunk || chunkHasJs(c, chunkGraph)) continue;
128
+ if (c === chunk || filterFn(c, chunkGraph)) continue;
128
129
  for (const id of c.ids) initialChunkIds.add(id);
129
130
  }
130
131
  return initialChunkIds;
@@ -92,7 +92,7 @@ const accessWithInit = (accessor, existingLength, initLast = false) => {
92
92
  * @property {LibraryType} type
93
93
  * @property {string[] | "global"} prefix name prefix
94
94
  * @property {string | false} declare declare name as variable
95
- * @property {"error"|"copy"|"assign"} unnamed behavior for unnamed library name
95
+ * @property {"error"|"static"|"copy"|"assign"} unnamed behavior for unnamed library name
96
96
  * @property {"copy"|"assign"=} named behavior for named library name
97
97
  */
98
98
 
@@ -222,9 +222,15 @@ class AssignLibraryPlugin extends AbstractLibraryPlugin {
222
222
  * @param {LibraryContext<T>} libraryContext context
223
223
  * @returns {string | undefined} bailout reason
224
224
  */
225
- embedInRuntimeBailout(module, { chunk }, { options, compilation }) {
225
+ embedInRuntimeBailout(
226
+ module,
227
+ { chunk, codeGenerationResults },
228
+ { options, compilation }
229
+ ) {
230
+ const { data } = codeGenerationResults.get(module, chunk.runtime);
226
231
  const topLevelDeclarations =
227
- module.buildInfo && module.buildInfo.topLevelDeclarations;
232
+ (data && data.get("topLevelDeclarations")) ||
233
+ (module.buildInfo && module.buildInfo.topLevelDeclarations);
228
234
  if (!topLevelDeclarations)
229
235
  return "it doesn't tell about top level declarations.";
230
236
  const fullNameResolved = this._getResolvedFullName(
@@ -261,19 +267,42 @@ class AssignLibraryPlugin extends AbstractLibraryPlugin {
261
267
  * @param {LibraryContext<T>} libraryContext context
262
268
  * @returns {Source} source with library export
263
269
  */
264
- renderStartup(source, module, { chunk }, { options, compilation }) {
270
+ renderStartup(
271
+ source,
272
+ module,
273
+ { moduleGraph, chunk },
274
+ { options, compilation }
275
+ ) {
265
276
  const fullNameResolved = this._getResolvedFullName(
266
277
  options,
267
278
  chunk,
268
279
  compilation
269
280
  );
281
+ const staticExports = this.unnamed === "static";
270
282
  const exportAccess = options.export
271
283
  ? propertyAccess(
272
284
  Array.isArray(options.export) ? options.export : [options.export]
273
285
  )
274
286
  : "";
275
287
  const result = new ConcatSource(source);
276
- if (options.name ? this.named === "copy" : this.unnamed === "copy") {
288
+ if (staticExports) {
289
+ const exportsInfo = moduleGraph.getExportsInfo(module);
290
+ const exportTarget = accessWithInit(
291
+ fullNameResolved,
292
+ this._getPrefix(compilation).length,
293
+ true
294
+ );
295
+ for (const exportInfo of exportsInfo.orderedExports) {
296
+ if (!exportInfo.provided) continue;
297
+ const nameAccess = propertyAccess([exportInfo.name]);
298
+ result.add(
299
+ `${exportTarget}${nameAccess} = __webpack_exports__${exportAccess}${nameAccess};\n`
300
+ );
301
+ }
302
+ result.add(
303
+ `Object.defineProperty(${exportTarget}, "__esModule", { value: true });\n`
304
+ );
305
+ } else if (options.name ? this.named === "copy" : this.unnamed === "copy") {
277
306
  result.add(
278
307
  `var __webpack_export_target__ = ${accessWithInit(
279
308
  fullNameResolved,
@@ -167,6 +167,17 @@ class EnableLibraryPlugin {
167
167
  }).apply(compiler);
168
168
  break;
169
169
  }
170
+ case "commonjs-static": {
171
+ //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
172
+ const AssignLibraryPlugin = require("./AssignLibraryPlugin");
173
+ new AssignLibraryPlugin({
174
+ type,
175
+ prefix: ["exports"],
176
+ declare: false,
177
+ unnamed: "static"
178
+ }).apply(compiler);
179
+ break;
180
+ }
170
181
  case "commonjs2":
171
182
  case "commonjs-module": {
172
183
  //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
@@ -54,6 +54,7 @@ const builtins = [
54
54
  "tty",
55
55
  "url",
56
56
  "util",
57
+ "util/types",
57
58
  "v8",
58
59
  "vm",
59
60
  "wasi",
@@ -46,7 +46,7 @@ class ReadFileChunkLoadingRuntimeModule extends RuntimeModule {
46
46
  );
47
47
  const conditionMap = chunkGraph.getChunkConditionMap(chunk, chunkHasJs);
48
48
  const hasJsMatcher = compileBooleanMatcher(conditionMap);
49
- const initialChunkIds = getInitialChunkIds(chunk, chunkGraph);
49
+ const initialChunkIds = getInitialChunkIds(chunk, chunkGraph, chunkHasJs);
50
50
 
51
51
  const outputName = this.compilation.getPath(
52
52
  getChunkFilenameTemplate(chunk, this.compilation.outputOptions),
@@ -46,7 +46,7 @@ class RequireChunkLoadingRuntimeModule extends RuntimeModule {
46
46
  );
47
47
  const conditionMap = chunkGraph.getChunkConditionMap(chunk, chunkHasJs);
48
48
  const hasJsMatcher = compileBooleanMatcher(conditionMap);
49
- const initialChunkIds = getInitialChunkIds(chunk, chunkGraph);
49
+ const initialChunkIds = getInitialChunkIds(chunk, chunkGraph, chunkHasJs);
50
50
 
51
51
  const outputName = this.compilation.getPath(
52
52
  getChunkFilenameTemplate(chunk, this.compilation.outputOptions),
@@ -39,6 +39,7 @@ const {
39
39
  /** @typedef {import("webpack-sources").Source} Source */
40
40
  /** @typedef {import("../../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
41
41
  /** @typedef {import("../ChunkGraph")} ChunkGraph */
42
+ /** @typedef {import("../CodeGenerationResults")} CodeGenerationResults */
42
43
  /** @typedef {import("../Compilation")} Compilation */
43
44
  /** @typedef {import("../Dependency")} Dependency */
44
45
  /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
@@ -821,10 +822,6 @@ class ConcatenatedModule extends Module {
821
822
  const topLevelDeclarations = this.buildInfo.topLevelDeclarations;
822
823
  if (topLevelDeclarations !== undefined) {
823
824
  for (const decl of m.buildInfo.topLevelDeclarations) {
824
- // reserved names will always be renamed
825
- if (RESERVED_NAMES.has(decl)) continue;
826
- // TODO actually this is incorrect since with renaming there could be more
827
- // We should do the renaming during build
828
825
  topLevelDeclarations.add(decl);
829
826
  }
830
827
  }
@@ -1077,7 +1074,8 @@ class ConcatenatedModule extends Module {
1077
1074
  runtimeTemplate,
1078
1075
  moduleGraph,
1079
1076
  chunkGraph,
1080
- runtime: generationRuntime
1077
+ runtime: generationRuntime,
1078
+ codeGenerationResults
1081
1079
  }) {
1082
1080
  /** @type {Set<string>} */
1083
1081
  const runtimeRequirements = new Set();
@@ -1104,12 +1102,15 @@ class ConcatenatedModule extends Module {
1104
1102
  runtimeTemplate,
1105
1103
  moduleGraph,
1106
1104
  chunkGraph,
1107
- runtime
1105
+ runtime,
1106
+ codeGenerationResults
1108
1107
  );
1109
1108
  }
1110
1109
 
1111
1110
  // List of all used names to avoid conflicts
1112
1111
  const allUsedNames = new Set(RESERVED_NAMES);
1112
+ // Updated Top level declarations are created by renaming
1113
+ const topLevelDeclarations = new Set();
1113
1114
 
1114
1115
  // List of additional names in scope for module references
1115
1116
  /** @type {Map<string, { usedNames: Set<string>, alreadyCheckedScopes: Set<TODO> }>} */
@@ -1254,6 +1255,7 @@ class ConcatenatedModule extends Module {
1254
1255
  );
1255
1256
  allUsedNames.add(newName);
1256
1257
  info.internalNames.set(name, newName);
1258
+ topLevelDeclarations.add(newName);
1257
1259
  const source = info.source;
1258
1260
  const allIdentifiers = new Set(
1259
1261
  references.map(r => r.identifier).concat(variable.identifiers)
@@ -1280,6 +1282,7 @@ class ConcatenatedModule extends Module {
1280
1282
  } else {
1281
1283
  allUsedNames.add(name);
1282
1284
  info.internalNames.set(name, name);
1285
+ topLevelDeclarations.add(name);
1283
1286
  }
1284
1287
  }
1285
1288
  let namespaceObjectName;
@@ -1297,6 +1300,7 @@ class ConcatenatedModule extends Module {
1297
1300
  allUsedNames.add(namespaceObjectName);
1298
1301
  }
1299
1302
  info.namespaceObjectName = namespaceObjectName;
1303
+ topLevelDeclarations.add(namespaceObjectName);
1300
1304
  break;
1301
1305
  }
1302
1306
  case "external": {
@@ -1308,6 +1312,7 @@ class ConcatenatedModule extends Module {
1308
1312
  );
1309
1313
  allUsedNames.add(externalName);
1310
1314
  info.name = externalName;
1315
+ topLevelDeclarations.add(externalName);
1311
1316
  break;
1312
1317
  }
1313
1318
  }
@@ -1320,6 +1325,7 @@ class ConcatenatedModule extends Module {
1320
1325
  );
1321
1326
  allUsedNames.add(externalNameInterop);
1322
1327
  info.interopNamespaceObjectName = externalNameInterop;
1328
+ topLevelDeclarations.add(externalNameInterop);
1323
1329
  }
1324
1330
  if (
1325
1331
  info.module.buildMeta.exportsType === "default" &&
@@ -1333,6 +1339,7 @@ class ConcatenatedModule extends Module {
1333
1339
  );
1334
1340
  allUsedNames.add(externalNameInterop);
1335
1341
  info.interopNamespaceObject2Name = externalNameInterop;
1342
+ topLevelDeclarations.add(externalNameInterop);
1336
1343
  }
1337
1344
  if (
1338
1345
  info.module.buildMeta.exportsType === "dynamic" ||
@@ -1346,6 +1353,7 @@ class ConcatenatedModule extends Module {
1346
1353
  );
1347
1354
  allUsedNames.add(externalNameInterop);
1348
1355
  info.interopDefaultAccessName = externalNameInterop;
1356
+ topLevelDeclarations.add(externalNameInterop);
1349
1357
  }
1350
1358
  }
1351
1359
 
@@ -1615,6 +1623,7 @@ ${defineGetters}`
1615
1623
  const data = new Map();
1616
1624
  if (chunkInitFragments.length > 0)
1617
1625
  data.set("chunkInitFragments", chunkInitFragments);
1626
+ data.set("topLevelDeclarations", topLevelDeclarations);
1618
1627
 
1619
1628
  /** @type {CodeGenerationResult} */
1620
1629
  const resultEntry = {
@@ -1634,6 +1643,7 @@ ${defineGetters}`
1634
1643
  * @param {ModuleGraph} moduleGraph moduleGraph
1635
1644
  * @param {ChunkGraph} chunkGraph chunkGraph
1636
1645
  * @param {RuntimeSpec} runtime runtime
1646
+ * @param {CodeGenerationResults} codeGenerationResults codeGenerationResults
1637
1647
  */
1638
1648
  _analyseModule(
1639
1649
  modulesMap,
@@ -1642,7 +1652,8 @@ ${defineGetters}`
1642
1652
  runtimeTemplate,
1643
1653
  moduleGraph,
1644
1654
  chunkGraph,
1645
- runtime
1655
+ runtime,
1656
+ codeGenerationResults
1646
1657
  ) {
1647
1658
  if (info.type === "concatenated") {
1648
1659
  const m = info.module;
@@ -1657,7 +1668,8 @@ ${defineGetters}`
1657
1668
  moduleGraph,
1658
1669
  chunkGraph,
1659
1670
  runtime,
1660
- concatenationScope
1671
+ concatenationScope,
1672
+ codeGenerationResults
1661
1673
  });
1662
1674
  const source = codeGenResult.sources.get("javascript");
1663
1675
  const data = codeGenResult.data;
@@ -1702,7 +1714,7 @@ ${defineGetters}`
1702
1714
  info.globalScope = globalScope;
1703
1715
  info.moduleScope = moduleScope;
1704
1716
  } catch (err) {
1705
- err.message += `\nwhile analysing module ${m.identifier()} for concatenation`;
1717
+ err.message += `\nwhile analyzing module ${m.identifier()} for concatenation`;
1706
1718
  throw err;
1707
1719
  }
1708
1720
  }
@@ -22,6 +22,7 @@ class AsyncModuleRuntimeModule extends HelperRuntimeModule {
22
22
  return Template.asString([
23
23
  'var webpackThen = typeof Symbol === "function" ? Symbol("webpack then") : "__webpack_then__";',
24
24
  'var webpackExports = typeof Symbol === "function" ? Symbol("webpack exports") : "__webpack_exports__";',
25
+ 'var webpackError = typeof Symbol === "function" ? Symbol("webpack error") : "__webpack_error__";',
25
26
  `var completeQueue = ${runtimeTemplate.basicFunction("queue", [
26
27
  "if(queue) {",
27
28
  Template.indent([
@@ -56,9 +57,13 @@ class AsyncModuleRuntimeModule extends HelperRuntimeModule {
56
57
  "obj[webpackExports] = r;",
57
58
  "completeQueue(queue);",
58
59
  "queue = 0;"
60
+ ])}, ${runtimeTemplate.basicFunction("e", [
61
+ "obj[webpackError] = e;",
62
+ "completeQueue(queue);",
63
+ "queue = 0;"
59
64
  ])});`,
60
- `var obj = {};
61
- obj[webpackThen] = ${runtimeTemplate.expressionFunction(
65
+ "var obj = {};",
66
+ `obj[webpackThen] = ${runtimeTemplate.expressionFunction(
62
67
  "queueFunction(queue, fn), dep['catch'](reject)",
63
68
  "fn, reject"
64
69
  )};`,
@@ -67,13 +72,13 @@ class AsyncModuleRuntimeModule extends HelperRuntimeModule {
67
72
  "}"
68
73
  ]),
69
74
  "}",
70
- `var ret = {};
71
- ret[webpackThen] = ${runtimeTemplate.expressionFunction(
75
+ "var ret = {};",
76
+ `ret[webpackThen] = ${runtimeTemplate.expressionFunction(
72
77
  "completeFunction(fn)",
73
78
  "fn"
74
- )};
75
- ret[webpackExports] = dep;
76
- return ret;`
79
+ )};`,
80
+ "ret[webpackExports] = dep;",
81
+ "return ret;"
77
82
  ])})`,
78
83
  "deps"
79
84
  )};`,
@@ -119,24 +124,29 @@ class AsyncModuleRuntimeModule extends HelperRuntimeModule {
119
124
  )};`,
120
125
  "module.exports = promise;",
121
126
  `body(${runtimeTemplate.basicFunction("deps", [
122
- "if(!deps) return outerResolve();",
123
127
  "currentDeps = wrapDeps(deps);",
124
- "var fn, result;",
128
+ "var fn;",
129
+ `var getResult = ${runtimeTemplate.returningFunction(
130
+ `currentDeps.map(${runtimeTemplate.basicFunction("d", [
131
+ "if(d[webpackError]) throw d[webpackError];",
132
+ "return d[webpackExports];"
133
+ ])})`
134
+ )}`,
125
135
  `var promise = new Promise(${runtimeTemplate.basicFunction(
126
136
  "resolve, reject",
127
137
  [
128
138
  `fn = ${runtimeTemplate.expressionFunction(
129
- `resolve(result = currentDeps.map(${runtimeTemplate.returningFunction(
130
- "d[webpackExports]",
131
- "d"
132
- )}))`
139
+ "resolve(getResult)"
133
140
  )};`,
134
141
  "fn.r = 0;",
135
142
  "whenAll(currentDeps, fn, reject);"
136
143
  ]
137
144
  )});`,
138
- "return fn.r ? promise : result;"
139
- ])}).then(outerResolve, reject);`,
145
+ "return fn.r ? promise : getResult();"
146
+ ])}, ${runtimeTemplate.expressionFunction(
147
+ "err && reject(promise[webpackError] = err), outerResolve()",
148
+ "err"
149
+ )});`,
140
150
  "isEvaluating = false;"
141
151
  ])};`
142
152
  ]);
@@ -0,0 +1,36 @@
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ */
4
+
5
+ "use strict";
6
+
7
+ const RuntimeGlobals = require("../RuntimeGlobals");
8
+ const Template = require("../Template");
9
+ const HelperRuntimeModule = require("./HelperRuntimeModule");
10
+
11
+ class CreateScriptRuntimeModule extends HelperRuntimeModule {
12
+ constructor() {
13
+ super("trusted types script");
14
+ }
15
+
16
+ /**
17
+ * @returns {string} runtime code
18
+ */
19
+ generate() {
20
+ const { compilation } = this;
21
+ const { runtimeTemplate, outputOptions } = compilation;
22
+ const { trustedTypes } = outputOptions;
23
+ const fn = RuntimeGlobals.createScript;
24
+
25
+ return Template.asString(
26
+ `${fn} = ${runtimeTemplate.returningFunction(
27
+ trustedTypes
28
+ ? `${RuntimeGlobals.getTrustedTypesPolicy}().createScript(script)`
29
+ : "script",
30
+ "script"
31
+ )};`
32
+ );
33
+ }
34
+ }
35
+
36
+ module.exports = CreateScriptRuntimeModule;
@@ -10,7 +10,7 @@ const HelperRuntimeModule = require("./HelperRuntimeModule");
10
10
 
11
11
  class CreateScriptUrlRuntimeModule extends HelperRuntimeModule {
12
12
  constructor() {
13
- super("trusted types");
13
+ super("trusted types script url");
14
14
  }
15
15
 
16
16
  /**
@@ -22,39 +22,14 @@ class CreateScriptUrlRuntimeModule extends HelperRuntimeModule {
22
22
  const { trustedTypes } = outputOptions;
23
23
  const fn = RuntimeGlobals.createScriptUrl;
24
24
 
25
- if (!trustedTypes) {
26
- // Skip Trusted Types logic.
27
- return Template.asString([
28
- `${fn} = ${runtimeTemplate.returningFunction("url", "url")};`
29
- ]);
30
- }
31
-
32
- return Template.asString([
33
- "var policy;",
34
- `${fn} = ${runtimeTemplate.basicFunction("url", [
35
- "// Create Trusted Type policy if Trusted Types are available and the policy doesn't exist yet.",
36
- "if (policy === undefined) {",
37
- Template.indent([
38
- "policy = {",
39
- Template.indent([
40
- `createScriptURL: ${runtimeTemplate.returningFunction(
41
- "url",
42
- "url"
43
- )}`
44
- ]),
45
- "};",
46
- 'if (typeof trustedTypes !== "undefined" && trustedTypes.createPolicy) {',
47
- Template.indent([
48
- `policy = trustedTypes.createPolicy(${JSON.stringify(
49
- trustedTypes.policyName
50
- )}, policy);`
51
- ]),
52
- "}"
53
- ]),
54
- "}",
55
- "return policy.createScriptURL(url);"
56
- ])};`
57
- ]);
25
+ return Template.asString(
26
+ `${fn} = ${runtimeTemplate.returningFunction(
27
+ trustedTypes
28
+ ? `${RuntimeGlobals.getTrustedTypesPolicy}().createScriptURL(url)`
29
+ : "url",
30
+ "url"
31
+ )};`
32
+ );
58
33
  }
59
34
  }
60
35