webpack 5.102.0 → 5.103.0

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 (146) hide show
  1. package/README.md +121 -134
  2. package/lib/ChunkGraph.js +2 -2
  3. package/lib/CodeGenerationResults.js +1 -1
  4. package/lib/CompatibilityPlugin.js +25 -2
  5. package/lib/Compilation.js +30 -13
  6. package/lib/ConcatenationScope.js +0 -15
  7. package/lib/ContextModule.js +3 -1
  8. package/lib/CssModule.js +6 -1
  9. package/lib/DefinePlugin.js +12 -12
  10. package/lib/Dependency.js +8 -1
  11. package/lib/DependencyTemplate.js +1 -0
  12. package/lib/DependencyTemplates.js +1 -1
  13. package/lib/DotenvPlugin.js +457 -0
  14. package/lib/EnvironmentPlugin.js +19 -16
  15. package/lib/EvalSourceMapDevToolPlugin.js +16 -0
  16. package/lib/ExportsInfo.js +6 -2
  17. package/lib/ExternalModule.js +20 -28
  18. package/lib/ExternalModuleFactoryPlugin.js +10 -8
  19. package/lib/ExternalsPlugin.js +2 -1
  20. package/lib/FileSystemInfo.js +9 -12
  21. package/lib/ManifestPlugin.js +235 -0
  22. package/lib/Module.js +3 -0
  23. package/lib/ModuleFilenameHelpers.js +1 -1
  24. package/lib/ModuleGraph.js +2 -1
  25. package/lib/ModuleSourceTypesConstants.js +0 -6
  26. package/lib/MultiCompiler.js +1 -1
  27. package/lib/NodeStuffPlugin.js +419 -121
  28. package/lib/NormalModule.js +18 -17
  29. package/lib/NormalModuleFactory.js +75 -4
  30. package/lib/RuntimeGlobals.js +22 -4
  31. package/lib/RuntimePlugin.js +27 -6
  32. package/lib/RuntimeTemplate.js +125 -57
  33. package/lib/SourceMapDevToolPlugin.js +26 -8
  34. package/lib/WebpackOptionsApply.js +33 -9
  35. package/lib/asset/AssetBytesGenerator.js +2 -1
  36. package/lib/asset/AssetGenerator.js +3 -5
  37. package/lib/asset/AssetSourceGenerator.js +1 -1
  38. package/lib/cache/getLazyHashedEtag.js +1 -1
  39. package/lib/config/browserslistTargetHandler.js +82 -76
  40. package/lib/config/defaults.js +105 -20
  41. package/lib/config/normalization.js +2 -1
  42. package/lib/config/target.js +7 -1
  43. package/lib/css/CssGenerator.js +283 -57
  44. package/lib/css/CssLoadingRuntimeModule.js +2 -0
  45. package/lib/css/CssMergeStyleSheetsRuntimeModule.js +56 -0
  46. package/lib/css/CssModulesPlugin.js +86 -40
  47. package/lib/css/CssParser.js +1174 -667
  48. package/lib/css/walkCssTokens.js +98 -1
  49. package/lib/dependencies/CommonJsImportsParserPlugin.js +0 -9
  50. package/lib/dependencies/CommonJsPlugin.js +12 -0
  51. package/lib/dependencies/ContextElementDependency.js +2 -2
  52. package/lib/dependencies/CssIcssExportDependency.js +247 -8
  53. package/lib/dependencies/CssIcssFromIdentifierDependency.js +124 -0
  54. package/lib/dependencies/CssIcssGlobalIdentifierDependency.js +48 -0
  55. package/lib/dependencies/CssIcssImportDependency.js +60 -54
  56. package/lib/dependencies/CssIcssLocalIdentifierDependency.js +61 -0
  57. package/lib/dependencies/{CssSelfLocalIdentifierDependency.js → CssIcssSelfLocalIdentifierDependency.js} +88 -10
  58. package/lib/dependencies/CssIcssSymbolDependency.js +31 -29
  59. package/lib/dependencies/CssImportDependency.js +15 -5
  60. package/lib/dependencies/ExternalModuleInitFragment.js +1 -1
  61. package/lib/dependencies/ExternalModuleInitFragmentDependency.js +95 -0
  62. package/lib/dependencies/HarmonyAcceptDependency.js +6 -1
  63. package/lib/dependencies/HarmonyAcceptImportDependency.js +2 -1
  64. package/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js +12 -1
  65. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +32 -21
  66. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +12 -8
  67. package/lib/dependencies/HarmonyImportDependency.js +27 -28
  68. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +28 -69
  69. package/lib/dependencies/HarmonyImportSideEffectDependency.js +4 -3
  70. package/lib/dependencies/HarmonyImportSpecifierDependency.js +10 -8
  71. package/lib/dependencies/ImportContextDependency.js +13 -0
  72. package/lib/dependencies/ImportDependency.js +10 -4
  73. package/lib/dependencies/ImportEagerDependency.js +6 -3
  74. package/lib/dependencies/ImportMetaPlugin.js +98 -10
  75. package/lib/dependencies/ImportParserPlugin.js +19 -21
  76. package/lib/dependencies/ImportPhase.js +121 -0
  77. package/lib/dependencies/ImportWeakDependency.js +6 -3
  78. package/lib/dependencies/ModuleDependency.js +5 -1
  79. package/lib/dependencies/ModuleHotAcceptDependency.js +1 -1
  80. package/lib/dependencies/WorkerPlugin.js +1 -3
  81. package/lib/esm/ExportWebpackRequireRuntimeModule.js +1 -8
  82. package/lib/hmr/LazyCompilationPlugin.js +1 -0
  83. package/lib/ids/HashedModuleIdsPlugin.js +5 -7
  84. package/lib/ids/IdHelpers.js +5 -2
  85. package/lib/index.js +6 -0
  86. package/lib/javascript/ChunkHelpers.js +16 -5
  87. package/lib/javascript/JavascriptGenerator.js +101 -101
  88. package/lib/javascript/JavascriptModulesPlugin.js +25 -16
  89. package/lib/javascript/JavascriptParser.js +143 -39
  90. package/lib/json/JsonParser.js +7 -1
  91. package/lib/library/ModuleLibraryPlugin.js +0 -10
  92. package/lib/library/SystemLibraryPlugin.js +19 -5
  93. package/lib/library/UmdLibraryPlugin.js +1 -1
  94. package/lib/node/NodeTargetPlugin.js +9 -1
  95. package/lib/node/ReadFileCompileWasmPlugin.js +0 -2
  96. package/lib/optimize/ConcatenatedModule.js +161 -135
  97. package/lib/optimize/RealContentHashPlugin.js +5 -3
  98. package/lib/runtime/AsyncModuleRuntimeModule.js +28 -18
  99. package/lib/runtime/AutoPublicPathRuntimeModule.js +8 -3
  100. package/lib/runtime/MakeDeferredNamespaceObjectRuntime.js +89 -55
  101. package/lib/serialization/FileMiddleware.js +1 -1
  102. package/lib/serialization/ObjectMiddleware.js +1 -1
  103. package/lib/stats/DefaultStatsFactoryPlugin.js +1 -1
  104. package/lib/util/Hash.js +35 -5
  105. package/lib/util/comparators.js +4 -3
  106. package/lib/util/create-schema-validation.js +1 -1
  107. package/lib/util/createHash.js +85 -15
  108. package/lib/util/hash/BatchedHash.js +47 -8
  109. package/lib/util/hash/wasm-hash.js +53 -13
  110. package/lib/util/internalSerializables.js +4 -4
  111. package/lib/util/jsonParseEvenBetterErrors.js +10 -0
  112. package/lib/wasm/EnableWasmLoadingPlugin.js +10 -4
  113. package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +8 -5
  114. package/lib/wasm-async/AsyncWebAssemblyParser.js +0 -9
  115. package/lib/wasm-sync/WebAssemblyJavascriptGenerator.js +8 -4
  116. package/lib/wasm-sync/WebAssemblyParser.js +0 -9
  117. package/lib/web/FetchCompileWasmPlugin.js +0 -2
  118. package/lib/web/JsonpChunkLoadingRuntimeModule.js +1 -1
  119. package/lib/webpack.js +85 -82
  120. package/module.d.ts +5 -0
  121. package/package.json +28 -26
  122. package/schemas/WebpackOptions.check.js +1 -1
  123. package/schemas/WebpackOptions.json +110 -15
  124. package/schemas/plugins/{HashedModuleIdsPlugin.check.d.ts → ManifestPlugin.check.d.ts} +1 -1
  125. package/schemas/plugins/ManifestPlugin.check.js +6 -0
  126. package/schemas/plugins/ManifestPlugin.json +98 -0
  127. package/schemas/plugins/SourceMapDevToolPlugin.check.js +1 -1
  128. package/schemas/plugins/SourceMapDevToolPlugin.json +16 -3
  129. package/schemas/plugins/container/ContainerReferencePlugin.check.js +1 -1
  130. package/schemas/plugins/container/ContainerReferencePlugin.json +4 -1
  131. package/schemas/plugins/container/ExternalsType.check.js +1 -1
  132. package/schemas/plugins/container/ModuleFederationPlugin.check.js +1 -1
  133. package/schemas/plugins/container/ModuleFederationPlugin.json +4 -1
  134. package/schemas/plugins/css/CssAutoParserOptions.check.js +1 -1
  135. package/schemas/plugins/css/CssGlobalGeneratorOptions.check.js +1 -1
  136. package/schemas/plugins/css/CssGlobalParserOptions.check.js +1 -1
  137. package/schemas/plugins/css/CssModuleGeneratorOptions.check.js +1 -1
  138. package/schemas/plugins/css/CssModuleParserOptions.check.js +1 -1
  139. package/schemas/plugins/css/CssParserOptions.check.js +1 -1
  140. package/schemas/plugins/ids/HashedModuleIdsPlugin.check.d.ts +7 -0
  141. package/schemas/plugins/ids/HashedModuleIdsPlugin.check.js +6 -0
  142. package/schemas/plugins/{HashedModuleIdsPlugin.json → ids/HashedModuleIdsPlugin.json} +15 -2
  143. package/schemas/plugins/json/JsonModulesPluginParser.check.js +1 -1
  144. package/types.d.ts +788 -127
  145. package/lib/dependencies/CssLocalIdentifierDependency.js +0 -252
  146. package/schemas/plugins/HashedModuleIdsPlugin.check.js +0 -6
@@ -8,8 +8,12 @@ const RuntimeGlobals = require("../RuntimeGlobals");
8
8
  const Template = require("../Template");
9
9
  const HelperRuntimeModule = require("./HelperRuntimeModule");
10
10
 
11
+ /** @typedef {import("../Module").RuntimeRequirements} RuntimeRequirements */
12
+ /** @typedef {import("../Module").ExportsType} ExportsType */
13
+ /** @typedef {import("../ChunkGraph").ModuleId} ModuleId */
14
+
11
15
  /**
12
- * @param {import("../Module").ExportsType} exportsType exports type
16
+ * @param {ExportsType} exportsType exports type
13
17
  * @returns {string} mode
14
18
  */
15
19
  function getMakeDeferredNamespaceModeFromExportsType(exportsType) {
@@ -19,61 +23,81 @@ function getMakeDeferredNamespaceModeFromExportsType(exportsType) {
19
23
  if (exportsType === "dynamic") return `/* ${exportsType} */ 3`;
20
24
  return "";
21
25
  }
26
+
22
27
  /**
23
- * @param {import("../ModuleTemplate").RuntimeTemplate} _runtimeTemplate runtimeTemplate
24
- * @param {import("../Module").ExportsType} exportsType exportsType
25
28
  * @param {string} moduleId moduleId
26
- * @param {(import("../ChunkGraph").ModuleId | null)[]} asyncDepsIds asyncDepsIds
27
- * @returns {string} function
29
+ * @param {ExportsType} exportsType exportsType
30
+ * @param {(ModuleId | null)[]} asyncDepsIds asyncDepsIds
31
+ * @param {RuntimeRequirements} runtimeRequirements runtime requirements
32
+ * @returns {string} call make optimized deferred namespace object
28
33
  */
29
34
  function getOptimizedDeferredModule(
30
- _runtimeTemplate,
31
- exportsType,
32
35
  moduleId,
33
- asyncDepsIds
36
+ exportsType,
37
+ asyncDepsIds,
38
+ runtimeRequirements
34
39
  ) {
35
- const isAsync = asyncDepsIds && asyncDepsIds.length;
36
- const init = `${RuntimeGlobals.require}(${moduleId})${
37
- isAsync ? `[${RuntimeGlobals.asyncModuleExportSymbol}]` : ""
38
- }`;
39
- const props = [
40
- `/* ${exportsType} */ get a() {`,
41
- // if exportsType is "namespace" we can generate the most optimized code,
42
- // on the second access, we can avoid trigger the getter.
43
- // we can also do this if exportsType is "dynamic" and there is a "__esModule" property on it.
44
- exportsType === "namespace" || exportsType === "dynamic"
45
- ? Template.indent([
46
- `var exports = ${init};`,
47
- `${
48
- exportsType === "dynamic" ? "if (exports.__esModule) " : ""
49
- }Object.defineProperty(this, "a", { value: exports });`,
50
- "return exports;"
51
- ])
52
- : Template.indent([`return ${init};`]),
53
- isAsync ? "}," : "}",
54
- isAsync
55
- ? `[${
56
- RuntimeGlobals.makeDeferredNamespaceObjectSymbol
57
- }]: ${JSON.stringify(asyncDepsIds.filter((x) => x !== null))}`
40
+ runtimeRequirements.add(RuntimeGlobals.makeOptimizedDeferredNamespaceObject);
41
+ const mode = getMakeDeferredNamespaceModeFromExportsType(exportsType);
42
+ return `${RuntimeGlobals.makeOptimizedDeferredNamespaceObject}(${moduleId}, ${mode}${
43
+ asyncDepsIds.length > 0
44
+ ? `, ${JSON.stringify(asyncDepsIds.filter((x) => x !== null))}`
58
45
  : ""
59
- ];
60
- return Template.asString(["{", Template.indent(props), "}"]);
46
+ })`;
61
47
  }
62
48
 
63
- const strictModuleCache = [
64
- "if (cachedModule && cachedModule.error === undefined) {",
65
- Template.indent([
66
- "var exports = cachedModule.exports;",
67
- "if (mode == 0) return exports;",
68
- `if (mode == 1) return ${RuntimeGlobals.createFakeNamespaceObject}(exports);`,
69
- `if (mode == 2) return ${RuntimeGlobals.createFakeNamespaceObject}(exports, 2);`,
70
- `if (mode == 3) return ${RuntimeGlobals.createFakeNamespaceObject}(exports, 6);` // 2 | 4
71
- ]),
72
- "}"
73
- ];
74
- const nonStrictModuleCache = [
75
- "// optimization not applied when output.strictModuleErrorHandling is off"
76
- ];
49
+ class MakeOptimizedDeferredNamespaceObjectRuntimeModule extends HelperRuntimeModule {
50
+ /**
51
+ * @param {boolean} hasAsyncRuntime if async module is used.
52
+ */
53
+ constructor(hasAsyncRuntime) {
54
+ super("make optimized deferred namespace object");
55
+ this.hasAsyncRuntime = hasAsyncRuntime;
56
+ }
57
+
58
+ /**
59
+ * @returns {string | null} runtime code
60
+ */
61
+ generate() {
62
+ if (!this.compilation) return null;
63
+ const fn = RuntimeGlobals.makeOptimizedDeferredNamespaceObject;
64
+ const hasAsync = this.hasAsyncRuntime;
65
+ return Template.asString([
66
+ // Note: must be a function (not arrow), because this is used in body!
67
+ `${fn} = function(moduleId, mode${hasAsync ? ", asyncDeps" : ""}) {`,
68
+ Template.indent([
69
+ "// mode: 0 => namespace (esm)",
70
+ "// mode: 1 => default-only (esm strict cjs)",
71
+ "// mode: 2 => default-with-named (esm-cjs compat)",
72
+ "// mode: 3 => dynamic (if exports has __esModule, then esm, otherwise default-with-named)",
73
+ "var r = this;",
74
+ hasAsync ? "var isAsync = asyncDeps && asyncDeps.length;" : "",
75
+ "var obj = {",
76
+ Template.indent([
77
+ "get a() {",
78
+ Template.indent([
79
+ "var exports = r(moduleId);",
80
+ hasAsync
81
+ ? `if(isAsync) exports = exports[${RuntimeGlobals.asyncModuleExportSymbol}];`
82
+ : "",
83
+ // if exportsType is "namespace" we can generate the most optimized code,
84
+ // on the second access, we can avoid trigger the getter.
85
+ // we can also do this if exportsType is "dynamic" and there is a "__esModule" property on it.
86
+ 'if(mode == 0 || (mode == 3 && exports.__esModule)) Object.defineProperty(this, "a", { value: exports });',
87
+ "return exports;"
88
+ ]),
89
+ "}"
90
+ ]),
91
+ "};",
92
+ hasAsync
93
+ ? `if(isAsync) obj[${RuntimeGlobals.deferredModuleAsyncTransitiveDependenciesSymbol}] = asyncDeps;`
94
+ : "",
95
+ "return obj;"
96
+ ]),
97
+ "};"
98
+ ]);
99
+ }
100
+ }
77
101
 
78
102
  class MakeDeferredNamespaceObjectRuntimeModule extends HelperRuntimeModule {
79
103
  /**
@@ -92,8 +116,6 @@ class MakeDeferredNamespaceObjectRuntimeModule extends HelperRuntimeModule {
92
116
  const { runtimeTemplate } = this.compilation;
93
117
  const fn = RuntimeGlobals.makeDeferredNamespaceObject;
94
118
  const hasAsync = this.hasAsyncRuntime;
95
- const strictError =
96
- this.compilation.options.output.strictModuleErrorHandling;
97
119
  const init = runtimeTemplate.supportsOptionalChaining()
98
120
  ? "init?.();"
99
121
  : "if (init) init();";
@@ -104,7 +126,18 @@ class MakeDeferredNamespaceObjectRuntimeModule extends HelperRuntimeModule {
104
126
  "// mode: 3 => dynamic (if exports has __esModule, then esm, otherwise default-with-named)",
105
127
  "",
106
128
  "var cachedModule = __webpack_module_cache__[moduleId];",
107
- ...(strictError ? strictModuleCache : nonStrictModuleCache),
129
+ "if (cachedModule && cachedModule.error === undefined) {",
130
+ Template.indent([
131
+ "var exports = cachedModule.exports;",
132
+ hasAsync
133
+ ? `if (${RuntimeGlobals.asyncModuleExportSymbol} in exports) exports = exports[${RuntimeGlobals.asyncModuleExportSymbol}];`
134
+ : "",
135
+ "if (mode == 0) return exports;",
136
+ `if (mode == 1) return ${RuntimeGlobals.createFakeNamespaceObject}(exports);`,
137
+ `if (mode == 2) return ${RuntimeGlobals.createFakeNamespaceObject}(exports, 2);`,
138
+ `if (mode == 3) return ${RuntimeGlobals.createFakeNamespaceObject}(exports, 6);` // 2 | 4
139
+ ]),
140
+ "}",
108
141
  "",
109
142
  `var init = ${runtimeTemplate.basicFunction("", [
110
143
  `ns = ${RuntimeGlobals.require}(moduleId);`,
@@ -137,9 +170,7 @@ class MakeDeferredNamespaceObjectRuntimeModule extends HelperRuntimeModule {
137
170
  "}"
138
171
  ])};`,
139
172
  "",
140
- `var ns = ${
141
- strictError ? "" : "cachedModule && cachedModule.exports || "
142
- }__webpack_module_deferred_exports__[moduleId] || (__webpack_module_deferred_exports__[moduleId] = { __proto__: null });`,
173
+ "var ns = __webpack_module_deferred_exports__[moduleId] || (__webpack_module_deferred_exports__[moduleId] = { __proto__: null });",
143
174
  "var handler = {",
144
175
  Template.indent([
145
176
  "__proto__: null,",
@@ -161,7 +192,7 @@ class MakeDeferredNamespaceObjectRuntimeModule extends HelperRuntimeModule {
161
192
  'case "__esModule":',
162
193
  "case Symbol.toStringTag:",
163
194
  hasAsync
164
- ? `case ${RuntimeGlobals.makeDeferredNamespaceObjectSymbol}:`
195
+ ? `case ${RuntimeGlobals.deferredModuleAsyncTransitiveDependenciesSymbol}:`
165
196
  : "",
166
197
  Template.indent("return true;"),
167
198
  'case "then":',
@@ -208,7 +239,10 @@ class MakeDeferredNamespaceObjectRuntimeModule extends HelperRuntimeModule {
208
239
  }
209
240
  }
210
241
 
211
- module.exports = MakeDeferredNamespaceObjectRuntimeModule;
242
+ module.exports.MakeDeferredNamespaceObjectRuntimeModule =
243
+ MakeDeferredNamespaceObjectRuntimeModule;
244
+ module.exports.MakeOptimizedDeferredNamespaceObjectRuntimeModule =
245
+ MakeOptimizedDeferredNamespaceObjectRuntimeModule;
212
246
  module.exports.getMakeDeferredNamespaceModeFromExportsType =
213
247
  getMakeDeferredNamespaceModeFromExportsType;
214
248
  module.exports.getOptimizedDeferredModule = getOptimizedDeferredModule;
@@ -55,7 +55,7 @@ const WRITE_LIMIT_CHUNK = 511 * 1024 * 1024;
55
55
  const hashForName = (buffers, hashFunction) => {
56
56
  const hash = createHash(hashFunction);
57
57
  for (const buf of buffers) hash.update(buf);
58
- return /** @type {string} */ (hash.digest("hex"));
58
+ return hash.digest("hex");
59
59
  };
60
60
 
61
61
  const COMPRESSION_CHUNK_SIZE = 100 * 1024 * 1024;
@@ -117,7 +117,7 @@ const setMapSize = (map, size) => {
117
117
  const toHash = (buffer, hashFunction) => {
118
118
  const hash = createHash(hashFunction);
119
119
  hash.update(buffer);
120
- return /** @type {string} */ (hash.digest("latin1"));
120
+ return hash.digest("latin1");
121
121
  };
122
122
 
123
123
  const ESCAPE = null;
@@ -722,7 +722,7 @@ const SIMPLE_EXTRACTORS = {
722
722
  }
723
723
  },
724
724
  hash: (object, compilation) => {
725
- object.hash = /** @type {string} */ (compilation.hash);
725
+ object.hash = compilation.hash;
726
726
  },
727
727
  version: (object) => {
728
728
  object.version = require("../../package.json").version;
package/lib/util/Hash.js CHANGED
@@ -5,14 +5,31 @@
5
5
 
6
6
  "use strict";
7
7
 
8
+ /** @typedef {import("../../declarations/WebpackOptions").HashDigest} Encoding */
9
+
8
10
  class Hash {
9
11
  /* istanbul ignore next */
10
12
  /**
11
13
  * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
12
14
  * @abstract
13
- * @param {string|Buffer} data data
14
- * @param {string=} inputEncoding data encoding
15
- * @returns {this} updated hash
15
+ * @overload
16
+ * @param {string | Buffer} data data
17
+ * @returns {Hash} updated hash
18
+ */
19
+ /**
20
+ * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
21
+ * @abstract
22
+ * @overload
23
+ * @param {string} data data
24
+ * @param {Encoding} inputEncoding data encoding
25
+ * @returns {Hash} updated hash
26
+ */
27
+ /**
28
+ * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
29
+ * @abstract
30
+ * @param {string | Buffer} data data
31
+ * @param {Encoding=} inputEncoding data encoding
32
+ * @returns {Hash} updated hash
16
33
  */
17
34
  update(data, inputEncoding) {
18
35
  const AbstractMethodError = require("../AbstractMethodError");
@@ -24,8 +41,21 @@ class Hash {
24
41
  /**
25
42
  * Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
26
43
  * @abstract
27
- * @param {string=} encoding encoding of the return value
28
- * @returns {string|Buffer} digest
44
+ * @overload
45
+ * @returns {Buffer} digest
46
+ */
47
+ /**
48
+ * Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
49
+ * @abstract
50
+ * @overload
51
+ * @param {Encoding} encoding encoding of the return value
52
+ * @returns {string} digest
53
+ */
54
+ /**
55
+ * Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
56
+ * @abstract
57
+ * @param {Encoding=} encoding encoding of the return value
58
+ * @returns {string | Buffer} digest
29
59
  */
30
60
  digest(encoding) {
31
61
  const AbstractMethodError = require("../AbstractMethodError");
@@ -19,6 +19,7 @@ const { compareRuntime } = require("./runtime");
19
19
  /** @typedef {import("../dependencies/HarmonyImportSpecifierDependency")} HarmonyImportSpecifierDependency */
20
20
  /** @typedef {import("../Module")} Module */
21
21
  /** @typedef {import("../ModuleGraph")} ModuleGraph */
22
+ /** @typedef {import("../dependencies/ModuleDependency")} ModuleDependency */
22
23
 
23
24
  /**
24
25
  * @typedef {object} DependencySourceOrder
@@ -524,9 +525,9 @@ const sortWithSourceOrder = (dependencies, dependencySourceOrderMap) => {
524
525
  );
525
526
  return main;
526
527
  }
527
- return /** @type { HarmonyImportSideEffectDependency | HarmonyImportSpecifierDependency} */ (
528
- dep
529
- ).sourceOrder;
528
+ return /** @type {number} */ (
529
+ /** @type {ModuleDependency} */ (dep).sourceOrder
530
+ );
530
531
  };
531
532
 
532
533
  /**
@@ -8,7 +8,7 @@
8
8
  const memoize = require("./memoize");
9
9
 
10
10
  /** @typedef {import("schema-utils").Schema} Schema */
11
- /** @typedef {import("schema-utils/declarations/validate").ValidationErrorConfiguration} ValidationErrorConfiguration */
11
+ /** @typedef {import("schema-utils").ValidationErrorConfiguration} ValidationErrorConfiguration */
12
12
 
13
13
  const getValidate = memoize(() => require("schema-utils").validate);
14
14
 
@@ -7,9 +7,10 @@
7
7
 
8
8
  const Hash = require("./Hash");
9
9
 
10
+ /** @typedef {import("../../declarations/WebpackOptions").HashDigest} Encoding */
10
11
  /** @typedef {import("../../declarations/WebpackOptions").HashFunction} HashFunction */
11
12
 
12
- const BULK_SIZE = 2000;
13
+ const BULK_SIZE = 3;
13
14
 
14
15
  // We are using an object instead of a Map as this will stay static during the runtime
15
16
  // so access to it can be optimized by v8
@@ -38,9 +39,22 @@ class BulkUpdateDecorator extends Hash {
38
39
 
39
40
  /**
40
41
  * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
41
- * @param {string|Buffer} data data
42
- * @param {string=} inputEncoding data encoding
43
- * @returns {this} updated hash
42
+ * @overload
43
+ * @param {string | Buffer} data data
44
+ * @returns {Hash} updated hash
45
+ */
46
+ /**
47
+ * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
48
+ * @overload
49
+ * @param {string} data data
50
+ * @param {Encoding} inputEncoding data encoding
51
+ * @returns {Hash} updated hash
52
+ */
53
+ /**
54
+ * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
55
+ * @param {string | Buffer} data data
56
+ * @param {Encoding=} inputEncoding data encoding
57
+ * @returns {Hash} updated hash
44
58
  */
45
59
  update(data, inputEncoding) {
46
60
  if (
@@ -55,7 +69,11 @@ class BulkUpdateDecorator extends Hash {
55
69
  this.hash.update(this.buffer);
56
70
  this.buffer = "";
57
71
  }
58
- this.hash.update(data, inputEncoding);
72
+ if (typeof data === "string" && inputEncoding) {
73
+ this.hash.update(data, inputEncoding);
74
+ } else {
75
+ this.hash.update(data);
76
+ }
59
77
  } else {
60
78
  this.buffer += data;
61
79
  if (this.buffer.length > BULK_SIZE) {
@@ -71,8 +89,19 @@ class BulkUpdateDecorator extends Hash {
71
89
 
72
90
  /**
73
91
  * Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
74
- * @param {string=} encoding encoding of the return value
75
- * @returns {string|Buffer} digest
92
+ * @overload
93
+ * @returns {Buffer} digest
94
+ */
95
+ /**
96
+ * Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
97
+ * @overload
98
+ * @param {Encoding} encoding encoding of the return value
99
+ * @returns {string} digest
100
+ */
101
+ /**
102
+ * Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
103
+ * @param {Encoding=} encoding encoding of the return value
104
+ * @returns {string | Buffer} digest
76
105
  */
77
106
  digest(encoding) {
78
107
  let digestCache;
@@ -91,9 +120,19 @@ class BulkUpdateDecorator extends Hash {
91
120
  if (buffer.length > 0) {
92
121
  this.hash.update(buffer);
93
122
  }
123
+ if (!encoding) {
124
+ const result = this.hash.digest();
125
+ if (digestCache !== undefined) {
126
+ digestCache.set(buffer, result);
127
+ }
128
+ return result;
129
+ }
94
130
  const digestResult = this.hash.digest(encoding);
131
+ // Compatibility with the old hash library
95
132
  const result =
96
- typeof digestResult === "string" ? digestResult : digestResult.toString();
133
+ typeof digestResult === "string"
134
+ ? digestResult
135
+ : /** @type {NodeJS.TypedArray} */ (digestResult).toString();
97
136
  if (digestCache !== undefined) {
98
137
  digestCache.set(buffer, result);
99
138
  }
@@ -110,9 +149,22 @@ class DebugHash extends Hash {
110
149
 
111
150
  /**
112
151
  * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
113
- * @param {string|Buffer} data data
114
- * @param {string=} inputEncoding data encoding
115
- * @returns {this} updated hash
152
+ * @overload
153
+ * @param {string | Buffer} data data
154
+ * @returns {Hash} updated hash
155
+ */
156
+ /**
157
+ * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
158
+ * @overload
159
+ * @param {string} data data
160
+ * @param {Encoding} inputEncoding data encoding
161
+ * @returns {Hash} updated hash
162
+ */
163
+ /**
164
+ * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
165
+ * @param {string | Buffer} data data
166
+ * @param {Encoding=} inputEncoding data encoding
167
+ * @returns {Hash} updated hash
116
168
  */
117
169
  update(data, inputEncoding) {
118
170
  if (typeof data !== "string") data = data.toString("utf8");
@@ -132,8 +184,19 @@ class DebugHash extends Hash {
132
184
 
133
185
  /**
134
186
  * Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
135
- * @param {string=} encoding encoding of the return value
136
- * @returns {string|Buffer} digest
187
+ * @overload
188
+ * @returns {Buffer} digest
189
+ */
190
+ /**
191
+ * Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
192
+ * @overload
193
+ * @param {Encoding} encoding encoding of the return value
194
+ * @returns {string} digest
195
+ */
196
+ /**
197
+ * Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
198
+ * @param {Encoding=} encoding encoding of the return value
199
+ * @returns {string | Buffer} digest
137
200
  */
138
201
  digest(encoding) {
139
202
  return Buffer.from(`@webpack-debug-digest@${this.string}`).toString("hex");
@@ -186,14 +249,21 @@ module.exports = (algorithm) => {
186
249
  case "native-md4":
187
250
  if (crypto === undefined) crypto = require("crypto");
188
251
  return new BulkUpdateDecorator(
189
- () => /** @type {typeof import("crypto")} */ (crypto).createHash("md4"),
252
+ () =>
253
+ /** @type {Hash} */ (
254
+ /** @type {typeof import("crypto")} */
255
+ (crypto).createHash("md4")
256
+ ),
190
257
  "md4"
191
258
  );
192
259
  default:
193
260
  if (crypto === undefined) crypto = require("crypto");
194
261
  return new BulkUpdateDecorator(
195
262
  () =>
196
- /** @type {typeof import("crypto")} */ (crypto).createHash(algorithm),
263
+ /** @type {Hash} */ (
264
+ /** @type {typeof import("crypto")} */
265
+ (crypto).createHash(algorithm)
266
+ ),
197
267
  algorithm
198
268
  );
199
269
  }
@@ -8,6 +8,8 @@
8
8
  const Hash = require("../Hash");
9
9
  const MAX_SHORT_STRING = require("./wasm-hash").MAX_SHORT_STRING;
10
10
 
11
+ /** @typedef {import("../../../declarations/WebpackOptions").HashDigest} Encoding */
12
+
11
13
  class BatchedHash extends Hash {
12
14
  /**
13
15
  * @param {Hash} hash hash
@@ -21,9 +23,22 @@ class BatchedHash extends Hash {
21
23
 
22
24
  /**
23
25
  * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
24
- * @param {string|Buffer} data data
25
- * @param {string=} inputEncoding data encoding
26
- * @returns {this} updated hash
26
+ * @overload
27
+ * @param {string | Buffer} data data
28
+ * @returns {Hash} updated hash
29
+ */
30
+ /**
31
+ * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
32
+ * @overload
33
+ * @param {string} data data
34
+ * @param {Encoding} inputEncoding data encoding
35
+ * @returns {Hash} updated hash
36
+ */
37
+ /**
38
+ * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
39
+ * @param {string | Buffer} data data
40
+ * @param {Encoding=} inputEncoding data encoding
41
+ * @returns {Hash} updated hash
27
42
  */
28
43
  update(data, inputEncoding) {
29
44
  if (this.string !== undefined) {
@@ -35,7 +50,11 @@ class BatchedHash extends Hash {
35
50
  this.string += data;
36
51
  return this;
37
52
  }
38
- this.hash.update(this.string, this.encoding);
53
+ if (this.encoding) {
54
+ this.hash.update(this.string, this.encoding);
55
+ } else {
56
+ this.hash.update(this.string);
57
+ }
39
58
  this.string = undefined;
40
59
  }
41
60
  if (typeof data === "string") {
@@ -46,8 +65,10 @@ class BatchedHash extends Hash {
46
65
  ) {
47
66
  this.string = data;
48
67
  this.encoding = inputEncoding;
49
- } else {
68
+ } else if (inputEncoding) {
50
69
  this.hash.update(data, inputEncoding);
70
+ } else {
71
+ this.hash.update(data);
51
72
  }
52
73
  } else {
53
74
  this.hash.update(data);
@@ -57,12 +78,30 @@ class BatchedHash extends Hash {
57
78
 
58
79
  /**
59
80
  * Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
60
- * @param {string=} encoding encoding of the return value
61
- * @returns {string|Buffer} digest
81
+ * @overload
82
+ * @returns {Buffer} digest
83
+ */
84
+ /**
85
+ * Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
86
+ * @overload
87
+ * @param {Encoding} encoding encoding of the return value
88
+ * @returns {string} digest
89
+ */
90
+ /**
91
+ * Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
92
+ * @param {Encoding=} encoding encoding of the return value
93
+ * @returns {string | Buffer} digest
62
94
  */
63
95
  digest(encoding) {
64
96
  if (this.string !== undefined) {
65
- this.hash.update(this.string, this.encoding);
97
+ if (this.encoding) {
98
+ this.hash.update(this.string, this.encoding);
99
+ } else {
100
+ this.hash.update(this.string);
101
+ }
102
+ }
103
+ if (!encoding) {
104
+ return this.hash.digest();
66
105
  }
67
106
  return this.hash.digest(encoding);
68
107
  }