webpack 5.96.1 → 5.97.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 (67) hide show
  1. package/lib/CssModule.js +5 -0
  2. package/lib/DefinePlugin.js +7 -1
  3. package/lib/DependencyTemplate.js +2 -2
  4. package/lib/EvalSourceMapDevToolPlugin.js +5 -0
  5. package/lib/FalseIIFEUmdWarning.js +19 -0
  6. package/lib/HotModuleReplacementPlugin.js +4 -0
  7. package/lib/Module.js +6 -0
  8. package/lib/ModuleSourceTypesConstants.js +12 -0
  9. package/lib/NormalModule.js +1 -0
  10. package/lib/RuntimeTemplate.js +7 -0
  11. package/lib/SourceMapDevToolPlugin.js +8 -0
  12. package/lib/WebpackOptionsApply.js +3 -1
  13. package/lib/asset/AssetModulesPlugin.js +7 -2
  14. package/lib/config/defaults.js +52 -36
  15. package/lib/config/normalization.js +0 -1
  16. package/lib/config/target.js +8 -8
  17. package/lib/css/CssGenerator.js +139 -35
  18. package/lib/css/CssLoadingRuntimeModule.js +108 -198
  19. package/lib/css/CssModulesPlugin.js +78 -124
  20. package/lib/css/CssParser.js +545 -121
  21. package/lib/css/walkCssTokens.js +41 -19
  22. package/lib/dependencies/CachedConstDependency.js +2 -1
  23. package/lib/dependencies/ContextDependencyTemplateAsId.js +3 -2
  24. package/lib/dependencies/{CssExportDependency.js → CssIcssExportDependency.js} +35 -35
  25. package/lib/dependencies/CssIcssImportDependency.js +118 -0
  26. package/lib/dependencies/CssIcssSymbolDependency.js +132 -0
  27. package/lib/dependencies/CssImportDependency.js +0 -8
  28. package/lib/dependencies/CssLocalIdentifierDependency.js +69 -73
  29. package/lib/dependencies/CssUrlDependency.js +1 -0
  30. package/lib/esm/ModuleChunkFormatPlugin.js +8 -4
  31. package/lib/esm/ModuleChunkLoadingRuntimeModule.js +17 -10
  32. package/lib/hmr/HotModuleReplacementRuntimeModule.js +0 -1
  33. package/lib/index.js +9 -3
  34. package/lib/javascript/EnableChunkLoadingPlugin.js +2 -4
  35. package/lib/javascript/JavascriptParser.js +1 -0
  36. package/lib/library/AssignLibraryPlugin.js +1 -1
  37. package/lib/library/EnableLibraryPlugin.js +17 -0
  38. package/lib/node/ReadFileCompileAsyncWasmPlugin.js +81 -78
  39. package/lib/node/ReadFileCompileWasmPlugin.js +76 -57
  40. package/lib/optimize/MergeDuplicateChunksPlugin.js +22 -2
  41. package/lib/sharing/ConsumeSharedPlugin.js +35 -12
  42. package/lib/sharing/utils.js +35 -4
  43. package/lib/stats/DefaultStatsFactoryPlugin.js +0 -5
  44. package/lib/util/Queue.js +1 -6
  45. package/lib/util/generateDebugId.js +33 -0
  46. package/lib/util/internalSerializables.js +6 -2
  47. package/lib/wasm/EnableWasmLoadingPlugin.js +36 -25
  48. package/lib/wasm-async/AsyncWasmLoadingRuntimeModule.js +26 -2
  49. package/lib/wasm-async/UniversalCompileAsyncWasmPlugin.js +103 -0
  50. package/lib/wasm-sync/WebAssemblyParser.js +1 -1
  51. package/lib/web/FetchCompileAsyncWasmPlugin.js +43 -44
  52. package/lib/web/FetchCompileWasmPlugin.js +4 -4
  53. package/package.json +4 -4
  54. package/schemas/WebpackOptions.check.js +1 -1
  55. package/schemas/WebpackOptions.json +34 -12
  56. package/schemas/plugins/BannerPlugin.json +1 -1
  57. package/schemas/plugins/SourceMapDevToolPlugin.check.js +1 -1
  58. package/schemas/plugins/SourceMapDevToolPlugin.json +4 -0
  59. package/schemas/plugins/css/CssAutoParserOptions.check.js +1 -1
  60. package/schemas/plugins/css/CssGlobalParserOptions.check.js +1 -1
  61. package/schemas/plugins/css/CssModuleParserOptions.check.js +1 -1
  62. package/schemas/plugins/css/CssParserOptions.check.js +1 -1
  63. package/schemas/plugins/optimize/MergeDuplicateChunksPlugin.check.d.ts +7 -0
  64. package/schemas/plugins/optimize/MergeDuplicateChunksPlugin.check.js +6 -0
  65. package/schemas/plugins/optimize/MergeDuplicateChunksPlugin.json +11 -0
  66. package/types.d.ts +94 -27
  67. package/lib/css/CssExportsGenerator.js +0 -207
@@ -10,7 +10,8 @@ const {
10
10
  ConcatSource,
11
11
  PrefixSource,
12
12
  ReplaceSource,
13
- CachedSource
13
+ CachedSource,
14
+ RawSource
14
15
  } = require("webpack-sources");
15
16
  const Compilation = require("../Compilation");
16
17
  const CssModule = require("../CssModule");
@@ -24,20 +25,23 @@ const {
24
25
  } = require("../ModuleTypeConstants");
25
26
  const RuntimeGlobals = require("../RuntimeGlobals");
26
27
  const SelfModuleFactory = require("../SelfModuleFactory");
28
+ const Template = require("../Template");
27
29
  const WebpackError = require("../WebpackError");
28
- const CssExportDependency = require("../dependencies/CssExportDependency");
30
+ const CssIcssExportDependency = require("../dependencies/CssIcssExportDependency");
31
+ const CssIcssImportDependency = require("../dependencies/CssIcssImportDependency");
32
+ const CssIcssSymbolDependency = require("../dependencies/CssIcssSymbolDependency");
29
33
  const CssImportDependency = require("../dependencies/CssImportDependency");
30
34
  const CssLocalIdentifierDependency = require("../dependencies/CssLocalIdentifierDependency");
31
35
  const CssSelfLocalIdentifierDependency = require("../dependencies/CssSelfLocalIdentifierDependency");
32
36
  const CssUrlDependency = require("../dependencies/CssUrlDependency");
33
37
  const StaticExportsDependency = require("../dependencies/StaticExportsDependency");
38
+ const JavascriptModulesPlugin = require("../javascript/JavascriptModulesPlugin");
34
39
  const { compareModulesByIdentifier } = require("../util/comparators");
35
40
  const createSchemaValidation = require("../util/create-schema-validation");
36
41
  const createHash = require("../util/createHash");
37
42
  const { getUndoPath } = require("../util/identifier");
38
43
  const memoize = require("../util/memoize");
39
44
  const nonNumericOnlyHash = require("../util/nonNumericOnlyHash");
40
- const CssExportsGenerator = require("./CssExportsGenerator");
41
45
  const CssGenerator = require("./CssGenerator");
42
46
  const CssParser = require("./CssParser");
43
47
 
@@ -49,7 +53,6 @@ const CssParser = require("./CssParser");
49
53
  /** @typedef {import("../Compilation").ChunkHashContext} ChunkHashContext */
50
54
  /** @typedef {import("../Compiler")} Compiler */
51
55
  /** @typedef {import("../CssModule").Inheritance} Inheritance */
52
- /** @typedef {import("../DependencyTemplate").CssExportsData} CssExportsData */
53
56
  /** @typedef {import("../Module")} Module */
54
57
  /** @typedef {import("../Template").RuntimeTemplate} RuntimeTemplate */
55
58
  /** @typedef {import("../TemplatedPathPlugin").TemplatePath} TemplatePath */
@@ -64,7 +67,6 @@ const CssParser = require("./CssParser");
64
67
  * @property {CodeGenerationResults} codeGenerationResults results of code generation
65
68
  * @property {RuntimeTemplate} runtimeTemplate the runtime template
66
69
  * @property {string} uniqueName the unique name
67
- * @property {boolean} cssHeadDataCompression need compress
68
70
  * @property {string} undoPath undo path to css file
69
71
  * @property {CssModule[]} modules modules
70
72
  */
@@ -75,7 +77,6 @@ const CssParser = require("./CssParser");
75
77
  * @property {ChunkGraph} chunkGraph the chunk graph
76
78
  * @property {CodeGenerationResults} codeGenerationResults results of code generation
77
79
  * @property {RuntimeTemplate} runtimeTemplate the runtime template
78
- * @property {string[]} metaData meta data for runtime
79
80
  * @property {string} undoPath undo path to css file
80
81
  */
81
82
 
@@ -158,51 +159,6 @@ const validateParserOptions = {
158
159
  /** @type {WeakMap<Compilation, CompilationHooks>} */
159
160
  const compilationHooksMap = new WeakMap();
160
161
 
161
- /**
162
- * @param {string} str string
163
- * @param {boolean=} omitOptionalUnderscore if true, optional underscore is not added
164
- * @returns {string} escaped string
165
- */
166
- const escapeCss = (str, omitOptionalUnderscore) => {
167
- const escaped = `${str}`.replace(
168
- // cspell:word uffff
169
- /[^a-zA-Z0-9_\u0081-\uFFFF-]/g,
170
- s => `\\${s}`
171
- );
172
- return !omitOptionalUnderscore && /^(?!--)[0-9_-]/.test(escaped)
173
- ? `_${escaped}`
174
- : escaped;
175
- };
176
-
177
- /**
178
- * @param {string} str string
179
- * @returns {string} encoded string
180
- */
181
- const lzwEncode = str => {
182
- /** @type {Map<string, string>} */
183
- const map = new Map();
184
- let encoded = "";
185
- let phrase = str[0];
186
- let code = 256;
187
- const maxCode = "\uFFFF".charCodeAt(0);
188
- for (let i = 1; i < str.length; i++) {
189
- const c = str[i];
190
- if (map.has(phrase + c)) {
191
- phrase += c;
192
- } else {
193
- encoded += phrase.length > 1 ? map.get(phrase) : phrase;
194
- map.set(phrase + c, String.fromCharCode(code));
195
- phrase = c;
196
- if (++code > maxCode) {
197
- code = 256;
198
- map.clear();
199
- }
200
- }
201
- }
202
- encoded += phrase.length > 1 ? map.get(phrase) : phrase;
203
- return encoded;
204
- };
205
-
206
162
  const PLUGIN_NAME = "CssModulesPlugin";
207
163
 
208
164
  class CssModulesPlugin {
@@ -233,7 +189,7 @@ class CssModulesPlugin {
233
189
 
234
190
  constructor() {
235
191
  /** @type {WeakMap<Source, { undoPath: string, inheritance: Inheritance, source: CachedSource }>} */
236
- this._moduleCache = new WeakMap();
192
+ this._moduleFactoryCache = new WeakMap();
237
193
  }
238
194
 
239
195
  /**
@@ -247,6 +203,14 @@ class CssModulesPlugin {
247
203
  (compilation, { normalModuleFactory }) => {
248
204
  const hooks = CssModulesPlugin.getCompilationHooks(compilation);
249
205
  const selfFactory = new SelfModuleFactory(compilation.moduleGraph);
206
+ compilation.dependencyFactories.set(
207
+ CssImportDependency,
208
+ normalModuleFactory
209
+ );
210
+ compilation.dependencyTemplates.set(
211
+ CssImportDependency,
212
+ new CssImportDependency.Template()
213
+ );
250
214
  compilation.dependencyFactories.set(
251
215
  CssUrlDependency,
252
216
  normalModuleFactory
@@ -267,17 +231,21 @@ class CssModulesPlugin {
267
231
  CssSelfLocalIdentifierDependency,
268
232
  new CssSelfLocalIdentifierDependency.Template()
269
233
  );
270
- compilation.dependencyTemplates.set(
271
- CssExportDependency,
272
- new CssExportDependency.Template()
273
- );
274
234
  compilation.dependencyFactories.set(
275
- CssImportDependency,
235
+ CssIcssImportDependency,
276
236
  normalModuleFactory
277
237
  );
278
238
  compilation.dependencyTemplates.set(
279
- CssImportDependency,
280
- new CssImportDependency.Template()
239
+ CssIcssImportDependency,
240
+ new CssIcssImportDependency.Template()
241
+ );
242
+ compilation.dependencyTemplates.set(
243
+ CssIcssExportDependency,
244
+ new CssIcssExportDependency.Template()
245
+ );
246
+ compilation.dependencyTemplates.set(
247
+ CssIcssSymbolDependency,
248
+ new CssIcssSymbolDependency.Template()
281
249
  );
282
250
  compilation.dependencyTemplates.set(
283
251
  StaticExportsDependency,
@@ -293,26 +261,34 @@ class CssModulesPlugin {
293
261
  .for(type)
294
262
  .tap(PLUGIN_NAME, parserOptions => {
295
263
  validateParserOptions[type](parserOptions);
296
- const { namedExports } = parserOptions;
264
+ const { url, import: importOption, namedExports } = parserOptions;
297
265
 
298
266
  switch (type) {
299
267
  case CSS_MODULE_TYPE:
300
268
  return new CssParser({
269
+ importOption,
270
+ url,
301
271
  namedExports
302
272
  });
303
273
  case CSS_MODULE_TYPE_GLOBAL:
304
274
  return new CssParser({
305
275
  defaultMode: "global",
276
+ importOption,
277
+ url,
306
278
  namedExports
307
279
  });
308
280
  case CSS_MODULE_TYPE_MODULE:
309
281
  return new CssParser({
310
282
  defaultMode: "local",
283
+ importOption,
284
+ url,
311
285
  namedExports
312
286
  });
313
287
  case CSS_MODULE_TYPE_AUTO:
314
288
  return new CssParser({
315
289
  defaultMode: "auto",
290
+ importOption,
291
+ url,
316
292
  namedExports
317
293
  });
318
294
  }
@@ -322,17 +298,7 @@ class CssModulesPlugin {
322
298
  .tap(PLUGIN_NAME, generatorOptions => {
323
299
  validateGeneratorOptions[type](generatorOptions);
324
300
 
325
- return generatorOptions.exportsOnly
326
- ? new CssExportsGenerator(
327
- generatorOptions.exportsConvention,
328
- generatorOptions.localIdentName,
329
- generatorOptions.esModule
330
- )
331
- : new CssGenerator(
332
- generatorOptions.exportsConvention,
333
- generatorOptions.localIdentName,
334
- generatorOptions.esModule
335
- );
301
+ return new CssGenerator(generatorOptions);
336
302
  });
337
303
  normalModuleFactory.hooks.createModuleClass
338
304
  .for(type)
@@ -395,8 +361,38 @@ class CssModulesPlugin {
395
361
  return new CssModule(createData);
396
362
  });
397
363
  }
364
+
365
+ JavascriptModulesPlugin.getCompilationHooks(
366
+ compilation
367
+ ).renderModuleContent.tap(PLUGIN_NAME, (source, module) => {
368
+ if (module instanceof CssModule && module.hot) {
369
+ const exports = module.buildInfo.cssData.exports;
370
+ const stringifiedExports = JSON.stringify(
371
+ JSON.stringify(
372
+ Array.from(exports).reduce((obj, [key, value]) => {
373
+ obj[key] = value;
374
+ return obj;
375
+ }, {})
376
+ )
377
+ );
378
+
379
+ const hmrCode = Template.asString([
380
+ "",
381
+ `var __webpack_css_exports__ = ${stringifiedExports};`,
382
+ "// only invalidate when locals change",
383
+ "if (module.hot.data && module.hot.data.__webpack_css_exports__ && module.hot.data.__webpack_css_exports__ != __webpack_css_exports__) {",
384
+ Template.indent("module.hot.invalidate();"),
385
+ "} else {",
386
+ Template.indent("module.hot.accept();"),
387
+ "}",
388
+ "module.hot.dispose(function(data) { data.__webpack_css_exports__ = __webpack_css_exports__; });"
389
+ ]);
390
+
391
+ return new ConcatSource(source, "\n", new RawSource(hmrCode));
392
+ }
393
+ });
398
394
  const orderedCssModulesPerChunk = new WeakMap();
399
- compilation.hooks.afterCodeGeneration.tap("CssModulesPlugin", () => {
395
+ compilation.hooks.afterCodeGeneration.tap(PLUGIN_NAME, () => {
400
396
  const { chunkGraph } = compilation;
401
397
  for (const chunk of compilation.chunks) {
402
398
  if (CssModulesPlugin.chunkHasCss(chunk, chunkGraph)) {
@@ -407,13 +403,10 @@ class CssModulesPlugin {
407
403
  }
408
404
  }
409
405
  });
410
- compilation.hooks.chunkHash.tap(
411
- "CssModulesPlugin",
412
- (chunk, hash, context) => {
413
- hooks.chunkHash.call(chunk, hash, context);
414
- }
415
- );
416
- compilation.hooks.contentHash.tap("CssModulesPlugin", chunk => {
406
+ compilation.hooks.chunkHash.tap(PLUGIN_NAME, (chunk, hash, context) => {
407
+ hooks.chunkHash.call(chunk, hash, context);
408
+ });
409
+ compilation.hooks.contentHash.tap(PLUGIN_NAME, chunk => {
417
410
  const {
418
411
  chunkGraph,
419
412
  codeGenerationResults,
@@ -483,8 +476,6 @@ class CssModulesPlugin {
483
476
  chunkGraph,
484
477
  codeGenerationResults,
485
478
  uniqueName: compilation.outputOptions.uniqueName,
486
- cssHeadDataCompression:
487
- compilation.outputOptions.cssHeadDataCompression,
488
479
  undoPath,
489
480
  modules,
490
481
  runtimeTemplate
@@ -522,7 +513,6 @@ class CssModulesPlugin {
522
513
  onceForChunkSet.add(chunk);
523
514
  if (!isEnabledForChunk(chunk)) return;
524
515
 
525
- set.add(RuntimeGlobals.moduleFactoriesAddOnly);
526
516
  set.add(RuntimeGlobals.makeNamespaceObject);
527
517
 
528
518
  const CssLoadingRuntimeModule = getCssLoadingRuntimeModule();
@@ -570,7 +560,6 @@ class CssModulesPlugin {
570
560
  }
571
561
  set.add(RuntimeGlobals.publicPath);
572
562
  set.add(RuntimeGlobals.getChunkCssFilename);
573
- set.add(RuntimeGlobals.moduleFactoriesAddOnly);
574
563
  });
575
564
  }
576
565
  );
@@ -728,8 +717,7 @@ class CssModulesPlugin {
728
717
  * @returns {Source} css module source
729
718
  */
730
719
  renderModule(module, renderContext, hooks) {
731
- const { codeGenerationResults, chunk, undoPath, chunkGraph, metaData } =
732
- renderContext;
720
+ const { codeGenerationResults, chunk, undoPath } = renderContext;
733
721
  const codeGenResult = codeGenerationResults.get(module, chunk.runtime);
734
722
  const moduleSourceContent =
735
723
  /** @type {Source} */
@@ -737,8 +725,7 @@ class CssModulesPlugin {
737
725
  codeGenResult.sources.get("css") ||
738
726
  codeGenResult.sources.get("css-import")
739
727
  );
740
-
741
- const cacheEntry = this._moduleCache.get(moduleSourceContent);
728
+ const cacheEntry = this._moduleFactoryCache.get(moduleSourceContent);
742
729
 
743
730
  /** @type {Inheritance} */
744
731
  const inheritance = [[module.cssLayer, module.supports, module.media]];
@@ -760,9 +747,9 @@ class CssModulesPlugin {
760
747
  ) {
761
748
  source = cacheEntry.source;
762
749
  } else {
763
- const moduleSourceCode = /** @type {string} */ (
764
- moduleSourceContent.source()
765
- );
750
+ const moduleSourceCode =
751
+ /** @type {string} */
752
+ (moduleSourceContent.source());
766
753
  const publicPathAutoRegex = new RegExp(
767
754
  CssUrlDependency.PUBLIC_PATH_AUTO,
768
755
  "g"
@@ -814,34 +801,13 @@ class CssModulesPlugin {
814
801
  }
815
802
 
816
803
  source = new CachedSource(moduleSource);
817
- this._moduleCache.set(moduleSourceContent, {
804
+ this._moduleFactoryCache.set(moduleSourceContent, {
818
805
  inheritance,
819
806
  undoPath,
820
807
  source
821
808
  });
822
809
  }
823
- /** @type {CssExportsData | undefined} */
824
- const cssExportsData =
825
- codeGenResult.data && codeGenResult.data.get("css-exports");
826
- const exports = cssExportsData && cssExportsData.exports;
827
- const esModule = cssExportsData && cssExportsData.esModule;
828
- let moduleId = String(chunkGraph.getModuleId(module));
829
-
830
- // When `optimization.moduleIds` is `named` the module id is a path, so we need to normalize it between platforms
831
- if (typeof moduleId === "string") {
832
- moduleId = moduleId.replace(/\\/g, "/");
833
- }
834
810
 
835
- metaData.push(
836
- `${
837
- exports
838
- ? Array.from(
839
- exports,
840
- ([n, v]) => `${escapeCss(n)}:${escapeCss(v)}/`
841
- ).join("")
842
- : ""
843
- }${esModule ? "&" : ""}${escapeCss(moduleId)}`
844
- );
845
811
  return tryRunOrWebpackError(
846
812
  () => hooks.renderModulePackage.call(source, module, renderContext),
847
813
  "CssModulesPlugin.getCompilationHooks().renderModulePackage"
@@ -855,8 +821,6 @@ class CssModulesPlugin {
855
821
  */
856
822
  renderChunk(
857
823
  {
858
- uniqueName,
859
- cssHeadDataCompression,
860
824
  undoPath,
861
825
  chunk,
862
826
  chunkGraph,
@@ -867,14 +831,11 @@ class CssModulesPlugin {
867
831
  hooks
868
832
  ) {
869
833
  const source = new ConcatSource();
870
- /** @type {string[]} */
871
- const metaData = [];
872
834
  for (const module of modules) {
873
835
  try {
874
836
  const moduleSource = this.renderModule(
875
837
  module,
876
838
  {
877
- metaData,
878
839
  undoPath,
879
840
  chunk,
880
841
  chunkGraph,
@@ -890,13 +851,6 @@ class CssModulesPlugin {
890
851
  throw err;
891
852
  }
892
853
  }
893
- const metaDataStr = metaData.join(",");
894
- source.add(
895
- `head{--webpack-${escapeCss(
896
- (uniqueName ? `${uniqueName}-` : "") + chunk.id,
897
- true
898
- )}:${cssHeadDataCompression ? lzwEncode(metaDataStr) : metaDataStr};}`
899
- );
900
854
  chunk.rendered = true;
901
855
  return source;
902
856
  }