webpack 5.105.4 → 5.106.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 (123) hide show
  1. package/README.md +3 -6
  2. package/lib/BannerPlugin.js +13 -13
  3. package/lib/Chunk.js +25 -0
  4. package/lib/ChunkGraph.js +8 -4
  5. package/lib/CleanPlugin.js +23 -20
  6. package/lib/CompatibilityPlugin.js +1 -1
  7. package/lib/Compilation.js +34 -11
  8. package/lib/Compiler.js +59 -1
  9. package/lib/CssModule.js +17 -2
  10. package/lib/Dependency.js +1 -1
  11. package/lib/DllPlugin.js +17 -17
  12. package/lib/DllReferencePlugin.js +20 -18
  13. package/lib/DotenvPlugin.js +29 -27
  14. package/lib/ExternalModule.js +39 -6
  15. package/lib/FileSystemInfo.js +3 -1
  16. package/lib/IgnorePlugin.js +12 -11
  17. package/lib/LoaderOptionsPlugin.js +17 -15
  18. package/lib/ManifestPlugin.js +27 -25
  19. package/lib/Module.js +69 -7
  20. package/lib/ModuleGraph.js +3 -0
  21. package/lib/ModuleParseError.js +1 -1
  22. package/lib/ModuleTypeConstants.js +1 -1
  23. package/lib/NormalModule.js +7 -3
  24. package/lib/NormalModuleFactory.js +20 -1
  25. package/lib/ProgressPlugin.js +39 -29
  26. package/lib/RuntimeGlobals.js +6 -0
  27. package/lib/RuntimeTemplate.js +1 -1
  28. package/lib/SourceMapDevToolPlugin.js +13 -11
  29. package/lib/TemplatedPathPlugin.js +4 -3
  30. package/lib/WatchIgnorePlugin.js +15 -13
  31. package/lib/WebpackOptionsApply.js +4 -4
  32. package/lib/asset/AssetModulesPlugin.js +50 -29
  33. package/lib/cli.js +3 -1
  34. package/lib/config/defaults.js +27 -13
  35. package/lib/config/normalization.js +3 -2
  36. package/lib/container/ContainerPlugin.js +46 -42
  37. package/lib/container/ContainerReferencePlugin.js +42 -26
  38. package/lib/container/FallbackModule.js +1 -1
  39. package/lib/container/ModuleFederationPlugin.js +17 -13
  40. package/lib/css/CssGenerator.js +306 -192
  41. package/lib/css/CssInjectStyleRuntimeModule.js +179 -0
  42. package/lib/css/CssLoadingRuntimeModule.js +1 -4
  43. package/lib/css/CssModulesPlugin.js +197 -98
  44. package/lib/css/CssParser.js +243 -134
  45. package/lib/css/walkCssTokens.js +115 -12
  46. package/lib/debug/ProfilingPlugin.js +16 -13
  47. package/lib/dependencies/AMDDefineDependencyParserPlugin.js +20 -15
  48. package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +4 -3
  49. package/lib/dependencies/CommonJsExportRequireDependency.js +4 -2
  50. package/lib/dependencies/CommonJsExportsDependency.js +1 -1
  51. package/lib/dependencies/CommonJsExportsParserPlugin.js +1 -1
  52. package/lib/dependencies/CommonJsFullRequireDependency.js +1 -1
  53. package/lib/dependencies/CommonJsImportsParserPlugin.js +63 -2
  54. package/lib/dependencies/CommonJsRequireContextDependency.js +21 -0
  55. package/lib/dependencies/CommonJsRequireDependency.js +42 -1
  56. package/lib/dependencies/CommonJsSelfReferenceDependency.js +1 -1
  57. package/lib/dependencies/ContextElementDependency.js +1 -1
  58. package/lib/dependencies/CssIcssExportDependency.js +211 -88
  59. package/lib/dependencies/CssIcssImportDependency.js +13 -70
  60. package/lib/dependencies/CssIcssSymbolDependency.js +19 -30
  61. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +13 -2
  62. package/lib/dependencies/HarmonyExportExpressionDependency.js +35 -5
  63. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +13 -3
  64. package/lib/dependencies/HarmonyExportInitFragment.js +1 -1
  65. package/lib/dependencies/HarmonyImportDependency.js +21 -8
  66. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +4 -1
  67. package/lib/dependencies/HarmonyImportSideEffectDependency.js +2 -2
  68. package/lib/dependencies/HarmonyImportSpecifierDependency.js +13 -2
  69. package/lib/dependencies/ImportContextDependency.js +1 -1
  70. package/lib/dependencies/ImportDependency.js +16 -2
  71. package/lib/dependencies/ImportMetaPlugin.js +39 -29
  72. package/lib/dependencies/ImportParserPlugin.js +4 -5
  73. package/lib/dependencies/ImportPhase.js +65 -22
  74. package/lib/dependencies/RequireEnsureDependenciesBlockParserPlugin.js +1 -1
  75. package/lib/esm/ModuleChunkFormatPlugin.js +1 -4
  76. package/lib/ids/HashedModuleIdsPlugin.js +21 -23
  77. package/lib/ids/OccurrenceChunkIdsPlugin.js +15 -11
  78. package/lib/ids/OccurrenceModuleIdsPlugin.js +15 -11
  79. package/lib/javascript/ArrayPushCallbackChunkFormatPlugin.js +1 -4
  80. package/lib/javascript/CommonJsChunkFormatPlugin.js +1 -4
  81. package/lib/javascript/EnableChunkLoadingPlugin.js +1 -2
  82. package/lib/javascript/JavascriptModulesPlugin.js +38 -12
  83. package/lib/javascript/JavascriptParser.js +5 -3
  84. package/lib/json/JsonModulesPlugin.js +28 -21
  85. package/lib/library/AssignLibraryPlugin.js +1 -1
  86. package/lib/library/ExportPropertyLibraryPlugin.js +1 -1
  87. package/lib/library/ModuleLibraryPlugin.js +35 -13
  88. package/lib/library/SystemLibraryPlugin.js +1 -1
  89. package/lib/node/ReadFileCompileAsyncWasmPlugin.js +22 -0
  90. package/lib/optimize/AggressiveSplittingPlugin.js +18 -31
  91. package/lib/optimize/ConcatenatedModule.js +4 -3
  92. package/lib/optimize/InnerGraphPlugin.js +11 -5
  93. package/lib/optimize/LimitChunkCountPlugin.js +22 -18
  94. package/lib/optimize/MergeDuplicateChunksPlugin.js +15 -12
  95. package/lib/optimize/MinChunkSizePlugin.js +20 -16
  96. package/lib/optimize/RemoveEmptyChunksPlugin.js +0 -1
  97. package/lib/rules/RuleSetCompiler.js +1 -0
  98. package/lib/schemes/HttpUriPlugin.js +20 -11
  99. package/lib/schemes/VirtualUrlPlugin.js +77 -30
  100. package/lib/serialization/FileMiddleware.js +7 -7
  101. package/lib/sharing/ConsumeSharedPlugin.js +32 -25
  102. package/lib/sharing/ProvideSharedPlugin.js +29 -25
  103. package/lib/util/{propertyName.js → property.js} +26 -1
  104. package/lib/wasm-async/AsyncWasmCompileRuntimeModule.js +148 -0
  105. package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +54 -0
  106. package/lib/wasm-async/AsyncWebAssemblyModulesPlugin.js +105 -0
  107. package/lib/wasm-async/AsyncWebAssemblyParser.js +30 -6
  108. package/lib/wasm-async/UniversalCompileAsyncWasmPlugin.js +26 -2
  109. package/lib/web/FetchCompileAsyncWasmPlugin.js +23 -0
  110. package/lib/web/JsonpTemplatePlugin.js +1 -0
  111. package/lib/webpack.js +34 -4
  112. package/package.json +18 -16
  113. package/schemas/WebpackOptions.check.js +1 -1
  114. package/schemas/WebpackOptions.json +82 -22
  115. package/schemas/plugins/css/CssModuleGeneratorOptions.check.js +1 -1
  116. package/schemas/plugins/css/CssModuleParserOptions.check.js +1 -1
  117. package/schemas/plugins/css/CssParserOptions.check.js +1 -1
  118. package/schemas/plugins/optimize/LimitChunkCountPlugin.json +1 -1
  119. package/schemas/plugins/schemes/VirtualUrlPlugin.check.js +1 -1
  120. package/schemas/plugins/schemes/VirtualUrlPlugin.json +8 -0
  121. package/types.d.ts +511 -114
  122. package/lib/util/create-schema-validation.js +0 -41
  123. package/lib/util/propertyAccess.js +0 -30
@@ -5,7 +5,12 @@
5
5
 
6
6
  "use strict";
7
7
 
8
- const { ConcatSource, RawSource, ReplaceSource } = require("webpack-sources");
8
+ const {
9
+ ConcatSource,
10
+ RawSource,
11
+ ReplaceSource,
12
+ SourceMapSource
13
+ } = require("webpack-sources");
9
14
  const { UsageState } = require("../ExportsInfo");
10
15
  const Generator = require("../Generator");
11
16
  const InitFragment = require("../InitFragment");
@@ -20,7 +25,7 @@ const RuntimeGlobals = require("../RuntimeGlobals");
20
25
  const Template = require("../Template");
21
26
  const CssImportDependency = require("../dependencies/CssImportDependency");
22
27
  const HarmonyImportSideEffectDependency = require("../dependencies/HarmonyImportSideEffectDependency");
23
- const { getUndoPath } = require("../util/identifier");
28
+
24
29
  const memoize = require("../util/memoize");
25
30
 
26
31
  /** @typedef {import("webpack-sources").Source} Source */
@@ -46,7 +51,7 @@ const memoize = require("../util/memoize");
46
51
  /** @typedef {import("../Module").RuntimeRequirements} RuntimeRequirements */
47
52
  /** @typedef {import("../../declarations/WebpackOptions").CssParserExportType} CssParserExportType */
48
53
 
49
- const getPropertyName = memoize(() => require("../util/propertyName"));
54
+ const getPropertyName = memoize(() => require("../util/property"));
50
55
  const getCssModulesPlugin = memoize(() => require("./CssModulesPlugin"));
51
56
 
52
57
  class CssGenerator extends Generator {
@@ -81,9 +86,10 @@ class CssGenerator extends Generator {
81
86
  * Generate JavaScript code that requires and concatenates all CSS imports
82
87
  * @param {NormalModule} module the module to generate CSS text for
83
88
  * @param {GenerateContext} generateContext the generate context
89
+ * @param {boolean} getDefaultExport whether to get the default export
84
90
  * @returns {{ expr: string, type: CssParserExportType }[]} JavaScript code that concatenates all imported CSS
85
91
  */
86
- _generateImportCode(module, generateContext) {
92
+ _generateImportCode(module, generateContext, getDefaultExport = true) {
87
93
  const moduleGraph = generateContext.moduleGraph;
88
94
  /** @type {{ expr: string, type: CssParserExportType }[]} */
89
95
  const parts = [];
@@ -101,15 +107,24 @@ class CssGenerator extends Generator {
101
107
  runtimeRequirements: generateContext.runtimeRequirements
102
108
  });
103
109
 
104
- generateContext.runtimeRequirements.add(
105
- RuntimeGlobals.compatGetDefaultExport
106
- );
107
- parts.push({
108
- expr: `(${RuntimeGlobals.compatGetDefaultExport}(${importVar})() || "")`,
109
- type: /** @type {CssParserExportType} */ (
110
- /** @type {BuildMeta} */ (depModule.buildMeta).exportType
111
- )
112
- });
110
+ if (getDefaultExport) {
111
+ generateContext.runtimeRequirements.add(
112
+ RuntimeGlobals.compatGetDefaultExport
113
+ );
114
+ parts.push({
115
+ expr: `(${RuntimeGlobals.compatGetDefaultExport}(${importVar})() || "")`,
116
+ type: /** @type {CssParserExportType} */ (
117
+ /** @type {CssModule} */ (depModule).exportType
118
+ )
119
+ });
120
+ } else {
121
+ parts.push({
122
+ expr: importVar,
123
+ type: /** @type {CssParserExportType} */ (
124
+ /** @type {CssModule} */ (depModule).exportType
125
+ )
126
+ });
127
+ }
113
128
  }
114
129
  }
115
130
 
@@ -117,12 +132,12 @@ class CssGenerator extends Generator {
117
132
  }
118
133
 
119
134
  /**
120
- * Generate CSS code for the current module
121
- * @param {NormalModule} module the module to generate CSS code for
135
+ * Generate CSS source for the current module
136
+ * @param {NormalModule} module the module to generate CSS source for
122
137
  * @param {GenerateContext} generateContext the generate context
123
- * @returns {string} the CSS code as string
138
+ * @returns {Source | null} the CSS source
124
139
  */
125
- _generateModuleCode(module, generateContext) {
140
+ _generateContentSource(module, generateContext) {
126
141
  const moduleSourceContent = /** @type {Source} */ (
127
142
  this.generate(module, {
128
143
  ...generateContext,
@@ -131,26 +146,19 @@ class CssGenerator extends Generator {
131
146
  );
132
147
 
133
148
  if (!moduleSourceContent) {
134
- return "";
149
+ return null;
135
150
  }
136
151
 
137
152
  const compilation = generateContext.runtimeTemplate.compilation;
138
- const { path: filename } = compilation.getPathWithInfo(
139
- compilation.outputOptions.cssChunkFilename,
140
- {
141
- runtime: generateContext.runtime,
142
- contentHashType: "css"
143
- }
144
- );
145
- const undoPath = getUndoPath(
146
- filename,
147
- compilation.outputOptions.path,
148
- false
149
- );
153
+ // For non-link exportTypes (style, text, css-style-sheet), url() in the CSS
154
+ // is resolved relative to the document URL (for <style> tags and CSSStyleSheet),
155
+ // not relative to any output file. Use empty undoPath so urls are relative to
156
+ // the output root.
157
+ const undoPath = "";
150
158
 
151
159
  const CssModulesPlugin = getCssModulesPlugin();
152
160
  const hooks = CssModulesPlugin.getCompilationHooks(compilation);
153
- const renderedSource = CssModulesPlugin.renderModule(
161
+ return CssModulesPlugin.renderModule(
154
162
  /** @type {CssModule} */ (module),
155
163
  {
156
164
  undoPath,
@@ -160,13 +168,23 @@ class CssGenerator extends Generator {
160
168
  },
161
169
  hooks
162
170
  );
171
+ }
163
172
 
164
- if (!renderedSource) {
165
- return "";
173
+ /**
174
+ * Convert a CSS Source to a JS string literal Source, preserving source map.
175
+ * Wraps the CSS content with JSON.stringify so it can be embedded in JS code.
176
+ * @param {Source} cssSource the CSS source
177
+ * @param {NormalModule} module the module
178
+ * @returns {Source} a Source representing a JS string literal
179
+ */
180
+ _cssSourceToJsStringLiteral(cssSource, module) {
181
+ const { source, map } = cssSource.sourceAndMap();
182
+ const content = /** @type {string} */ (source);
183
+ const escaped = JSON.stringify(content);
184
+ if (map) {
185
+ return new SourceMapSource(escaped, module.identifier(), map, content);
166
186
  }
167
-
168
- const content = renderedSource.source();
169
- return typeof content === "string" ? content : content.toString("utf8");
187
+ return new RawSource(escaped);
170
188
  }
171
189
 
172
190
  /**
@@ -263,12 +281,10 @@ class CssGenerator extends Generator {
263
281
  * @returns {Source | null} generated code
264
282
  */
265
283
  generate(module, generateContext) {
266
- const exportType = /** @type {BuildMeta} */ (module.buildMeta).exportType;
284
+ const exportType = /** @type {CssModule} */ (module).exportType || "link";
267
285
  const source =
268
- generateContext.type === JAVASCRIPT_TYPE
269
- ? exportType === "link"
270
- ? new ReplaceSource(new RawSource(""))
271
- : new ReplaceSource(/** @type {Source} */ (module.originalSource()))
286
+ generateContext.type === JAVASCRIPT_TYPE && exportType === "link"
287
+ ? new ReplaceSource(new RawSource(""))
272
288
  : new ReplaceSource(/** @type {Source} */ (module.originalSource()));
273
289
  /** @type {InitFragment<GenerateContext>[]} */
274
290
  const initFragments = [];
@@ -283,155 +299,262 @@ class CssGenerator extends Generator {
283
299
  cssData
284
300
  });
285
301
 
286
- const generateCssText = () => {
287
- const importCode = this._generateImportCode(module, generateContext);
288
- const moduleCode = this._generateModuleCode(module, generateContext);
289
-
290
- if (importCode.length > 0) {
291
- if (
292
- exportType === "css-style-sheet" ||
293
- importCode.some((part) => part.type !== exportType)
294
- ) {
295
- generateContext.runtimeRequirements.add(
296
- RuntimeGlobals.cssMergeStyleSheets
297
- );
298
-
299
- return `${RuntimeGlobals.cssMergeStyleSheets}([${[...importCode.map((part) => part.expr), JSON.stringify(moduleCode)].join(", ")}])`;
300
- }
301
- return generateContext.runtimeTemplate.concatenation(
302
- ...importCode,
303
- moduleCode
304
- );
305
- }
306
- return JSON.stringify(moduleCode);
307
- };
308
-
309
- /**
310
- * @returns {string | null} the default export
311
- */
312
- const generateJSDefaultExport = () => {
313
- switch (exportType) {
314
- case "text": {
315
- return generateCssText();
316
- }
317
- case "css-style-sheet": {
318
- const constOrVar = generateContext.runtimeTemplate.renderConst();
319
- return `(${generateContext.runtimeTemplate.basicFunction("", [
320
- `${constOrVar} cssText = ${generateCssText()};`,
321
- `${constOrVar} sheet = new CSSStyleSheet();`,
322
- "sheet.replaceSync(cssText);",
323
- "return sheet;"
324
- ])})()`;
325
- }
326
- default:
327
- return null;
328
- }
329
- };
330
-
331
302
  switch (generateContext.type) {
332
303
  case JAVASCRIPT_TYPE: {
333
- const isCSSModule = /** @type {BuildMeta} */ (module.buildMeta)
334
- .isCSSModule;
335
- const defaultExport = generateJSDefaultExport();
336
- /**
337
- * @param {string} name the export name
338
- * @param {string} value the export value
339
- * @returns {string} the value to be used in the export
340
- */
341
- const stringifyExportValue = (name, value) => {
342
- if (defaultExport) {
343
- return name === "default" ? value : JSON.stringify(value);
304
+ const generateContentCode = () => {
305
+ switch (exportType) {
306
+ case "style": {
307
+ const cssSource = this._generateContentSource(
308
+ module,
309
+ generateContext
310
+ );
311
+ if (!cssSource) return "";
312
+ const moduleId = generateContext.chunkGraph.getModuleId(module);
313
+
314
+ generateContext.runtimeRequirements.add(
315
+ RuntimeGlobals.cssInjectStyle
316
+ );
317
+
318
+ return new ConcatSource(
319
+ `${RuntimeGlobals.cssInjectStyle}(${JSON.stringify(moduleId || "")}, `,
320
+ this._cssSourceToJsStringLiteral(cssSource, module),
321
+ ");"
322
+ );
323
+ }
324
+
325
+ default:
326
+ return "";
344
327
  }
345
- return JSON.stringify(value);
346
328
  };
329
+ const generateImportCode = () => {
330
+ switch (exportType) {
331
+ case "style": {
332
+ return this._generateImportCode(module, generateContext, false)
333
+ .map((part) => `${part.expr};`)
334
+ .join("\n");
335
+ }
336
+ default:
337
+ return "";
338
+ }
339
+ };
340
+ const generateExportCode = () => {
341
+ /** @returns {Source} generated CSS text as JS expression */
342
+ const generateCssText = () => {
343
+ const importCode = this._generateImportCode(
344
+ module,
345
+ generateContext
346
+ );
347
+ const cssSource = this._generateContentSource(
348
+ module,
349
+ generateContext
350
+ );
351
+ const jsLiteral = cssSource
352
+ ? this._cssSourceToJsStringLiteral(cssSource, module)
353
+ : new RawSource('""');
354
+
355
+ if (importCode.length > 0) {
356
+ if (
357
+ exportType === "css-style-sheet" ||
358
+ importCode.some((part) => part.type !== exportType)
359
+ ) {
360
+ generateContext.runtimeRequirements.add(
361
+ RuntimeGlobals.cssMergeStyleSheets
362
+ );
363
+
364
+ const args = importCode.map((part) => part.expr);
365
+ return new ConcatSource(
366
+ `${RuntimeGlobals.cssMergeStyleSheets}([${args.join(", ")}, `,
367
+ jsLiteral,
368
+ "])"
369
+ );
370
+ }
371
+ return new ConcatSource(
372
+ `${generateContext.runtimeTemplate.concatenation(
373
+ ...importCode
374
+ )} + `,
375
+ jsLiteral
376
+ );
377
+ }
378
+ return jsLiteral;
379
+ };
380
+ /**
381
+ * @returns {Source | null} the default export
382
+ */
383
+ const generateJSDefaultExport = () => {
384
+ switch (exportType) {
385
+ case "text": {
386
+ return generateCssText();
387
+ }
388
+ case "css-style-sheet": {
389
+ const constOrVar =
390
+ generateContext.runtimeTemplate.renderConst();
391
+ const cssText = generateCssText();
392
+ const fnPrefix =
393
+ generateContext.runtimeTemplate.supportsArrowFunction()
394
+ ? "() => {\n"
395
+ : "function() {\n";
396
+ const body =
397
+ `${constOrVar} sheet = new CSSStyleSheet();\n` +
398
+ "sheet.replaceSync(cssText);\n" +
399
+ "return sheet;\n";
400
+ return new ConcatSource(
401
+ `(${fnPrefix}${constOrVar} cssText = `,
402
+ cssText,
403
+ `;\n${body}})()`
404
+ );
405
+ }
406
+ default:
407
+ return null;
408
+ }
409
+ };
347
410
 
348
- /** @type {BuildInfo} */
349
- (module.buildInfo).cssData = cssData;
350
-
351
- // Required for HMR
352
- if (module.hot) {
353
- generateContext.runtimeRequirements.add(RuntimeGlobals.module);
354
- }
411
+ const isCSSModule = /** @type {BuildMeta} */ (module.buildMeta)
412
+ .isCSSModule;
413
+ /** @type {Source | null} */
414
+ const defaultExport = generateJSDefaultExport();
355
415
 
356
- if (defaultExport) {
357
- cssData.exports.set("default", /** @type {string} */ (defaultExport));
358
- }
416
+ /** @type {BuildInfo} */
417
+ (module.buildInfo).cssData = cssData;
359
418
 
360
- if (cssData.exports.size === 0 && !isCSSModule) {
361
- return new RawSource("");
362
- }
419
+ // Required for HMR
420
+ if (module.hot) {
421
+ generateContext.runtimeRequirements.add(RuntimeGlobals.module);
422
+ }
363
423
 
364
- if (generateContext.concatenationScope) {
365
- const source = new ConcatSource();
366
- /** @type {Set<string>} */
367
- const usedIdentifiers = new Set();
368
- const { RESERVED_IDENTIFIER } = getPropertyName();
424
+ if (!defaultExport && cssData.exports.size === 0 && !isCSSModule) {
425
+ return new RawSource("");
426
+ }
369
427
 
370
- for (const [name, v] of cssData.exports) {
371
- const usedName = generateContext.moduleGraph
372
- .getExportInfo(module, name)
373
- .getUsedName(name, generateContext.runtime);
374
- if (!usedName) {
375
- continue;
428
+ if (generateContext.concatenationScope) {
429
+ const source = new ConcatSource();
430
+ /** @type {Set<string>} */
431
+ const usedIdentifiers = new Set();
432
+ const { RESERVED_IDENTIFIER } = getPropertyName();
433
+
434
+ if (defaultExport) {
435
+ const usedName = generateContext.moduleGraph
436
+ .getExportInfo(module, "default")
437
+ .getUsedName("default", generateContext.runtime);
438
+ if (usedName) {
439
+ let identifier = Template.toIdentifier(usedName);
440
+ if (RESERVED_IDENTIFIER.has(identifier)) {
441
+ identifier = `_${identifier}`;
442
+ }
443
+ usedIdentifiers.add(identifier);
444
+ generateContext.concatenationScope.registerExport(
445
+ "default",
446
+ identifier
447
+ );
448
+ source.add(
449
+ `${generateContext.runtimeTemplate.renderConst()} ${identifier} = `
450
+ );
451
+ source.add(defaultExport);
452
+ source.add(";\n");
453
+ }
376
454
  }
377
455
 
378
- let identifier = Template.toIdentifier(usedName);
379
- if (RESERVED_IDENTIFIER.has(identifier)) {
380
- identifier = `_${identifier}`;
381
- }
382
- let i = 0;
383
- while (usedIdentifiers.has(identifier)) {
384
- identifier = Template.toIdentifier(name + i);
385
- i += 1;
456
+ for (const [name, v] of cssData.exports) {
457
+ const usedName = generateContext.moduleGraph
458
+ .getExportInfo(module, name)
459
+ .getUsedName(name, generateContext.runtime);
460
+ if (!usedName) {
461
+ continue;
462
+ }
463
+
464
+ let identifier = Template.toIdentifier(usedName);
465
+ if (RESERVED_IDENTIFIER.has(identifier)) {
466
+ identifier = `_${identifier}`;
467
+ }
468
+ let i = 0;
469
+ while (usedIdentifiers.has(identifier)) {
470
+ identifier = Template.toIdentifier(name + i);
471
+ i += 1;
472
+ }
473
+ usedIdentifiers.add(identifier);
474
+ generateContext.concatenationScope.registerExport(
475
+ name,
476
+ identifier
477
+ );
478
+ source.add(
479
+ `${generateContext.runtimeTemplate.renderConst()} ${identifier} = ${JSON.stringify(v)};\n`
480
+ );
386
481
  }
387
- usedIdentifiers.add(identifier);
388
- generateContext.concatenationScope.registerExport(name, identifier);
389
- source.add(
390
- `${generateContext.runtimeTemplate.renderConst()} ${identifier} = ${stringifyExportValue(name, v)};\n`
482
+ return source;
483
+ }
484
+
485
+ const needNsObj =
486
+ this._esModule &&
487
+ generateContext.moduleGraph
488
+ .getExportsInfo(module)
489
+ .otherExportsInfo.getUsed(generateContext.runtime) !==
490
+ UsageState.Unused;
491
+
492
+ if (needNsObj) {
493
+ generateContext.runtimeRequirements.add(
494
+ RuntimeGlobals.makeNamespaceObject
391
495
  );
392
496
  }
393
- return source;
394
- }
395
497
 
396
- const needNsObj =
397
- this._esModule &&
398
- generateContext.moduleGraph
399
- .getExportsInfo(module)
400
- .otherExportsInfo.getUsed(generateContext.runtime) !==
401
- UsageState.Unused;
498
+ // Should be after `concatenationScope` to allow module inlining
499
+ generateContext.runtimeRequirements.add(RuntimeGlobals.module);
402
500
 
403
- if (needNsObj) {
404
- generateContext.runtimeRequirements.add(
405
- RuntimeGlobals.makeNamespaceObject
501
+ if (!isCSSModule && !needNsObj) {
502
+ return new ConcatSource(
503
+ `${module.moduleArgument}.exports = `,
504
+ /** @type {Source} */ (defaultExport)
505
+ );
506
+ }
507
+
508
+ const result = new ConcatSource();
509
+ result.add(
510
+ `${needNsObj ? `${RuntimeGlobals.makeNamespaceObject}(` : ""}${
511
+ module.moduleArgument
512
+ }.exports = {\n`
406
513
  );
407
- }
408
514
 
409
- // Should be after `concatenationScope` to allow module inlining
410
- generateContext.runtimeRequirements.add(RuntimeGlobals.module);
515
+ if (defaultExport) {
516
+ result.add('\t"default": ');
517
+ result.add(defaultExport);
518
+ if (cssData.exports.size > 0) {
519
+ result.add(",\n");
520
+ }
521
+ }
411
522
 
412
- if (!isCSSModule && !needNsObj) {
413
- return new RawSource(
414
- `${module.moduleArgument}.exports = ${defaultExport}`
415
- );
416
- }
523
+ /** @type {string[]} */
524
+ const exportEntries = [];
525
+ for (const [name, v] of cssData.exports) {
526
+ exportEntries.push(
527
+ `\t${JSON.stringify(name)}: ${JSON.stringify(v)}`
528
+ );
529
+ }
530
+ if (exportEntries.length > 0) {
531
+ result.add(exportEntries.join(",\n"));
532
+ }
417
533
 
418
- /** @type {string[]} */
419
- const exports = [];
534
+ result.add(`\n}${needNsObj ? ")" : ""};`);
535
+ return result;
536
+ };
420
537
 
421
- for (const [name, v] of cssData.exports) {
422
- exports.push(
423
- `\t${JSON.stringify(name)}: ${stringifyExportValue(name, v)}`
424
- );
425
- }
538
+ const codeParts = this._exportsOnly
539
+ ? [generateExportCode()]
540
+ : [generateImportCode(), generateContentCode(), generateExportCode()];
426
541
 
427
- return new RawSource(
428
- `${needNsObj ? `${RuntimeGlobals.makeNamespaceObject}(` : ""}${
429
- module.moduleArgument
430
- }.exports = {\n${exports.join(",\n")}\n}${needNsObj ? ")" : ""};`
431
- );
542
+ const source = new ConcatSource();
543
+ for (const part of codeParts) {
544
+ if (part) {
545
+ source.add(part);
546
+ source.add("\n");
547
+ }
548
+ }
549
+ return source;
432
550
  }
433
551
  case CSS_TYPE: {
434
- if (!this._generatesJsOnly(module)) {
552
+ if (
553
+ !(
554
+ this._exportsOnly ||
555
+ /** @type {boolean} */ (exportType && exportType !== "link")
556
+ )
557
+ ) {
435
558
  generateContext.runtimeRequirements.add(RuntimeGlobals.hasCssModules);
436
559
  }
437
560
 
@@ -468,9 +591,12 @@ class CssGenerator extends Generator {
468
591
  * @returns {SourceTypes} available types (do not mutate)
469
592
  */
470
593
  getTypes(module) {
471
- const exportType = /** @type {BuildMeta} */ (module.buildMeta).exportType;
472
- const sourceTypes = new Set();
594
+ const exportType = /** @type {CssModule} */ (module).exportType || "link";
595
+ if (exportType === "style") {
596
+ return JAVASCRIPT_TYPES;
597
+ }
473
598
 
599
+ const sourceTypes = new Set();
474
600
  const connections = this._moduleGraph.getIncomingConnections(module);
475
601
 
476
602
  for (const connection of connections) {
@@ -495,9 +621,8 @@ class CssGenerator extends Generator {
495
621
  sourceTypes.add(JAVASCRIPT_TYPE);
496
622
  } else {
497
623
  const originModule = /** @type {CssModule} */ connection.originModule;
498
- const originExportType = /** @type {BuildMeta} */ (
499
- originModule.buildMeta
500
- ).exportType;
624
+ const originExportType = /** @type {CssModule} */ (originModule)
625
+ .exportType;
501
626
  if (
502
627
  /** @type {boolean} */ (
503
628
  originExportType && originExportType !== "link"
@@ -507,7 +632,10 @@ class CssGenerator extends Generator {
507
632
  }
508
633
  }
509
634
  }
510
- if (this._generatesJsOnly(module)) {
635
+ if (
636
+ this._exportsOnly ||
637
+ /** @type {boolean} */ (exportType && exportType !== "link")
638
+ ) {
511
639
  if (sourceTypes.has(JAVASCRIPT_TYPE)) {
512
640
  return JAVASCRIPT_TYPES;
513
641
  }
@@ -538,12 +666,12 @@ class CssGenerator extends Generator {
538
666
  return 0;
539
667
  }
540
668
  const exports = cssData.exports;
541
- const stringifiedExports = JSON.stringify(
542
- [...exports].reduce((obj, [key, value]) => {
543
- obj[key] = value;
544
- return obj;
545
- }, /** @type {Record<string, string>} */ ({}))
546
- );
669
+ /** @type {Record<string, string>} */
670
+ const exportsObj = {};
671
+ for (const [key, value] of exports) {
672
+ exportsObj[key] = value;
673
+ }
674
+ const stringifiedExports = JSON.stringify(exportsObj);
547
675
 
548
676
  return stringifiedExports.length + 42;
549
677
  }
@@ -568,20 +696,6 @@ class CssGenerator extends Generator {
568
696
  updateHash(hash, { module }) {
569
697
  hash.update(/** @type {boolean} */ (this._esModule).toString());
570
698
  }
571
-
572
- /**
573
- * @param {NormalModule} module module
574
- * @returns {boolean} true if the module only outputs JavaScript
575
- */
576
- _generatesJsOnly(module) {
577
- const exportType = /** @type {BuildMeta} */ (module.buildMeta).exportType;
578
- return (
579
- this._exportsOnly ||
580
- /** @type {boolean} */ (exportType && exportType !== "link")
581
- );
582
- }
583
699
  }
584
700
 
585
701
  module.exports = CssGenerator;
586
-
587
- module.exports = CssGenerator;