webpack 5.103.0 → 5.104.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 (177) hide show
  1. package/hot/dev-server.js +18 -3
  2. package/hot/emitter-event-target.js +7 -0
  3. package/hot/lazy-compilation-node.js +45 -29
  4. package/hot/lazy-compilation-universal.js +18 -0
  5. package/hot/lazy-compilation-web.js +15 -5
  6. package/hot/load-http.js +7 -0
  7. package/hot/only-dev-server.js +19 -4
  8. package/lib/APIPlugin.js +6 -0
  9. package/lib/Chunk.js +1 -1
  10. package/lib/ChunkGraph.js +9 -7
  11. package/lib/ChunkGroup.js +8 -5
  12. package/lib/CleanPlugin.js +6 -3
  13. package/lib/CodeGenerationResults.js +2 -1
  14. package/lib/CompatibilityPlugin.js +3 -0
  15. package/lib/Compilation.js +33 -19
  16. package/lib/Compiler.js +3 -3
  17. package/lib/ContextModule.js +6 -3
  18. package/lib/ContextModuleFactory.js +6 -4
  19. package/lib/DefinePlugin.js +34 -3
  20. package/lib/DelegatedModule.js +7 -4
  21. package/lib/DllModule.js +6 -3
  22. package/lib/DotenvPlugin.js +11 -6
  23. package/lib/ExportsInfo.js +5 -5
  24. package/lib/ExternalModule.js +8 -7
  25. package/lib/ExternalModuleFactoryPlugin.js +1 -1
  26. package/lib/FileSystemInfo.js +1 -1
  27. package/lib/Generator.js +10 -7
  28. package/lib/HookWebpackError.js +33 -4
  29. package/lib/HotModuleReplacementPlugin.js +22 -0
  30. package/lib/ManifestPlugin.js +1 -1
  31. package/lib/Module.js +24 -15
  32. package/lib/ModuleBuildError.js +1 -1
  33. package/lib/ModuleError.js +1 -1
  34. package/lib/ModuleFilenameHelpers.js +1 -1
  35. package/lib/ModuleGraph.js +27 -12
  36. package/lib/ModuleGraphConnection.js +2 -2
  37. package/lib/ModuleSourceTypeConstants.js +189 -0
  38. package/lib/ModuleTypeConstants.js +1 -4
  39. package/lib/ModuleWarning.js +1 -1
  40. package/lib/NodeStuffPlugin.js +52 -42
  41. package/lib/NormalModule.js +6 -4
  42. package/lib/NormalModuleFactory.js +7 -10
  43. package/lib/Parser.js +1 -1
  44. package/lib/RawModule.js +7 -4
  45. package/lib/RuntimeModule.js +1 -1
  46. package/lib/RuntimeTemplate.js +5 -1
  47. package/lib/SizeFormatHelpers.js +1 -1
  48. package/lib/SourceMapDevToolPlugin.js +6 -1
  49. package/lib/Template.js +17 -6
  50. package/lib/TemplatedPathPlugin.js +5 -6
  51. package/lib/WebpackError.js +0 -1
  52. package/lib/WebpackOptionsApply.js +37 -9
  53. package/lib/asset/AssetBytesGenerator.js +15 -11
  54. package/lib/asset/AssetGenerator.js +30 -24
  55. package/lib/asset/AssetSourceGenerator.js +15 -11
  56. package/lib/asset/RawDataUrlModule.js +6 -3
  57. package/lib/buildChunkGraph.js +4 -2
  58. package/lib/cache/PackFileCacheStrategy.js +6 -5
  59. package/lib/cli.js +2 -43
  60. package/lib/config/browserslistTargetHandler.js +19 -0
  61. package/lib/config/defaults.js +128 -43
  62. package/lib/config/normalization.js +2 -2
  63. package/lib/config/target.js +5 -0
  64. package/lib/container/ContainerEntryModule.js +6 -3
  65. package/lib/container/FallbackModule.js +6 -3
  66. package/lib/container/RemoteModule.js +1 -3
  67. package/lib/css/CssGenerator.js +26 -24
  68. package/lib/css/CssLoadingRuntimeModule.js +12 -4
  69. package/lib/css/CssModulesPlugin.js +29 -74
  70. package/lib/css/CssParser.js +828 -341
  71. package/lib/css/walkCssTokens.js +33 -13
  72. package/lib/dependencies/CachedConstDependency.js +24 -10
  73. package/lib/dependencies/CommonJsRequireContextDependency.js +1 -1
  74. package/lib/dependencies/ContextDependencyHelpers.js +2 -2
  75. package/lib/dependencies/ContextDependencyTemplateAsRequireCall.js +3 -1
  76. package/lib/dependencies/CssIcssExportDependency.js +242 -104
  77. package/lib/dependencies/CssIcssImportDependency.js +61 -4
  78. package/lib/dependencies/CssIcssSymbolDependency.js +2 -6
  79. package/lib/dependencies/CssImportDependency.js +2 -1
  80. package/lib/dependencies/CssUrlDependency.js +3 -2
  81. package/lib/dependencies/DynamicExports.js +7 -7
  82. package/lib/dependencies/ExternalModuleDependency.js +7 -4
  83. package/lib/dependencies/ExternalModuleInitFragment.js +2 -1
  84. package/lib/dependencies/ExternalModuleInitFragmentDependency.js +2 -1
  85. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +3 -2
  86. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +6 -4
  87. package/lib/dependencies/HarmonyExports.js +4 -4
  88. package/lib/dependencies/HarmonyImportDependency.js +8 -3
  89. package/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +1 -1
  90. package/lib/dependencies/ImportMetaPlugin.js +57 -0
  91. package/lib/dependencies/ImportParserPlugin.js +2 -2
  92. package/lib/dependencies/LocalModulesHelpers.js +3 -3
  93. package/lib/dependencies/WorkerPlugin.js +2 -2
  94. package/lib/dependencies/getFunctionExpression.js +1 -1
  95. package/lib/esm/ModuleChunkFormatPlugin.js +5 -4
  96. package/lib/hmr/HotModuleReplacement.runtime.js +2 -1
  97. package/lib/hmr/LazyCompilationPlugin.js +4 -3
  98. package/lib/ids/IdHelpers.js +16 -7
  99. package/lib/javascript/ChunkHelpers.js +1 -1
  100. package/lib/javascript/JavascriptGenerator.js +4 -3
  101. package/lib/javascript/JavascriptModulesPlugin.js +57 -24
  102. package/lib/javascript/JavascriptParser.js +19 -6
  103. package/lib/json/JsonGenerator.js +5 -4
  104. package/lib/json/JsonParser.js +2 -1
  105. package/lib/library/AbstractLibraryPlugin.js +1 -1
  106. package/lib/library/AmdLibraryPlugin.js +4 -1
  107. package/lib/library/ExportPropertyLibraryPlugin.js +4 -1
  108. package/lib/library/ModuleLibraryPlugin.js +41 -13
  109. package/lib/library/SystemLibraryPlugin.js +4 -1
  110. package/lib/library/UmdLibraryPlugin.js +1 -1
  111. package/lib/logging/Logger.js +5 -4
  112. package/lib/logging/createConsoleLogger.js +2 -2
  113. package/lib/optimize/ConcatenatedModule.js +47 -32
  114. package/lib/optimize/ModuleConcatenationPlugin.js +5 -4
  115. package/lib/optimize/SideEffectsFlagPlugin.js +3 -2
  116. package/lib/optimize/SplitChunksPlugin.js +60 -46
  117. package/lib/rules/RuleSetCompiler.js +1 -1
  118. package/lib/runtime/GetChunkFilenameRuntimeModule.js +3 -2
  119. package/lib/schemes/HttpUriPlugin.js +97 -10
  120. package/lib/serialization/AggregateErrorSerializer.js +1 -2
  121. package/lib/serialization/ObjectMiddleware.js +0 -2
  122. package/lib/serialization/SingleItemMiddleware.js +1 -1
  123. package/lib/sharing/ConsumeSharedModule.js +1 -1
  124. package/lib/sharing/ConsumeSharedPlugin.js +5 -3
  125. package/lib/sharing/ProvideSharedModule.js +1 -1
  126. package/lib/sharing/resolveMatchedConfigs.js +15 -9
  127. package/lib/sharing/utils.js +1 -1
  128. package/lib/stats/DefaultStatsFactoryPlugin.js +8 -5
  129. package/lib/stats/DefaultStatsPresetPlugin.js +1 -1
  130. package/lib/stats/DefaultStatsPrinterPlugin.js +1 -1
  131. package/lib/util/StringXor.js +1 -1
  132. package/lib/util/URLAbsoluteSpecifier.js +2 -2
  133. package/lib/util/binarySearchBounds.js +2 -2
  134. package/lib/util/comparators.js +53 -76
  135. package/lib/util/compileBooleanMatcher.js +78 -6
  136. package/lib/util/createHash.js +20 -199
  137. package/lib/util/deprecation.js +1 -1
  138. package/lib/util/deterministicGrouping.js +6 -3
  139. package/lib/util/fs.js +75 -75
  140. package/lib/util/hash/BatchedHash.js +10 -9
  141. package/lib/util/hash/BulkUpdateHash.js +138 -0
  142. package/lib/util/hash/DebugHash.js +75 -0
  143. package/lib/util/hash/hash-digest.js +216 -0
  144. package/lib/util/identifier.js +82 -17
  145. package/lib/util/internalSerializables.js +2 -6
  146. package/lib/util/runtime.js +3 -3
  147. package/lib/util/source.js +2 -2
  148. package/lib/wasm-async/AsyncWebAssemblyGenerator.js +3 -2
  149. package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +3 -2
  150. package/lib/wasm-sync/WebAssemblyGenerator.js +9 -6
  151. package/lib/wasm-sync/WebAssemblyJavascriptGenerator.js +3 -2
  152. package/lib/wasm-sync/WebAssemblyModulesPlugin.js +6 -2
  153. package/lib/webpack.js +1 -1
  154. package/package.json +30 -25
  155. package/schemas/WebpackOptions.check.js +1 -1
  156. package/schemas/WebpackOptions.json +59 -82
  157. package/schemas/plugins/css/CssModuleGeneratorOptions.check.js +1 -1
  158. package/schemas/plugins/css/CssModuleParserOptions.check.js +1 -1
  159. package/types.d.ts +225 -158
  160. package/lib/ModuleSourceTypesConstants.js +0 -117
  161. package/lib/dependencies/CssIcssFromIdentifierDependency.js +0 -124
  162. package/lib/dependencies/CssIcssGlobalIdentifierDependency.js +0 -48
  163. package/lib/dependencies/CssIcssLocalIdentifierDependency.js +0 -61
  164. package/lib/dependencies/CssIcssSelfLocalIdentifierDependency.js +0 -190
  165. package/lib/util/jsonParseEvenBetterErrors.js +0 -10
  166. package/schemas/plugins/css/CssAutoGeneratorOptions.check.d.ts +0 -7
  167. package/schemas/plugins/css/CssAutoGeneratorOptions.check.js +0 -6
  168. package/schemas/plugins/css/CssAutoGeneratorOptions.json +0 -3
  169. package/schemas/plugins/css/CssAutoParserOptions.check.d.ts +0 -7
  170. package/schemas/plugins/css/CssAutoParserOptions.check.js +0 -6
  171. package/schemas/plugins/css/CssAutoParserOptions.json +0 -3
  172. package/schemas/plugins/css/CssGlobalGeneratorOptions.check.d.ts +0 -7
  173. package/schemas/plugins/css/CssGlobalGeneratorOptions.check.js +0 -6
  174. package/schemas/plugins/css/CssGlobalGeneratorOptions.json +0 -3
  175. package/schemas/plugins/css/CssGlobalParserOptions.check.d.ts +0 -7
  176. package/schemas/plugins/css/CssGlobalParserOptions.check.js +0 -6
  177. package/schemas/plugins/css/CssGlobalParserOptions.json +0 -3
@@ -5,11 +5,14 @@
5
5
 
6
6
  "use strict";
7
7
 
8
+ const { CSS_TYPE, JAVASCRIPT_TYPE } = require("../ModuleSourceTypeConstants");
9
+ const WebpackError = require("../WebpackError");
8
10
  const { cssExportConvention } = require("../util/conventions");
9
11
  const createHash = require("../util/createHash");
10
12
  const { makePathsRelative } = require("../util/identifier");
11
13
  const makeSerializable = require("../util/makeSerializable");
12
14
  const memoize = require("../util/memoize");
15
+ const nonNumericOnlyHash = require("../util/nonNumericOnlyHash");
13
16
  const CssIcssImportDependency = require("./CssIcssImportDependency");
14
17
  const NullDependency = require("./NullDependency");
15
18
 
@@ -20,6 +23,7 @@ const getCssParser = memoize(() => require("../css/CssParser"));
20
23
  /** @typedef {import("../../declarations/WebpackOptions").CssGeneratorLocalIdentName} CssGeneratorLocalIdentName */
21
24
  /** @typedef {import("../CssModule")} CssModule */
22
25
  /** @typedef {import("../Dependency")} Dependency */
26
+ /** @typedef {import("../Dependency").ReferencedExports} ReferencedExports */
23
27
  /** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */
24
28
  /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
25
29
  /** @typedef {import("../DependencyTemplate").CssDependencyTemplateContext} DependencyTemplateContext */
@@ -28,6 +32,7 @@ const getCssParser = memoize(() => require("../css/CssParser"));
28
32
  /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
29
33
  /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
30
34
  /** @typedef {import("../util/Hash")} Hash */
35
+ /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
31
36
  /** @typedef {import("../ChunkGraph")} ChunkGraph */
32
37
  /** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
33
38
  /** @typedef {import("../css/CssParser").Range} Range */
@@ -43,7 +48,7 @@ const getLocalIdent = (local, module, chunkGraph, runtimeTemplate) => {
43
48
  const generator = /** @type {CssGenerator} */ (module.generator);
44
49
  const localIdentName =
45
50
  /** @type {CssGeneratorLocalIdentName} */
46
- (generator.localIdentName);
51
+ (generator.options.localIdentName);
47
52
  const relativeResourcePath = makePathsRelative(
48
53
  /** @type {string} */
49
54
  (module.context),
@@ -51,38 +56,92 @@ const getLocalIdent = (local, module, chunkGraph, runtimeTemplate) => {
51
56
  (module.getResource()),
52
57
  runtimeTemplate.compilation.compiler.root
53
58
  );
54
- const { hashFunction, hashDigest, hashDigestLength, hashSalt, uniqueName } =
55
- runtimeTemplate.outputOptions;
56
- const hash = createHash(hashFunction);
59
+ const { uniqueName } = runtimeTemplate.outputOptions;
57
60
 
58
- if (hashSalt) {
59
- hash.update(hashSalt);
60
- }
61
+ let localIdentHash = "";
62
+
63
+ if (/\[(fullhash|hash)\]/.test(localIdentName)) {
64
+ const hashSalt = generator.options.localIdentHashSalt;
65
+ const hashDigest =
66
+ /** @type {string} */
67
+ (generator.options.localIdentHashDigest);
68
+ const hashDigestLength = generator.options.localIdentHashDigestLength;
69
+ const { hashFunction } = runtimeTemplate.outputOptions;
70
+
71
+ const hash = createHash(hashFunction);
72
+
73
+ if (hashSalt) {
74
+ hash.update(hashSalt);
75
+ }
61
76
 
62
- hash.update(relativeResourcePath);
77
+ if (uniqueName) {
78
+ hash.update(uniqueName);
79
+ }
63
80
 
64
- if (!/\[local\]/.test(localIdentName)) {
81
+ hash.update(relativeResourcePath);
65
82
  hash.update(local);
83
+
84
+ localIdentHash = hash.digest(hashDigest).slice(0, hashDigestLength);
66
85
  }
67
86
 
68
- const localIdentHash = hash.digest(hashDigest).slice(0, hashDigestLength);
69
-
70
- return runtimeTemplate.compilation
71
- .getPath(localIdentName, {
72
- filename: relativeResourcePath,
73
- hash: localIdentHash,
74
- contentHash: localIdentHash,
75
- chunkGraph,
76
- module
77
- })
78
- .replace(/\[local\]/g, local)
79
- .replace(/\[uniqueName\]/g, /** @type {string} */ (uniqueName))
80
- .replace(/^((-?[0-9])|--)/, "_$1");
87
+ let contentHash = "";
88
+
89
+ if (/\[contenthash\]/.test(localIdentName)) {
90
+ const hash = createHash(runtimeTemplate.outputOptions.hashFunction);
91
+ const source = module.originalSource();
92
+
93
+ if (source) {
94
+ hash.update(source.buffer());
95
+ }
96
+
97
+ if (module.error) {
98
+ hash.update(module.error.toString());
99
+ }
100
+
101
+ const fullContentHash = hash.digest(
102
+ runtimeTemplate.outputOptions.hashDigest
103
+ );
104
+
105
+ contentHash = nonNumericOnlyHash(
106
+ fullContentHash,
107
+ runtimeTemplate.outputOptions.hashDigestLength
108
+ );
109
+ }
110
+
111
+ let localIdent = runtimeTemplate.compilation.getPath(localIdentName, {
112
+ prepareId: (id) => {
113
+ if (typeof id !== "string") return id;
114
+
115
+ return id
116
+ .replace(/^([.-]|[^a-zA-Z0-9_-])+/, "")
117
+ .replace(/[^a-zA-Z0-9_-]+/g, "_");
118
+ },
119
+ filename: relativeResourcePath,
120
+ hash: localIdentHash,
121
+ contentHash,
122
+ chunkGraph,
123
+ module
124
+ });
125
+
126
+ if (/\[local\]/.test(localIdentName)) {
127
+ localIdent = localIdent.replace(/\[local\]/g, local);
128
+ }
129
+
130
+ if (/\[uniqueName\]/.test(localIdentName)) {
131
+ localIdent = localIdent.replace(
132
+ /\[uniqueName\]/g,
133
+ /** @type {string} */ (uniqueName)
134
+ );
135
+ }
136
+
137
+ // Protect the first character from unsupported values
138
+ return localIdent.replace(/^((-?[0-9])|--)/, "_$1");
81
139
  };
82
- // 0 - replace, 1 - append, 2 - once
83
- /** @typedef {0 | 1 | 2} ExportMode */
84
- // 0 - none, 1 - name, 1 - value
85
- /** @typedef {0 | 1 | 2} InterpolationMode */
140
+
141
+ // 0 - replace, 1 - replace, 2 - append, 2 - once
142
+ /** @typedef {0 | 1 | 2 | 3 | 4} ExportMode */
143
+ // 0 - normal, 1 - custom css variable, 2 - grid custom ident
144
+ /** @typedef {0 | 1 | 2} ExportType */
86
145
 
87
146
  class CssIcssExportDependency extends NullDependency {
88
147
  /**
@@ -90,20 +149,27 @@ class CssIcssExportDependency extends NullDependency {
90
149
  *
91
150
  * :export { LOCAL_NAME: EXPORT_NAME }
92
151
  * @param {string} name export name
93
- * @param {string} value export value or true when we need interpolate name as a value
94
- * @param {string=} reexport reexport name
152
+ * @param {string | [string, string, boolean]} value export value or true when we need interpolate name as a value
95
153
  * @param {Range=} range range
154
+ * @param {boolean=} interpolate true when value need to be interpolated, otherwise false
155
+ * @param {ExportMode=} exportMode export mode
156
+ * @param {ExportType=} exportType export type
96
157
  */
97
- constructor(name, value, reexport, range) {
158
+ constructor(
159
+ name,
160
+ value,
161
+ range,
162
+ interpolate = false,
163
+ exportMode = CssIcssExportDependency.EXPORT_MODE.REPLACE,
164
+ exportType = CssIcssExportDependency.EXPORT_TYPE.NORMAL
165
+ ) {
98
166
  super();
99
167
  this.name = name;
100
168
  this.value = value;
101
- this.reexport = reexport;
102
169
  this.range = range;
103
- /** @type {undefined | InterpolationMode} */
104
- this.interpolationMode = undefined;
105
- /** @type {ExportMode} */
106
- this.exportMode = CssIcssExportDependency.EXPORT_MODE.REPLACE;
170
+ this.interpolate = interpolate;
171
+ this.exportMode = exportMode;
172
+ this.exportType = exportType;
107
173
  this._hashUpdate = undefined;
108
174
  }
109
175
 
@@ -124,21 +190,52 @@ class CssIcssExportDependency extends NullDependency {
124
190
  return this._conventionNames;
125
191
  }
126
192
 
193
+ /**
194
+ * Returns list of exports referenced by this dependency
195
+ * @param {ModuleGraph} moduleGraph module graph
196
+ * @param {RuntimeSpec} runtime the runtime for which the module is analysed
197
+ * @returns {ReferencedExports} referenced exports
198
+ */
199
+ getReferencedExports(moduleGraph, runtime) {
200
+ if (
201
+ this.exportMode === CssIcssExportDependency.EXPORT_MODE.SELF_REFERENCE
202
+ ) {
203
+ return [
204
+ {
205
+ name: [this.name],
206
+ canMangle: true
207
+ }
208
+ ];
209
+ }
210
+
211
+ return super.getReferencedExports(moduleGraph, runtime);
212
+ }
213
+
127
214
  /**
128
215
  * Returns the exported names
129
216
  * @param {ModuleGraph} moduleGraph module graph
130
217
  * @returns {ExportsSpec | undefined} export names
131
218
  */
132
219
  getExports(moduleGraph) {
220
+ if (
221
+ this.exportMode === CssIcssExportDependency.EXPORT_MODE.NONE ||
222
+ this.exportMode === CssIcssExportDependency.EXPORT_MODE.SELF_REFERENCE
223
+ ) {
224
+ return;
225
+ }
226
+
133
227
  const module = /** @type {CssModule} */ (moduleGraph.getParentModule(this));
134
228
  const generator = /** @type {CssGenerator} */ (module.generator);
135
229
  const names = this.getExportsConventionNames(
136
230
  this.name,
137
231
  /** @type {CssGeneratorExportsConvention} */
138
- (generator.convention)
232
+ (generator.options.exportsConvention)
139
233
  );
140
234
  return {
141
- exports: names.map((name) => ({
235
+ exports: [
236
+ ...names,
237
+ ...(Array.isArray(this.value) ? [this.value[1]] : [])
238
+ ].map((name) => ({
142
239
  name,
143
240
  canMangle: true
144
241
  })),
@@ -146,6 +243,34 @@ class CssIcssExportDependency extends NullDependency {
146
243
  };
147
244
  }
148
245
 
246
+ /**
247
+ * Returns warnings
248
+ * @param {ModuleGraph} moduleGraph module graph
249
+ * @returns {WebpackError[] | null | undefined} warnings
250
+ */
251
+ getWarnings(moduleGraph) {
252
+ if (
253
+ this.exportMode === CssIcssExportDependency.EXPORT_MODE.SELF_REFERENCE &&
254
+ !Array.isArray(this.value)
255
+ ) {
256
+ const module = moduleGraph.getParentModule(this);
257
+
258
+ if (
259
+ module &&
260
+ !moduleGraph.getExportsInfo(module).isExportProvided(this.value)
261
+ ) {
262
+ const error = new WebpackError(
263
+ `Self-referencing name "${this.value}" not found`
264
+ );
265
+ error.module = module;
266
+
267
+ return [error];
268
+ }
269
+ }
270
+
271
+ return null;
272
+ }
273
+
149
274
  /**
150
275
  * Update the hash
151
276
  * @param {Hash} hash hash to be updated
@@ -161,9 +286,9 @@ class CssIcssExportDependency extends NullDependency {
161
286
  const names = this.getExportsConventionNames(
162
287
  this.name,
163
288
  /** @type {CssGeneratorExportsConvention} */
164
- (generator.convention)
289
+ (generator.options.exportsConvention)
165
290
  );
166
- this._hashUpdate = `exportsConvention|${JSON.stringify(names)}|localIdentName|${JSON.stringify(generator.localIdentName)}`;
291
+ this._hashUpdate = `exportsConvention|${JSON.stringify(names)}|localIdentName|${JSON.stringify(generator.options.localIdentName)}`;
167
292
  }
168
293
  hash.update(this._hashUpdate);
169
294
  }
@@ -175,10 +300,10 @@ class CssIcssExportDependency extends NullDependency {
175
300
  const { write } = context;
176
301
  write(this.name);
177
302
  write(this.value);
178
- write(this.reexport);
179
303
  write(this.range);
180
- write(this.interpolationMode);
304
+ write(this.interpolate);
181
305
  write(this.exportMode);
306
+ write(this.exportType);
182
307
  super.serialize(context);
183
308
  }
184
309
 
@@ -189,10 +314,10 @@ class CssIcssExportDependency extends NullDependency {
189
314
  const { read } = context;
190
315
  this.name = read();
191
316
  this.value = read();
192
- this.reexport = read();
193
317
  this.range = read();
194
- this.interpolationMode = read();
318
+ this.interpolate = read();
195
319
  this.exportMode = read();
320
+ this.exportType = read();
196
321
  super.deserialize(context);
197
322
  }
198
323
  }
@@ -222,17 +347,21 @@ CssIcssExportDependency.Template = class CssIcssExportDependencyTemplate extends
222
347
  nestedDep instanceof CssIcssExportDependency &&
223
348
  symbol === nestedDep.name
224
349
  ) {
225
- if (nestedDep.reexport) {
226
- return this.findReference(nestedDep.reexport, {
350
+ if (Array.isArray(nestedDep.value)) {
351
+ return this.findReference(nestedDep.value[1], {
227
352
  ...templateContext,
228
353
  module
229
354
  });
230
355
  }
231
356
 
232
- return CssIcssExportDependency.Template.getIdentifier(nestedDep, {
233
- ...templateContext,
234
- module
235
- });
357
+ return CssIcssExportDependency.Template.getIdentifier(
358
+ nestedDep.value,
359
+ nestedDep,
360
+ {
361
+ ...templateContext,
362
+ module
363
+ }
364
+ );
236
365
  }
237
366
  }
238
367
  }
@@ -240,45 +369,30 @@ CssIcssExportDependency.Template = class CssIcssExportDependencyTemplate extends
240
369
  }
241
370
 
242
371
  /**
372
+ * @param {string} value value to identifier
243
373
  * @param {Dependency} dependency the dependency for which the template should be applied
244
374
  * @param {DependencyTemplateContext} templateContext the context object
245
375
  * @returns {string} identifier
246
376
  */
247
- static getIdentifier(dependency, templateContext) {
377
+ static getIdentifier(value, dependency, templateContext) {
248
378
  const dep = /** @type {CssIcssExportDependency} */ (dependency);
249
379
 
250
- if (
251
- dep.interpolationMode ===
252
- CssIcssExportDependency.INTERPOLATION_MODE.NAME ||
253
- dep.interpolationMode === CssIcssExportDependency.INTERPOLATION_MODE.VALUE
254
- ) {
255
- const { module: m, moduleGraph, runtime } = templateContext;
380
+ if (dep.interpolate) {
381
+ const { module: m } = templateContext;
256
382
  const module = /** @type {CssModule} */ (m);
257
383
  const generator = /** @type {CssGenerator} */ (module.generator);
258
- const names = dep.getExportsConventionNames(
259
- dep.interpolationMode ===
260
- CssIcssExportDependency.INTERPOLATION_MODE.NAME
261
- ? dep.name
262
- : dep.value,
384
+ const local = cssExportConvention(
385
+ value,
263
386
  /** @type {CssGeneratorExportsConvention} */
264
- (generator.convention)
265
- );
266
- const usedNames =
267
- /** @type {string[]} */
268
- (
269
- names
270
- .map((name) =>
271
- moduleGraph.getExportInfo(module, name).getUsedName(name, runtime)
272
- )
273
- .filter(Boolean)
274
- );
275
- const local = usedNames.length === 0 ? names[0] : usedNames[0];
387
+ (generator.options.exportsConvention)
388
+ )[0];
276
389
  const prefix =
277
- /** @type {CssIcssExportDependency & { prefix: string }} */
278
- (dependency).prefix;
390
+ dep.exportType === CssIcssExportDependency.EXPORT_TYPE.CUSTOM_VARIABLE
391
+ ? "--"
392
+ : "";
279
393
 
280
394
  return (
281
- (prefix || "") +
395
+ prefix +
282
396
  getCssParser().escapeIdentifier(
283
397
  getLocalIdent(
284
398
  local,
@@ -302,7 +416,7 @@ CssIcssExportDependency.Template = class CssIcssExportDependencyTemplate extends
302
416
  */
303
417
  apply(dependency, source, templateContext) {
304
418
  const dep = /** @type {CssIcssExportDependency} */ (dependency);
305
- if (!dep.range && templateContext.type !== "javascript") return;
419
+ if (!dep.range && templateContext.type !== JAVASCRIPT_TYPE) return;
306
420
  const { cssData } = templateContext;
307
421
  const { module: m, moduleGraph, runtime } = templateContext;
308
422
  const module = /** @type {CssModule} */ (m);
@@ -310,7 +424,7 @@ CssIcssExportDependency.Template = class CssIcssExportDependencyTemplate extends
310
424
  const names = dep.getExportsConventionNames(
311
425
  dep.name,
312
426
  /** @type {CssGeneratorExportsConvention} */
313
- (generator.convention)
427
+ (generator.options.exportsConvention)
314
428
  );
315
429
  const usedNames =
316
430
  /** @type {string[]} */
@@ -323,42 +437,63 @@ CssIcssExportDependency.Template = class CssIcssExportDependencyTemplate extends
323
437
  );
324
438
 
325
439
  const allNames = new Set([...usedNames, ...names]);
440
+ const isReference = Array.isArray(dep.value);
326
441
 
327
442
  /** @type {string} */
328
443
  let value;
329
444
 
330
- if (dep.reexport) {
445
+ if (isReference && dep.value[2] === true) {
331
446
  const resolved = CssIcssExportDependencyTemplate.findReference(
332
- dep.reexport,
447
+ dep.value[1],
333
448
  templateContext
334
449
  );
335
450
 
336
- if (resolved) {
337
- dep.value = resolved;
338
- }
451
+ // Fallback to the original name if not found
452
+ value = resolved || dep.value[0];
453
+ } else {
454
+ value = isReference ? dep.value[1] : /** @type {string} */ (dep.value);
339
455
  }
340
456
 
341
- if (typeof dep.interpolationMode !== "undefined") {
457
+ if (dep.interpolate) {
342
458
  value = CssIcssExportDependencyTemplate.getIdentifier(
459
+ value,
343
460
  dep,
344
461
  templateContext
345
462
  );
346
- } else {
347
- value = dep.value;
348
463
  }
349
464
 
350
- if (templateContext.type === "javascript") {
465
+ if (
466
+ dep.exportType ===
467
+ CssIcssExportDependency.EXPORT_TYPE.GRID_CUSTOM_IDENTIFIER
468
+ ) {
469
+ value += `-${dep.name}`;
470
+ }
471
+
472
+ if (
473
+ templateContext.type === JAVASCRIPT_TYPE &&
474
+ dep.exportMode !== CssIcssExportDependency.EXPORT_MODE.NONE
475
+ ) {
351
476
  for (const used of allNames) {
352
- if (dep.exportMode === 2) {
477
+ if (dep.exportMode === CssIcssExportDependency.EXPORT_MODE.ONCE) {
478
+ const newValue = getCssParser().unescapeIdentifier(value);
479
+ if (isReference) {
480
+ cssData.exports.set(dep.value[1], newValue);
481
+ }
353
482
  if (cssData.exports.has(used)) return;
354
- cssData.exports.set(
355
- used,
356
- `${getCssParser().unescapeIdentifier(value)}`
357
- );
483
+ cssData.exports.set(used, newValue);
358
484
  } else {
359
485
  const originalValue =
360
- dep.exportMode === 0 ? undefined : cssData.exports.get(used);
361
- const newValue = getCssParser().unescapeIdentifier(value);
486
+ dep.exportMode === CssIcssExportDependency.EXPORT_MODE.REPLACE
487
+ ? undefined
488
+ : cssData.exports.get(used);
489
+
490
+ const newValue =
491
+ dep.exportMode ===
492
+ CssIcssExportDependency.EXPORT_MODE.SELF_REFERENCE
493
+ ? cssData.exports.get(
494
+ isReference ? dep.value[0] : /** @type {string} */ (dep.value)
495
+ ) || value
496
+ : getCssParser().unescapeIdentifier(value);
362
497
 
363
498
  cssData.exports.set(
364
499
  used,
@@ -368,26 +503,29 @@ CssIcssExportDependency.Template = class CssIcssExportDependencyTemplate extends
368
503
  }
369
504
  } else if (
370
505
  dep.range &&
371
- templateContext.type === "css" &&
372
- dep.exportMode !== 1
506
+ templateContext.type === CSS_TYPE &&
507
+ dep.exportMode !== CssIcssExportDependency.EXPORT_MODE.APPEND &&
508
+ dep.exportMode !== CssIcssExportDependency.EXPORT_MODE.SELF_REFERENCE
373
509
  ) {
374
510
  source.replace(dep.range[0], dep.range[1] - 1, value);
375
511
  }
376
512
  }
377
513
  };
378
514
 
379
- /** @type {Record<"REPLACE" | "APPEND" | "ONCE", ExportMode>} */
515
+ /** @type {Record<"NONE" | "REPLACE" | "APPEND" | "ONCE" | "SELF_REFERENCE", ExportMode>} */
380
516
  CssIcssExportDependency.EXPORT_MODE = {
381
- REPLACE: 0,
382
- APPEND: 1,
383
- ONCE: 2
517
+ NONE: 0,
518
+ REPLACE: 1,
519
+ APPEND: 2,
520
+ ONCE: 3,
521
+ SELF_REFERENCE: 4
384
522
  };
385
523
 
386
- /** @type {Record<"NONE" | "NAME" | "VALUE", InterpolationMode>} */
387
- CssIcssExportDependency.INTERPOLATION_MODE = {
388
- NONE: 0,
389
- NAME: 1,
390
- VALUE: 2
524
+ /** @type {Record<"NORMAL" | "CUSTOM_VARIABLE" | "GRID_CUSTOM_IDENTIFIER", ExportType>} */
525
+ CssIcssExportDependency.EXPORT_TYPE = {
526
+ NORMAL: 0,
527
+ CUSTOM_VARIABLE: 1,
528
+ GRID_CUSTOM_IDENTIFIER: 2
391
529
  };
392
530
 
393
531
  makeSerializable(
@@ -6,13 +6,16 @@
6
6
  "use strict";
7
7
 
8
8
  const WebpackError = require("../WebpackError");
9
+ const { cssExportConvention } = require("../util/conventions");
9
10
  const makeSerializable = require("../util/makeSerializable");
11
+ const memoize = require("../util/memoize");
10
12
  const CssImportDependency = require("./CssImportDependency");
11
13
 
12
14
  /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
13
15
  /** @typedef {import("../Dependency")} Dependency */
14
16
  /** @typedef {import("../DependencyTemplate").CssDependencyTemplateContext} DependencyTemplateContext */
15
17
  /** @typedef {import("../Module")} Module */
18
+ /** @typedef {import("../CssModule")} CssModule */
16
19
  /** @typedef {import("../ModuleGraph")} ModuleGraph */
17
20
  /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
18
21
  /** @typedef {import("../javascript/JavascriptParser").Range} Range */
@@ -20,20 +23,41 @@ const CssImportDependency = require("./CssImportDependency");
20
23
  /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
21
24
  /** @typedef {import("../Dependency").ReferencedExports} ReferencedExports */
22
25
  /** @typedef {import("../../declarations/WebpackOptions").CssGeneratorExportsConvention} CssGeneratorExportsConvention */
26
+ /** @typedef {import("./CssIcssExportDependency").ExportMode} ExportMode */
27
+ /** @typedef {import("./CssIcssExportDependency").ExportType} ExportType */
28
+
29
+ const getCssIcssExportDependency = memoize(() =>
30
+ require("./CssIcssExportDependency")
31
+ );
23
32
 
24
33
  class CssIcssImportDependency extends CssImportDependency {
25
34
  /**
26
35
  * Example of dependency:
27
36
  *
28
- * :import('./style.css') { IMPORTED_NAME: v-primary }
37
+ * :import('./style.css') { value: name }
29
38
  * @param {string} request request request path which needs resolving
30
39
  * @param {Range} range the range of dependency
31
40
  * @param {"local" | "global"} mode mode of the parsed CSS
32
- * @param {string} name importName name
41
+ * @param {string} name name
42
+ * @param {string=} exportName export value
43
+ * @param {ExportMode=} exportMode export mode
44
+ * @param {ExportType=} exportType export type
33
45
  */
34
- constructor(request, range, mode, name) {
46
+ constructor(
47
+ request,
48
+ range,
49
+ mode,
50
+ name,
51
+ exportName = undefined,
52
+ exportMode = getCssIcssExportDependency().EXPORT_MODE.NONE,
53
+ exportType = getCssIcssExportDependency().EXPORT_TYPE.NORMAL
54
+ ) {
35
55
  super(request, range, mode);
36
56
  this.name = name;
57
+ this.value = exportName;
58
+ this.interpolate = true;
59
+ this.exportMode = exportMode;
60
+ this.exportType = exportType;
37
61
  }
38
62
 
39
63
  get type() {
@@ -47,6 +71,15 @@ class CssIcssImportDependency extends CssImportDependency {
47
71
  return `${super.getResourceIdentifier()}|mode${this.mode}|name${this.name}`;
48
72
  }
49
73
 
74
+ /**
75
+ * @param {string} name export name
76
+ * @param {CssGeneratorExportsConvention} convention convention of the export name
77
+ * @returns {string[]} convention results
78
+ */
79
+ getExportsConventionNames(name, convention) {
80
+ return cssExportConvention(name, convention);
81
+ }
82
+
50
83
  /**
51
84
  * Returns list of exports referenced by this dependency
52
85
  * @param {ModuleGraph} moduleGraph module graph
@@ -91,6 +124,10 @@ class CssIcssImportDependency extends CssImportDependency {
91
124
  serialize(context) {
92
125
  const { write } = context;
93
126
  write(this.name);
127
+ write(this.value);
128
+ write(this.interpolate);
129
+ write(this.exportMode);
130
+ write(this.exportType);
94
131
  super.serialize(context);
95
132
  }
96
133
 
@@ -100,6 +137,10 @@ class CssIcssImportDependency extends CssImportDependency {
100
137
  deserialize(context) {
101
138
  const { read } = context;
102
139
  this.name = read();
140
+ this.value = read();
141
+ this.interpolate = read();
142
+ this.exportMode = read();
143
+ this.exportType = read();
103
144
  super.deserialize(context);
104
145
  }
105
146
  }
@@ -114,7 +155,23 @@ CssIcssImportDependency.Template = class CssIcssImportDependencyTemplate extends
114
155
  * @returns {void}
115
156
  */
116
157
  apply(dependency, source, templateContext) {
117
- // We remove everything in CSS parser
158
+ const dep = /** @type {CssIcssImportDependency} */ (dependency);
159
+
160
+ if (dep.value) {
161
+ const { moduleGraph } = templateContext;
162
+ const module =
163
+ /** @type {CssModule} */
164
+ (moduleGraph.getModule(dep));
165
+ const CssIcssExportDependency = getCssIcssExportDependency();
166
+ const template = new CssIcssExportDependency.Template();
167
+ const originalName = dep.name;
168
+ const originalExportName = dep.value;
169
+ dep.value = originalName;
170
+ dep.name = originalExportName;
171
+ template.apply(dep, source, { ...templateContext, module });
172
+ dep.name = originalName;
173
+ dep.value = originalExportName;
174
+ }
118
175
  }
119
176
  };
120
177