webpack 5.95.0 → 5.96.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 (145) hide show
  1. package/README.md +1 -1
  2. package/lib/AsyncDependenciesBlock.js +1 -1
  3. package/lib/BannerPlugin.js +2 -1
  4. package/lib/Chunk.js +30 -0
  5. package/lib/ChunkGraph.js +11 -6
  6. package/lib/ChunkGroup.js +2 -2
  7. package/lib/CleanPlugin.js +4 -5
  8. package/lib/CodeGenerationResults.js +6 -5
  9. package/lib/Compilation.js +71 -48
  10. package/lib/Compiler.js +7 -5
  11. package/lib/ConcatenationScope.js +7 -20
  12. package/lib/ContextModule.js +7 -8
  13. package/lib/CssModule.js +25 -21
  14. package/lib/DefinePlugin.js +14 -8
  15. package/lib/DelegatedModule.js +3 -3
  16. package/lib/DllModule.js +4 -4
  17. package/lib/DynamicEntryPlugin.js +29 -22
  18. package/lib/EvalDevToolModulePlugin.js +5 -2
  19. package/lib/EvalSourceMapDevToolPlugin.js +5 -2
  20. package/lib/ExternalModule.js +40 -7
  21. package/lib/ExternalModuleFactoryPlugin.js +33 -9
  22. package/lib/FileSystemInfo.js +12 -8
  23. package/lib/Generator.js +5 -4
  24. package/lib/HotModuleReplacementPlugin.js +8 -6
  25. package/lib/IgnorePlugin.js +19 -1
  26. package/lib/LoaderOptionsPlugin.js +3 -1
  27. package/lib/Module.js +9 -8
  28. package/lib/ModuleSourceTypesConstants.js +100 -0
  29. package/lib/NormalModule.js +27 -17
  30. package/lib/NormalModuleFactory.js +38 -22
  31. package/lib/OptionsApply.js +12 -1
  32. package/lib/ProgressPlugin.js +50 -10
  33. package/lib/RawModule.js +3 -4
  34. package/lib/RuntimeModule.js +3 -4
  35. package/lib/RuntimePlugin.js +11 -4
  36. package/lib/RuntimeTemplate.js +13 -42
  37. package/lib/SourceMapDevToolPlugin.js +10 -7
  38. package/lib/Watching.js +2 -2
  39. package/lib/WebpackOptionsApply.js +42 -21
  40. package/lib/asset/AssetGenerator.js +347 -194
  41. package/lib/asset/AssetModulesPlugin.js +2 -1
  42. package/lib/asset/AssetSourceGenerator.js +82 -27
  43. package/lib/asset/RawDataUrlModule.js +5 -4
  44. package/lib/buildChunkGraph.js +2 -2
  45. package/lib/cache/PackFileCacheStrategy.js +69 -31
  46. package/lib/cache/ResolverCachePlugin.js +248 -173
  47. package/lib/config/defaults.js +134 -126
  48. package/lib/container/ContainerEntryModule.js +3 -4
  49. package/lib/container/ContainerPlugin.js +8 -0
  50. package/lib/container/FallbackModule.js +2 -2
  51. package/lib/container/HoistContainerReferencesPlugin.js +250 -0
  52. package/lib/container/ModuleFederationPlugin.js +38 -1
  53. package/lib/container/RemoteModule.js +4 -2
  54. package/lib/container/RemoteRuntimeModule.js +4 -2
  55. package/lib/css/CssExportsGenerator.js +16 -12
  56. package/lib/css/CssGenerator.js +22 -16
  57. package/lib/css/CssLoadingRuntimeModule.js +7 -6
  58. package/lib/css/CssModulesPlugin.js +122 -77
  59. package/lib/css/CssParser.js +655 -526
  60. package/lib/css/walkCssTokens.js +1168 -338
  61. package/lib/debug/ProfilingPlugin.js +5 -0
  62. package/lib/dependencies/CommonJsExportsParserPlugin.js +5 -2
  63. package/lib/dependencies/CommonJsImportsParserPlugin.js +3 -6
  64. package/lib/dependencies/CssExportDependency.js +3 -3
  65. package/lib/dependencies/CssLocalIdentifierDependency.js +26 -17
  66. package/lib/dependencies/CssUrlDependency.js +33 -3
  67. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +3 -3
  68. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +39 -14
  69. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +15 -82
  70. package/lib/dependencies/HarmonyImportSpecifierDependency.js +5 -2
  71. package/lib/dependencies/ImportParserPlugin.js +9 -7
  72. package/lib/dependencies/LoaderPlugin.js +19 -0
  73. package/lib/dependencies/SystemPlugin.js +2 -1
  74. package/lib/dependencies/URLPlugin.js +7 -1
  75. package/lib/dependencies/WorkerPlugin.js +1 -1
  76. package/lib/esm/ModuleChunkLoadingRuntimeModule.js +4 -2
  77. package/lib/hmr/HotModuleReplacement.runtime.js +1 -0
  78. package/lib/hmr/JavascriptHotModuleReplacement.runtime.js +1 -0
  79. package/lib/hmr/LazyCompilationPlugin.js +16 -4
  80. package/lib/hmr/lazyCompilationBackend.js +1 -7
  81. package/lib/index.js +35 -6
  82. package/lib/javascript/EnableChunkLoadingPlugin.js +2 -2
  83. package/lib/javascript/JavascriptGenerator.js +8 -8
  84. package/lib/javascript/JavascriptModulesPlugin.js +126 -73
  85. package/lib/javascript/JavascriptParser.js +338 -117
  86. package/lib/json/JsonGenerator.js +5 -5
  87. package/lib/library/EnableLibraryPlugin.js +2 -2
  88. package/lib/library/ExportPropertyLibraryPlugin.js +1 -1
  89. package/lib/library/UmdLibraryPlugin.js +16 -8
  90. package/lib/logging/Logger.js +11 -11
  91. package/lib/logging/createConsoleLogger.js +14 -14
  92. package/lib/logging/truncateArgs.js +1 -1
  93. package/lib/node/NodeWatchFileSystem.js +3 -1
  94. package/lib/node/ReadFileCompileAsyncWasmPlugin.js +20 -18
  95. package/lib/node/ReadFileCompileWasmPlugin.js +1 -2
  96. package/lib/node/nodeConsole.js +11 -8
  97. package/lib/optimize/AggressiveSplittingPlugin.js +21 -7
  98. package/lib/optimize/ConcatenatedModule.js +43 -145
  99. package/lib/optimize/FlagIncludedChunksPlugin.js +6 -0
  100. package/lib/optimize/InnerGraphPlugin.js +57 -16
  101. package/lib/optimize/LimitChunkCountPlugin.js +2 -4
  102. package/lib/optimize/ModuleConcatenationPlugin.js +4 -2
  103. package/lib/optimize/RealContentHashPlugin.js +1 -1
  104. package/lib/optimize/SideEffectsFlagPlugin.js +6 -3
  105. package/lib/rules/RuleSetCompiler.js +2 -2
  106. package/lib/runtime/GetChunkFilenameRuntimeModule.js +2 -2
  107. package/lib/schemes/DataUriPlugin.js +1 -1
  108. package/lib/serialization/BinaryMiddleware.js +32 -19
  109. package/lib/serialization/ObjectMiddleware.js +23 -9
  110. package/lib/serialization/SerializerMiddleware.js +3 -2
  111. package/lib/serialization/types.js +2 -2
  112. package/lib/sharing/ConsumeSharedModule.js +2 -3
  113. package/lib/sharing/ConsumeSharedRuntimeModule.js +3 -1
  114. package/lib/sharing/ProvideSharedModule.js +2 -3
  115. package/lib/stats/DefaultStatsFactoryPlugin.js +22 -20
  116. package/lib/stats/StatsFactory.js +12 -12
  117. package/lib/stats/StatsPrinter.js +7 -7
  118. package/lib/util/AsyncQueue.js +17 -1
  119. package/lib/util/IterableHelpers.js +1 -1
  120. package/lib/util/SetHelpers.js +1 -1
  121. package/lib/util/cleverMerge.js +48 -24
  122. package/lib/util/concatenate.js +227 -0
  123. package/lib/util/create-schema-validation.js +22 -9
  124. package/lib/util/deprecation.js +86 -28
  125. package/lib/util/fs.js +9 -9
  126. package/lib/util/hash/wasm-hash.js +12 -1
  127. package/lib/util/magicComment.js +21 -0
  128. package/lib/util/makeSerializable.js +24 -1
  129. package/lib/util/memoize.js +2 -1
  130. package/lib/util/runtime.js +4 -1
  131. package/lib/util/semver.js +130 -23
  132. package/lib/wasm/EnableWasmLoadingPlugin.js +2 -2
  133. package/lib/wasm-async/AsyncWasmLoadingRuntimeModule.js +3 -3
  134. package/lib/wasm-async/AsyncWebAssemblyGenerator.js +5 -5
  135. package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +5 -5
  136. package/lib/wasm-sync/WebAssemblyGenerator.js +8 -9
  137. package/lib/wasm-sync/WebAssemblyJavascriptGenerator.js +5 -5
  138. package/lib/web/FetchCompileAsyncWasmPlugin.js +1 -2
  139. package/lib/web/FetchCompileWasmPlugin.js +1 -2
  140. package/lib/web/JsonpChunkLoadingRuntimeModule.js +6 -6
  141. package/package.json +17 -18
  142. package/schemas/WebpackOptions.check.js +1 -1
  143. package/schemas/WebpackOptions.json +8 -2
  144. package/types.d.ts +801 -259
  145. package/lib/util/mergeScope.js +0 -76
@@ -10,6 +10,16 @@ const path = require("path");
10
10
  const { RawSource } = require("webpack-sources");
11
11
  const ConcatenationScope = require("../ConcatenationScope");
12
12
  const Generator = require("../Generator");
13
+ const {
14
+ NO_TYPES,
15
+ ASSET_TYPES,
16
+ ASSET_AND_JS_TYPES,
17
+ ASSET_AND_JS_AND_CSS_URL_TYPES,
18
+ ASSET_AND_CSS_URL_TYPES,
19
+ JS_TYPES,
20
+ JS_AND_CSS_URL_TYPES,
21
+ CSS_URL_TYPES
22
+ } = require("../ModuleSourceTypesConstants");
13
23
  const { ASSET_MODULE_TYPE } = require("../ModuleTypeConstants");
14
24
  const RuntimeGlobals = require("../RuntimeGlobals");
15
25
  const CssUrlDependency = require("../dependencies/CssUrlDependency");
@@ -24,12 +34,17 @@ const nonNumericOnlyHash = require("../util/nonNumericOnlyHash");
24
34
  /** @typedef {import("../../declarations/WebpackOptions").AssetModuleOutputPath} AssetModuleOutputPath */
25
35
  /** @typedef {import("../../declarations/WebpackOptions").RawPublicPath} RawPublicPath */
26
36
  /** @typedef {import("../Compilation")} Compilation */
37
+ /** @typedef {import("../Compilation").AssetInfo} AssetInfo */
38
+ /** @typedef {import("../Compilation").InterpolatedPathAndAssetInfo} InterpolatedPathAndAssetInfo */
27
39
  /** @typedef {import("../Compiler")} Compiler */
28
40
  /** @typedef {import("../Generator").GenerateContext} GenerateContext */
29
41
  /** @typedef {import("../Generator").UpdateHashContext} UpdateHashContext */
30
42
  /** @typedef {import("../Module")} Module */
31
43
  /** @typedef {import("../Module").BuildInfo} BuildInfo */
44
+ /** @typedef {import("../Module").BuildMeta} BuildMeta */
32
45
  /** @typedef {import("../Module").ConcatenationBailoutReasonContext} ConcatenationBailoutReasonContext */
46
+ /** @typedef {import("../Module").SourceTypes} SourceTypes */
47
+ /** @typedef {import("../ModuleGraph")} ModuleGraph */
33
48
  /** @typedef {import("../NormalModule")} NormalModule */
34
49
  /** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
35
50
  /** @typedef {import("../TemplatedPathPlugin").TemplatePath} TemplatePath */
@@ -164,25 +179,32 @@ const decodeDataUriContent = (encoding, content) => {
164
179
  }
165
180
  };
166
181
 
167
- const JS_TYPES = new Set(["javascript"]);
168
- const JS_AND_ASSET_TYPES = new Set(["javascript", ASSET_MODULE_TYPE]);
169
182
  const DEFAULT_ENCODING = "base64";
170
183
 
171
184
  class AssetGenerator extends Generator {
172
185
  /**
186
+ * @param {ModuleGraph} moduleGraph the module graph
173
187
  * @param {AssetGeneratorOptions["dataUrl"]=} dataUrlOptions the options for the data url
174
188
  * @param {AssetModuleFilename=} filename override for output.assetModuleFilename
175
189
  * @param {RawPublicPath=} publicPath override for output.assetModulePublicPath
176
190
  * @param {AssetModuleOutputPath=} outputPath the output path for the emitted file which is not included in the runtime import
177
191
  * @param {boolean=} emit generate output asset
178
192
  */
179
- constructor(dataUrlOptions, filename, publicPath, outputPath, emit) {
193
+ constructor(
194
+ moduleGraph,
195
+ dataUrlOptions,
196
+ filename,
197
+ publicPath,
198
+ outputPath,
199
+ emit
200
+ ) {
180
201
  super();
181
202
  this.dataUrlOptions = dataUrlOptions;
182
203
  this.filename = filename;
183
204
  this.publicPath = publicPath;
184
205
  this.outputPath = outputPath;
185
206
  this.emit = emit;
207
+ this._moduleGraph = moduleGraph;
186
208
  }
187
209
 
188
210
  /**
@@ -260,218 +282,349 @@ class AssetGenerator extends Generator {
260
282
  }
261
283
 
262
284
  /**
285
+ * @param {NormalModule} module module for which the code should be generated
286
+ * @returns {string} DataURI
287
+ */
288
+ generateDataUri(module) {
289
+ const source = /** @type {Source} */ (module.originalSource());
290
+
291
+ let encodedSource;
292
+
293
+ if (typeof this.dataUrlOptions === "function") {
294
+ encodedSource = this.dataUrlOptions.call(null, source.source(), {
295
+ filename: module.matchResource || module.resource,
296
+ module
297
+ });
298
+ } else {
299
+ /** @type {"base64" | false | undefined} */
300
+ let encoding =
301
+ /** @type {AssetGeneratorDataUrlOptions} */
302
+ (this.dataUrlOptions).encoding;
303
+ if (
304
+ encoding === undefined &&
305
+ module.resourceResolveData &&
306
+ module.resourceResolveData.encoding !== undefined
307
+ ) {
308
+ encoding = module.resourceResolveData.encoding;
309
+ }
310
+ if (encoding === undefined) {
311
+ encoding = DEFAULT_ENCODING;
312
+ }
313
+ const mimeType = this.getMimeType(module);
314
+
315
+ let encodedContent;
316
+
317
+ if (
318
+ module.resourceResolveData &&
319
+ module.resourceResolveData.encoding === encoding &&
320
+ decodeDataUriContent(
321
+ module.resourceResolveData.encoding,
322
+ module.resourceResolveData.encodedContent
323
+ ).equals(source.buffer())
324
+ ) {
325
+ encodedContent = module.resourceResolveData.encodedContent;
326
+ } else {
327
+ encodedContent = encodeDataUri(encoding, source);
328
+ }
329
+
330
+ encodedSource = `data:${mimeType}${
331
+ encoding ? `;${encoding}` : ""
332
+ },${encodedContent}`;
333
+ }
334
+
335
+ return encodedSource;
336
+ }
337
+
338
+ /**
339
+ * @private
263
340
  * @param {NormalModule} module module for which the code should be generated
264
341
  * @param {GenerateContext} generateContext context for generate
265
- * @returns {Source} generated code
342
+ * @param {string} contentHash the content hash
343
+ * @returns {{ filename: string, originalFilename: string, assetInfo: AssetInfo }} info
266
344
  */
267
- generate(
345
+ _getFilenameWithInfo(
268
346
  module,
269
- {
270
- runtime,
271
- concatenationScope,
272
- chunkGraph,
273
- runtimeTemplate,
274
- runtimeRequirements,
275
- type,
276
- getData
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);
277
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
278
398
  ) {
279
- switch (type) {
280
- case ASSET_MODULE_TYPE:
281
- return /** @type {Source} */ (module.originalSource());
282
- default: {
283
- let content;
284
- const originalSource = /** @type {Source} */ (module.originalSource());
285
- if (
286
- /** @type {BuildInfo} */
287
- (module.buildInfo).dataUrl
288
- ) {
289
- let encodedSource;
290
- if (typeof this.dataUrlOptions === "function") {
291
- encodedSource = this.dataUrlOptions.call(
292
- null,
293
- originalSource.source(),
294
- {
295
- filename: module.matchResource || module.resource,
296
- module
297
- }
298
- );
299
- } else {
300
- /** @type {"base64" | false | undefined} */
301
- let encoding =
302
- /** @type {AssetGeneratorDataUrlOptions} */
303
- (this.dataUrlOptions).encoding;
304
- if (
305
- encoding === undefined &&
306
- module.resourceResolveData &&
307
- module.resourceResolveData.encoding !== undefined
308
- ) {
309
- encoding = module.resourceResolveData.encoding;
310
- }
311
- if (encoding === undefined) {
312
- encoding = DEFAULT_ENCODING;
313
- }
314
- const mimeType = this.getMimeType(module);
315
-
316
- let encodedContent;
317
-
318
- if (
319
- module.resourceResolveData &&
320
- module.resourceResolveData.encoding === encoding &&
321
- decodeDataUriContent(
322
- module.resourceResolveData.encoding,
323
- module.resourceResolveData.encodedContent
324
- ).equals(originalSource.buffer())
325
- ) {
326
- encodedContent = module.resourceResolveData.encodedContent;
327
- } else {
328
- encodedContent = encodeDataUri(encoding, originalSource);
329
- }
330
-
331
- encodedSource = `data:${mimeType}${
332
- encoding ? `;${encoding}` : ""
333
- },${encodedContent}`;
334
- }
335
- const data =
336
- /** @type {NonNullable<GenerateContext["getData"]>} */
337
- (getData)();
338
- data.set("url", Buffer.from(encodedSource));
339
- content = JSON.stringify(encodedSource);
340
- } else {
341
- const assetModuleFilename =
342
- this.filename ||
343
- /** @type {AssetModuleFilename} */
344
- (runtimeTemplate.outputOptions.assetModuleFilename);
345
- const hash = createHash(
346
- /** @type {Algorithm} */
347
- (runtimeTemplate.outputOptions.hashFunction)
348
- );
349
- if (runtimeTemplate.outputOptions.hashSalt) {
350
- hash.update(runtimeTemplate.outputOptions.hashSalt);
351
- }
352
- hash.update(originalSource.buffer());
353
- const fullHash = /** @type {string} */ (
354
- hash.digest(runtimeTemplate.outputOptions.hashDigest)
355
- );
356
- const contentHash = nonNumericOnlyHash(
357
- fullHash,
358
- /** @type {number} */
359
- (runtimeTemplate.outputOptions.hashDigestLength)
360
- );
361
- /** @type {BuildInfo} */
362
- (module.buildInfo).fullContentHash = fullHash;
363
- const sourceFilename = this.getSourceFileName(
364
- module,
365
- runtimeTemplate
366
- );
367
- let { path: filename, info: assetInfo } =
368
- runtimeTemplate.compilation.getAssetPathWithInfo(
369
- assetModuleFilename,
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),
370
444
  {
371
- module,
372
- runtime,
373
- filename: sourceFilename,
374
- chunkGraph,
375
- contentHash
445
+ hash: compilation.hash
376
446
  }
377
447
  );
378
- let assetPath;
379
- let assetPathForCss;
380
- if (this.publicPath !== undefined) {
381
- const { path, info } =
382
- runtimeTemplate.compilation.getAssetPathWithInfo(
383
- this.publicPath,
384
- {
385
- module,
386
- runtime,
387
- filename: sourceFilename,
388
- chunkGraph,
389
- contentHash
390
- }
391
- );
392
- assetInfo = mergeAssetInfo(assetInfo, info);
393
- assetPath = JSON.stringify(path + filename);
394
- assetPathForCss = path + filename;
395
- } else {
396
- runtimeRequirements.add(RuntimeGlobals.publicPath); // add __webpack_require__.p
397
- assetPath = runtimeTemplate.concatenation(
398
- { expr: RuntimeGlobals.publicPath },
399
- filename
400
- );
401
- const compilation = runtimeTemplate.compilation;
402
- const path =
403
- compilation.outputOptions.publicPath === "auto"
404
- ? CssUrlDependency.PUBLIC_PATH_AUTO
405
- : compilation.getAssetPath(
406
- /** @type {TemplatePath} */
407
- (compilation.outputOptions.publicPath),
408
- {
409
- hash: compilation.hash
410
- }
411
- );
412
- assetPathForCss = path + filename;
413
- }
414
- assetInfo = {
415
- sourceFilename,
416
- ...assetInfo
417
- };
418
- if (this.outputPath) {
419
- const { path: outputPath, info } =
420
- runtimeTemplate.compilation.getAssetPathWithInfo(
421
- this.outputPath,
422
- {
423
- module,
424
- runtime,
425
- filename: sourceFilename,
426
- chunkGraph,
427
- contentHash
428
- }
429
- );
430
- assetInfo = mergeAssetInfo(assetInfo, info);
431
- filename = path.posix.join(outputPath, filename);
432
- }
433
- /** @type {BuildInfo} */
434
- (module.buildInfo).filename = filename;
435
- /** @type {BuildInfo} */
436
- (module.buildInfo).assetInfo = assetInfo;
437
- if (getData) {
438
- // Due to code generation caching module.buildInfo.XXX can't used to store such information
439
- // It need to be stored in the code generation results instead, where it's cached too
440
- // TODO webpack 6 For back-compat reasons we also store in on module.buildInfo
441
- const data = getData();
442
- data.set("fullContentHash", fullHash);
443
- data.set("filename", filename);
444
- data.set("assetInfo", assetInfo);
445
- data.set("assetPathForCss", assetPathForCss);
446
- }
447
- content = assetPath;
448
- }
449
448
 
450
- if (concatenationScope) {
451
- concatenationScope.registerNamespaceExport(
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
+ /**
460
+ * @param {NormalModule} module module for which the code should be generated
461
+ * @param {GenerateContext} generateContext context for generate
462
+ * @returns {Source | null} generated code
463
+ */
464
+ generate(module, generateContext) {
465
+ const {
466
+ type,
467
+ getData,
468
+ runtimeTemplate,
469
+ runtimeRequirements,
470
+ concatenationScope
471
+ } = generateContext;
472
+
473
+ let content;
474
+
475
+ const needContent = type === "javascript" || type === "css-url";
476
+
477
+ const data = getData ? getData() : undefined;
478
+
479
+ if (
480
+ /** @type {BuildInfo} */
481
+ (module.buildInfo).dataUrl &&
482
+ needContent
483
+ ) {
484
+ const encodedSource = this.generateDataUri(module);
485
+ content =
486
+ type === "javascript" ? JSON.stringify(encodedSource) : encodedSource;
487
+
488
+ if (data) {
489
+ data.set("url", { [type]: content, ...data.get("url") });
490
+ }
491
+ } else {
492
+ const hash = createHash(
493
+ /** @type {Algorithm} */
494
+ (runtimeTemplate.outputOptions.hashFunction)
495
+ );
496
+
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
+ if (data) {
508
+ data.set("fullContentHash", fullHash);
509
+ }
510
+
511
+ /** @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
+ }
524
+
525
+ const { originalFilename, filename, assetInfo } =
526
+ this._getFilenameWithInfo(module, generateContext, contentHash);
527
+
528
+ if (data) {
529
+ data.set("filename", filename);
530
+ }
531
+
532
+ let { assetPath, assetInfo: newAssetInfo } = this._getAssetPathWithInfo(
533
+ module,
534
+ generateContext,
535
+ originalFilename,
536
+ assetInfo,
537
+ contentHash
538
+ );
539
+
540
+ if (data && (type === "javascript" || type === "css-url")) {
541
+ data.set("url", { [type]: assetPath, ...data.get("url") });
542
+ }
543
+
544
+ if (data && data.get("assetInfo")) {
545
+ newAssetInfo = mergeAssetInfo(data.get("assetInfo"), newAssetInfo);
546
+ }
547
+
548
+ if (data) {
549
+ data.set("assetInfo", newAssetInfo);
550
+ }
551
+
552
+ // Due to code generation caching module.buildInfo.XXX can't used to store such information
553
+ // It need to be stored in the code generation results instead, where it's cached too
554
+ // TODO webpack 6 For back-compat reasons we also store in on module.buildInfo
555
+ /** @type {BuildInfo} */
556
+ (module.buildInfo).filename = filename;
557
+
558
+ /** @type {BuildInfo} */
559
+ (module.buildInfo).assetInfo = newAssetInfo;
560
+
561
+ content = assetPath;
562
+ }
563
+
564
+ if (type === "javascript") {
565
+ if (concatenationScope) {
566
+ concatenationScope.registerNamespaceExport(
567
+ ConcatenationScope.NAMESPACE_OBJECT_EXPORT
568
+ );
569
+
570
+ return new RawSource(
571
+ `${runtimeTemplate.supportsConst() ? "const" : "var"} ${
452
572
  ConcatenationScope.NAMESPACE_OBJECT_EXPORT
453
- );
454
- return new RawSource(
455
- `${runtimeTemplate.supportsConst() ? "const" : "var"} ${
456
- ConcatenationScope.NAMESPACE_OBJECT_EXPORT
457
- } = ${content};`
458
- );
459
- }
460
- runtimeRequirements.add(RuntimeGlobals.module);
461
- return new RawSource(`${RuntimeGlobals.module}.exports = ${content};`);
573
+ } = ${content};`
574
+ );
462
575
  }
576
+
577
+ runtimeRequirements.add(RuntimeGlobals.module);
578
+
579
+ return new RawSource(`${RuntimeGlobals.module}.exports = ${content};`);
580
+ } else if (type === "css-url") {
581
+ return null;
463
582
  }
583
+
584
+ return /** @type {Source} */ (module.originalSource());
464
585
  }
465
586
 
466
587
  /**
467
588
  * @param {NormalModule} module fresh module
468
- * @returns {Set<string>} available types (do not mutate)
589
+ * @returns {SourceTypes} available types (do not mutate)
469
590
  */
470
591
  getTypes(module) {
592
+ const sourceTypes = new Set();
593
+ const connections = this._moduleGraph.getIncomingConnections(module);
594
+
595
+ for (const connection of connections) {
596
+ if (!connection.originModule) {
597
+ continue;
598
+ }
599
+
600
+ sourceTypes.add(connection.originModule.type.split("/")[0]);
601
+ }
602
+
471
603
  if ((module.buildInfo && module.buildInfo.dataUrl) || this.emit === false) {
472
- return JS_TYPES;
604
+ if (sourceTypes) {
605
+ if (sourceTypes.has("javascript") && sourceTypes.has("css")) {
606
+ return JS_AND_CSS_URL_TYPES;
607
+ } else if (sourceTypes.has("javascript")) {
608
+ return JS_TYPES;
609
+ } else if (sourceTypes.has("css")) {
610
+ return CSS_URL_TYPES;
611
+ }
612
+ }
613
+
614
+ return NO_TYPES;
473
615
  }
474
- return JS_AND_ASSET_TYPES;
616
+
617
+ if (sourceTypes) {
618
+ if (sourceTypes.has("javascript") && sourceTypes.has("css")) {
619
+ return ASSET_AND_JS_AND_CSS_URL_TYPES;
620
+ } else if (sourceTypes.has("javascript")) {
621
+ return ASSET_AND_JS_TYPES;
622
+ } else if (sourceTypes.has("css")) {
623
+ return ASSET_AND_CSS_URL_TYPES;
624
+ }
625
+ }
626
+
627
+ return ASSET_TYPES;
475
628
  }
476
629
 
477
630
  /**
@@ -165,6 +165,7 @@ class AssetModulesPlugin {
165
165
  const AssetGenerator = getAssetGenerator();
166
166
 
167
167
  return new AssetGenerator(
168
+ compilation.moduleGraph,
168
169
  dataUrl,
169
170
  filename,
170
171
  publicPath,
@@ -178,7 +179,7 @@ class AssetModulesPlugin {
178
179
  .tap(plugin, () => {
179
180
  const AssetSourceGenerator = getAssetSourceGenerator();
180
181
 
181
- return new AssetSourceGenerator();
182
+ return new AssetSourceGenerator(compilation.moduleGraph);
182
183
  });
183
184
 
184
185
  compilation.hooks.renderManifest.tap(plugin, (result, options) => {