webpack 5.98.0 → 5.99.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 (251) hide show
  1. package/README.md +7 -3
  2. package/lib/AsyncDependenciesBlock.js +3 -1
  3. package/lib/BannerPlugin.js +1 -1
  4. package/lib/Cache.js +9 -7
  5. package/lib/CacheFacade.js +5 -5
  6. package/lib/Chunk.js +2 -2
  7. package/lib/ChunkGraph.js +21 -16
  8. package/lib/ChunkTemplate.js +6 -6
  9. package/lib/CleanPlugin.js +10 -10
  10. package/lib/CodeGenerationResults.js +4 -3
  11. package/lib/CompatibilityPlugin.js +4 -1
  12. package/lib/Compilation.js +326 -152
  13. package/lib/Compiler.js +13 -18
  14. package/lib/ConditionalInitFragment.js +1 -1
  15. package/lib/ConstPlugin.js +5 -3
  16. package/lib/ContextModule.js +4 -2
  17. package/lib/ContextModuleFactory.js +3 -3
  18. package/lib/ContextReplacementPlugin.js +43 -16
  19. package/lib/DefinePlugin.js +25 -24
  20. package/lib/DelegatedModule.js +4 -2
  21. package/lib/DelegatedModuleFactoryPlugin.js +2 -1
  22. package/lib/Dependency.js +19 -13
  23. package/lib/DependencyTemplates.js +4 -3
  24. package/lib/DllModule.js +4 -2
  25. package/lib/DllModuleFactory.js +2 -2
  26. package/lib/DllReferencePlugin.js +2 -1
  27. package/lib/DynamicEntryPlugin.js +1 -1
  28. package/lib/EnvironmentPlugin.js +4 -2
  29. package/lib/ExportsInfo.js +72 -40
  30. package/lib/ExternalModule.js +14 -5
  31. package/lib/ExternalModuleFactoryPlugin.js +24 -12
  32. package/lib/FileSystemInfo.js +129 -94
  33. package/lib/FlagDependencyExportsPlugin.js +6 -4
  34. package/lib/FlagDependencyUsagePlugin.js +1 -1
  35. package/lib/Generator.js +29 -1
  36. package/lib/HookWebpackError.js +2 -2
  37. package/lib/HotModuleReplacementPlugin.js +3 -9
  38. package/lib/IgnoreErrorModuleFactory.js +2 -2
  39. package/lib/IgnorePlugin.js +0 -5
  40. package/lib/InitFragment.js +1 -1
  41. package/lib/LoaderOptionsPlugin.js +8 -5
  42. package/lib/MainTemplate.js +7 -7
  43. package/lib/Module.js +40 -17
  44. package/lib/ModuleBuildError.js +3 -1
  45. package/lib/ModuleDependencyError.js +4 -3
  46. package/lib/ModuleDependencyWarning.js +4 -3
  47. package/lib/ModuleFactory.js +9 -3
  48. package/lib/ModuleFilenameHelpers.js +13 -13
  49. package/lib/ModuleGraph.js +20 -14
  50. package/lib/ModuleGraphConnection.js +7 -13
  51. package/lib/ModuleNotFoundError.js +1 -1
  52. package/lib/ModuleParseError.js +2 -1
  53. package/lib/ModuleSourceTypesConstants.js +11 -0
  54. package/lib/ModuleTemplate.js +5 -5
  55. package/lib/ModuleTypeConstants.js +15 -0
  56. package/lib/MultiCompiler.js +4 -4
  57. package/lib/MultiStats.js +1 -1
  58. package/lib/NormalModule.js +101 -54
  59. package/lib/NormalModuleFactory.js +38 -33
  60. package/lib/NormalModuleReplacementPlugin.js +3 -2
  61. package/lib/NullFactory.js +2 -2
  62. package/lib/Parser.js +4 -3
  63. package/lib/ProgressPlugin.js +1 -2
  64. package/lib/RawModule.js +4 -2
  65. package/lib/RecordIdsPlugin.js +6 -2
  66. package/lib/RequestShortener.js +3 -1
  67. package/lib/ResolverFactory.js +12 -9
  68. package/lib/RuntimeModule.js +4 -2
  69. package/lib/RuntimeTemplate.js +2 -1
  70. package/lib/SelfModuleFactory.js +2 -2
  71. package/lib/SourceMapDevToolPlugin.js +0 -8
  72. package/lib/Template.js +6 -5
  73. package/lib/TemplatedPathPlugin.js +15 -12
  74. package/lib/WebpackOptionsApply.js +1 -1
  75. package/lib/asset/AssetGenerator.js +237 -170
  76. package/lib/asset/AssetModulesPlugin.js +50 -8
  77. package/lib/asset/AssetSourceGenerator.js +18 -0
  78. package/lib/asset/RawDataUrlModule.js +4 -2
  79. package/lib/buildChunkGraph.js +14 -4
  80. package/lib/cache/MemoryWithGcCachePlugin.js +6 -2
  81. package/lib/cache/PackFileCacheStrategy.js +137 -121
  82. package/lib/cache/ResolverCachePlugin.js +15 -7
  83. package/lib/cache/getLazyHashedEtag.js +4 -3
  84. package/lib/cli.js +23 -15
  85. package/lib/config/defaults.js +93 -26
  86. package/lib/config/normalization.js +14 -13
  87. package/lib/config/target.js +8 -8
  88. package/lib/container/ContainerEntryModule.js +4 -2
  89. package/lib/container/ContainerEntryModuleFactory.js +2 -2
  90. package/lib/container/FallbackModule.js +4 -2
  91. package/lib/container/FallbackModuleFactory.js +2 -2
  92. package/lib/container/RemoteModule.js +4 -2
  93. package/lib/container/options.js +5 -5
  94. package/lib/css/CssGenerator.js +71 -9
  95. package/lib/css/CssModulesPlugin.js +30 -5
  96. package/lib/css/CssParser.js +37 -17
  97. package/lib/css/walkCssTokens.js +17 -17
  98. package/lib/debug/ProfilingPlugin.js +98 -38
  99. package/lib/dependencies/AMDDefineDependencyParserPlugin.js +10 -4
  100. package/lib/dependencies/AMDPlugin.js +5 -2
  101. package/lib/dependencies/AMDRequireArrayDependency.js +4 -3
  102. package/lib/dependencies/AMDRequireContextDependency.js +2 -1
  103. package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +15 -7
  104. package/lib/dependencies/AMDRuntimeModules.js +3 -1
  105. package/lib/dependencies/CommonJsExportRequireDependency.js +4 -5
  106. package/lib/dependencies/CommonJsExportsParserPlugin.js +3 -3
  107. package/lib/dependencies/CommonJsImportsParserPlugin.js +8 -7
  108. package/lib/dependencies/CommonJsRequireContextDependency.js +2 -1
  109. package/lib/dependencies/ContextDependencyHelpers.js +13 -6
  110. package/lib/dependencies/CssIcssExportDependency.js +15 -12
  111. package/lib/dependencies/CssIcssImportDependency.js +4 -1
  112. package/lib/dependencies/CssLocalIdentifierDependency.js +17 -14
  113. package/lib/dependencies/ExportsInfoDependency.js +6 -1
  114. package/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js +5 -5
  115. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +22 -15
  116. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +23 -8
  117. package/lib/dependencies/HarmonyExportSpecifierDependency.js +2 -2
  118. package/lib/dependencies/HarmonyImportDependency.js +8 -6
  119. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +19 -14
  120. package/lib/dependencies/HarmonyImportSpecifierDependency.js +5 -5
  121. package/lib/dependencies/ImportContextDependency.js +2 -1
  122. package/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +8 -4
  123. package/lib/dependencies/JsonExportsDependency.js +24 -8
  124. package/lib/dependencies/LoaderPlugin.js +4 -14
  125. package/lib/dependencies/RequireContextDependency.js +2 -1
  126. package/lib/dependencies/RequireContextDependencyParserPlugin.js +6 -3
  127. package/lib/dependencies/RequireEnsureDependenciesBlock.js +3 -2
  128. package/lib/dependencies/RequireEnsureDependenciesBlockParserPlugin.js +11 -5
  129. package/lib/dependencies/RequireResolveContextDependency.js +1 -1
  130. package/lib/dependencies/WebAssemblyExportImportedDependency.js +1 -1
  131. package/lib/dependencies/WorkerDependency.js +6 -3
  132. package/lib/dependencies/WorkerPlugin.js +100 -41
  133. package/lib/esm/ModuleChunkFormatPlugin.js +5 -0
  134. package/lib/hmr/HotModuleReplacement.runtime.js +1 -0
  135. package/lib/hmr/JavascriptHotModuleReplacement.runtime.js +1 -0
  136. package/lib/hmr/LazyCompilationPlugin.js +32 -24
  137. package/lib/hmr/lazyCompilationBackend.js +1 -1
  138. package/lib/ids/DeterministicModuleIdsPlugin.js +1 -1
  139. package/lib/ids/HashedModuleIdsPlugin.js +2 -1
  140. package/lib/ids/IdHelpers.js +15 -14
  141. package/lib/ids/SyncModuleIdsPlugin.js +9 -5
  142. package/lib/index.js +5 -5
  143. package/lib/javascript/BasicEvaluatedExpression.js +6 -6
  144. package/lib/javascript/JavascriptGenerator.js +11 -1
  145. package/lib/javascript/JavascriptModulesPlugin.js +51 -31
  146. package/lib/javascript/JavascriptParser.js +272 -188
  147. package/lib/javascript/JavascriptParserHelpers.js +10 -9
  148. package/lib/javascript/StartupHelpers.js +4 -1
  149. package/lib/json/JsonData.js +4 -4
  150. package/lib/json/JsonGenerator.js +54 -22
  151. package/lib/json/JsonModulesPlugin.js +16 -2
  152. package/lib/json/JsonParser.js +8 -4
  153. package/lib/library/AbstractLibraryPlugin.js +7 -3
  154. package/lib/library/AssignLibraryPlugin.js +29 -1
  155. package/lib/library/EnableLibraryPlugin.js +7 -10
  156. package/lib/library/ExportPropertyLibraryPlugin.js +4 -1
  157. package/lib/library/ModuleLibraryPlugin.js +121 -15
  158. package/lib/logging/Logger.js +2 -2
  159. package/lib/logging/createConsoleLogger.js +4 -4
  160. package/lib/node/NodeEnvironmentPlugin.js +6 -2
  161. package/lib/node/ReadFileCompileAsyncWasmPlugin.js +1 -2
  162. package/lib/node/ReadFileCompileWasmPlugin.js +1 -2
  163. package/lib/node/nodeConsole.js +3 -1
  164. package/lib/optimize/AggressiveSplittingPlugin.js +1 -1
  165. package/lib/optimize/ConcatenatedModule.js +19 -12
  166. package/lib/optimize/InnerGraph.js +3 -2
  167. package/lib/optimize/InnerGraphPlugin.js +13 -7
  168. package/lib/optimize/LimitChunkCountPlugin.js +20 -0
  169. package/lib/optimize/ModuleConcatenationPlugin.js +10 -7
  170. package/lib/optimize/RealContentHashPlugin.js +8 -4
  171. package/lib/optimize/SideEffectsFlagPlugin.js +4 -2
  172. package/lib/optimize/SplitChunksPlugin.js +87 -65
  173. package/lib/rules/BasicEffectRulePlugin.js +9 -1
  174. package/lib/rules/BasicMatcherRulePlugin.js +15 -4
  175. package/lib/rules/ObjectMatcherRulePlugin.js +12 -3
  176. package/lib/rules/RuleSetCompiler.js +25 -14
  177. package/lib/rules/UseEffectRulePlugin.js +47 -17
  178. package/lib/runtime/GetChunkFilenameRuntimeModule.js +6 -6
  179. package/lib/schemes/HttpUriPlugin.js +38 -17
  180. package/lib/serialization/BinaryMiddleware.js +52 -19
  181. package/lib/serialization/FileMiddleware.js +78 -48
  182. package/lib/serialization/ObjectMiddleware.js +78 -29
  183. package/lib/serialization/PlainObjectSerializer.js +1 -1
  184. package/lib/serialization/Serializer.js +15 -10
  185. package/lib/serialization/SerializerMiddleware.js +80 -41
  186. package/lib/serialization/SingleItemMiddleware.js +10 -7
  187. package/lib/serialization/types.js +1 -1
  188. package/lib/sharing/ConsumeSharedModule.js +4 -2
  189. package/lib/sharing/ProvideSharedModule.js +4 -2
  190. package/lib/sharing/ProvideSharedModuleFactory.js +5 -3
  191. package/lib/sharing/utils.js +2 -2
  192. package/lib/stats/DefaultStatsFactoryPlugin.js +80 -78
  193. package/lib/stats/DefaultStatsPresetPlugin.js +43 -23
  194. package/lib/stats/DefaultStatsPrinterPlugin.js +85 -43
  195. package/lib/stats/StatsFactory.js +11 -11
  196. package/lib/stats/StatsPrinter.js +7 -7
  197. package/lib/util/ArrayHelpers.js +2 -4
  198. package/lib/util/ArrayQueue.js +1 -1
  199. package/lib/util/AsyncQueue.js +4 -4
  200. package/lib/util/IterableHelpers.js +1 -1
  201. package/lib/util/LazyBucketSortedSet.js +41 -23
  202. package/lib/util/LazySet.js +3 -2
  203. package/lib/util/MapHelpers.js +1 -1
  204. package/lib/util/ParallelismFactorCalculator.js +1 -1
  205. package/lib/util/Semaphore.js +3 -3
  206. package/lib/util/SetHelpers.js +1 -1
  207. package/lib/util/SortableSet.js +9 -7
  208. package/lib/util/TupleQueue.js +9 -8
  209. package/lib/util/TupleSet.js +2 -2
  210. package/lib/util/WeakTupleMap.js +12 -11
  211. package/lib/util/binarySearchBounds.js +2 -1
  212. package/lib/util/cleverMerge.js +84 -54
  213. package/lib/util/comparators.js +22 -21
  214. package/lib/util/compileBooleanMatcher.js +3 -3
  215. package/lib/util/concatenate.js +6 -4
  216. package/lib/util/create-schema-validation.js +4 -4
  217. package/lib/util/createHash.js +2 -2
  218. package/lib/util/deprecation.js +35 -33
  219. package/lib/util/deterministicGrouping.js +6 -6
  220. package/lib/util/findGraphRoots.js +1 -1
  221. package/lib/util/fs.js +39 -39
  222. package/lib/util/hash/wasm-hash.js +2 -2
  223. package/lib/util/identifier.js +15 -18
  224. package/lib/util/makeSerializable.js +1 -1
  225. package/lib/util/memoize.js +4 -1
  226. package/lib/util/objectToMap.js +3 -2
  227. package/lib/util/processAsyncTree.js +2 -2
  228. package/lib/util/propertyName.js +0 -1
  229. package/lib/util/registerExternalSerializer.js +15 -18
  230. package/lib/util/removeBOM.js +25 -0
  231. package/lib/util/runtime.js +34 -27
  232. package/lib/util/serialization.js +5 -16
  233. package/lib/util/smartGrouping.js +3 -3
  234. package/lib/wasm-async/AsyncWasmLoadingRuntimeModule.js +3 -3
  235. package/lib/wasm-async/AsyncWebAssemblyGenerator.js +11 -0
  236. package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +10 -0
  237. package/lib/wasm-async/UniversalCompileAsyncWasmPlugin.js +4 -0
  238. package/lib/wasm-sync/WebAssemblyGenerator.js +28 -12
  239. package/lib/wasm-sync/WebAssemblyJavascriptGenerator.js +10 -0
  240. package/lib/wasm-sync/WebAssemblyParser.js +9 -4
  241. package/lib/webworker/ImportScriptsChunkLoadingPlugin.js +3 -0
  242. package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +30 -25
  243. package/package.json +19 -17
  244. package/schemas/WebpackOptions.check.js +1 -1
  245. package/schemas/WebpackOptions.json +48 -13
  246. package/schemas/plugins/JsonModulesPluginGenerator.check.d.ts +7 -0
  247. package/schemas/plugins/JsonModulesPluginGenerator.check.js +6 -0
  248. package/schemas/plugins/JsonModulesPluginGenerator.json +11 -0
  249. package/schemas/plugins/SourceMapDevToolPlugin.json +2 -2
  250. package/types.d.ts +918 -615
  251. package/lib/library/ModernModuleLibraryPlugin.js +0 -144
@@ -32,7 +32,9 @@ const nonNumericOnlyHash = require("../util/nonNumericOnlyHash");
32
32
  /** @typedef {import("../../declarations/WebpackOptions").AssetGeneratorOptions} AssetGeneratorOptions */
33
33
  /** @typedef {import("../../declarations/WebpackOptions").AssetModuleFilename} AssetModuleFilename */
34
34
  /** @typedef {import("../../declarations/WebpackOptions").AssetModuleOutputPath} AssetModuleOutputPath */
35
+ /** @typedef {import("../../declarations/WebpackOptions").AssetResourceGeneratorOptions} AssetResourceGeneratorOptions */
35
36
  /** @typedef {import("../../declarations/WebpackOptions").RawPublicPath} RawPublicPath */
37
+ /** @typedef {import("../ChunkGraph")} ChunkGraph */
36
38
  /** @typedef {import("../Compilation")} Compilation */
37
39
  /** @typedef {import("../Compilation").AssetInfo} AssetInfo */
38
40
  /** @typedef {import("../Compilation").InterpolatedPathAndAssetInfo} InterpolatedPathAndAssetInfo */
@@ -50,12 +52,13 @@ const nonNumericOnlyHash = require("../util/nonNumericOnlyHash");
50
52
  /** @typedef {import("../TemplatedPathPlugin").TemplatePath} TemplatePath */
51
53
  /** @typedef {import("../util/Hash")} Hash */
52
54
  /** @typedef {import("../util/createHash").Algorithm} Algorithm */
55
+ /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
53
56
 
54
57
  /**
55
58
  * @template T
56
59
  * @template U
57
- * @param {Array<T> | Set<T>} a a
58
- * @param {Array<U> | Set<U>} b b
60
+ * @param {string | Array<T> | Set<T> | undefined} a a
61
+ * @param {string | Array<U> | Set<U> | undefined} b b
59
62
  * @returns {Array<T> & Array<U>} array
60
63
  */
61
64
  const mergeMaybeArrays = (a, b) => {
@@ -68,13 +71,12 @@ const mergeMaybeArrays = (a, b) => {
68
71
  };
69
72
 
70
73
  /**
71
- * @template {object} T
72
- * @template {object} U
73
- * @param {TODO} a a
74
- * @param {TODO} b b
75
- * @returns {T & U} object
74
+ * @param {AssetInfo} a a
75
+ * @param {AssetInfo} b b
76
+ * @returns {AssetInfo} object
76
77
  */
77
78
  const mergeAssetInfo = (a, b) => {
79
+ /** @type {AssetInfo} */
78
80
  const result = { ...a, ...b };
79
81
  for (const key of Object.keys(a)) {
80
82
  if (key in b) {
@@ -93,7 +95,12 @@ const mergeAssetInfo = (a, b) => {
93
95
  result[key] = a[key] || b[key];
94
96
  break;
95
97
  case "related":
96
- result[key] = mergeRelatedInfo(a[key], b[key]);
98
+ result[key] = mergeRelatedInfo(
99
+ /** @type {NonNullable<AssetInfo["related"]>} */
100
+ (a[key]),
101
+ /** @type {NonNullable<AssetInfo["related"]>} */
102
+ (b[key])
103
+ );
97
104
  break;
98
105
  default:
99
106
  throw new Error(`Can't handle conflicting asset info for ${key}`);
@@ -104,11 +111,9 @@ const mergeAssetInfo = (a, b) => {
104
111
  };
105
112
 
106
113
  /**
107
- * @template {object} T
108
- * @template {object} U
109
- * @param {TODO} a a
110
- * @param {TODO} b b
111
- * @returns {T & U} object
114
+ * @param {NonNullable<AssetInfo["related"]>} a a
115
+ * @param {NonNullable<AssetInfo["related"]>} b b
116
+ * @returns {NonNullable<AssetInfo["related"]>} object
112
117
  */
113
118
  const mergeRelatedInfo = (a, b) => {
114
119
  const result = { ...a, ...b };
@@ -212,7 +217,7 @@ class AssetGenerator extends Generator {
212
217
  * @param {RuntimeTemplate} runtimeTemplate runtime template
213
218
  * @returns {string} source file name
214
219
  */
215
- getSourceFileName(module, runtimeTemplate) {
220
+ static getSourceFileName(module, runtimeTemplate) {
216
221
  return makePathsRelative(
217
222
  runtimeTemplate.compilation.compiler.context,
218
223
  module.matchResource || module.resource,
@@ -220,6 +225,180 @@ class AssetGenerator extends Generator {
220
225
  ).replace(/^\.\//, "");
221
226
  }
222
227
 
228
+ /**
229
+ * @param {NormalModule} module module
230
+ * @param {RuntimeTemplate} runtimeTemplate runtime template
231
+ * @returns {[string, string]} return full hash and non-numeric full hash
232
+ */
233
+ static getFullContentHash(module, runtimeTemplate) {
234
+ const hash = createHash(
235
+ /** @type {Algorithm} */
236
+ (runtimeTemplate.outputOptions.hashFunction)
237
+ );
238
+
239
+ if (runtimeTemplate.outputOptions.hashSalt) {
240
+ hash.update(runtimeTemplate.outputOptions.hashSalt);
241
+ }
242
+
243
+ const source = module.originalSource();
244
+
245
+ if (source) {
246
+ hash.update(source.buffer());
247
+ }
248
+
249
+ if (module.error) {
250
+ hash.update(module.error.toString());
251
+ }
252
+
253
+ const fullContentHash = /** @type {string} */ (
254
+ hash.digest(runtimeTemplate.outputOptions.hashDigest)
255
+ );
256
+
257
+ /** @type {string} */
258
+ const contentHash = nonNumericOnlyHash(
259
+ fullContentHash,
260
+ /** @type {number} */
261
+ (runtimeTemplate.outputOptions.hashDigestLength)
262
+ );
263
+
264
+ return [fullContentHash, contentHash];
265
+ }
266
+
267
+ /**
268
+ * @param {NormalModule} module module for which the code should be generated
269
+ * @param {Pick<AssetResourceGeneratorOptions, "filename" | "outputPath">} generatorOptions generator options
270
+ * @param {{ runtime: RuntimeSpec, runtimeTemplate: RuntimeTemplate, chunkGraph: ChunkGraph }} generateContext context for generate
271
+ * @param {string} contentHash the content hash
272
+ * @returns {{ filename: string, originalFilename: string, assetInfo: AssetInfo }} info
273
+ */
274
+ static getFilenameWithInfo(
275
+ module,
276
+ generatorOptions,
277
+ { runtime, runtimeTemplate, chunkGraph },
278
+ contentHash
279
+ ) {
280
+ const assetModuleFilename =
281
+ generatorOptions.filename ||
282
+ /** @type {AssetModuleFilename} */
283
+ (runtimeTemplate.outputOptions.assetModuleFilename);
284
+
285
+ const sourceFilename = AssetGenerator.getSourceFileName(
286
+ module,
287
+ runtimeTemplate
288
+ );
289
+ let { path: filename, info: assetInfo } =
290
+ runtimeTemplate.compilation.getAssetPathWithInfo(assetModuleFilename, {
291
+ module,
292
+ runtime,
293
+ filename: sourceFilename,
294
+ chunkGraph,
295
+ contentHash
296
+ });
297
+
298
+ const originalFilename = filename;
299
+
300
+ if (generatorOptions.outputPath) {
301
+ const { path: outputPath, info } =
302
+ runtimeTemplate.compilation.getAssetPathWithInfo(
303
+ generatorOptions.outputPath,
304
+ {
305
+ module,
306
+ runtime,
307
+ filename: sourceFilename,
308
+ chunkGraph,
309
+ contentHash
310
+ }
311
+ );
312
+ filename = path.posix.join(outputPath, filename);
313
+ assetInfo = mergeAssetInfo(assetInfo, info);
314
+ }
315
+
316
+ return { originalFilename, filename, assetInfo };
317
+ }
318
+
319
+ /**
320
+ * @param {NormalModule} module module for which the code should be generated
321
+ * @param {Pick<AssetResourceGeneratorOptions, "publicPath">} generatorOptions generator options
322
+ * @param {GenerateContext} generateContext context for generate
323
+ * @param {string} filename the filename
324
+ * @param {AssetInfo} assetInfo the asset info
325
+ * @param {string} contentHash the content hash
326
+ * @returns {{ assetPath: string, assetInfo: AssetInfo }} asset path and info
327
+ */
328
+ static getAssetPathWithInfo(
329
+ module,
330
+ generatorOptions,
331
+ { runtime, runtimeTemplate, type, chunkGraph, runtimeRequirements },
332
+ filename,
333
+ assetInfo,
334
+ contentHash
335
+ ) {
336
+ const sourceFilename = AssetGenerator.getSourceFileName(
337
+ module,
338
+ runtimeTemplate
339
+ );
340
+
341
+ let assetPath;
342
+
343
+ if (generatorOptions.publicPath !== undefined && type === "javascript") {
344
+ const { path, info } = runtimeTemplate.compilation.getAssetPathWithInfo(
345
+ generatorOptions.publicPath,
346
+ {
347
+ module,
348
+ runtime,
349
+ filename: sourceFilename,
350
+ chunkGraph,
351
+ contentHash
352
+ }
353
+ );
354
+ assetInfo = mergeAssetInfo(assetInfo, info);
355
+ assetPath = JSON.stringify(path + filename);
356
+ } else if (
357
+ generatorOptions.publicPath !== undefined &&
358
+ type === "css-url"
359
+ ) {
360
+ const { path, info } = runtimeTemplate.compilation.getAssetPathWithInfo(
361
+ generatorOptions.publicPath,
362
+ {
363
+ module,
364
+ runtime,
365
+ filename: sourceFilename,
366
+ chunkGraph,
367
+ contentHash
368
+ }
369
+ );
370
+ assetInfo = mergeAssetInfo(assetInfo, info);
371
+ assetPath = path + filename;
372
+ } else if (type === "javascript") {
373
+ // add __webpack_require__.p
374
+ runtimeRequirements.add(RuntimeGlobals.publicPath);
375
+ assetPath = runtimeTemplate.concatenation(
376
+ { expr: RuntimeGlobals.publicPath },
377
+ filename
378
+ );
379
+ } else if (type === "css-url") {
380
+ const compilation = runtimeTemplate.compilation;
381
+ const path =
382
+ compilation.outputOptions.publicPath === "auto"
383
+ ? CssUrlDependency.PUBLIC_PATH_AUTO
384
+ : compilation.getAssetPath(
385
+ /** @type {TemplatePath} */
386
+ (compilation.outputOptions.publicPath),
387
+ {
388
+ hash: compilation.hash
389
+ }
390
+ );
391
+
392
+ assetPath = path + filename;
393
+ }
394
+
395
+ return {
396
+ // eslint-disable-next-line object-shorthand
397
+ assetPath: /** @type {string} */ (assetPath),
398
+ assetInfo: { sourceFilename, ...assetInfo }
399
+ };
400
+ }
401
+
223
402
  /**
224
403
  * @param {NormalModule} module module for which the bailout reason should be determined
225
404
  * @param {ConcatenationBailoutReasonContext} context context
@@ -335,127 +514,6 @@ class AssetGenerator extends Generator {
335
514
  return encodedSource;
336
515
  }
337
516
 
338
- /**
339
- * @private
340
- * @param {NormalModule} module module for which the code should be generated
341
- * @param {GenerateContext} generateContext context for generate
342
- * @param {string} contentHash the content hash
343
- * @returns {{ filename: string, originalFilename: string, assetInfo: AssetInfo }} info
344
- */
345
- _getFilenameWithInfo(
346
- module,
347
- { runtime, runtimeTemplate, chunkGraph },
348
- contentHash
349
- ) {
350
- const assetModuleFilename =
351
- this.filename ||
352
- /** @type {AssetModuleFilename} */
353
- (runtimeTemplate.outputOptions.assetModuleFilename);
354
-
355
- const sourceFilename = this.getSourceFileName(module, runtimeTemplate);
356
- let { path: filename, info: assetInfo } =
357
- runtimeTemplate.compilation.getAssetPathWithInfo(assetModuleFilename, {
358
- module,
359
- runtime,
360
- filename: sourceFilename,
361
- chunkGraph,
362
- contentHash
363
- });
364
-
365
- const originalFilename = filename;
366
-
367
- if (this.outputPath) {
368
- const { path: outputPath, info } =
369
- runtimeTemplate.compilation.getAssetPathWithInfo(this.outputPath, {
370
- module,
371
- runtime,
372
- filename: sourceFilename,
373
- chunkGraph,
374
- contentHash
375
- });
376
- filename = path.posix.join(outputPath, filename);
377
- assetInfo = mergeAssetInfo(assetInfo, info);
378
- }
379
-
380
- return { originalFilename, filename, assetInfo };
381
- }
382
-
383
- /**
384
- * @private
385
- * @param {NormalModule} module module for which the code should be generated
386
- * @param {GenerateContext} generateContext context for generate
387
- * @param {string} filename the filename
388
- * @param {AssetInfo} assetInfo the asset info
389
- * @param {string} contentHash the content hash
390
- * @returns {{ assetPath: string, assetInfo: AssetInfo }} asset path and info
391
- */
392
- _getAssetPathWithInfo(
393
- module,
394
- { runtimeTemplate, runtime, chunkGraph, type, runtimeRequirements },
395
- filename,
396
- assetInfo,
397
- contentHash
398
- ) {
399
- const sourceFilename = this.getSourceFileName(module, runtimeTemplate);
400
-
401
- let assetPath;
402
-
403
- if (this.publicPath !== undefined && type === "javascript") {
404
- const { path, info } = runtimeTemplate.compilation.getAssetPathWithInfo(
405
- this.publicPath,
406
- {
407
- module,
408
- runtime,
409
- filename: sourceFilename,
410
- chunkGraph,
411
- contentHash
412
- }
413
- );
414
- assetInfo = mergeAssetInfo(assetInfo, info);
415
- assetPath = JSON.stringify(path + filename);
416
- } else if (this.publicPath !== undefined && type === "css-url") {
417
- const { path, info } = runtimeTemplate.compilation.getAssetPathWithInfo(
418
- this.publicPath,
419
- {
420
- module,
421
- runtime,
422
- filename: sourceFilename,
423
- chunkGraph,
424
- contentHash
425
- }
426
- );
427
- assetInfo = mergeAssetInfo(assetInfo, info);
428
- assetPath = path + filename;
429
- } else if (type === "javascript") {
430
- // add __webpack_require__.p
431
- runtimeRequirements.add(RuntimeGlobals.publicPath);
432
- assetPath = runtimeTemplate.concatenation(
433
- { expr: RuntimeGlobals.publicPath },
434
- filename
435
- );
436
- } else if (type === "css-url") {
437
- const compilation = runtimeTemplate.compilation;
438
- const path =
439
- compilation.outputOptions.publicPath === "auto"
440
- ? CssUrlDependency.PUBLIC_PATH_AUTO
441
- : compilation.getAssetPath(
442
- /** @type {TemplatePath} */
443
- (compilation.outputOptions.publicPath),
444
- {
445
- hash: compilation.hash
446
- }
447
- );
448
-
449
- assetPath = path + filename;
450
- }
451
-
452
- return {
453
- // eslint-disable-next-line object-shorthand
454
- assetPath: /** @type {string} */ (assetPath),
455
- assetInfo: { sourceFilename, ...assetInfo }
456
- };
457
- }
458
-
459
517
  /**
460
518
  * @param {NormalModule} module module for which the code should be generated
461
519
  * @param {GenerateContext} generateContext context for generate
@@ -489,53 +547,40 @@ class AssetGenerator extends Generator {
489
547
  data.set("url", { [type]: content, ...data.get("url") });
490
548
  }
491
549
  } else {
492
- const hash = createHash(
493
- /** @type {Algorithm} */
494
- (runtimeTemplate.outputOptions.hashFunction)
550
+ const [fullContentHash, contentHash] = AssetGenerator.getFullContentHash(
551
+ module,
552
+ runtimeTemplate
495
553
  );
496
554
 
497
- if (runtimeTemplate.outputOptions.hashSalt) {
498
- hash.update(runtimeTemplate.outputOptions.hashSalt);
499
- }
500
-
501
- hash.update(/** @type {Source} */ (module.originalSource()).buffer());
502
-
503
- const fullHash =
504
- /** @type {string} */
505
- (hash.digest(runtimeTemplate.outputOptions.hashDigest));
506
-
507
555
  if (data) {
508
- data.set("fullContentHash", fullHash);
556
+ data.set("fullContentHash", fullContentHash);
557
+ data.set("contentHash", contentHash);
509
558
  }
510
559
 
511
560
  /** @type {BuildInfo} */
512
- (module.buildInfo).fullContentHash = fullHash;
513
-
514
- /** @type {string} */
515
- const contentHash = nonNumericOnlyHash(
516
- fullHash,
517
- /** @type {number} */
518
- (generateContext.runtimeTemplate.outputOptions.hashDigestLength)
519
- );
520
-
521
- if (data) {
522
- data.set("contentHash", contentHash);
523
- }
561
+ (module.buildInfo).fullContentHash = fullContentHash;
524
562
 
525
563
  const { originalFilename, filename, assetInfo } =
526
- this._getFilenameWithInfo(module, generateContext, contentHash);
564
+ AssetGenerator.getFilenameWithInfo(
565
+ module,
566
+ { filename: this.filename, outputPath: this.outputPath },
567
+ generateContext,
568
+ contentHash
569
+ );
527
570
 
528
571
  if (data) {
529
572
  data.set("filename", filename);
530
573
  }
531
574
 
532
- let { assetPath, assetInfo: newAssetInfo } = this._getAssetPathWithInfo(
533
- module,
534
- generateContext,
535
- originalFilename,
536
- assetInfo,
537
- contentHash
538
- );
575
+ let { assetPath, assetInfo: newAssetInfo } =
576
+ AssetGenerator.getAssetPathWithInfo(
577
+ module,
578
+ { publicPath: this.publicPath },
579
+ generateContext,
580
+ originalFilename,
581
+ assetInfo,
582
+ contentHash
583
+ );
539
584
 
540
585
  if (data && (type === "javascript" || type === "css-url")) {
541
586
  data.set("url", { [type]: assetPath, ...data.get("url") });
@@ -584,6 +629,27 @@ class AssetGenerator extends Generator {
584
629
  return /** @type {Source} */ (module.originalSource());
585
630
  }
586
631
 
632
+ /**
633
+ * @param {Error} error the error
634
+ * @param {NormalModule} module module for which the code should be generated
635
+ * @param {GenerateContext} generateContext context for generate
636
+ * @returns {Source | null} generated code
637
+ */
638
+ generateError(error, module, generateContext) {
639
+ switch (generateContext.type) {
640
+ case "asset": {
641
+ return new RawSource(error.message);
642
+ }
643
+ case "javascript": {
644
+ return new RawSource(
645
+ `throw new Error(${JSON.stringify(error.message)});`
646
+ );
647
+ }
648
+ default:
649
+ return null;
650
+ }
651
+ }
652
+
587
653
  /**
588
654
  * @param {NormalModule} module fresh module
589
655
  * @returns {SourceTypes} available types (do not mutate)
@@ -669,6 +735,7 @@ class AssetGenerator extends Generator {
669
735
  */
670
736
  updateHash(hash, updateHashContext) {
671
737
  const { module } = updateHashContext;
738
+
672
739
  if (
673
740
  /** @type {BuildInfo} */
674
741
  (module.buildInfo).dataUrl
@@ -704,7 +771,7 @@ class AssetGenerator extends Generator {
704
771
  const pathData = {
705
772
  module,
706
773
  runtime,
707
- filename: this.getSourceFileName(module, runtimeTemplate),
774
+ filename: AssetGenerator.getSourceFileName(module, runtimeTemplate),
708
775
  chunkGraph,
709
776
  contentHash: runtimeTemplate.contentHashReplacement
710
777
  };
@@ -19,10 +19,12 @@ const memoize = require("../util/memoize");
19
19
  /** @typedef {import("webpack-sources").Source} Source */
20
20
  /** @typedef {import("../../declarations/WebpackOptions").AssetParserOptions} AssetParserOptions */
21
21
  /** @typedef {import("../Chunk")} Chunk */
22
+ /** @typedef {import("../Compilation").AssetInfo} AssetInfo */
22
23
  /** @typedef {import("../Compiler")} Compiler */
23
24
  /** @typedef {import("../Module")} Module */
24
25
  /** @typedef {import("../Module").BuildInfo} BuildInfo */
25
26
  /** @typedef {import("../Module").CodeGenerationResult} CodeGenerationResult */
27
+ /** @typedef {import("../NormalModule")} NormalModule */
26
28
 
27
29
  /**
28
30
  * @param {string} name name of definitions
@@ -184,7 +186,7 @@ class AssetModulesPlugin {
184
186
 
185
187
  compilation.hooks.renderManifest.tap(plugin, (result, options) => {
186
188
  const { chunkGraph } = compilation;
187
- const { chunk, codeGenerationResults } = options;
189
+ const { chunk, codeGenerationResults, runtimeTemplate } = options;
188
190
 
189
191
  const modules = chunkGraph.getOrderedChunkModulesIterableBySourceType(
190
192
  chunk,
@@ -203,18 +205,58 @@ class AssetModulesPlugin {
203
205
  /** @type {NonNullable<CodeGenerationResult["data"]>} */
204
206
  (codeGenResult.data);
205
207
  const errored = module.getNumberOfErrors() > 0;
208
+
209
+ /** @type {string} */
210
+ let entryFilename;
211
+ /** @type {AssetInfo} */
212
+ let entryInfo;
213
+ /** @type {string} */
214
+ let entryHash;
215
+
216
+ if (errored) {
217
+ const erroredModule = /** @type {NormalModule} */ (module);
218
+ const AssetGenerator = getAssetGenerator();
219
+ const [fullContentHash, contentHash] =
220
+ AssetGenerator.getFullContentHash(
221
+ erroredModule,
222
+ runtimeTemplate
223
+ );
224
+ const { filename, assetInfo } =
225
+ AssetGenerator.getFilenameWithInfo(
226
+ erroredModule,
227
+ {
228
+ filename:
229
+ erroredModule.generatorOptions &&
230
+ erroredModule.generatorOptions.filename,
231
+ outputPath:
232
+ erroredModule.generatorOptions &&
233
+ erroredModule.generatorOptions.outputPath
234
+ },
235
+ {
236
+ runtime: chunk.runtime,
237
+ runtimeTemplate,
238
+ chunkGraph
239
+ },
240
+ contentHash
241
+ );
242
+ entryFilename = filename;
243
+ entryInfo = assetInfo;
244
+ entryHash = fullContentHash;
245
+ } else {
246
+ entryFilename = buildInfo.filename || data.get("filename");
247
+ entryInfo = buildInfo.assetInfo || data.get("assetInfo");
248
+ entryHash =
249
+ buildInfo.fullContentHash || data.get("fullContentHash");
250
+ }
251
+
206
252
  result.push({
207
253
  render: () =>
208
254
  /** @type {Source} */ (codeGenResult.sources.get(type)),
209
- filename: errored
210
- ? module.nameForCondition()
211
- : buildInfo.filename || data.get("filename"),
212
- info: buildInfo.assetInfo || data.get("assetInfo"),
255
+ filename: entryFilename,
256
+ info: entryInfo,
213
257
  auxiliary: true,
214
258
  identifier: `assetModule${chunkGraph.getModuleId(module)}`,
215
- hash: errored
216
- ? chunkGraph.getModuleHash(module, chunk.runtime)
217
- : buildInfo.fullContentHash || data.get("fullContentHash")
259
+ hash: entryHash
218
260
  });
219
261
  } catch (err) {
220
262
  /** @type {Error} */ (err).message +=
@@ -90,6 +90,24 @@ class AssetSourceGenerator extends Generator {
90
90
  }
91
91
  }
92
92
 
93
+ /**
94
+ * @param {Error} error the error
95
+ * @param {NormalModule} module module for which the code should be generated
96
+ * @param {GenerateContext} generateContext context for generate
97
+ * @returns {Source | null} generated code
98
+ */
99
+ generateError(error, module, generateContext) {
100
+ switch (generateContext.type) {
101
+ case "javascript": {
102
+ return new RawSource(
103
+ `throw new Error(${JSON.stringify(error.message)});`
104
+ );
105
+ }
106
+ default:
107
+ return null;
108
+ }
109
+ }
110
+
93
111
  /**
94
112
  * @param {NormalModule} module module for which the bailout reason should be determined
95
113
  * @param {ConcatenationBailoutReasonContext} context context
@@ -15,8 +15,10 @@ const makeSerializable = require("../util/makeSerializable");
15
15
  /** @typedef {import("../../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
16
16
  /** @typedef {import("../Compilation")} Compilation */
17
17
  /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
18
+ /** @typedef {import("../Module").BuildCallback} BuildCallback */
18
19
  /** @typedef {import("../Module").CodeGenerationContext} CodeGenerationContext */
19
20
  /** @typedef {import("../Module").CodeGenerationResult} CodeGenerationResult */
21
+ /** @typedef {import("../Module").NeedBuildCallback} NeedBuildCallback */
20
22
  /** @typedef {import("../Module").NeedBuildContext} NeedBuildContext */
21
23
  /** @typedef {import("../Module").SourceTypes} SourceTypes */
22
24
  /** @typedef {import("../RequestShortener")} RequestShortener */
@@ -77,7 +79,7 @@ class RawDataUrlModule extends Module {
77
79
 
78
80
  /**
79
81
  * @param {NeedBuildContext} context context info
80
- * @param {function((WebpackError | null)=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
82
+ * @param {NeedBuildCallback} callback callback function, returns true, if the module needs a rebuild
81
83
  * @returns {void}
82
84
  */
83
85
  needBuild(context, callback) {
@@ -89,7 +91,7 @@ class RawDataUrlModule extends Module {
89
91
  * @param {Compilation} compilation the compilation
90
92
  * @param {ResolverWithOptions} resolver the resolver
91
93
  * @param {InputFileSystem} fs the file system
92
- * @param {function(WebpackError=): void} callback callback function
94
+ * @param {BuildCallback} callback callback function
93
95
  * @returns {void}
94
96
  */
95
97
  build(options, compilation, resolver, fs, callback) {
@@ -508,7 +508,9 @@ const visitModules = (
508
508
  cgi = {
509
509
  chunkGroup: entrypoint,
510
510
  initialized: false,
511
- runtime: entrypoint.options.runtime || entrypoint.name,
511
+ runtime:
512
+ entrypoint.options.runtime ||
513
+ /** @type {string | undefined} */ (entrypoint.name),
512
514
  minAvailableModules: ZERO_BIGINT,
513
515
  availableModulesToBeMerged: [],
514
516
  skippedItems: undefined,
@@ -527,11 +529,19 @@ const visitModules = (
527
529
  ? entryOptions.asyncChunks
528
530
  : chunkGroupInfo.asyncChunks
529
531
  };
530
- chunkGroupInfoMap.set(entrypoint, cgi);
532
+ chunkGroupInfoMap.set(
533
+ entrypoint,
534
+ /** @type {ChunkGroupInfo} */
535
+ (cgi)
536
+ );
531
537
 
532
538
  chunkGraph.connectBlockAndChunkGroup(b, entrypoint);
533
539
  if (chunkName) {
534
- namedAsyncEntrypoints.set(chunkName, cgi);
540
+ namedAsyncEntrypoints.set(
541
+ chunkName,
542
+ /** @type {ChunkGroupInfo} */
543
+ (cgi)
544
+ );
535
545
  }
536
546
  } else {
537
547
  entrypoint = /** @type {Entrypoint} */ (cgi.chunkGroup);
@@ -551,7 +561,7 @@ const visitModules = (
551
561
  module,
552
562
  chunk: entrypoint.chunks[0],
553
563
  chunkGroup: entrypoint,
554
- chunkGroupInfo: cgi
564
+ chunkGroupInfo: /** @type {ChunkGroupInfo} */ (cgi)
555
565
  });
556
566
  } else if (!chunkGroupInfo.asyncChunks || !chunkGroupInfo.chunkLoading) {
557
567
  // Just queue the block into the current chunk group