webpack 5.52.1 → 5.55.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.

Potentially problematic release.


This version of webpack might be problematic. Click here for more details.

Files changed (69) hide show
  1. package/lib/AsyncDependenciesBlock.js +9 -2
  2. package/lib/CacheFacade.js +10 -3
  3. package/lib/ChunkGraph.js +19 -8
  4. package/lib/CodeGenerationResults.js +7 -2
  5. package/lib/Compilation.js +586 -143
  6. package/lib/Compiler.js +13 -4
  7. package/lib/Dependency.js +11 -0
  8. package/lib/DependencyTemplates.js +8 -2
  9. package/lib/EvalDevToolModulePlugin.js +2 -1
  10. package/lib/EvalSourceMapDevToolPlugin.js +2 -1
  11. package/lib/ExternalModule.js +18 -9
  12. package/lib/FileSystemInfo.js +101 -170
  13. package/lib/FlagDependencyExportsPlugin.js +25 -16
  14. package/lib/JavascriptMetaInfoPlugin.js +6 -1
  15. package/lib/ModuleFactory.js +1 -0
  16. package/lib/ModuleFilenameHelpers.js +21 -7
  17. package/lib/ModuleGraph.js +90 -21
  18. package/lib/NodeStuffInWebError.js +34 -0
  19. package/lib/NodeStuffPlugin.js +59 -16
  20. package/lib/NormalModuleFactory.js +8 -43
  21. package/lib/SourceMapDevToolPlugin.js +7 -3
  22. package/lib/WebpackOptionsApply.js +19 -1
  23. package/lib/cache/PackFileCacheStrategy.js +11 -1
  24. package/lib/cache/getLazyHashedEtag.js +35 -8
  25. package/lib/config/defaults.js +32 -12
  26. package/lib/dependencies/CachedConstDependency.js +4 -3
  27. package/lib/dependencies/CommonJsExportRequireDependency.js +19 -9
  28. package/lib/dependencies/CommonJsFullRequireDependency.js +11 -9
  29. package/lib/dependencies/ConstDependency.js +12 -4
  30. package/lib/dependencies/ContextDependency.js +8 -0
  31. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +179 -163
  32. package/lib/dependencies/HarmonyImportDependency.js +4 -1
  33. package/lib/dependencies/JsonExportsDependency.js +7 -1
  34. package/lib/dependencies/ModuleDecoratorDependency.js +5 -2
  35. package/lib/dependencies/ModuleDependency.js +8 -0
  36. package/lib/dependencies/NullDependency.js +8 -4
  37. package/lib/dependencies/ProvidedDependency.js +6 -2
  38. package/lib/dependencies/PureExpressionDependency.js +5 -1
  39. package/lib/dependencies/RuntimeRequirementsDependency.js +5 -1
  40. package/lib/dependencies/WebAssemblyExportImportedDependency.js +9 -0
  41. package/lib/ids/IdHelpers.js +21 -8
  42. package/lib/ids/NamedChunkIdsPlugin.js +3 -0
  43. package/lib/ids/NamedModuleIdsPlugin.js +3 -1
  44. package/lib/index.js +6 -0
  45. package/lib/javascript/BasicEvaluatedExpression.js +3 -0
  46. package/lib/javascript/JavascriptParser.js +22 -4
  47. package/lib/javascript/JavascriptParserHelpers.js +0 -2
  48. package/lib/node/NodeTargetPlugin.js +1 -0
  49. package/lib/optimize/ConcatenatedModule.js +25 -4
  50. package/lib/optimize/InnerGraph.js +22 -2
  51. package/lib/optimize/ModuleConcatenationPlugin.js +2 -1
  52. package/lib/schemes/HttpUriPlugin.js +1 -2
  53. package/lib/serialization/BinaryMiddleware.js +11 -2
  54. package/lib/serialization/FileMiddleware.js +24 -7
  55. package/lib/serialization/ObjectMiddleware.js +19 -8
  56. package/lib/util/WeakTupleMap.js +95 -92
  57. package/lib/util/createHash.js +10 -0
  58. package/lib/util/hash/BatchedHash.js +65 -0
  59. package/lib/util/hash/xxhash64.js +154 -0
  60. package/lib/util/internalSerializables.js +1 -0
  61. package/lib/util/serialization.js +4 -4
  62. package/package.json +11 -7
  63. package/schemas/WebpackOptions.check.js +1 -1
  64. package/schemas/WebpackOptions.json +19 -3
  65. package/schemas/plugins/HashedModuleIdsPlugin.check.js +1 -1
  66. package/schemas/plugins/HashedModuleIdsPlugin.json +20 -2
  67. package/schemas/plugins/IgnorePlugin.check.js +1 -1
  68. package/schemas/plugins/IgnorePlugin.json +4 -2
  69. package/types.d.ts +215 -25
@@ -8,6 +8,7 @@
8
8
  const createHash = require("../util/createHash");
9
9
 
10
10
  /** @typedef {import("../util/Hash")} Hash */
11
+ /** @typedef {typeof import("../util/Hash")} HashConstructor */
11
12
 
12
13
  /**
13
14
  * @typedef {Object} HashableObject
@@ -17,10 +18,12 @@ const createHash = require("../util/createHash");
17
18
  class LazyHashedEtag {
18
19
  /**
19
20
  * @param {HashableObject} obj object with updateHash method
21
+ * @param {string | HashConstructor} hashFunction the hash function to use
20
22
  */
21
- constructor(obj) {
23
+ constructor(obj, hashFunction = "md4") {
22
24
  this._obj = obj;
23
25
  this._hash = undefined;
26
+ this._hashFunction = hashFunction;
24
27
  }
25
28
 
26
29
  /**
@@ -28,7 +31,7 @@ class LazyHashedEtag {
28
31
  */
29
32
  toString() {
30
33
  if (this._hash === undefined) {
31
- const hash = createHash("md4");
34
+ const hash = createHash(this._hashFunction);
32
35
  this._obj.updateHash(hash);
33
36
  this._hash = /** @type {string} */ (hash.digest("base64"));
34
37
  }
@@ -36,18 +39,42 @@ class LazyHashedEtag {
36
39
  }
37
40
  }
38
41
 
39
- /** @type {WeakMap<HashableObject, LazyHashedEtag>} */
40
- const map = new WeakMap();
42
+ /** @type {Map<string | HashConstructor, WeakMap<HashableObject, LazyHashedEtag>>} */
43
+ const mapStrings = new Map();
44
+
45
+ /** @type {WeakMap<HashConstructor, WeakMap<HashableObject, LazyHashedEtag>>} */
46
+ const mapObjects = new WeakMap();
41
47
 
42
48
  /**
43
49
  * @param {HashableObject} obj object with updateHash method
50
+ * @param {string | HashConstructor} hashFunction the hash function to use
44
51
  * @returns {LazyHashedEtag} etag
45
52
  */
46
- const getter = obj => {
47
- const hash = map.get(obj);
53
+ const getter = (obj, hashFunction = "md4") => {
54
+ let innerMap;
55
+ if (typeof hashFunction === "string") {
56
+ innerMap = mapStrings.get(hashFunction);
57
+ if (innerMap === undefined) {
58
+ const newHash = new LazyHashedEtag(obj, hashFunction);
59
+ innerMap = new WeakMap();
60
+ innerMap.set(obj, newHash);
61
+ mapStrings.set(hashFunction, innerMap);
62
+ return newHash;
63
+ }
64
+ } else {
65
+ innerMap = mapObjects.get(hashFunction);
66
+ if (innerMap === undefined) {
67
+ const newHash = new LazyHashedEtag(obj, hashFunction);
68
+ innerMap = new WeakMap();
69
+ innerMap.set(obj, newHash);
70
+ mapObjects.set(hashFunction, innerMap);
71
+ return newHash;
72
+ }
73
+ }
74
+ const hash = innerMap.get(obj);
48
75
  if (hash !== undefined) return hash;
49
- const newHash = new LazyHashedEtag(obj);
50
- map.set(obj, newHash);
76
+ const newHash = new LazyHashedEtag(obj, hashFunction);
77
+ innerMap.set(obj, newHash);
51
78
  return newHash;
52
79
  };
53
80
 
@@ -159,20 +159,21 @@ const applyWebpackOptionsDefaults = options => {
159
159
  D(options, "recordsInputPath", false);
160
160
  D(options, "recordsOutputPath", false);
161
161
 
162
+ applyExperimentsDefaults(options.experiments, { production, development });
163
+
162
164
  F(options, "cache", () =>
163
165
  development ? { type: /** @type {"memory"} */ ("memory") } : false
164
166
  );
165
167
  applyCacheDefaults(options.cache, {
166
168
  name: name || "default",
167
169
  mode: mode || "production",
168
- development
170
+ development,
171
+ cacheUnaffected: options.experiments.cacheUnaffected
169
172
  });
170
173
  const cache = !!options.cache;
171
174
 
172
175
  applySnapshotDefaults(options.snapshot, { production });
173
176
 
174
- applyExperimentsDefaults(options.experiments, { production, development });
175
-
176
177
  applyModuleDefaults(options.module, {
177
178
  cache,
178
179
  syncWebAssembly: options.experiments.syncWebAssembly,
@@ -190,7 +191,8 @@ const applyWebpackOptionsDefaults = options => {
190
191
  outputModule: options.experiments.outputModule,
191
192
  development,
192
193
  entry: options.entry,
193
- module: options.module
194
+ module: options.module,
195
+ futureDefaults: options.experiments.futureDefaults
194
196
  });
195
197
 
196
198
  applyExternalsPresetsDefaults(options.externalsPresets, {
@@ -211,7 +213,10 @@ const applyWebpackOptionsDefaults = options => {
211
213
  : "var";
212
214
  });
213
215
 
214
- applyNodeDefaults(options.node, { targetProperties });
216
+ applyNodeDefaults(options.node, {
217
+ futureDefaults: options.experiments.futureDefaults,
218
+ targetProperties
219
+ });
215
220
 
216
221
  F(options, "performance", () =>
217
222
  production &&
@@ -262,6 +267,8 @@ const applyExperimentsDefaults = (experiments, { production, development }) => {
262
267
  D(experiments, "layers", false);
263
268
  D(experiments, "lazyCompilation", false);
264
269
  D(experiments, "buildHttp", false);
270
+ D(experiments, "futureDefaults", false);
271
+ D(experiments, "cacheUnaffected", experiments.futureDefaults);
265
272
 
266
273
  if (typeof experiments.buildHttp === "object") {
267
274
  D(experiments.buildHttp, "frozen", production);
@@ -275,9 +282,13 @@ const applyExperimentsDefaults = (experiments, { production, development }) => {
275
282
  * @param {string} options.name name
276
283
  * @param {string} options.mode mode
277
284
  * @param {boolean} options.development is development mode
285
+ * @param {boolean} options.cacheUnaffected the cacheUnaffected experiment is enabled
278
286
  * @returns {void}
279
287
  */
280
- const applyCacheDefaults = (cache, { name, mode, development }) => {
288
+ const applyCacheDefaults = (
289
+ cache,
290
+ { name, mode, development, cacheUnaffected }
291
+ ) => {
281
292
  if (cache === false) return;
282
293
  switch (cache.type) {
283
294
  case "filesystem":
@@ -321,12 +332,14 @@ const applyCacheDefaults = (cache, { name, mode, development }) => {
321
332
  D(cache, "maxMemoryGenerations", development ? 5 : Infinity);
322
333
  D(cache, "maxAge", 1000 * 60 * 60 * 24 * 60); // 1 month
323
334
  D(cache, "allowCollectingMemory", development);
335
+ D(cache, "memoryCacheUnaffected", development && cacheUnaffected);
324
336
  D(cache.buildDependencies, "defaultWebpack", [
325
337
  path.resolve(__dirname, "..") + path.sep
326
338
  ]);
327
339
  break;
328
340
  case "memory":
329
341
  D(cache, "maxGenerations", Infinity);
342
+ D(cache, "cacheUnaffected", development && cacheUnaffected);
330
343
  break;
331
344
  }
332
345
  };
@@ -576,6 +589,7 @@ const applyModuleDefaults = (
576
589
  * @param {boolean} options.development is development mode
577
590
  * @param {Entry} options.entry entry option
578
591
  * @param {ModuleOptions} options.module module option
592
+ * @param {boolean} options.futureDefaults is future defaults enabled
579
593
  * @returns {void}
580
594
  */
581
595
  const applyOutputDefaults = (
@@ -587,7 +601,8 @@ const applyOutputDefaults = (
587
601
  outputModule,
588
602
  development,
589
603
  entry,
590
- module
604
+ module,
605
+ futureDefaults
591
606
  }
592
607
  ) => {
593
608
  /**
@@ -784,7 +799,7 @@ const applyOutputDefaults = (
784
799
  : ""
785
800
  );
786
801
  D(output, "chunkLoadTimeout", 120000);
787
- D(output, "hashFunction", "md4");
802
+ D(output, "hashFunction", futureDefaults ? "xxhash64" : "md4");
788
803
  D(output, "hashDigest", "hex");
789
804
  D(output, "hashDigestLength", 20);
790
805
  D(output, "strictModuleExceptionHandling", false);
@@ -943,21 +958,26 @@ const applyLoaderDefaults = (loader, { targetProperties }) => {
943
958
  * @param {WebpackNode} node options
944
959
  * @param {Object} options options
945
960
  * @param {TargetProperties | false} options.targetProperties target properties
961
+ * @param {boolean} options.futureDefaults is future defaults enabled
946
962
  * @returns {void}
947
963
  */
948
- const applyNodeDefaults = (node, { targetProperties }) => {
964
+ const applyNodeDefaults = (node, { futureDefaults, targetProperties }) => {
949
965
  if (node === false) return;
966
+
950
967
  F(node, "global", () => {
951
968
  if (targetProperties && targetProperties.global) return false;
952
- return true;
969
+ // TODO webpack 6 should always default to false
970
+ return futureDefaults ? "warn" : true;
953
971
  });
954
972
  F(node, "__filename", () => {
955
973
  if (targetProperties && targetProperties.node) return "eval-only";
956
- return "mock";
974
+ // TODO webpack 6 should always default to false
975
+ return futureDefaults ? "warn-mock" : "mock";
957
976
  });
958
977
  F(node, "__dirname", () => {
959
978
  if (targetProperties && targetProperties.node) return "eval-only";
960
- return "mock";
979
+ // TODO webpack 6 should always default to false
980
+ return futureDefaults ? "warn-mock" : "mock";
961
981
  });
962
982
  };
963
983
 
@@ -27,6 +27,7 @@ class CachedConstDependency extends NullDependency {
27
27
  this.expression = expression;
28
28
  this.range = range;
29
29
  this.identifier = identifier;
30
+ this._hashUpdate = undefined;
30
31
  }
31
32
 
32
33
  /**
@@ -36,9 +37,9 @@ class CachedConstDependency extends NullDependency {
36
37
  * @returns {void}
37
38
  */
38
39
  updateHash(hash, context) {
39
- hash.update(this.identifier + "");
40
- hash.update(this.range + "");
41
- hash.update(this.expression + "");
40
+ if (this._hashUpdate === undefined)
41
+ this._hashUpdate = "" + this.identifier + this.range + this.expression;
42
+ hash.update(this._hashUpdate);
42
43
  }
43
44
 
44
45
  serialize(context) {
@@ -18,6 +18,7 @@ const processExportInfo = require("./processExportInfo");
18
18
  /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
19
19
  /** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */
20
20
  /** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
21
+ /** @typedef {import("../Dependency").TRANSITIVE} TRANSITIVE */
21
22
  /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
22
23
  /** @typedef {import("../Module")} Module */
23
24
  /** @typedef {import("../ModuleGraph")} ModuleGraph */
@@ -43,6 +44,13 @@ class CommonJsExportRequireDependency extends ModuleDependency {
43
44
  return "cjs export require";
44
45
  }
45
46
 
47
+ /**
48
+ * @returns {boolean | TRANSITIVE} true, when changes to the referenced module could affect the referencing module; TRANSITIVE, when changes to the referenced module could affect referencing modules of the referencing module
49
+ */
50
+ couldAffectReferencingModule() {
51
+ return Dependency.TRANSITIVE;
52
+ }
53
+
46
54
  /**
47
55
  * @param {ModuleGraph} moduleGraph the module graph
48
56
  * @returns {string[]} the imported id
@@ -332,15 +340,17 @@ CommonJsExportRequireDependency.Template = class CommonJsExportRequireDependency
332
340
  weak: dep.weak,
333
341
  runtimeRequirements
334
342
  });
335
- const ids = dep.getIds(moduleGraph);
336
- const usedImported = moduleGraph
337
- .getExportsInfo(importedModule)
338
- .getUsedName(ids, runtime);
339
- if (usedImported) {
340
- const comment = equals(usedImported, ids)
341
- ? ""
342
- : Template.toNormalComment(propertyAccess(ids)) + " ";
343
- requireExpr += `${comment}${propertyAccess(usedImported)}`;
343
+ if (importedModule) {
344
+ const ids = dep.getIds(moduleGraph);
345
+ const usedImported = moduleGraph
346
+ .getExportsInfo(importedModule)
347
+ .getUsedName(ids, runtime);
348
+ if (usedImported) {
349
+ const comment = equals(usedImported, ids)
350
+ ? ""
351
+ : Template.toNormalComment(propertyAccess(ids)) + " ";
352
+ requireExpr += `${comment}${propertyAccess(usedImported)}`;
353
+ }
344
354
  }
345
355
 
346
356
  switch (type) {
@@ -108,15 +108,17 @@ CommonJsFullRequireDependency.Template = class CommonJsFullRequireDependencyTemp
108
108
  weak: dep.weak,
109
109
  runtimeRequirements
110
110
  });
111
- const ids = dep.names;
112
- const usedImported = moduleGraph
113
- .getExportsInfo(importedModule)
114
- .getUsedName(ids, runtime);
115
- if (usedImported) {
116
- const comment = equals(usedImported, ids)
117
- ? ""
118
- : Template.toNormalComment(propertyAccess(ids)) + " ";
119
- requireExpr += `${comment}${propertyAccess(usedImported)}`;
111
+ if (importedModule) {
112
+ const ids = dep.names;
113
+ const usedImported = moduleGraph
114
+ .getExportsInfo(importedModule)
115
+ .getUsedName(ids, runtime);
116
+ if (usedImported) {
117
+ const comment = equals(usedImported, ids)
118
+ ? ""
119
+ : Template.toNormalComment(propertyAccess(ids)) + " ";
120
+ requireExpr += `${comment}${propertyAccess(usedImported)}`;
121
+ }
120
122
  }
121
123
  source.replace(dep.range[0], dep.range[1] - 1, requireExpr);
122
124
  }
@@ -30,6 +30,7 @@ class ConstDependency extends NullDependency {
30
30
  this.runtimeRequirements = runtimeRequirements
31
31
  ? new Set(runtimeRequirements)
32
32
  : null;
33
+ this._hashUpdate = undefined;
33
34
  }
34
35
 
35
36
  /**
@@ -39,10 +40,17 @@ class ConstDependency extends NullDependency {
39
40
  * @returns {void}
40
41
  */
41
42
  updateHash(hash, context) {
42
- hash.update(this.range + "");
43
- hash.update(this.expression + "");
44
- if (this.runtimeRequirements)
45
- hash.update(Array.from(this.runtimeRequirements).join() + "");
43
+ if (this._hashUpdate === undefined) {
44
+ let hashUpdate = "" + this.range + "|" + this.expression;
45
+ if (this.runtimeRequirements) {
46
+ for (const item of this.runtimeRequirements) {
47
+ hashUpdate += "|";
48
+ hashUpdate += item;
49
+ }
50
+ }
51
+ this._hashUpdate = hashUpdate;
52
+ }
53
+ hash.update(this._hashUpdate);
46
54
  }
47
55
 
48
56
  /**
@@ -11,6 +11,7 @@ const makeSerializable = require("../util/makeSerializable");
11
11
  const memoize = require("../util/memoize");
12
12
 
13
13
  /** @typedef {import("../ContextModule").ContextOptions} ContextOptions */
14
+ /** @typedef {import("../Dependency").TRANSITIVE} TRANSITIVE */
14
15
  /** @typedef {import("../ModuleGraph")} ModuleGraph */
15
16
  /** @typedef {import("../WebpackError")} WebpackError */
16
17
 
@@ -54,6 +55,13 @@ class ContextDependency extends Dependency {
54
55
  return "commonjs";
55
56
  }
56
57
 
58
+ /**
59
+ * @returns {boolean | TRANSITIVE} true, when changes to the referenced module could affect the referencing module; TRANSITIVE, when changes to the referenced module could affect referencing modules of the referencing module
60
+ */
61
+ couldAffectReferencingModule() {
62
+ return true;
63
+ }
64
+
57
65
  /**
58
66
  * @returns {string | null} an identifier to merge equal requests
59
67
  */