webpack 5.108.4 → 5.109.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.
Files changed (200) hide show
  1. package/bin/webpack.js +12 -0
  2. package/lib/APIPlugin.js +48 -29
  3. package/lib/CacheFacade.js +8 -0
  4. package/lib/ChunkGraph.js +17 -15
  5. package/lib/ChunkGroup.js +1 -0
  6. package/lib/CompatibilityPlugin.js +62 -29
  7. package/lib/Compilation.js +58 -6
  8. package/lib/ConstPlugin.js +11 -11
  9. package/lib/ContextModule.js +239 -6
  10. package/lib/ContextModuleFactory.js +139 -14
  11. package/lib/DefinePlugin.js +433 -112
  12. package/lib/Entrypoint.js +1 -1
  13. package/lib/ExportsInfo.js +22 -5
  14. package/lib/ExportsInfoApiPlugin.js +2 -2
  15. package/lib/ExternalModule.js +107 -5
  16. package/lib/ExternalModuleFactoryPlugin.js +20 -1
  17. package/lib/FileSystemInfo.js +256 -100
  18. package/lib/FlagDependencyUsagePlugin.js +33 -12
  19. package/lib/HotModuleReplacementPlugin.js +18 -15
  20. package/lib/InitFragment.js +20 -32
  21. package/lib/Module.js +3 -0
  22. package/lib/ModuleFilenameHelpers.js +2 -2
  23. package/lib/ModuleTypeConstants.js +8 -1
  24. package/lib/MultiCompiler.js +13 -2
  25. package/lib/MultiWatching.js +1 -0
  26. package/lib/NodeStuffPlugin.js +51 -33
  27. package/lib/NormalModule.js +175 -401
  28. package/lib/NormalModuleFactory.js +76 -2
  29. package/lib/ProgressPlugin.js +211 -119
  30. package/lib/ProvidePlugin.js +2 -2
  31. package/lib/RuntimeGlobals.js +47 -0
  32. package/lib/RuntimePlugin.js +67 -107
  33. package/lib/RuntimeTemplate.js +304 -16
  34. package/lib/Template.js +23 -3
  35. package/lib/UseStrictPlugin.js +1 -1
  36. package/lib/WebpackIsIncludedPlugin.js +1 -1
  37. package/lib/WebpackOptionsApply.js +24 -19
  38. package/lib/asset/AssetGenerator.js +110 -28
  39. package/lib/asset/AssetModulesPlugin.js +92 -2
  40. package/lib/asset/WebManifestGenerator.js +164 -0
  41. package/lib/asset/WebManifestParser.js +130 -0
  42. package/lib/async-modules/AwaitDependenciesInitFragment.js +14 -4
  43. package/lib/async-modules/isGeneratorLowered.js +26 -0
  44. package/lib/cache/PackFileCacheStrategy.js +9 -3
  45. package/lib/config/browserslistTargetHandler.js +40 -0
  46. package/lib/config/defaults.js +219 -29
  47. package/lib/config/normalization.js +26 -0
  48. package/lib/config/target.js +9 -1
  49. package/lib/container/HoistContainerReferencesPlugin.js +47 -55
  50. package/lib/css/CssGenerator.js +40 -31
  51. package/lib/css/CssLoadingRuntimeModule.js +5 -1
  52. package/lib/css/CssModulesPlugin.js +13 -0
  53. package/lib/css/CssParser.js +729 -313
  54. package/lib/css/syntax.js +1371 -861
  55. package/lib/dependencies/AMDDefineDependencyParserPlugin.js +6 -6
  56. package/lib/dependencies/AMDPlugin.js +2 -2
  57. package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +13 -13
  58. package/lib/dependencies/CommonJsDependencyHelpers.js +39 -0
  59. package/lib/dependencies/CommonJsExportRequireDependency.js +19 -0
  60. package/lib/dependencies/CommonJsExportsDependency.js +47 -2
  61. package/lib/dependencies/CommonJsExportsParserPlugin.js +10 -10
  62. package/lib/dependencies/CommonJsImportsParserPlugin.js +18 -18
  63. package/lib/dependencies/CommonJsPlugin.js +2 -2
  64. package/lib/dependencies/CommonJsRequireDependency.js +26 -1
  65. package/lib/dependencies/CommonJsSelfReferenceDependency.js +32 -1
  66. package/lib/dependencies/ContextDependencyHelpers.js +3 -3
  67. package/lib/dependencies/CreateRequireParserPlugin.js +6 -6
  68. package/lib/dependencies/CssIcssExportDependency.js +162 -71
  69. package/lib/dependencies/CssUrlDependency.js +35 -4
  70. package/lib/dependencies/ExternalModuleInitFragmentDependency.js +2 -2
  71. package/lib/dependencies/HarmonyCompatibilityDependency.js +32 -14
  72. package/lib/dependencies/HarmonyDetectionParserPlugin.js +43 -13
  73. package/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js +2 -1
  74. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +7 -19
  75. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +17 -9
  76. package/lib/dependencies/HarmonyImportDependency.js +13 -2
  77. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +11 -11
  78. package/lib/dependencies/HarmonyImportSpecifierDependency.js +10 -7
  79. package/lib/dependencies/HarmonyModulesPlugin.js +6 -0
  80. package/lib/dependencies/HarmonyTopLevelThisParserPlugin.js +1 -1
  81. package/lib/dependencies/HtmlEntryDependency.js +797 -172
  82. package/lib/dependencies/HtmlSourceDependency.js +26 -0
  83. package/lib/dependencies/ImportDependency.js +43 -1
  84. package/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +31 -18
  85. package/lib/dependencies/ImportMetaContextPlugin.js +38 -7
  86. package/lib/dependencies/ImportMetaGlobDependency.js +81 -0
  87. package/lib/dependencies/ImportMetaGlobDependencyParserPlugin.js +78 -0
  88. package/lib/dependencies/ImportMetaGlobHelpers.js +660 -0
  89. package/lib/dependencies/ImportMetaPlugin.js +532 -180
  90. package/lib/dependencies/ImportMetaResolveDependency.js +90 -0
  91. package/lib/dependencies/ImportParserPlugin.js +80 -17
  92. package/lib/dependencies/ImportPhase.js +10 -8
  93. package/lib/dependencies/LoaderPlugin.js +5 -1
  94. package/lib/dependencies/RequireContextDependencyParserPlugin.js +1 -1
  95. package/lib/dependencies/RequireEnsureDependenciesBlockParserPlugin.js +3 -6
  96. package/lib/dependencies/RequireIncludeDependencyParserPlugin.js +4 -11
  97. package/lib/dependencies/SystemPlugin.js +2 -4
  98. package/lib/dependencies/TopLevelAwaitDependency.js +93 -0
  99. package/lib/dependencies/URLDependency.js +141 -21
  100. package/lib/dependencies/{WorkerPlugin.js → WorkerAndWorkletPlugin.js} +324 -112
  101. package/lib/dependencies/WorkerDependency.js +86 -18
  102. package/lib/dependencies/WorkletDependency.js +278 -0
  103. package/lib/dll/DelegatedModuleFactoryPlugin.js +19 -0
  104. package/lib/errors/EnvironmentNotSupportAsyncWarning.js +1 -1
  105. package/lib/errors/LoaderLoadingError.js +20 -0
  106. package/lib/errors/ModuleParseError.js +39 -22
  107. package/lib/errors/NodeStuffInWebError.js +1 -1
  108. package/lib/esm/ModuleChunkFormatPlugin.js +47 -14
  109. package/lib/esm/ModuleChunkLoadingPlugin.js +8 -1
  110. package/lib/esm/ModuleChunkLoadingRuntimeModule.js +29 -1
  111. package/lib/hmr/lazyCompilationBackend.js +51 -12
  112. package/lib/html/HtmlGenerator.js +907 -15
  113. package/lib/html/HtmlModule.js +2 -2
  114. package/lib/html/HtmlModulesPlugin.js +610 -166
  115. package/lib/html/HtmlParser.js +603 -247
  116. package/lib/html/favicon.svg +1 -0
  117. package/lib/html/syntax.js +3451 -2762
  118. package/lib/ids/IdHelpers.js +25 -11
  119. package/lib/index.js +6 -0
  120. package/lib/javascript/ArrayPushCallbackChunkFormatPlugin.js +4 -5
  121. package/lib/javascript/BasicEvaluatedExpression.js +366 -48
  122. package/lib/javascript/JavascriptGenerator.js +197 -49
  123. package/lib/javascript/JavascriptModulesPlugin.js +178 -24
  124. package/lib/javascript/JavascriptParser.js +704 -355
  125. package/lib/javascript/JavascriptParserHelpers.js +3 -6
  126. package/lib/javascript/syntax.js +5037 -0
  127. package/lib/library/ModuleLibraryPlugin.js +38 -1
  128. package/lib/loaders/LoaderRunner.js +714 -0
  129. package/lib/loaders/loadLoader.js +100 -0
  130. package/lib/node/ReadFileCompileAsyncWasmPlugin.js +29 -2
  131. package/lib/node/ReadFileCompileWasmPlugin.js +24 -2
  132. package/lib/optimize/AggressiveMergingPlugin.js +19 -15
  133. package/lib/optimize/ConcatenatedModule.js +139 -31
  134. package/lib/optimize/FlagIncludedChunksPlugin.js +13 -3
  135. package/lib/optimize/InnerGraphPlugin.js +50 -30
  136. package/lib/optimize/ModuleConcatenationPlugin.js +139 -24
  137. package/lib/optimize/SideEffectsFlagPlugin.js +86 -43
  138. package/lib/optimize/SplitChunksPlugin.js +146 -44
  139. package/lib/prefetch/ChunkPrefetchPreloadPlugin.js +15 -0
  140. package/lib/prefetch/ChunkPreloadTriggerRuntimeModule.js +24 -13
  141. package/lib/prefetch/ResourceHintPlugin.js +687 -0
  142. package/lib/prefetch/ResourceHintRuntimeModule.js +149 -0
  143. package/lib/prefetch/StartupAssetHintRuntimeModule.js +210 -0
  144. package/lib/prefetch/parseResourceHintOptions.js +103 -0
  145. package/lib/rules/RuleSetCompiler.js +114 -0
  146. package/lib/runtime/AsyncModuleGeneratorRuntimeModule.js +56 -0
  147. package/lib/runtime/AsyncModuleRuntimeModule.js +24 -8
  148. package/lib/runtime/AutoPublicPathRuntimeModule.js +7 -1
  149. package/lib/runtime/CommonJsWrapRuntimeModule.js +37 -0
  150. package/lib/runtime/GetWorkletBootstrapRuntimeModule.js +59 -0
  151. package/lib/runtime/MakeDeferredNamespaceObjectRuntime.js +204 -8
  152. package/lib/runtime/SetAnonymousDefaultNameRuntimeModule.js +4 -1
  153. package/lib/schemes/HttpUriPlugin.js +40 -1
  154. package/lib/schemes/VirtualUrlPlugin.js +1 -2
  155. package/lib/serialization/BinaryMiddleware.js +270 -938
  156. package/lib/serialization/FileMiddleware.js +29 -3
  157. package/lib/stats/DefaultStatsFactoryPlugin.js +229 -194
  158. package/lib/stats/DefaultStatsPresetPlugin.js +2 -0
  159. package/lib/stats/DefaultStatsPrinterPlugin.js +36 -168
  160. package/lib/stats/StatsFactory.js +46 -17
  161. package/lib/url/URLParserPlugin.js +66 -13
  162. package/lib/util/AsyncQueue.js +122 -72
  163. package/lib/util/SetHelpers.js +1 -1
  164. package/lib/util/StackedMap.js +122 -48
  165. package/lib/util/binarySearchBounds.js +2 -2
  166. package/lib/util/concatenate.js +40 -14
  167. package/lib/util/createHooksRegistry.js +13 -5
  168. package/lib/util/dataURL.js +2 -2
  169. package/lib/util/deterministicGrouping.js +7 -5
  170. package/lib/util/formatSize.js +6 -2
  171. package/lib/util/globUtils.js +710 -0
  172. package/lib/util/identifier.js +23 -6
  173. package/lib/util/implicitTypeLoaderFallback.js +62 -0
  174. package/lib/util/internalSerializables.js +88 -71
  175. package/lib/util/magicComment.js +121 -25
  176. package/lib/util/numberHash.js +2 -2
  177. package/lib/wasm-async/AsyncWasmCompileRuntimeModule.js +24 -18
  178. package/lib/wasm-async/AsyncWasmLoadingRuntimeModule.js +33 -22
  179. package/lib/wasm-sync/WasmChunkLoadingRuntimeModule.js +79 -16
  180. package/lib/web/FetchCompileAsyncWasmPlugin.js +19 -0
  181. package/lib/web/FetchCompileWasmPlugin.js +14 -0
  182. package/lib/web/JsonpChunkLoadingRuntimeModule.js +2 -0
  183. package/lib/webpack.js +65 -0
  184. package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +3 -0
  185. package/module.d.ts +21 -0
  186. package/package.json +12 -11
  187. package/schemas/WebpackOptions.check.js +1 -1
  188. package/schemas/WebpackOptions.json +759 -36
  189. package/schemas/plugins/HtmlParserOptions.check.js +1 -1
  190. package/schemas/plugins/ProgressPlugin.check.js +1 -1
  191. package/schemas/plugins/ProgressPlugin.json +17 -1
  192. package/schemas/plugins/container/ContainerReferencePlugin.check.js +1 -1
  193. package/schemas/plugins/container/ContainerReferencePlugin.json +1 -0
  194. package/schemas/plugins/container/ExternalsType.check.js +1 -1
  195. package/schemas/plugins/container/ModuleFederationPlugin.check.js +1 -1
  196. package/schemas/plugins/container/ModuleFederationPlugin.json +1 -0
  197. package/schemas/plugins/css/CssAutoOrModuleParserOptions.check.js +1 -1
  198. package/schemas/plugins/css/CssModuleParserOptions.check.js +1 -1
  199. package/schemas/plugins/css/CssParserOptions.check.js +1 -1
  200. package/types.d.ts +1212 -99
@@ -52,6 +52,7 @@ const getDefaultTarget = (context) => {
52
52
  * @property {boolean | null} nodeBuiltinModuleGetter node.js exposes `process.getBuiltinModule()` to synchronously load core modules
53
53
  * @property {boolean | null} importMetaDirnameAndFilename node.js allows to use `import.meta.dirname` and `import.meta.filename`
54
54
  * @property {boolean | null} document has document available (allows script tags)
55
+ * @property {(boolean | null)=} modulePreload supports `<link rel="modulepreload">` (optional; absent means "assume supported")
55
56
  * @property {boolean | null} importScripts has importScripts available
56
57
  * @property {boolean | null} importScriptsInWorker has importScripts available when creating a worker
57
58
  * @property {boolean | null} fetchWasm has fetch function available for WebAssembly
@@ -79,6 +80,7 @@ const getDefaultTarget = (context) => {
79
80
  * @property {boolean | null} spread spread and rest in array/object literals and calls is available
80
81
  * @property {boolean | null} templateLiteral template literal is available
81
82
  * @property {boolean | null} asyncFunction async functions and await are available
83
+ * @property {boolean | null} generator generator functions and yield are available
82
84
  */
83
85
 
84
86
  /**
@@ -259,6 +261,7 @@ You can also more options via the 'target' option: 'browserslist' / 'browserslis
259
261
  methodShorthand: v(4),
260
262
  arrowFunction: v(6),
261
263
  asyncFunction: v(7, 6),
264
+ generator: v(4),
262
265
  forOf: v(5),
263
266
  destructuring: v(6),
264
267
  bigIntLiteral: v(10, 4),
@@ -315,7 +318,8 @@ You can also more options via the 'target' option: 'browserslist' / 'browserslis
315
318
  optionalChaining: true,
316
319
  spread: true,
317
320
  templateLiteral: true,
318
- asyncFunction: true
321
+ asyncFunction: true,
322
+ generator: true
319
323
  };
320
324
  }
321
325
  ],
@@ -362,6 +366,7 @@ You can also more options via the 'target' option: 'browserslist' / 'browserslis
362
366
  methodShorthand: true,
363
367
  arrowFunction: true,
364
368
  asyncFunction: true,
369
+ generator: true,
365
370
  forOf: true,
366
371
  destructuring: true,
367
372
  bigIntLiteral: true,
@@ -418,6 +423,7 @@ You can also more options via the 'target' option: 'browserslist' / 'browserslis
418
423
  methodShorthand: v(1, 1),
419
424
  arrowFunction: v(1, 1),
420
425
  asyncFunction: v(1, 7),
426
+ generator: v(1, 1),
421
427
  forOf: v(0, 36),
422
428
  destructuring: v(1, 1),
423
429
  bigIntLiteral: v(4),
@@ -466,6 +472,7 @@ You can also more options via the 'target' option: 'browserslist' / 'browserslis
466
472
  methodShorthand: v(0, 15),
467
473
  arrowFunction: v(0, 15),
468
474
  asyncFunction: v(0, 21),
475
+ generator: v(0, 15),
469
476
  forOf: v(0, 13),
470
477
  destructuring: v(0, 15),
471
478
  bigIntLiteral: v(0, 32),
@@ -495,6 +502,7 @@ You can also more options via the 'target' option: 'browserslist' / 'browserslis
495
502
  destructuring: v >= 2015,
496
503
  module: v >= 2015,
497
504
  asyncFunction: v >= 2017,
505
+ generator: v >= 2015,
498
506
  globalThis: v >= 2020,
499
507
  symbol: v >= 2015,
500
508
  hasOwn: v >= 2022,
@@ -38,12 +38,13 @@ class HoistContainerReferences {
38
38
  const depsToTrace = new Set();
39
39
  /** @type {Set<Dependency>} */
40
40
  const entryExternalsToHoist = new Set();
41
- hooks.addContainerEntryDependency.tap(PLUGIN_NAME, (dep) => {
41
+ // Both hooks feed the same trace set, so they share one callback.
42
+ /** @type {(dep: Dependency) => void} */
43
+ const traceDep = (dep) => {
42
44
  depsToTrace.add(dep);
43
- });
44
- hooks.addFederationRuntimeDependency.tap(PLUGIN_NAME, (dep) => {
45
- depsToTrace.add(dep);
46
- });
45
+ };
46
+ hooks.addContainerEntryDependency.tap(PLUGIN_NAME, traceDep);
47
+ hooks.addFederationRuntimeDependency.tap(PLUGIN_NAME, traceDep);
47
48
 
48
49
  compilation.hooks.addEntry.tap(PLUGIN_NAME, (entryDep) => {
49
50
  if (entryDep.type === "entry") {
@@ -76,46 +77,26 @@ class HoistContainerReferences {
76
77
  * @param {Set<Dependency>} entryExternalsToHoist Set of container entry dependencies to hoist.
77
78
  */
78
79
  hoistModulesInChunks(compilation, depsToTrace, entryExternalsToHoist) {
79
- const { chunkGraph, moduleGraph } = compilation;
80
+ const { moduleGraph } = compilation;
80
81
 
81
- // loop over entry points
82
+ // Entry externals: hoist the external modules (e.g. RemoteModule) they reference.
82
83
  for (const dep of entryExternalsToHoist) {
83
84
  const entryModule = moduleGraph.getModule(dep);
84
85
  if (!entryModule) continue;
85
- // get all the external module types and hoist them to the runtime chunk, this will get RemoteModule externals
86
86
  const allReferencedModules = getAllReferencedModules(
87
87
  compilation,
88
88
  entryModule,
89
89
  "external",
90
90
  false
91
91
  );
92
-
93
- const containerRuntimes = chunkGraph.getModuleRuntimes(entryModule);
94
- /** @type {Set<string>} */
95
- const runtimes = new Set();
96
-
97
- for (const runtimeSpec of containerRuntimes) {
98
- forEachRuntime(runtimeSpec, (runtimeKey) => {
99
- if (runtimeKey) {
100
- runtimes.add(runtimeKey);
101
- }
102
- });
103
- }
104
-
105
- for (const runtime of runtimes) {
106
- const runtimeChunk = compilation.namedChunks.get(runtime);
107
- if (!runtimeChunk) continue;
108
-
109
- for (const module of allReferencedModules) {
110
- if (!chunkGraph.isModuleInChunk(module, runtimeChunk)) {
111
- chunkGraph.connectChunkAndModule(runtimeChunk, module);
112
- }
113
- }
114
- }
115
- this.cleanUpChunks(compilation, allReferencedModules);
92
+ this.hoistReferencedModules(
93
+ compilation,
94
+ entryModule,
95
+ allReferencedModules
96
+ );
116
97
  }
117
98
 
118
- // handle container entry specifically
99
+ // Container entries: hoist the initial graph plus its external references.
119
100
  for (const dep of depsToTrace) {
120
101
  const containerEntryModule = moduleGraph.getModule(dep);
121
102
  if (!containerEntryModule) continue;
@@ -125,43 +106,54 @@ class HoistContainerReferences {
125
106
  "initial",
126
107
  false
127
108
  );
128
-
129
109
  const allRemoteReferences = getAllReferencedModules(
130
110
  compilation,
131
111
  containerEntryModule,
132
112
  "external",
133
113
  false
134
114
  );
135
-
136
115
  for (const remote of allRemoteReferences) {
137
116
  allReferencedModules.add(remote);
138
117
  }
118
+ this.hoistReferencedModules(
119
+ compilation,
120
+ containerEntryModule,
121
+ allReferencedModules
122
+ );
123
+ }
124
+ }
139
125
 
140
- const containerRuntimes =
141
- chunkGraph.getModuleRuntimes(containerEntryModule);
142
- /** @type {Set<string>} */
143
- const runtimes = new Set();
144
-
145
- for (const runtimeSpec of containerRuntimes) {
146
- forEachRuntime(runtimeSpec, (runtimeKey) => {
147
- if (runtimeKey) {
148
- runtimes.add(runtimeKey);
149
- }
150
- });
151
- }
126
+ /**
127
+ * Connect `referencedModules` into each runtime chunk of `entryModule`, then
128
+ * prune the chunks they were hoisted out of. The two passes above build
129
+ * `referencedModules` differently but hoist them identically.
130
+ * @param {Compilation} compilation The webpack compilation instance.
131
+ * @param {Module} entryModule The module whose runtimes receive the modules.
132
+ * @param {Set<Module>} referencedModules The modules to hoist.
133
+ */
134
+ hoistReferencedModules(compilation, entryModule, referencedModules) {
135
+ const { chunkGraph } = compilation;
136
+ /** @type {Set<string>} */
137
+ const runtimes = new Set();
138
+ for (const runtimeSpec of chunkGraph.getModuleRuntimes(entryModule)) {
139
+ forEachRuntime(runtimeSpec, (runtimeKey) => {
140
+ if (runtimeKey) {
141
+ runtimes.add(runtimeKey);
142
+ }
143
+ });
144
+ }
152
145
 
153
- for (const runtime of runtimes) {
154
- const runtimeChunk = compilation.namedChunks.get(runtime);
155
- if (!runtimeChunk) continue;
146
+ for (const runtime of runtimes) {
147
+ const runtimeChunk = compilation.namedChunks.get(runtime);
148
+ if (!runtimeChunk) continue;
156
149
 
157
- for (const module of allReferencedModules) {
158
- if (!chunkGraph.isModuleInChunk(module, runtimeChunk)) {
159
- chunkGraph.connectChunkAndModule(runtimeChunk, module);
160
- }
150
+ for (const module of referencedModules) {
151
+ if (!chunkGraph.isModuleInChunk(module, runtimeChunk)) {
152
+ chunkGraph.connectChunkAndModule(runtimeChunk, module);
161
153
  }
162
154
  }
163
- this.cleanUpChunks(compilation, allReferencedModules);
164
155
  }
156
+ this.cleanUpChunks(compilation, referencedModules);
165
157
  }
166
158
 
167
159
  /**
@@ -402,20 +402,28 @@ class CssGenerator extends Generator {
402
402
  return new RawSource("");
403
403
  }
404
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
+
405
419
  if (generateContext.concatenationScope) {
406
420
  const source = new ConcatSource();
407
421
  /** @type {Set<string>} */
408
422
  const usedIdentifiers = new Set();
409
423
  const { RESERVED_IDENTIFIER } = getPropertyName();
410
- const exportsInfo =
411
- generateContext.moduleGraph.getExportsInfo(module);
412
424
 
413
425
  if (defaultExport) {
414
- const usedName = /** @type {string | false} */ (
415
- exportsInfo
416
- .getExportInfo("default")
417
- .getUsedName("default", generateContext.runtime)
418
- );
426
+ const usedName = getUsedExportName("default");
419
427
  if (usedName) {
420
428
  let identifier = Template.toIdentifier(usedName);
421
429
  if (RESERVED_IDENTIFIER.has(identifier)) {
@@ -435,16 +443,8 @@ class CssGenerator extends Generator {
435
443
  }
436
444
 
437
445
  for (const [name, v] of cssData.exports) {
438
- const usedName = /** @type {string | false} */ (
439
- exportsInfo
440
- .getExportInfo(name)
441
- .getUsedName(name, generateContext.runtime)
442
- );
443
- if (!usedName) {
444
- continue;
445
- }
446
-
447
- let identifier = Template.toIdentifier(usedName);
446
+ const usedName = getUsedExportName(name);
447
+ let identifier = Template.toIdentifier(usedName || name);
448
448
  if (RESERVED_IDENTIFIER.has(identifier)) {
449
449
  identifier = `_${identifier}`;
450
450
  }
@@ -469,9 +469,7 @@ class CssGenerator extends Generator {
469
469
 
470
470
  const needNsObj =
471
471
  this._esModule &&
472
- generateContext.moduleGraph
473
- .getExportsInfo(module)
474
- .otherExportsInfo.getUsed(generateContext.runtime) !==
472
+ exportsInfo.otherExportsInfo.getUsed(generateContext.runtime) !==
475
473
  UsageState.Unused;
476
474
 
477
475
  if (needNsObj) {
@@ -483,7 +481,25 @@ class CssGenerator extends Generator {
483
481
  // Should be after `concatenationScope` to allow module inlining
484
482
  generateContext.runtimeRequirements.add(RuntimeGlobals.module);
485
483
 
486
- 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
+ ) {
487
503
  return new ConcatSource(
488
504
  `${module.moduleArgument}.exports = `,
489
505
  /** @type {Source} */ (defaultExport)
@@ -497,21 +513,14 @@ class CssGenerator extends Generator {
497
513
  }.exports = {\n`
498
514
  );
499
515
 
500
- if (defaultExport) {
516
+ if (defaultUsedName) {
501
517
  result.add('\t"default": ');
502
- result.add(defaultExport);
503
- if (cssData.exports.size > 0) {
518
+ result.add(/** @type {Source} */ (defaultExport));
519
+ if (exportEntries.length > 0) {
504
520
  result.add(",\n");
505
521
  }
506
522
  }
507
523
 
508
- /** @type {string[]} */
509
- const exportEntries = [];
510
- for (const [name, v] of cssData.exports) {
511
- exportEntries.push(
512
- `\t${JSON.stringify(name)}: ${JSON.stringify(v)}`
513
- );
514
- }
515
524
  if (exportEntries.length > 0) {
516
525
  result.add(exportEntries.join(",\n"));
517
526
  }
@@ -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 {
@@ -231,9 +232,21 @@ class CssModulesPlugin {
231
232
  * @returns {void}
232
233
  */
233
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";
234
239
  compiler.hooks.compilation.tap(
235
240
  PLUGIN_NAME,
236
241
  (compilation, { normalModuleFactory }) => {
242
+ if (implicitlyEnabled) {
243
+ implicitTypeLoaderFallback(
244
+ normalModuleFactory,
245
+ PLUGIN_NAME,
246
+ /\.css$/i,
247
+ CSS_MODULE_TYPE_AUTO
248
+ );
249
+ }
237
250
  const hooks = CssModulesPlugin.getCompilationHooks(compilation);
238
251
  compilation.dependencyFactories.set(
239
252
  CssImportDependency,