webpack 5.108.3 → 5.109.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.
Files changed (191) hide show
  1. package/lib/APIPlugin.js +48 -29
  2. package/lib/CacheFacade.js +8 -0
  3. package/lib/ChunkGroup.js +1 -0
  4. package/lib/CompatibilityPlugin.js +31 -29
  5. package/lib/Compilation.js +50 -8
  6. package/lib/ConstPlugin.js +11 -11
  7. package/lib/ContextModule.js +239 -6
  8. package/lib/ContextModuleFactory.js +139 -14
  9. package/lib/DefinePlugin.js +396 -101
  10. package/lib/Entrypoint.js +1 -1
  11. package/lib/ExportsInfoApiPlugin.js +2 -2
  12. package/lib/ExternalModule.js +107 -5
  13. package/lib/ExternalModuleFactoryPlugin.js +20 -1
  14. package/lib/FileSystemInfo.js +130 -74
  15. package/lib/FlagDependencyExportsPlugin.js +55 -8
  16. package/lib/HotModuleReplacementPlugin.js +18 -15
  17. package/lib/LazyBarrel.js +30 -3
  18. package/lib/Module.js +3 -0
  19. package/lib/ModuleFilenameHelpers.js +2 -2
  20. package/lib/ModuleTypeConstants.js +8 -1
  21. package/lib/MultiCompiler.js +13 -2
  22. package/lib/MultiWatching.js +1 -0
  23. package/lib/NodeStuffPlugin.js +51 -33
  24. package/lib/NormalModule.js +169 -405
  25. package/lib/NormalModuleFactory.js +76 -2
  26. package/lib/ProgressPlugin.js +211 -119
  27. package/lib/ProvidePlugin.js +2 -2
  28. package/lib/RuntimeGlobals.js +47 -0
  29. package/lib/RuntimePlugin.js +33 -0
  30. package/lib/RuntimeTemplate.js +304 -16
  31. package/lib/UseStrictPlugin.js +1 -1
  32. package/lib/WebpackIsIncludedPlugin.js +1 -1
  33. package/lib/WebpackOptionsApply.js +24 -19
  34. package/lib/asset/AssetGenerator.js +110 -28
  35. package/lib/asset/AssetModulesPlugin.js +92 -2
  36. package/lib/asset/WebManifestGenerator.js +164 -0
  37. package/lib/asset/WebManifestParser.js +130 -0
  38. package/lib/async-modules/AwaitDependenciesInitFragment.js +14 -4
  39. package/lib/async-modules/isGeneratorLowered.js +26 -0
  40. package/lib/cache/PackFileCacheStrategy.js +9 -3
  41. package/lib/config/browserslistTargetHandler.js +40 -0
  42. package/lib/config/defaults.js +222 -32
  43. package/lib/config/normalization.js +26 -0
  44. package/lib/config/target.js +9 -1
  45. package/lib/css/CssGenerator.js +73 -73
  46. package/lib/css/CssLoadingRuntimeModule.js +5 -1
  47. package/lib/css/CssModulesPlugin.js +92 -53
  48. package/lib/css/CssParser.js +843 -302
  49. package/lib/css/syntax.js +1219 -483
  50. package/lib/dependencies/AMDDefineDependencyParserPlugin.js +6 -6
  51. package/lib/dependencies/AMDPlugin.js +2 -2
  52. package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +13 -13
  53. package/lib/dependencies/CommonJsDependencyHelpers.js +39 -0
  54. package/lib/dependencies/CommonJsExportsDependency.js +47 -2
  55. package/lib/dependencies/CommonJsExportsParserPlugin.js +10 -10
  56. package/lib/dependencies/CommonJsImportsParserPlugin.js +18 -18
  57. package/lib/dependencies/CommonJsPlugin.js +2 -2
  58. package/lib/dependencies/CommonJsRequireDependency.js +26 -1
  59. package/lib/dependencies/CommonJsSelfReferenceDependency.js +32 -1
  60. package/lib/dependencies/ContextDependencyHelpers.js +3 -3
  61. package/lib/dependencies/CreateRequireParserPlugin.js +6 -6
  62. package/lib/dependencies/CssIcssExportDependency.js +190 -87
  63. package/lib/dependencies/CssUrlDependency.js +35 -4
  64. package/lib/dependencies/ExternalModuleInitFragmentDependency.js +2 -2
  65. package/lib/dependencies/HarmonyCompatibilityDependency.js +32 -14
  66. package/lib/dependencies/HarmonyDetectionParserPlugin.js +43 -13
  67. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +7 -19
  68. package/lib/dependencies/HarmonyImportDependency.js +12 -1
  69. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +34 -107
  70. package/lib/dependencies/HarmonyImportGuard.js +192 -20
  71. package/lib/dependencies/HarmonyModulesPlugin.js +6 -0
  72. package/lib/dependencies/HarmonyTopLevelThisParserPlugin.js +1 -1
  73. package/lib/dependencies/HtmlEntryDependency.js +797 -172
  74. package/lib/dependencies/HtmlSourceDependency.js +26 -0
  75. package/lib/dependencies/ImportDependency.js +43 -1
  76. package/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +31 -18
  77. package/lib/dependencies/ImportMetaContextPlugin.js +38 -7
  78. package/lib/dependencies/ImportMetaGlobDependency.js +81 -0
  79. package/lib/dependencies/ImportMetaGlobDependencyParserPlugin.js +78 -0
  80. package/lib/dependencies/ImportMetaGlobHelpers.js +660 -0
  81. package/lib/dependencies/ImportMetaPlugin.js +532 -180
  82. package/lib/dependencies/ImportMetaResolveDependency.js +90 -0
  83. package/lib/dependencies/ImportParserPlugin.js +87 -26
  84. package/lib/dependencies/ImportPhase.js +10 -8
  85. package/lib/dependencies/LoaderPlugin.js +5 -1
  86. package/lib/dependencies/RequireContextDependencyParserPlugin.js +1 -1
  87. package/lib/dependencies/RequireEnsureDependenciesBlockParserPlugin.js +3 -6
  88. package/lib/dependencies/RequireIncludeDependencyParserPlugin.js +4 -11
  89. package/lib/dependencies/SystemPlugin.js +2 -4
  90. package/lib/dependencies/TopLevelAwaitDependency.js +93 -0
  91. package/lib/dependencies/URLDependency.js +141 -21
  92. package/lib/dependencies/{WorkerPlugin.js → WorkerAndWorkletPlugin.js} +324 -112
  93. package/lib/dependencies/WorkerDependency.js +86 -18
  94. package/lib/dependencies/WorkletDependency.js +278 -0
  95. package/lib/dll/DelegatedModuleFactoryPlugin.js +19 -0
  96. package/lib/errors/EnvironmentNotSupportAsyncWarning.js +1 -1
  97. package/lib/errors/LoaderLoadingError.js +20 -0
  98. package/lib/errors/ModuleParseError.js +39 -22
  99. package/lib/errors/NodeStuffInWebError.js +1 -1
  100. package/lib/esm/ModuleChunkFormatPlugin.js +47 -14
  101. package/lib/esm/ModuleChunkLoadingPlugin.js +8 -1
  102. package/lib/esm/ModuleChunkLoadingRuntimeModule.js +29 -1
  103. package/lib/hmr/lazyCompilationBackend.js +32 -8
  104. package/lib/html/HtmlGenerator.js +940 -91
  105. package/lib/html/HtmlModule.js +3 -2
  106. package/lib/html/HtmlModulesPlugin.js +625 -170
  107. package/lib/html/HtmlParser.js +812 -340
  108. package/lib/html/favicon.svg +1 -0
  109. package/lib/html/syntax.js +2116 -834
  110. package/lib/ids/IdHelpers.js +1 -1
  111. package/lib/index.js +6 -0
  112. package/lib/javascript/ArrayPushCallbackChunkFormatPlugin.js +4 -5
  113. package/lib/javascript/BasicEvaluatedExpression.js +366 -48
  114. package/lib/javascript/JavascriptGenerator.js +197 -49
  115. package/lib/javascript/JavascriptModulesPlugin.js +178 -24
  116. package/lib/javascript/JavascriptParser.js +711 -355
  117. package/lib/javascript/JavascriptParserHelpers.js +3 -6
  118. package/lib/javascript/syntax.js +4380 -0
  119. package/lib/library/ModuleLibraryPlugin.js +38 -1
  120. package/lib/loaders/LoaderRunner.js +714 -0
  121. package/lib/loaders/loadLoader.js +100 -0
  122. package/lib/node/ReadFileCompileAsyncWasmPlugin.js +19 -0
  123. package/lib/node/ReadFileCompileWasmPlugin.js +14 -0
  124. package/lib/optimize/ConcatenatedModule.js +120 -16
  125. package/lib/optimize/FlagIncludedChunksPlugin.js +13 -3
  126. package/lib/optimize/InnerGraphPlugin.js +50 -30
  127. package/lib/optimize/ModuleConcatenationPlugin.js +69 -3
  128. package/lib/optimize/SideEffectsFlagPlugin.js +86 -43
  129. package/lib/prefetch/ChunkPrefetchPreloadPlugin.js +15 -0
  130. package/lib/prefetch/ChunkPreloadTriggerRuntimeModule.js +24 -13
  131. package/lib/prefetch/ResourceHintPlugin.js +663 -0
  132. package/lib/prefetch/ResourceHintRuntimeModule.js +149 -0
  133. package/lib/prefetch/StartupAssetHintRuntimeModule.js +210 -0
  134. package/lib/prefetch/parseResourceHintOptions.js +103 -0
  135. package/lib/rules/RuleSetCompiler.js +114 -0
  136. package/lib/runtime/AsyncModuleGeneratorRuntimeModule.js +56 -0
  137. package/lib/runtime/AsyncModuleRuntimeModule.js +24 -8
  138. package/lib/runtime/AutoPublicPathRuntimeModule.js +7 -1
  139. package/lib/runtime/CommonJsWrapRuntimeModule.js +37 -0
  140. package/lib/runtime/GetWorkletBootstrapRuntimeModule.js +59 -0
  141. package/lib/runtime/MakeDeferredNamespaceObjectRuntime.js +204 -8
  142. package/lib/runtime/SetAnonymousDefaultNameRuntimeModule.js +4 -1
  143. package/lib/schemes/HttpUriPlugin.js +40 -1
  144. package/lib/schemes/VirtualUrlPlugin.js +1 -2
  145. package/lib/serialization/FileMiddleware.js +29 -3
  146. package/lib/stats/DefaultStatsFactoryPlugin.js +32 -3
  147. package/lib/stats/DefaultStatsPresetPlugin.js +2 -0
  148. package/lib/stats/DefaultStatsPrinterPlugin.js +12 -2
  149. package/lib/stats/StatsFactory.js +15 -8
  150. package/lib/url/URLParserPlugin.js +98 -19
  151. package/lib/util/AsyncQueue.js +122 -72
  152. package/lib/util/LocConverter.js +9 -8
  153. package/lib/util/SetHelpers.js +1 -1
  154. package/lib/util/SourceProcessor.js +41 -28
  155. package/lib/util/StackedMap.js +122 -48
  156. package/lib/util/binarySearchBounds.js +2 -2
  157. package/lib/util/concatenate.js +40 -14
  158. package/lib/util/createHooksRegistry.js +13 -5
  159. package/lib/util/dataURL.js +2 -2
  160. package/lib/util/deterministicGrouping.js +7 -5
  161. package/lib/util/formatSize.js +6 -2
  162. package/lib/util/globUtils.js +710 -0
  163. package/lib/util/identifier.js +23 -6
  164. package/lib/util/implicitTypeLoaderFallback.js +62 -0
  165. package/lib/util/internalSerializables.js +88 -71
  166. package/lib/util/magicComment.js +121 -25
  167. package/lib/util/numberHash.js +2 -2
  168. package/lib/wasm-async/AsyncWasmCompileRuntimeModule.js +24 -18
  169. package/lib/wasm-async/AsyncWasmLoadingRuntimeModule.js +33 -22
  170. package/lib/wasm-sync/WasmChunkLoadingRuntimeModule.js +79 -16
  171. package/lib/web/FetchCompileAsyncWasmPlugin.js +19 -0
  172. package/lib/web/FetchCompileWasmPlugin.js +14 -0
  173. package/lib/web/JsonpChunkLoadingRuntimeModule.js +2 -0
  174. package/lib/webpack.js +65 -0
  175. package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +3 -0
  176. package/module.d.ts +21 -0
  177. package/package.json +14 -13
  178. package/schemas/WebpackOptions.check.js +1 -1
  179. package/schemas/WebpackOptions.json +759 -36
  180. package/schemas/plugins/HtmlParserOptions.check.js +1 -1
  181. package/schemas/plugins/ProgressPlugin.check.js +1 -1
  182. package/schemas/plugins/ProgressPlugin.json +17 -1
  183. package/schemas/plugins/container/ContainerReferencePlugin.check.js +1 -1
  184. package/schemas/plugins/container/ContainerReferencePlugin.json +1 -0
  185. package/schemas/plugins/container/ExternalsType.check.js +1 -1
  186. package/schemas/plugins/container/ModuleFederationPlugin.check.js +1 -1
  187. package/schemas/plugins/container/ModuleFederationPlugin.json +1 -0
  188. package/schemas/plugins/css/CssAutoOrModuleParserOptions.check.js +1 -1
  189. package/schemas/plugins/css/CssModuleParserOptions.check.js +1 -1
  190. package/schemas/plugins/css/CssParserOptions.check.js +1 -1
  191. package/types.d.ts +1300 -325
@@ -48,7 +48,8 @@ const CSS_TYPE_PREFIX = `${CSS_TYPE}/`;
48
48
  /** @typedef {import("../CodeGenerationResults")} CodeGenerationResults */
49
49
  /** @typedef {import("../Dependency")} Dependency */
50
50
  /** @typedef {import("../DependencyTemplate").CssData} CssData */
51
- /** @typedef {import("../DependencyTemplate").CssDependencyTemplateContext} DependencyTemplateContext */
51
+ /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
52
+ /** @typedef {import("../DependencyTemplate").CssDependencyTemplateContext} CssDependencyTemplateContext */
52
53
  /** @typedef {import("../Generator").GenerateContext} GenerateContext */
53
54
  /** @typedef {import("../Generator").UpdateHashContext} UpdateHashContext */
54
55
  /** @typedef {import("../Module").BuildInfo} BuildInfo */
@@ -191,28 +192,39 @@ class CssGenerator extends Generator {
191
192
 
192
193
  /**
193
194
  * Processes the provided module.
194
- * @param {NormalModule} module the current module
195
195
  * @param {Dependency} dependency the dependency to generate
196
- * @param {InitFragment<GenerateContext>[]} initFragments mutable list of init fragments
197
196
  * @param {ReplaceSource} source the current replace source which can be modified
198
- * @param {GenerateContext & { cssData: CssData }} generateContext the render context
197
+ * @param {DependencyTemplateContext & { cssData: CssData, type: string }} templateContext the template context (shared across all dependencies of the module)
199
198
  * @returns {void}
200
199
  */
201
- sourceDependency(module, dependency, initFragments, source, generateContext) {
200
+ sourceDependency(dependency, source, templateContext) {
202
201
  const constructor =
203
202
  /** @type {DependencyConstructor} */
204
203
  (dependency.constructor);
205
- const template = generateContext.dependencyTemplates.get(constructor);
204
+ const template = templateContext.dependencyTemplates.get(constructor);
206
205
  if (!template) {
207
206
  throw new Error(
208
207
  `No template for dependency: ${dependency.constructor.name}`
209
208
  );
210
209
  }
211
210
 
212
- /** @type {DependencyTemplateContext} */
211
+ template.apply(dependency, source, templateContext);
212
+ }
213
+
214
+ /**
215
+ * Processes the provided module.
216
+ * @param {NormalModule} module the module to generate
217
+ * @param {InitFragment<GenerateContext>[]} initFragments mutable list of init fragments
218
+ * @param {ReplaceSource} source the current replace source which can be modified
219
+ * @param {GenerateContext & { cssData: CssData }} generateContext the generateContext
220
+ * @returns {void}
221
+ */
222
+ sourceModule(module, initFragments, source, generateContext) {
223
+ // Only `dependency` varies across `template.apply` calls, so one context
224
+ // (and one lazy `chunkInitFragments` getter) serves the whole module.
213
225
  /** @type {InitFragment<GenerateContext>[] | undefined} */
214
226
  let chunkInitFragments;
215
- /** @type {DependencyTemplateContext} */
227
+ /** @type {CssDependencyTemplateContext} */
216
228
  const templateContext = {
217
229
  runtimeTemplate: generateContext.runtimeTemplate,
218
230
  dependencyTemplates: generateContext.dependencyTemplates,
@@ -244,37 +256,13 @@ class CssGenerator extends Generator {
244
256
  }
245
257
  };
246
258
 
247
- template.apply(dependency, source, templateContext);
248
- }
249
-
250
- /**
251
- * Processes the provided module.
252
- * @param {NormalModule} module the module to generate
253
- * @param {InitFragment<GenerateContext>[]} initFragments mutable list of init fragments
254
- * @param {ReplaceSource} source the current replace source which can be modified
255
- * @param {GenerateContext & { cssData: CssData }} generateContext the generateContext
256
- * @returns {void}
257
- */
258
- sourceModule(module, initFragments, source, generateContext) {
259
259
  for (const dependency of module.dependencies) {
260
- this.sourceDependency(
261
- module,
262
- dependency,
263
- initFragments,
264
- source,
265
- generateContext
266
- );
260
+ this.sourceDependency(dependency, source, templateContext);
267
261
  }
268
262
 
269
263
  if (module.presentationalDependencies !== undefined) {
270
264
  for (const dependency of module.presentationalDependencies) {
271
- this.sourceDependency(
272
- module,
273
- dependency,
274
- initFragments,
275
- source,
276
- generateContext
277
- );
265
+ this.sourceDependency(dependency, source, templateContext);
278
266
  }
279
267
  }
280
268
  }
@@ -414,20 +402,28 @@ class CssGenerator extends Generator {
414
402
  return new RawSource("");
415
403
  }
416
404
 
405
+ const exportsInfo =
406
+ generateContext.moduleGraph.getExportsInfo(module);
407
+
408
+ /**
409
+ * @param {string} name export name
410
+ * @returns {string | false} used export name when the export is referenced
411
+ */
412
+ const getUsedExportName = (name) =>
413
+ /** @type {string | false} */ (
414
+ exportsInfo
415
+ .getExportInfo(name)
416
+ .getUsedName(name, generateContext.runtime)
417
+ );
418
+
417
419
  if (generateContext.concatenationScope) {
418
420
  const source = new ConcatSource();
419
421
  /** @type {Set<string>} */
420
422
  const usedIdentifiers = new Set();
421
423
  const { RESERVED_IDENTIFIER } = getPropertyName();
422
- const exportsInfo =
423
- generateContext.moduleGraph.getExportsInfo(module);
424
424
 
425
425
  if (defaultExport) {
426
- const usedName = /** @type {string | false} */ (
427
- exportsInfo
428
- .getExportInfo("default")
429
- .getUsedName("default", generateContext.runtime)
430
- );
426
+ const usedName = getUsedExportName("default");
431
427
  if (usedName) {
432
428
  let identifier = Template.toIdentifier(usedName);
433
429
  if (RESERVED_IDENTIFIER.has(identifier)) {
@@ -447,16 +443,8 @@ class CssGenerator extends Generator {
447
443
  }
448
444
 
449
445
  for (const [name, v] of cssData.exports) {
450
- const usedName = /** @type {string | false} */ (
451
- exportsInfo
452
- .getExportInfo(name)
453
- .getUsedName(name, generateContext.runtime)
454
- );
455
- if (!usedName) {
456
- continue;
457
- }
458
-
459
- let identifier = Template.toIdentifier(usedName);
446
+ const usedName = getUsedExportName(name);
447
+ let identifier = Template.toIdentifier(usedName || name);
460
448
  if (RESERVED_IDENTIFIER.has(identifier)) {
461
449
  identifier = `_${identifier}`;
462
450
  }
@@ -481,9 +469,7 @@ class CssGenerator extends Generator {
481
469
 
482
470
  const needNsObj =
483
471
  this._esModule &&
484
- generateContext.moduleGraph
485
- .getExportsInfo(module)
486
- .otherExportsInfo.getUsed(generateContext.runtime) !==
472
+ exportsInfo.otherExportsInfo.getUsed(generateContext.runtime) !==
487
473
  UsageState.Unused;
488
474
 
489
475
  if (needNsObj) {
@@ -495,7 +481,25 @@ class CssGenerator extends Generator {
495
481
  // Should be after `concatenationScope` to allow module inlining
496
482
  generateContext.runtimeRequirements.add(RuntimeGlobals.module);
497
483
 
498
- if (!isCssModule && !needNsObj) {
484
+ const defaultUsedName = defaultExport
485
+ ? getUsedExportName("default")
486
+ : false;
487
+
488
+ /** @type {string[]} */
489
+ const exportEntries = [];
490
+ for (const [name, v] of cssData.exports) {
491
+ const exportKey = getUsedExportName(name) || name;
492
+ exportEntries.push(
493
+ `\t${JSON.stringify(exportKey)}: ${JSON.stringify(v)}`
494
+ );
495
+ }
496
+
497
+ if (
498
+ !isCssModule &&
499
+ !needNsObj &&
500
+ exportEntries.length === 0 &&
501
+ defaultUsedName
502
+ ) {
499
503
  return new ConcatSource(
500
504
  `${module.moduleArgument}.exports = `,
501
505
  /** @type {Source} */ (defaultExport)
@@ -509,21 +513,14 @@ class CssGenerator extends Generator {
509
513
  }.exports = {\n`
510
514
  );
511
515
 
512
- if (defaultExport) {
516
+ if (defaultUsedName) {
513
517
  result.add('\t"default": ');
514
- result.add(defaultExport);
515
- if (cssData.exports.size > 0) {
518
+ result.add(/** @type {Source} */ (defaultExport));
519
+ if (exportEntries.length > 0) {
516
520
  result.add(",\n");
517
521
  }
518
522
  }
519
523
 
520
- /** @type {string[]} */
521
- const exportEntries = [];
522
- for (const [name, v] of cssData.exports) {
523
- exportEntries.push(
524
- `\t${JSON.stringify(name)}: ${JSON.stringify(v)}`
525
- );
526
- }
527
524
  if (exportEntries.length > 0) {
528
525
  result.add(exportEntries.join(",\n"));
529
526
  }
@@ -623,10 +620,13 @@ class CssGenerator extends Generator {
623
620
 
624
621
  let hasJs = false;
625
622
  let hasCssText = false;
623
+ // Uncached per call (#20800) — the common link case stops scanning once
624
+ // js is seen; `hasCssText` only matters for exports-only / non-link.
625
+ const needsCssText = this._exportsOnly || exportType !== "link";
626
626
  const connections = this._moduleGraph.getIncomingConnections(module);
627
627
 
628
628
  for (const connection of connections) {
629
- if (hasJs && hasCssText) break;
629
+ if (hasJs && (hasCssText || !needsCssText)) break;
630
630
  if (
631
631
  exportType === "link" &&
632
632
  connection.dependency instanceof CssImportDependency
@@ -704,15 +704,15 @@ class CssGenerator extends Generator {
704
704
  }
705
705
  return 0;
706
706
  }
707
- const exports = cssData.exports;
708
- /** @type {Record<string, string>} */
709
- const exportsObj = {};
710
- for (const [key, value] of exports) {
711
- exportsObj[key] = value;
707
+ // `JSON.stringify({...exports}).length` computed entry-wise, without
708
+ // materializing the object or the full string.
709
+ let length = 2 + (cssData.exports.size - 1);
710
+ for (const [key, value] of cssData.exports) {
711
+ length +=
712
+ JSON.stringify(key).length + 1 + JSON.stringify(value).length;
712
713
  }
713
- const stringifiedExports = JSON.stringify(exportsObj);
714
714
 
715
- return stringifiedExports.length + 42;
715
+ return length + 42;
716
716
  }
717
717
  case CSS_TEXT_TYPE:
718
718
  case CSS_TYPE: {
@@ -99,7 +99,9 @@ class CssLoadingRuntimeModule extends RuntimeModule {
99
99
  const withPreload =
100
100
  this._runtimeRequirements.has(RuntimeGlobals.preloadChunkHandlers) &&
101
101
  (environment.document || isNeutralPlatform) &&
102
- chunk.hasChildByOrder(chunkGraph, "preload", true, chunkHasCss);
102
+ (chunk.hasChildByOrder(chunkGraph, "preload", true, chunkHasCss) ||
103
+ // `parser.javascript.dynamicImportCssPreload` — CSS-only preload order.
104
+ chunk.hasChildByOrder(chunkGraph, "cssPreload", true, chunkHasCss));
103
105
 
104
106
  const { linkPreload, linkPrefetch, createStylesheet, linkInsert } =
105
107
  CssLoadingRuntimeModule.getCompilationHooks(compilation);
@@ -295,6 +297,7 @@ class CssLoadingRuntimeModule extends RuntimeModule {
295
297
  "error.name = 'ChunkLoadError';",
296
298
  "error.type = errorType;",
297
299
  "error.request = realHref;",
300
+ "error.event = event;",
298
301
  "installedChunkData[1](error);"
299
302
  ]),
300
303
  "} else {",
@@ -548,6 +551,7 @@ class CssLoadingRuntimeModule extends RuntimeModule {
548
551
  "error.name = 'ChunkLoadError';",
549
552
  "error.type = errorType;",
550
553
  "error.request = realHref;",
554
+ "error.event = event;",
551
555
  "reject(error);"
552
556
  ]),
553
557
  "} else {",
@@ -39,6 +39,7 @@ const { compareModulesByFullName } = require("../util/comparators");
39
39
  const createHash = require("../util/createHash");
40
40
  const createHooksRegistry = require("../util/createHooksRegistry");
41
41
  const { getUndoPath } = require("../util/identifier");
42
+ const implicitTypeLoaderFallback = require("../util/implicitTypeLoaderFallback");
42
43
  const memoize = require("../util/memoize");
43
44
  const { digestNonNumericOnlyWithFull } = require("../util/nonNumericOnlyHash");
44
45
  const {
@@ -133,6 +134,42 @@ const getCssInjectStyleRuntimeModule = memoize(() =>
133
134
  */
134
135
  const publicPathPlaceholderPlans = new WeakMap();
135
136
 
137
+ /**
138
+ * Memoized inheritance chain per module, revalidated against its source
139
+ * fields — `updateCacheModule` reassigns them on watch rebuilds.
140
+ * @type {WeakMap<CssModule, { cssLayer: CssModule["cssLayer"], supports: CssModule["supports"], media: CssModule["media"], inheritance: CssModule["inheritance"], chain: Inheritance }>}
141
+ */
142
+ const moduleInheritanceCache = new WeakMap();
143
+
144
+ /**
145
+ * Gets the memoized inheritance chain of a css module.
146
+ * @param {CssModule} module css module
147
+ * @returns {Inheritance} inheritance chain including the module's own entry
148
+ */
149
+ const getModuleInheritance = (module) => {
150
+ const entry = moduleInheritanceCache.get(module);
151
+ if (
152
+ entry !== undefined &&
153
+ entry.cssLayer === module.cssLayer &&
154
+ entry.supports === module.supports &&
155
+ entry.media === module.media &&
156
+ entry.inheritance === module.inheritance
157
+ ) {
158
+ return entry.chain;
159
+ }
160
+ /** @type {Inheritance} */
161
+ const chain = [[module.cssLayer, module.supports, module.media]];
162
+ if (module.inheritance) chain.push(...module.inheritance);
163
+ moduleInheritanceCache.set(module, {
164
+ cssLayer: module.cssLayer,
165
+ supports: module.supports,
166
+ media: module.media,
167
+ inheritance: module.inheritance,
168
+ chain
169
+ });
170
+ return chain;
171
+ };
172
+
136
173
  /**
137
174
  * Locate the public-path placeholders in a materialized module source.
138
175
  * @param {string} content materialized module source
@@ -195,9 +232,21 @@ class CssModulesPlugin {
195
232
  * @returns {void}
196
233
  */
197
234
  apply(compiler) {
235
+ // `"auto"` marks implicit enablement (no user rule for CSS files) — see
236
+ // `applyExperimentsDefaults`. Only then loaders win over the built-in type.
237
+ // TODO webpack 6: css defaults to `true`, drop this implicit-only fallback.
238
+ const implicitlyEnabled = compiler.options.experiments.css === "auto";
198
239
  compiler.hooks.compilation.tap(
199
240
  PLUGIN_NAME,
200
241
  (compilation, { normalModuleFactory }) => {
242
+ if (implicitlyEnabled) {
243
+ implicitTypeLoaderFallback(
244
+ normalModuleFactory,
245
+ PLUGIN_NAME,
246
+ /\.css$/i,
247
+ CSS_MODULE_TYPE_AUTO
248
+ );
249
+ }
201
250
  const hooks = CssModulesPlugin.getCompilationHooks(compilation);
202
251
  compilation.dependencyFactories.set(
203
252
  CssImportDependency,
@@ -857,55 +906,55 @@ class CssModulesPlugin {
857
906
  return this.getModulesInOrder(chunk, modules, compilation);
858
907
  };
859
908
 
860
- return /** @type {CssModule[]} */ ([
861
- ...orderModules(
862
- chunkGraph.getOrderedChunkModulesIterableBySourceType(
863
- chunk,
864
- CSS_IMPORT_TYPE,
865
- compareModulesByFullName(compilation.compiler)
866
- )
867
- ),
868
- ...orderModules(
869
- chunkGraph.getOrderedChunkModulesIterableBySourceType(
870
- chunk,
871
- CSS_TYPE,
872
- compareModulesByFullName(compilation.compiler)
873
- )
874
- ).map((module) => {
909
+ const comparator = compareModulesByFullName(compilation.compiler);
910
+ const importModules = orderModules(
911
+ chunkGraph.getOrderedChunkModulesIterableBySourceType(
912
+ chunk,
913
+ CSS_IMPORT_TYPE,
914
+ comparator
915
+ )
916
+ );
917
+ const cssModules = orderModules(
918
+ chunkGraph.getOrderedChunkModulesIterableBySourceType(
919
+ chunk,
920
+ CSS_TYPE,
921
+ comparator
922
+ )
923
+ );
924
+ for (const module of cssModules) {
925
+ if (
926
+ typeof (
927
+ /** @type {CssModuleBuildInfo} */ (module.buildInfo).charset
928
+ ) !== "undefined"
929
+ ) {
875
930
  if (
876
- typeof (
931
+ typeof charset !== "undefined" &&
932
+ charset !==
877
933
  /** @type {CssModuleBuildInfo} */ (module.buildInfo).charset
878
- ) !== "undefined"
879
934
  ) {
880
- if (
881
- typeof charset !== "undefined" &&
882
- charset !==
935
+ const err = new WebpackError(
936
+ `Conflicting @charset at-rules detected: the module ${module.readableIdentifier(
937
+ compilation.requestShortener
938
+ )} (in chunk ${chunk.name || chunk.id}) specifies "${
883
939
  /** @type {CssModuleBuildInfo} */ (module.buildInfo).charset
884
- ) {
885
- const err = new WebpackError(
886
- `Conflicting @charset at-rules detected: the module ${module.readableIdentifier(
887
- compilation.requestShortener
888
- )} (in chunk ${chunk.name || chunk.id}) specifies "${
889
- /** @type {CssModuleBuildInfo} */ (module.buildInfo).charset
890
- }", but "${charset}" was expected, all modules must use the same character set`
891
- );
940
+ }", but "${charset}" was expected, all modules must use the same character set`
941
+ );
892
942
 
893
- err.chunk = chunk;
894
- err.module = module;
895
- err.hideStack = true;
943
+ err.chunk = chunk;
944
+ err.module = module;
945
+ err.hideStack = true;
896
946
 
897
- compilation.warnings.push(err);
898
- }
947
+ compilation.warnings.push(err);
948
+ }
899
949
 
900
- if (typeof charset === "undefined") {
901
- charset = /** @type {CssModuleBuildInfo} */ (module.buildInfo)
902
- .charset;
903
- }
950
+ if (typeof charset === "undefined") {
951
+ charset = /** @type {CssModuleBuildInfo} */ (module.buildInfo)
952
+ .charset;
904
953
  }
954
+ }
955
+ }
905
956
 
906
- return module;
907
- })
908
- ]);
957
+ return /** @type {CssModule[]} */ ([...importModules, ...cssModules]);
909
958
  }
910
959
 
911
960
  /**
@@ -920,11 +969,7 @@ class CssModulesPlugin {
920
969
  renderContext;
921
970
  const cacheEntry = moduleFactoryCache.get(moduleSourceContent);
922
971
 
923
- /** @type {Inheritance} */
924
- const inheritance = [[module.cssLayer, module.supports, module.media]];
925
- if (module.inheritance) {
926
- inheritance.push(...module.inheritance);
927
- }
972
+ const inheritance = getModuleInheritance(module);
928
973
 
929
974
  /** @type {CachedSource} */
930
975
  let source;
@@ -932,14 +977,8 @@ class CssModulesPlugin {
932
977
  cacheEntry &&
933
978
  cacheEntry.undoPath === undoPath &&
934
979
  cacheEntry.hash === hash &&
935
- cacheEntry.inheritance.length === inheritance.length &&
936
- cacheEntry.inheritance.every(([layer, supports, media], i) => {
937
- const item = inheritance[i];
938
- if (Array.isArray(item)) {
939
- return layer === item[0] && supports === item[1] && media === item[2];
940
- }
941
- return false;
942
- })
980
+ // Memoized per module, so identity implies equal entries.
981
+ cacheEntry.inheritance === inheritance
943
982
  ) {
944
983
  source = cacheEntry.source;
945
984
  } else {