webpack 5.106.2 → 5.107.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (235) hide show
  1. package/README.md +2 -2
  2. package/lib/APIPlugin.js +1 -1
  3. package/lib/BannerPlugin.js +3 -4
  4. package/lib/Cache.js +3 -6
  5. package/lib/Chunk.js +21 -25
  6. package/lib/ChunkGroup.js +57 -15
  7. package/lib/CompatibilityPlugin.js +8 -7
  8. package/lib/Compilation.js +67 -37
  9. package/lib/Compiler.js +4 -13
  10. package/lib/ContextModule.js +2 -2
  11. package/lib/DefinePlugin.js +2 -2
  12. package/lib/Dependency.js +22 -1
  13. package/lib/DependencyTemplate.js +2 -1
  14. package/lib/EnvironmentPlugin.js +1 -1
  15. package/lib/EvalSourceMapDevToolPlugin.js +8 -10
  16. package/lib/ExportsInfo.js +30 -34
  17. package/lib/ExternalModule.js +91 -26
  18. package/lib/ExternalModuleFactoryPlugin.js +7 -1
  19. package/lib/FileSystemInfo.js +187 -72
  20. package/lib/Generator.js +3 -3
  21. package/lib/HotModuleReplacementPlugin.js +26 -8
  22. package/lib/IgnorePlugin.js +2 -1
  23. package/lib/Module.js +20 -19
  24. package/lib/ModuleFactory.js +1 -1
  25. package/lib/ModuleNotFoundError.js +3 -84
  26. package/lib/ModuleSourceTypeConstants.js +51 -19
  27. package/lib/ModuleTypeConstants.js +12 -3
  28. package/lib/MultiCompiler.js +2 -2
  29. package/lib/NodeStuffPlugin.js +1 -1
  30. package/lib/NormalModule.js +119 -77
  31. package/lib/NormalModuleFactory.js +47 -27
  32. package/lib/Parser.js +1 -1
  33. package/lib/ProgressPlugin.js +129 -56
  34. package/lib/RuntimeGlobals.js +5 -5
  35. package/lib/RuntimeModule.js +9 -7
  36. package/lib/RuntimePlugin.js +12 -1
  37. package/lib/SourceMapDevToolPlugin.js +250 -49
  38. package/lib/Template.js +1 -1
  39. package/lib/TemplatedPathPlugin.js +22 -4
  40. package/lib/WarnCaseSensitiveModulesPlugin.js +70 -2
  41. package/lib/WarnDeprecatedOptionPlugin.js +1 -1
  42. package/lib/WarnNoModeSetPlugin.js +16 -1
  43. package/lib/Watching.js +2 -3
  44. package/lib/WebpackError.js +3 -77
  45. package/lib/WebpackIsIncludedPlugin.js +1 -1
  46. package/lib/WebpackOptionsApply.js +13 -1
  47. package/lib/asset/AssetBytesGenerator.js +12 -8
  48. package/lib/asset/AssetGenerator.js +36 -22
  49. package/lib/asset/AssetModulesPlugin.js +6 -8
  50. package/lib/asset/AssetSourceGenerator.js +12 -8
  51. package/lib/buildChunkGraph.js +4 -6
  52. package/lib/cache/PackFileCacheStrategy.js +4 -4
  53. package/lib/cli.js +3 -1
  54. package/lib/config/defaults.js +197 -10
  55. package/lib/config/normalization.js +3 -1
  56. package/lib/css/CssGenerator.js +320 -105
  57. package/lib/css/CssInjectStyleRuntimeModule.js +44 -42
  58. package/lib/css/CssLoadingRuntimeModule.js +22 -4
  59. package/lib/{CssModule.js → css/CssModule.js} +15 -15
  60. package/lib/css/CssModulesPlugin.js +168 -88
  61. package/lib/css/CssParser.js +566 -269
  62. package/lib/css/walkCssTokens.js +148 -2
  63. package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +1 -1
  64. package/lib/dependencies/CommonJsDependencyHelpers.js +63 -0
  65. package/lib/dependencies/CommonJsExportRequireDependency.js +54 -10
  66. package/lib/dependencies/CommonJsExportsParserPlugin.js +1 -1
  67. package/lib/dependencies/CommonJsFullRequireDependency.js +32 -9
  68. package/lib/dependencies/CommonJsImportsParserPlugin.js +112 -4
  69. package/lib/dependencies/CommonJsRequireDependency.js +67 -4
  70. package/lib/dependencies/ContextDependency.js +1 -1
  71. package/lib/dependencies/ContextDependencyHelpers.js +1 -1
  72. package/lib/dependencies/CreateRequireParserPlugin.js +1 -1
  73. package/lib/dependencies/CriticalDependencyWarning.js +1 -1
  74. package/lib/dependencies/CssIcssExportDependency.js +332 -67
  75. package/lib/dependencies/CssIcssImportDependency.js +49 -7
  76. package/lib/dependencies/CssIcssSymbolDependency.js +11 -3
  77. package/lib/dependencies/CssImportDependency.js +8 -0
  78. package/lib/dependencies/CssUrlDependency.js +28 -2
  79. package/lib/dependencies/HarmonyDetectionParserPlugin.js +22 -2
  80. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +8 -7
  81. package/lib/dependencies/HarmonyExportExpressionDependency.js +22 -14
  82. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +110 -3
  83. package/lib/dependencies/HarmonyImportDependency.js +10 -2
  84. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +22 -1
  85. package/lib/dependencies/HarmonyImportSpecifierDependency.js +1 -1
  86. package/lib/{HarmonyLinkingError.js → dependencies/HarmonyLinkingError.js} +5 -3
  87. package/lib/dependencies/HtmlInlineScriptDependency.js +133 -0
  88. package/lib/dependencies/HtmlInlineStyleDependency.js +101 -0
  89. package/lib/dependencies/HtmlScriptSrcDependency.js +557 -0
  90. package/lib/dependencies/HtmlSourceDependency.js +128 -0
  91. package/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +1 -1
  92. package/lib/dependencies/ImportParserPlugin.js +2 -2
  93. package/lib/dependencies/ImportPhase.js +1 -1
  94. package/lib/dependencies/RequireIncludeDependencyParserPlugin.js +1 -1
  95. package/lib/{RequireJsStuffPlugin.js → dependencies/RequireJsStuffPlugin.js} +7 -7
  96. package/lib/dependencies/SystemPlugin.js +1 -1
  97. package/lib/dependencies/WebAssemblyImportDependency.js +1 -1
  98. package/lib/dependencies/WorkerPlugin.js +2 -2
  99. package/lib/{DelegatedModule.js → dll/DelegatedModule.js} +31 -31
  100. package/lib/{DelegatedModuleFactoryPlugin.js → dll/DelegatedModuleFactoryPlugin.js} +4 -4
  101. package/lib/{DelegatedPlugin.js → dll/DelegatedPlugin.js} +2 -2
  102. package/lib/{DllEntryPlugin.js → dll/DllEntryPlugin.js} +4 -4
  103. package/lib/{DllModule.js → dll/DllModule.js} +24 -24
  104. package/lib/{DllModuleFactory.js → dll/DllModuleFactory.js} +4 -4
  105. package/lib/{DllPlugin.js → dll/DllPlugin.js} +6 -5
  106. package/lib/{DllReferencePlugin.js → dll/DllReferencePlugin.js} +14 -14
  107. package/lib/{LibManifestPlugin.js → dll/LibManifestPlugin.js} +9 -9
  108. package/lib/{AsyncDependencyToInitialChunkError.js → errors/AsyncDependencyToInitialChunkError.js} +2 -2
  109. package/lib/errors/BuildCycleError.js +1 -1
  110. package/lib/{ChunkRenderError.js → errors/ChunkRenderError.js} +1 -1
  111. package/lib/{CodeGenerationError.js → errors/CodeGenerationError.js} +1 -1
  112. package/lib/{CommentCompilationWarning.js → errors/CommentCompilationWarning.js} +3 -3
  113. package/lib/{ConcurrentCompilationError.js → errors/ConcurrentCompilationError.js} +4 -2
  114. package/lib/{EnvironmentNotSupportAsyncWarning.js → errors/EnvironmentNotSupportAsyncWarning.js} +4 -4
  115. package/lib/{HookWebpackError.js → errors/HookWebpackError.js} +5 -5
  116. package/lib/{IgnoreErrorModuleFactory.js → errors/IgnoreErrorModuleFactory.js} +4 -4
  117. package/lib/{InvalidDependenciesModuleWarning.js → errors/InvalidDependenciesModuleWarning.js} +3 -3
  118. package/lib/errors/JSONParseError.js +114 -0
  119. package/lib/{ModuleBuildError.js → errors/ModuleBuildError.js} +5 -5
  120. package/lib/{ModuleDependencyError.js → errors/ModuleDependencyError.js} +2 -2
  121. package/lib/{ModuleDependencyWarning.js → errors/ModuleDependencyWarning.js} +4 -4
  122. package/lib/{ModuleError.js → errors/ModuleError.js} +5 -5
  123. package/lib/{ModuleHashingError.js → errors/ModuleHashingError.js} +1 -1
  124. package/lib/errors/ModuleNotFoundError.js +91 -0
  125. package/lib/{ModuleParseError.js → errors/ModuleParseError.js} +8 -6
  126. package/lib/{ModuleRestoreError.js → errors/ModuleRestoreError.js} +1 -1
  127. package/lib/{ModuleStoreError.js → errors/ModuleStoreError.js} +1 -1
  128. package/lib/{ModuleWarning.js → errors/ModuleWarning.js} +5 -5
  129. package/lib/{NodeStuffInWebError.js → errors/NodeStuffInWebError.js} +4 -4
  130. package/lib/errors/NonErrorEmittedError.js +28 -0
  131. package/lib/{UnhandledSchemeError.js → errors/UnhandledSchemeError.js} +2 -2
  132. package/lib/{UnsupportedFeatureWarning.js → errors/UnsupportedFeatureWarning.js} +3 -3
  133. package/lib/errors/WebpackError.js +84 -0
  134. package/lib/html/HtmlGenerator.js +379 -0
  135. package/lib/html/HtmlModulesPlugin.js +429 -0
  136. package/lib/html/HtmlParser.js +1489 -0
  137. package/lib/html/walkHtmlTokens.js +3249 -0
  138. package/lib/ids/IdHelpers.js +2 -1
  139. package/lib/index.js +36 -15
  140. package/lib/javascript/JavascriptModulesPlugin.js +91 -10
  141. package/lib/javascript/JavascriptParser.js +197 -16
  142. package/lib/javascript/JavascriptParserHelpers.js +1 -1
  143. package/lib/json/JsonParser.js +7 -16
  144. package/lib/library/AbstractLibraryPlugin.js +1 -1
  145. package/lib/library/EnableLibraryPlugin.js +1 -1
  146. package/lib/{FalseIIFEUmdWarning.js → library/FalseIIFEUmdWarning.js} +1 -1
  147. package/lib/library/ModuleLibraryPlugin.js +74 -0
  148. package/lib/node/NodeEnvironmentPlugin.js +4 -2
  149. package/lib/node/nodeConsole.js +113 -64
  150. package/lib/optimize/ConcatenatedModule.js +51 -6
  151. package/lib/optimize/InnerGraph.js +1 -1
  152. package/lib/optimize/InnerGraphPlugin.js +11 -1
  153. package/lib/optimize/MinMaxSizeWarning.js +4 -4
  154. package/lib/optimize/ModuleConcatenationPlugin.js +15 -7
  155. package/lib/optimize/RealContentHashPlugin.js +89 -26
  156. package/lib/optimize/SideEffectsFlagPlugin.js +112 -5
  157. package/lib/optimize/SplitChunksPlugin.js +5 -5
  158. package/lib/performance/AssetsOverSizeLimitWarning.js +2 -2
  159. package/lib/performance/EntrypointsOverSizeLimitWarning.js +2 -2
  160. package/lib/performance/NoAsyncChunksWarning.js +5 -3
  161. package/lib/performance/SizeLimitsPlugin.js +1 -1
  162. package/lib/prefetch/ChunkPrefetchTriggerRuntimeModule.js +4 -1
  163. package/lib/rules/UseEffectRulePlugin.js +4 -3
  164. package/lib/runtime/AutoPublicPathRuntimeModule.js +3 -3
  165. package/lib/runtime/GetChunkFilenameRuntimeModule.js +5 -5
  166. package/lib/runtime/MakeDeferredNamespaceObjectRuntime.js +119 -13
  167. package/lib/runtime/SetAnonymousDefaultNameRuntimeModule.js +35 -0
  168. package/lib/schemes/DataUriPlugin.js +13 -1
  169. package/lib/schemes/VirtualUrlPlugin.js +1 -1
  170. package/lib/serialization/SerializerMiddleware.js +2 -2
  171. package/lib/sharing/ConsumeSharedPlugin.js +4 -10
  172. package/lib/sharing/ConsumeSharedRuntimeModule.js +8 -4
  173. package/lib/sharing/ProvideSharedModule.js +1 -1
  174. package/lib/sharing/ProvideSharedPlugin.js +5 -5
  175. package/lib/sharing/resolveMatchedConfigs.js +1 -1
  176. package/lib/stats/DefaultStatsFactoryPlugin.js +2 -2
  177. package/lib/stats/DefaultStatsPresetPlugin.js +1 -1
  178. package/lib/stats/DefaultStatsPrinterPlugin.js +1 -1
  179. package/lib/stats/StatsFactory.js +1 -1
  180. package/lib/typescript/TypeScriptPlugin.js +210 -0
  181. package/lib/url/URLParserPlugin.js +2 -2
  182. package/lib/util/AsyncQueue.js +2 -2
  183. package/lib/util/Hash.js +2 -2
  184. package/lib/util/LocConverter.js +53 -0
  185. package/lib/util/SortableSet.js +1 -1
  186. package/lib/util/cleverMerge.js +2 -2
  187. package/lib/util/comparators.js +3 -3
  188. package/lib/util/concatenate.js +3 -3
  189. package/lib/util/conventions.js +42 -1
  190. package/lib/util/createMappings.js +118 -0
  191. package/lib/{formatLocation.js → util/formatLocation.js} +2 -2
  192. package/lib/{SizeFormatHelpers.js → util/formatSize.js} +3 -1
  193. package/lib/util/fs.js +8 -8
  194. package/lib/util/hash/md4.js +1 -1
  195. package/lib/util/hash/xxhash64.js +1 -1
  196. package/lib/util/identifier.js +48 -0
  197. package/lib/util/internalSerializables.js +35 -19
  198. package/lib/util/magicComment.js +10 -7
  199. package/lib/util/parseJson.js +2 -73
  200. package/lib/util/source.js +21 -0
  201. package/lib/util/topologicalSort.js +69 -0
  202. package/lib/wasm-async/AsyncWebAssemblyModulesPlugin.js +3 -4
  203. package/lib/wasm-async/AsyncWebAssemblyParser.js +1 -1
  204. package/lib/wasm-sync/UnsupportedWebAssemblyFeatureError.js +5 -3
  205. package/lib/wasm-sync/WasmFinalizeExportsPlugin.js +1 -1
  206. package/lib/wasm-sync/WebAssemblyInInitialChunkError.js +5 -3
  207. package/lib/webpack.js +3 -1
  208. package/package.json +24 -22
  209. package/schemas/WebpackOptions.check.js +1 -1
  210. package/schemas/WebpackOptions.json +129 -12
  211. package/schemas/plugins/{DllPlugin.check.d.ts → HtmlGeneratorOptions.check.d.ts} +1 -1
  212. package/schemas/plugins/HtmlGeneratorOptions.check.js +6 -0
  213. package/schemas/plugins/HtmlGeneratorOptions.json +3 -0
  214. package/schemas/plugins/ProgressPlugin.check.js +1 -1
  215. package/schemas/plugins/ProgressPlugin.json +22 -0
  216. package/schemas/plugins/container/ContainerReferencePlugin.check.js +1 -1
  217. package/schemas/plugins/container/ContainerReferencePlugin.json +1 -0
  218. package/schemas/plugins/container/ExternalsType.check.js +1 -1
  219. package/schemas/plugins/container/ModuleFederationPlugin.check.js +1 -1
  220. package/schemas/plugins/container/ModuleFederationPlugin.json +1 -0
  221. package/schemas/plugins/{DllReferencePlugin.check.d.ts → css/CssAutoOrModuleParserOptions.check.d.ts} +1 -1
  222. package/schemas/plugins/css/CssAutoOrModuleParserOptions.check.js +6 -0
  223. package/schemas/plugins/css/CssAutoOrModuleParserOptions.json +3 -0
  224. package/schemas/plugins/dll/DllPlugin.check.d.ts +7 -0
  225. package/schemas/plugins/dll/DllReferencePlugin.check.d.ts +7 -0
  226. package/types.d.ts +1153 -233
  227. package/lib/CaseSensitiveModulesWarning.js +0 -80
  228. package/lib/GraphHelpers.js +0 -49
  229. package/lib/NoModeWarning.js +0 -23
  230. package/lib/css/CssMergeStyleSheetsRuntimeModule.js +0 -57
  231. /package/lib/{AbstractMethodError.js → errors/AbstractMethodError.js} +0 -0
  232. /package/schemas/plugins/{DllPlugin.check.js → dll/DllPlugin.check.js} +0 -0
  233. /package/schemas/plugins/{DllPlugin.json → dll/DllPlugin.json} +0 -0
  234. /package/schemas/plugins/{DllReferencePlugin.check.js → dll/DllReferencePlugin.check.js} +0 -0
  235. /package/schemas/plugins/{DllReferencePlugin.json → dll/DllReferencePlugin.json} +0 -0
@@ -23,7 +23,6 @@ const { makePathsAbsolute } = require("./util/identifier");
23
23
  /** @typedef {import("../declarations/plugins/SourceMapDevToolPlugin").Rules} Rules */
24
24
  /** @typedef {import("./Compiler")} Compiler */
25
25
  /** @typedef {import("./ChunkGraph").ModuleId} ModuleId */
26
- /** @typedef {import("./TemplatedPathPlugin").TemplatePath} TemplatePath */
27
26
 
28
27
  /** @type {WeakMap<Source, Source>} */
29
28
  const cache = new WeakMap();
@@ -103,17 +102,12 @@ class EvalSourceMapDevToolPlugin {
103
102
  };
104
103
 
105
104
  if (m instanceof NormalModule) {
106
- const module = /** @type {NormalModule} */ (m);
107
- if (!matchModule(module.resource)) {
105
+ if (!matchModule(m.resource)) {
108
106
  return result(source);
109
107
  }
110
108
  } else if (m instanceof ConcatenatedModule) {
111
- const concatModule = /** @type {ConcatenatedModule} */ (m);
112
- if (concatModule.rootModule instanceof NormalModule) {
113
- const module = /** @type {NormalModule} */ (
114
- concatModule.rootModule
115
- );
116
- if (!matchModule(module.resource)) {
109
+ if (m.rootModule instanceof NormalModule) {
110
+ if (!matchModule(m.rootModule.resource)) {
117
111
  return result(source);
118
112
  }
119
113
  } else {
@@ -146,9 +140,13 @@ class EvalSourceMapDevToolPlugin {
146
140
  sourceMap = { ...sourceMap };
147
141
  const context = compiler.context;
148
142
  const root = compiler.root;
143
+ const cachedAbsolutify = makePathsAbsolute.bindContextCache(
144
+ context,
145
+ root
146
+ );
149
147
  const modules = sourceMap.sources.map((source) => {
150
148
  if (!source.startsWith("webpack://")) return source;
151
- source = makePathsAbsolute(context, source.slice(10), root);
149
+ source = cachedAbsolutify(source.slice(10));
152
150
  const module = compilation.findModule(source);
153
151
  return module || source;
154
152
  });
@@ -114,6 +114,21 @@ class ExportsInfo {
114
114
  constructor() {
115
115
  /** @type {Exports} */
116
116
  this._exports = new Map();
117
+
118
+ // `_otherExportsInfo` is a fallback entry for unlisted exports. Two roles:
119
+ // 1. factory template — `getExportInfo` creates `new ExportInfo(name, this)`,
120
+ // so created export info extends its properties.
121
+ // 2. flags whether the whole exportsInfo can be statically analyzed.
122
+ // Its `used` reachable values:
123
+ // - NoInfo: no use analysis yet (`optimization#usedExports` off), or used without info
124
+ // - Unused: analyzed, no unlisted export needed
125
+ // - Unknown: used in unknown way
126
+ // - Used/OnlyPropertiesUsed: never reached
127
+ // Its `provided` reachable values:
128
+ // - undefined: provision not determined yet
129
+ // - false: determined, no unlisted export is provided
130
+ // - null: only runtime knows (dynamic/unknown exports)
131
+ // - true: never reached
117
132
  /** @type {ExportInfo} */
118
133
  this._otherExportsInfo = new ExportInfo(null);
119
134
  /** @type {ExportInfo} */
@@ -239,22 +254,12 @@ class ExportsInfo {
239
254
 
240
255
  setHasProvideInfo() {
241
256
  for (const exportInfo of this._exports.values()) {
242
- if (exportInfo.provided === undefined) {
243
- exportInfo.provided = false;
244
- }
245
- if (exportInfo.canMangleProvide === undefined) {
246
- exportInfo.canMangleProvide = true;
247
- }
257
+ exportInfo.setHasProvideInfo();
248
258
  }
249
259
  if (this._redirectTo !== undefined) {
250
260
  this._redirectTo.setHasProvideInfo();
251
261
  } else {
252
- if (this._otherExportsInfo.provided === undefined) {
253
- this._otherExportsInfo.provided = false;
254
- }
255
- if (this._otherExportsInfo.canMangleProvide === undefined) {
256
- this._otherExportsInfo.canMangleProvide = true;
257
- }
262
+ this._otherExportsInfo.setHasProvideInfo();
258
263
  }
259
264
  }
260
265
 
@@ -436,20 +441,8 @@ class ExportsInfo {
436
441
  if (this._redirectTo.setUsedInUnknownWay(runtime)) {
437
442
  changed = true;
438
443
  }
439
- } else {
440
- if (
441
- this._otherExportsInfo.setUsedConditionally(
442
- (used) => used < UsageState.Unknown,
443
- UsageState.Unknown,
444
- runtime
445
- )
446
- ) {
447
- changed = true;
448
- }
449
- if (this._otherExportsInfo.canMangleUse !== false) {
450
- this._otherExportsInfo.canMangleUse = false;
451
- changed = true;
452
- }
444
+ } else if (this._otherExportsInfo.setUsedInUnknownWay(runtime)) {
445
+ changed = true;
453
446
  }
454
447
  return changed;
455
448
  }
@@ -470,14 +463,8 @@ class ExportsInfo {
470
463
  if (this._redirectTo.setUsedWithoutInfo(runtime)) {
471
464
  changed = true;
472
465
  }
473
- } else {
474
- if (this._otherExportsInfo.setUsed(UsageState.NoInfo, runtime)) {
475
- changed = true;
476
- }
477
- if (this._otherExportsInfo.canMangleUse !== false) {
478
- this._otherExportsInfo.canMangleUse = false;
479
- changed = true;
480
- }
466
+ } else if (this._otherExportsInfo.setUsedWithoutInfo(runtime)) {
467
+ changed = true;
481
468
  }
482
469
  return changed;
483
470
  }
@@ -1030,6 +1017,15 @@ class ExportInfo {
1030
1017
  return changed;
1031
1018
  }
1032
1019
 
1020
+ setHasProvideInfo() {
1021
+ if (this.provided === undefined) {
1022
+ this.provided = false;
1023
+ }
1024
+ if (this.canMangleProvide === undefined) {
1025
+ this.canMangleProvide = true;
1026
+ }
1027
+ }
1028
+
1033
1029
  setHasUseInfo() {
1034
1030
  if (!this._hasUseInRuntimeInfo) {
1035
1031
  this._hasUseInRuntimeInfo = true;
@@ -8,13 +8,13 @@
8
8
  const { SyncBailHook } = require("tapable");
9
9
  const { OriginalSource, RawSource } = require("webpack-sources");
10
10
  const ConcatenationScope = require("./ConcatenationScope");
11
- const EnvironmentNotSupportAsyncWarning = require("./EnvironmentNotSupportAsyncWarning");
12
11
  const { UsageState } = require("./ExportsInfo");
13
12
  const InitFragment = require("./InitFragment");
14
13
  const Module = require("./Module");
15
14
  const {
15
+ ASSET_URL_TYPE,
16
+ ASSET_URL_TYPES,
16
17
  CSS_IMPORT_TYPES,
17
- CSS_URL_TYPES,
18
18
  JAVASCRIPT_TYPE,
19
19
  JAVASCRIPT_TYPES
20
20
  } = require("./ModuleSourceTypeConstants");
@@ -22,7 +22,9 @@ const { JAVASCRIPT_MODULE_TYPE_DYNAMIC } = require("./ModuleTypeConstants");
22
22
  const RuntimeGlobals = require("./RuntimeGlobals");
23
23
  const Template = require("./Template");
24
24
  const { DEFAULTS } = require("./config/defaults");
25
+ const { ImportPhaseUtils } = require("./dependencies/ImportPhase");
25
26
  const StaticExportsDependency = require("./dependencies/StaticExportsDependency");
27
+ const EnvironmentNotSupportAsyncWarning = require("./errors/EnvironmentNotSupportAsyncWarning");
26
28
  const createHash = require("./util/createHash");
27
29
  const extractUrlAndGlobal = require("./util/extractUrlAndGlobal");
28
30
  const makeSerializable = require("./util/makeSerializable");
@@ -62,15 +64,16 @@ const { register } = require("./util/serialization");
62
64
  /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
63
65
  /** @typedef {import("./javascript/JavascriptModulesPlugin").ChunkRenderContext} ChunkRenderContext */
64
66
  /** @typedef {import("./javascript/JavascriptParser").ImportAttributes} ImportAttributes */
67
+ /** @typedef {import("./dependencies/ImportPhase").ImportPhaseType} ImportPhaseType */
65
68
  /** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
66
69
  /** @typedef {import("./serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
67
70
  /** @typedef {import("./util/Hash")} Hash */
68
71
  /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
69
72
  /** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
70
73
 
71
- /** @typedef {{ attributes?: ImportAttributes, externalType: "import" | "module" | undefined }} ImportDependencyMeta */
74
+ /** @typedef {{ attributes?: ImportAttributes, phase?: ImportPhaseType, externalType: "import" | "module" | undefined }} ImportDependencyMeta */
72
75
  /** @typedef {{ layer?: string, supports?: string, media?: string }} CssImportDependencyMeta */
73
- /** @typedef {{ sourceType: "css-url" }} AssetDependencyMeta */
76
+ /** @typedef {{ sourceType: "asset-url" | "css-url" }} AssetDependencyMeta */
74
77
 
75
78
  /** @typedef {ImportDependencyMeta | CssImportDependencyMeta | AssetDependencyMeta} DependencyMeta */
76
79
 
@@ -202,15 +205,25 @@ const getSourceForImportExternal = (
202
205
  runtimeTemplate,
203
206
  dependencyMeta
204
207
  ) => {
205
- const importName = runtimeTemplate.outputOptions.importFunctionName;
208
+ const baseImportName = runtimeTemplate.outputOptions.importFunctionName;
206
209
  if (
207
210
  !runtimeTemplate.supportsDynamicImport() &&
208
- (importName === "import" || importName === "module-import")
211
+ (baseImportName === "import" || baseImportName === "module-import")
209
212
  ) {
210
213
  throw new Error(
211
214
  "The target environment doesn't support 'import()' so it's not possible to use external type 'import'"
212
215
  );
213
216
  }
217
+ const phase = dependencyMeta && dependencyMeta.phase;
218
+ // `import.defer(…)` and `import.source(…)` are only valid forms of the
219
+ // native `import(…)` function, so we only emit the phase suffix when the
220
+ // importFunctionName is the default `"import"`.
221
+ const importName =
222
+ baseImportName === "import" && ImportPhaseUtils.isDefer(phase)
223
+ ? "import.defer"
224
+ : baseImportName === "import" && ImportPhaseUtils.isSource(phase)
225
+ ? "import.source"
226
+ : baseImportName;
214
227
  const attributes =
215
228
  dependencyMeta && dependencyMeta.attributes
216
229
  ? dependencyMeta.attributes._isLegacyAssert
@@ -293,7 +306,9 @@ class ModuleExternalInitFragment extends InitFragment {
293
306
  "",
294
307
  InitFragment.STAGE_HARMONY_IMPORTS,
295
308
  0,
296
- `external module import ${ident} ${imported === true ? imported : imported.join(" ")}`
309
+ `external module import ${ident} ${
310
+ imported === true ? imported : imported.join(" ")
311
+ }`
297
312
  );
298
313
  this._ident = ident;
299
314
  this._request = request;
@@ -332,23 +347,34 @@ class ModuleExternalInitFragment extends InitFragment {
332
347
  } = this;
333
348
  const attributes =
334
349
  dependencyMeta && dependencyMeta.attributes
335
- ? dependencyMeta.attributes._isLegacyAssert &&
336
- dependencyMeta.attributes._isLegacyAssert
350
+ ? dependencyMeta.attributes._isLegacyAssert
337
351
  ? ` assert ${JSON.stringify(
338
352
  dependencyMeta.attributes,
339
353
  importAssertionReplacer
340
354
  )}`
341
355
  : ` with ${JSON.stringify(dependencyMeta.attributes)}`
342
356
  : "";
357
+ const phase = dependencyMeta && dependencyMeta.phase;
343
358
  let content = "";
344
359
  if (imported === true) {
345
360
  // namespace
346
- content = `import * as ${identifier} from ${JSON.stringify(request)}${
347
- attributes
348
- };\n`;
361
+ const phaseKeyword = ImportPhaseUtils.isDefer(phase) ? "defer " : "";
362
+ content = `import ${phaseKeyword}* as ${identifier} from ${JSON.stringify(
363
+ request
364
+ )}${attributes};\n`;
349
365
  } else if (imported.length === 0) {
350
366
  // just import, no use
351
367
  content = `import ${JSON.stringify(request)}${attributes};\n`;
368
+ } else if (
369
+ ImportPhaseUtils.isSource(phase) &&
370
+ imported.length === 1 &&
371
+ imported[0][0] === "default"
372
+ ) {
373
+ // `import source x from "…"` — the source-phase form binds the source
374
+ // object directly to a single identifier (no namespace, no destructuring).
375
+ content = `import source ${imported[0][1]} from ${JSON.stringify(
376
+ request
377
+ )}${attributes};\n`;
352
378
  } else {
353
379
  content = `import { ${imported
354
380
  .map(([name, finalName]) => {
@@ -469,6 +495,7 @@ const getSourceForModuleExternal = (
469
495
  dependencyMeta,
470
496
  concatenationScope
471
497
  ) => {
498
+ const phase = dependencyMeta && dependencyMeta.phase;
472
499
  /** @type {Imported} */
473
500
  let imported = true;
474
501
  if (concatenationScope) {
@@ -497,6 +524,14 @@ const getSourceForModuleExternal = (
497
524
  imported = true;
498
525
  }
499
526
 
527
+ // `import defer …` is only valid as `import defer * as ns from "…"`, so
528
+ // keep the namespace form even if usage analysis would otherwise narrow
529
+ // the import down to specific names. Defer + concatenation is semantically
530
+ // at odds (lazy vs. eager), so we preserve the user-written shape here.
531
+ if (ImportPhaseUtils.isDefer(phase)) {
532
+ imported = true;
533
+ }
534
+
500
535
  const initFragment = new ModuleExternalInitFragment(
501
536
  moduleAndSpecifiers[0],
502
537
  imported,
@@ -702,13 +737,14 @@ class ExternalModule extends Module {
702
737
  * @returns {SourceTypes} types available (do not mutate)
703
738
  */
704
739
  getSourceTypes() {
705
- if (
706
- this.externalType === "asset" &&
707
- this.dependencyMeta &&
708
- /** @type {AssetDependencyMeta} */
709
- (this.dependencyMeta).sourceType === "css-url"
710
- ) {
711
- return CSS_URL_TYPES;
740
+ if (this.externalType === "asset" && this.dependencyMeta) {
741
+ const sourceType =
742
+ /** @type {AssetDependencyMeta} */
743
+ (this.dependencyMeta).sourceType;
744
+ // TODO webpack 6 drop "css-url" once the alias is removed
745
+ if (sourceType === ASSET_URL_TYPE || sourceType === "css-url") {
746
+ return ASSET_URL_TYPES;
747
+ }
712
748
  } else if (this.externalType === "css-import") {
713
749
  return CSS_IMPORT_TYPES;
714
750
  }
@@ -751,7 +787,21 @@ class ExternalModule extends Module {
751
787
  * @returns {string} a unique identifier of the module
752
788
  */
753
789
  identifier() {
754
- return `external ${this._resolveExternalType(this.externalType)} ${JSON.stringify(this.request)}`;
790
+ let id = `external ${this._resolveExternalType(
791
+ this.externalType
792
+ )} ${JSON.stringify(this.request)}`;
793
+ const meta = /** @type {ImportDependencyMeta | undefined} */ (
794
+ this.dependencyMeta
795
+ );
796
+ if (meta) {
797
+ if (meta.phase) {
798
+ id += `|phase=${ImportPhaseUtils.stringify(meta.phase)}`;
799
+ }
800
+ if (meta.attributes) {
801
+ id += `|attributes=${JSON.stringify(meta.attributes)}`;
802
+ }
803
+ }
804
+ return id;
755
805
  }
756
806
 
757
807
  /**
@@ -1065,12 +1115,14 @@ class ExternalModule extends Module {
1065
1115
  data.set("url", { javascript: /** @type {string} */ (request) });
1066
1116
  return { sources, runtimeRequirements: RUNTIME_REQUIREMENTS, data };
1067
1117
  }
1068
- case "css-url": {
1118
+ // TODO webpack 6 remove "css-url" alias
1119
+ case "css-url":
1120
+ case "asset-url": {
1069
1121
  /** @type {Sources} */
1070
1122
  const sources = new Map();
1071
1123
  /** @type {CodeGenerationResultData} */
1072
1124
  const data = new Map();
1073
- data.set("url", { "css-url": /** @type {string} */ (request) });
1125
+ data.set("url", { [ASSET_URL_TYPE]: /** @type {string} */ (request) });
1074
1126
  return { sources, runtimeRequirements: RUNTIME_REQUIREMENTS, data };
1075
1127
  }
1076
1128
  case "css-import": {
@@ -1126,7 +1178,9 @@ class ExternalModule extends Module {
1126
1178
  scope.registerRawExport(specifier, finalName);
1127
1179
  }
1128
1180
  } else if (concatenationScope) {
1129
- sourceString = `${runtimeTemplate.renderConst()} ${ConcatenationScope.NAMESPACE_OBJECT_EXPORT} = ${sourceString};`;
1181
+ sourceString = `${runtimeTemplate.renderConst()} ${
1182
+ ConcatenationScope.NAMESPACE_OBJECT_EXPORT
1183
+ } = ${sourceString};`;
1130
1184
  concatenationScope.registerNamespaceExport(
1131
1185
  ConcatenationScope.NAMESPACE_OBJECT_EXPORT
1132
1186
  );
@@ -1194,10 +1248,21 @@ class ExternalModule extends Module {
1194
1248
  updateHash(hash, context) {
1195
1249
  const { chunkGraph } = context;
1196
1250
  hash.update(
1197
- `${this._resolveExternalType(this.externalType)}${JSON.stringify(this.request)}${this.isOptional(
1198
- chunkGraph.moduleGraph
1199
- )}`
1251
+ `${this._resolveExternalType(this.externalType)}${JSON.stringify(
1252
+ this.request
1253
+ )}${this.isOptional(chunkGraph.moduleGraph)}`
1200
1254
  );
1255
+ const meta = /** @type {ImportDependencyMeta | undefined} */ (
1256
+ this.dependencyMeta
1257
+ );
1258
+ if (meta) {
1259
+ if (meta.phase) {
1260
+ hash.update(`|phase=${ImportPhaseUtils.stringify(meta.phase)}`);
1261
+ }
1262
+ if (meta.attributes) {
1263
+ hash.update(`|attributes=${JSON.stringify(meta.attributes)}`);
1264
+ }
1265
+ }
1201
1266
  super.updateHash(hash, context);
1202
1267
  }
1203
1268
 
@@ -7,6 +7,7 @@
7
7
 
8
8
  const util = require("util");
9
9
  const ExternalModule = require("./ExternalModule");
10
+ const { ASSET_URL_TYPE } = require("./ModuleSourceTypeConstants");
10
11
  const ContextElementDependency = require("./dependencies/ContextElementDependency");
11
12
  const CssImportDependency = require("./dependencies/CssImportDependency");
12
13
  const CssUrlDependency = require("./dependencies/CssUrlDependency");
@@ -193,6 +194,11 @@ class ExternalModuleFactoryPlugin {
193
194
 
194
195
  dependencyMeta = {
195
196
  attributes: dependency.attributes,
197
+ phase:
198
+ dependency instanceof HarmonyImportDependency ||
199
+ dependency instanceof ImportDependency
200
+ ? dependency.phase
201
+ : undefined,
196
202
  externalType
197
203
  };
198
204
  } else if (dependency instanceof CssImportDependency) {
@@ -207,7 +213,7 @@ class ExternalModuleFactoryPlugin {
207
213
  resolvedType === "asset" &&
208
214
  dependency instanceof CssUrlDependency
209
215
  ) {
210
- dependencyMeta = { sourceType: "css-url" };
216
+ dependencyMeta = { sourceType: ASSET_URL_TYPE };
211
217
  }
212
218
 
213
219
  callback(