webpack 5.112.0 → 5.112.2

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 (263) hide show
  1. package/bin/webpack.js +13 -7
  2. package/lib/Compilation.js +198 -61
  3. package/lib/Compiler.js +12 -25
  4. package/lib/asset/AssetBytesGenerator.js +14 -6
  5. package/lib/asset/AssetBytesParser.js +10 -8
  6. package/lib/asset/AssetGenerator.js +36 -48
  7. package/lib/asset/AssetModule.js +11 -0
  8. package/lib/asset/AssetModulesPlugin.js +5 -4
  9. package/lib/asset/AssetParser.js +8 -8
  10. package/lib/asset/AssetSourceGenerator.js +16 -8
  11. package/lib/asset/AssetSourceParser.js +10 -8
  12. package/lib/asset/WebManifestGenerator.js +11 -6
  13. package/lib/asset/WebManifestParser.js +3 -3
  14. package/lib/async-modules/AsyncModuleHelpers.js +8 -2
  15. package/lib/async-modules/InferAsyncModulesPlugin.js +7 -2
  16. package/lib/cache/CacheFacade.js +1 -1
  17. package/lib/cache/PackFileCacheStrategy.js +6 -10
  18. package/lib/cache/ResolverCachePlugin.js +58 -8
  19. package/lib/config/WebpackOptionsApply.js +8 -28
  20. package/lib/config/cli.js +9 -6
  21. package/lib/config/defaults.js +24 -13
  22. package/lib/config/getClaimedAssetTypes.js +2 -2
  23. package/lib/config/target.js +1 -1
  24. package/lib/context/ContextModule.js +1 -1
  25. package/lib/context/ContextModuleFactory.js +1 -1
  26. package/lib/context/ContextReplacementPlugin.js +3 -9
  27. package/lib/css/CssGenerator.js +40 -47
  28. package/lib/css/CssModule.js +10 -0
  29. package/lib/css/CssModulesPlugin.js +13 -6
  30. package/lib/css/CssParser.js +33 -32
  31. package/lib/css/conventions.js +1 -1
  32. package/lib/css/data.js +224 -219
  33. package/lib/css/syntax-parser.js +1312 -312
  34. package/lib/css/syntax-printer.js +1142 -1025
  35. package/lib/debug/ProfilingPlugin.js +65 -43
  36. package/lib/define/DefinePlugin.js +10 -15
  37. package/lib/define/DotenvPlugin.js +10 -6
  38. package/lib/define/EnvironmentPlugin.js +4 -2
  39. package/lib/dependencies/amd/AMDDefineDependencyParserPlugin.js +1 -2
  40. package/lib/dependencies/amd/AMDRequireDependenciesBlockParserPlugin.js +4 -1
  41. package/lib/dependencies/commonjs/CommonJsExportRequireDependency.js +1 -4
  42. package/lib/dependencies/commonjs/CommonJsExportsParserPlugin.js +18 -33
  43. package/lib/dependencies/commonjs/CommonJsImportsParserPlugin.js +1 -3
  44. package/lib/dependencies/commonjs/CommonJsObjectExportDependency.js +4 -4
  45. package/lib/dependencies/commonjs/CommonJsPlugin.js +2 -5
  46. package/lib/dependencies/commonjs/RequireEnsureDependenciesBlockParserPlugin.js +4 -1
  47. package/lib/dependencies/context/ImportMetaContextDependencyParserPlugin.js +1 -2
  48. package/lib/dependencies/context/ImportMetaGlobDependencyParserPlugin.js +4 -7
  49. package/lib/dependencies/context/ImportMetaGlobHelpers.js +8 -10
  50. package/lib/dependencies/core/CachedConstDependency.js +9 -6
  51. package/lib/dependencies/css/CssIcssExportDependency.js +6 -7
  52. package/lib/dependencies/css/CssIcssImportDependency.js +1 -3
  53. package/lib/dependencies/css/CssUrlDependency.js +2 -6
  54. package/lib/dependencies/esm/ESMDetectionParserPlugin.js +2 -4
  55. package/lib/dependencies/esm/ESMExportExpressionDependency.js +2 -2
  56. package/lib/dependencies/esm/ESMExportImportedSpecifierDependency.js +16 -18
  57. package/lib/dependencies/esm/ESMExportSpecifierDependency.js +20 -3
  58. package/lib/dependencies/esm/ESMExports.js +2 -10
  59. package/lib/dependencies/esm/ESMImportDependency.js +17 -2
  60. package/lib/dependencies/esm/ESMImportDependencyParserPlugin.js +6 -17
  61. package/lib/dependencies/esm/ESMImportGuard.js +11 -23
  62. package/lib/dependencies/esm/ESMImportSideEffectDependency.js +1 -1
  63. package/lib/dependencies/esm/ESMImportSpecifierDependency.js +4 -1
  64. package/lib/dependencies/esm/ESMLiveExportDependency.js +311 -0
  65. package/lib/dependencies/esm/ESMLiveExportParserPlugin.js +862 -0
  66. package/lib/dependencies/esm/ESMModulesPlugin.js +1 -3
  67. package/lib/dependencies/esm/TopLevelAwaitDependency.js +1 -8
  68. package/lib/dependencies/helpers/DynamicExports.js +4 -6
  69. package/lib/dependencies/html/HtmlEntryDependency.js +5 -14
  70. package/lib/dependencies/html/HtmlInlineHtmlDependency.js +1 -4
  71. package/lib/dependencies/html/HtmlInlineScriptDependency.js +2 -6
  72. package/lib/dependencies/html/HtmlInlineStyleDependency.js +1 -4
  73. package/lib/dependencies/import/ImportParserPlugin.js +3 -6
  74. package/lib/dependencies/import-meta/ImportMetaPlugin.js +4 -6
  75. package/lib/dependencies/url/URLParserPlugin.js +1 -1
  76. package/lib/dependencies/worker/WorkerAndWorkletPlugin.js +51 -20
  77. package/lib/devtool/ModuleFilenameHelpers.js +1 -1
  78. package/lib/devtool/SourceMapDevToolPlugin.js +1 -1
  79. package/lib/dll/DelegatedModule.js +3 -3
  80. package/lib/dll/DllModule.js +3 -1
  81. package/lib/dll/DllReferencePlugin.js +1 -4
  82. package/lib/errors/AbstractMethodError.js +1 -1
  83. package/lib/errors/ErrorHelpers.js +2 -1
  84. package/lib/errors/HookWebpackError.js +2 -1
  85. package/lib/errors/JSONParseError.js +4 -4
  86. package/lib/errors/WebpackError.js +3 -2
  87. package/lib/esm/ModuleChunkFormatPlugin.js +36 -1
  88. package/lib/externals/ExternalModule.js +12 -9
  89. package/lib/externals/ExternalModuleFactoryPlugin.js +1 -2
  90. package/lib/externals/ExternalsPlugin.js +1 -1
  91. package/lib/fs/FileSystemInfo.js +4 -4
  92. package/lib/graph/AsyncDependenciesBlock.js +1 -1
  93. package/lib/graph/ChunkGraph.js +5 -0
  94. package/lib/graph/Dependency.js +8 -2
  95. package/lib/graph/ExportsInfo.js +1 -1
  96. package/lib/graph/ModuleGraph.js +14 -3
  97. package/lib/graph/buildChunkGraph.js +17 -19
  98. package/lib/graph/findGraphRoots.js +1 -1
  99. package/lib/hmr/HotModuleReplacement.runtime.js +10 -9
  100. package/lib/hmr/HotModuleReplacementPlugin.js +5 -11
  101. package/lib/hmr/JavascriptHotModuleReplacement.runtime.js +16 -19
  102. package/lib/hmr/LazyCompilationPlugin.js +1 -7
  103. package/lib/html/HtmlGenerator.js +25 -17
  104. package/lib/html/HtmlModule.js +11 -1
  105. package/lib/html/HtmlModulesPlugin.js +12 -11
  106. package/lib/html/HtmlParser.js +10 -9
  107. package/lib/html/builtinEmbeddedRenderer.js +31 -33
  108. package/lib/html/data.js +42 -21
  109. package/lib/html/htmlMinify.js +4 -1
  110. package/lib/html/syntax-parser.js +34 -35
  111. package/lib/html/syntax-printer.js +2 -3
  112. package/lib/html/xmlMinify.js +4 -1
  113. package/lib/ids/HashedModuleIdsPlugin.js +1 -1
  114. package/lib/index.js +13 -12
  115. package/lib/javascript/APIPlugin.js +7 -13
  116. package/lib/javascript/BasicEvaluatedExpression.js +5 -5
  117. package/lib/javascript/CommonJsChunkFormatPlugin.js +2 -7
  118. package/lib/javascript/ConstPlugin.js +1 -1
  119. package/lib/javascript/JavascriptGenerator.js +16 -11
  120. package/lib/javascript/JavascriptMetaInfoPlugin.js +3 -15
  121. package/lib/javascript/JavascriptModule.js +10 -0
  122. package/lib/javascript/JavascriptModulesPlugin.js +2 -4
  123. package/lib/javascript/JavascriptParser.js +99 -11
  124. package/lib/javascript/ScopeAnalyzer.js +183 -107
  125. package/lib/javascript/UseStrictPlugin.js +2 -5
  126. package/lib/javascript/jsMinify.js +1 -1
  127. package/lib/javascript/regexp.js +2 -2
  128. package/lib/javascript/syntax-parser.js +2114 -1506
  129. package/lib/javascript/syntax-printer-data.js +167 -1
  130. package/lib/javascript/syntax-printer.js +9554 -2782
  131. package/lib/json/JsonGenerator.js +15 -7
  132. package/lib/json/JsonModule.js +11 -0
  133. package/lib/json/JsonParser.js +7 -6
  134. package/lib/library/AssignLibraryPlugin.js +1 -1
  135. package/lib/library/JsonpLibraryPlugin.js +1 -1
  136. package/lib/library/ModuleLibraryPlugin.js +399 -23
  137. package/lib/loaders/LoaderRunner.js +76 -38
  138. package/lib/loaders/loadLoader.js +16 -10
  139. package/lib/module/Generator.js +17 -9
  140. package/lib/module/Module.js +2 -2
  141. package/lib/module/NormalModule.js +7 -5
  142. package/lib/module/NormalModuleFactory.js +11 -17
  143. package/lib/module/Parser.js +9 -2
  144. package/lib/module/extractSourceMap.js +69 -18
  145. package/lib/module/findCaseMismatch.js +2 -2
  146. package/lib/node/ExternalModuleInitFragment.js +2 -2
  147. package/lib/node/ReadFileCompileAsyncWasmPlugin.js +4 -12
  148. package/lib/node/ReadFileCompileWasmPlugin.js +2 -6
  149. package/lib/node/nodeConsole.js +4 -3
  150. package/lib/optimize/CircularModulesPlugin.js +1 -1
  151. package/lib/optimize/ConcatenatedModule.js +4 -8
  152. package/lib/optimize/FlagDependencyExportsPlugin.js +6 -11
  153. package/lib/optimize/FlagDependencyUsagePlugin.js +28 -3
  154. package/lib/optimize/InlineExports.js +4 -7
  155. package/lib/optimize/LazyBarrels.js +41 -5
  156. package/lib/optimize/ModuleConcatenationPlugin.js +8 -10
  157. package/lib/optimize/RealContentHashPlugin.js +2 -6
  158. package/lib/optimize/SideEffectsFlagPlugin.js +7 -21
  159. package/lib/optimize/SplitChunksPlugin.js +11 -24
  160. package/lib/optimize/TupleQueue.js +1 -1
  161. package/lib/output/CleanPlugin.js +3 -1
  162. package/lib/output/CopyPlugin.js +3 -4
  163. package/lib/performance/EvalUsagePlugin.js +1 -3
  164. package/lib/performance/HotspotsPlugin.js +33 -9
  165. package/lib/performance/PureAnnotationsPlugin.js +16 -10
  166. package/lib/performance/TopLevelThisPlugin.js +17 -9
  167. package/lib/prefetch/ResourceHintPlugin.js +1 -1
  168. package/lib/provide/ProvidedDependency.js +36 -4
  169. package/lib/rules/BasicEffectRulePlugin.js +1 -1
  170. package/lib/rules/RuleSetCompiler.js +15 -5
  171. package/lib/rules/UseEffectRulePlugin.js +1 -3
  172. package/lib/runtime/AsyncModuleRuntimeModule.js +7 -1
  173. package/lib/runtime/GetChunkFilenameRuntimeModule.js +1 -5
  174. package/lib/runtime/RuntimeGlobals.js +6 -0
  175. package/lib/runtime/RuntimePlugin.js +2 -0
  176. package/lib/schemes/HttpUriPlugin.js +1 -2
  177. package/lib/schemes/VirtualUrlPlugin.js +3 -1
  178. package/lib/serialization/AggregateErrorSerializer.js +3 -3
  179. package/lib/serialization/BinaryMiddleware.js +4 -1
  180. package/lib/serialization/ErrorObjectSerializer.js +2 -5
  181. package/lib/serialization/ObjectMiddleware.js +17 -15
  182. package/lib/serialization/PlainObjectSerializer.js +3 -7
  183. package/lib/serialization/Serializer.js +21 -11
  184. package/lib/serialization/SerializerMiddleware.js +8 -6
  185. package/lib/stats/DefaultStatsFactoryPlugin.js +35 -35
  186. package/lib/stats/DefaultStatsPresetPlugin.js +2 -6
  187. package/lib/stats/DefaultStatsPrinterPlugin.js +12 -11
  188. package/lib/stats/StatsFactory.js +194 -24
  189. package/lib/stats/StatsPrinter.js +126 -13
  190. package/lib/template/DependencyTemplate.js +1 -1
  191. package/lib/template/RuntimeTemplate.js +13 -32
  192. package/lib/template/Template.js +1 -1
  193. package/lib/util/SourceProcessor.js +146 -29
  194. package/lib/util/WeakTupleMap.js +10 -4
  195. package/lib/util/cleverMerge.js +61 -33
  196. package/lib/util/comparators.js +9 -5
  197. package/lib/util/dataURL.js +2 -0
  198. package/lib/util/deprecation.js +4 -3
  199. package/lib/util/globUtils.js +1 -4
  200. package/lib/util/internalSerializables.js +2 -0
  201. package/lib/util/makeSerializable.js +6 -4
  202. package/lib/util/memoize.js +1 -1
  203. package/lib/util/parseJson.js +2 -2
  204. package/lib/util/serialization.js +31 -27
  205. package/lib/wasm/AsyncWasmModule.js +12 -2
  206. package/lib/wasm/AsyncWebAssemblyGenerator.js +12 -4
  207. package/lib/wasm/AsyncWebAssemblyJavascriptGenerator.js +13 -5
  208. package/lib/wasm/AsyncWebAssemblyParser.js +7 -5
  209. package/lib/wasm/UniversalCompileAsyncWasmPlugin.js +2 -6
  210. package/lib/wasm-sync/SyncWasmModule.js +16 -1
  211. package/lib/wasm-sync/WebAssemblyGenerator.js +15 -10
  212. package/lib/wasm-sync/WebAssemblyJavascriptGenerator.js +13 -5
  213. package/lib/wasm-sync/WebAssemblyParser.js +10 -12
  214. package/lib/web/FetchCompileAsyncWasmPlugin.js +4 -12
  215. package/lib/web/FetchCompileWasmPlugin.js +2 -6
  216. package/package.json +18 -19
  217. package/schemas/WebpackOptions.check.js +3 -3
  218. package/schemas/WebpackOptions.json +2 -2
  219. package/schemas/plugins/BannerPlugin.check.js +3 -3
  220. package/schemas/plugins/HtmlGeneratorOptions.check.js +3 -3
  221. package/schemas/plugins/HtmlParserOptions.check.js +3 -3
  222. package/schemas/plugins/IgnorePlugin.check.js +3 -3
  223. package/schemas/plugins/LoaderOptionsPlugin.check.js +3 -3
  224. package/schemas/plugins/ManifestPlugin.check.js +3 -3
  225. package/schemas/plugins/ProgressPlugin.check.js +3 -3
  226. package/schemas/plugins/SSRManifestPlugin.check.js +3 -3
  227. package/schemas/plugins/SourceMapDevToolPlugin.check.js +3 -3
  228. package/schemas/plugins/WatchIgnorePlugin.check.js +3 -3
  229. package/schemas/plugins/asset/AssetGeneratorOptions.check.js +3 -3
  230. package/schemas/plugins/asset/AssetInlineGeneratorOptions.check.js +3 -3
  231. package/schemas/plugins/asset/AssetParserOptions.check.js +3 -3
  232. package/schemas/plugins/asset/AssetResourceGeneratorOptions.check.js +3 -3
  233. package/schemas/plugins/container/ContainerPlugin.check.js +3 -3
  234. package/schemas/plugins/container/ContainerReferencePlugin.check.js +3 -3
  235. package/schemas/plugins/container/ExternalsType.check.js +3 -3
  236. package/schemas/plugins/container/ModuleFederationPlugin.check.js +3 -3
  237. package/schemas/plugins/css/CssAutoOrModuleParserOptions.check.js +3 -3
  238. package/schemas/plugins/css/CssGeneratorOptions.check.js +3 -3
  239. package/schemas/plugins/css/CssModuleGeneratorOptions.check.js +3 -3
  240. package/schemas/plugins/css/CssModuleParserOptions.check.js +3 -3
  241. package/schemas/plugins/css/CssParserOptions.check.js +3 -3
  242. package/schemas/plugins/debug/ProfilingPlugin.check.js +3 -3
  243. package/schemas/plugins/define/DotenvPluginOptions.check.js +7 -0
  244. package/schemas/plugins/define/DotenvPluginOptions.json +3 -0
  245. package/schemas/plugins/dll/DllPlugin.check.js +3 -3
  246. package/schemas/plugins/dll/DllReferencePlugin.check.js +3 -3
  247. package/schemas/plugins/ids/HashedModuleIdsPlugin.check.js +3 -3
  248. package/schemas/plugins/ids/OccurrenceChunkIdsPlugin.check.js +3 -3
  249. package/schemas/plugins/ids/OccurrenceModuleIdsPlugin.check.js +3 -3
  250. package/schemas/plugins/json/JsonModulesPluginGenerator.check.js +3 -3
  251. package/schemas/plugins/json/JsonModulesPluginParser.check.js +3 -3
  252. package/schemas/plugins/optimize/AggressiveSplittingPlugin.check.js +3 -3
  253. package/schemas/plugins/optimize/LimitChunkCountPlugin.check.js +3 -3
  254. package/schemas/plugins/optimize/MergeDuplicateChunksPlugin.check.js +3 -3
  255. package/schemas/plugins/optimize/MinChunkSizePlugin.check.js +3 -3
  256. package/schemas/plugins/output/CleanOptions.check.js +7 -0
  257. package/schemas/plugins/output/CleanOptions.json +3 -0
  258. package/schemas/plugins/schemes/HttpUriPlugin.check.js +3 -3
  259. package/schemas/plugins/schemes/VirtualUrlPlugin.check.js +3 -3
  260. package/schemas/plugins/sharing/ConsumeSharedPlugin.check.js +3 -3
  261. package/schemas/plugins/sharing/ProvideSharedPlugin.check.js +3 -3
  262. package/schemas/plugins/sharing/SharePlugin.check.js +3 -3
  263. package/types.d.ts +1482 -358
@@ -20,15 +20,20 @@ const { isAbsolute, join } = require("../fs/fs");
20
20
  * @typedef {object} SourceMappingURL
21
21
  * @property {string} sourceMappingURL
22
22
  * @property {string} replacementString
23
+ * @property {number} replacementStart
23
24
  */
24
25
 
25
26
  // Matches only the last occurrence of sourceMappingURL
26
27
  const innerRegex = /\s*[#@]\s*sourceMappingURL\s*=\s*([^\s'"]*)\s*/;
27
28
 
28
29
  const validProtocolPattern = /^[a-z][a-z0-9+.-]*:/i;
30
+ const whitespaceRegex = /\s/;
31
+ const lineBreakRegex = /[\r\n\u2028\u2029]/;
32
+ const ambiguousCommentRegex = /['"`]|\*\//;
33
+ const ambiguousBlockCommentRegex = /['"`\]]|\*\/(?!$)/;
29
34
 
30
35
  const sourceMappingURLRegex = new RegExp(
31
- "(?:" +
36
+ "^(?:" +
32
37
  "/\\*" +
33
38
  "(?:\\s*\r?\n(?://)?)?" +
34
39
  `(?:${innerRegex.source})` +
@@ -37,7 +42,7 @@ const sourceMappingURLRegex = new RegExp(
37
42
  "|" +
38
43
  `//(?:${innerRegex.source})` +
39
44
  ")" +
40
- "\\s*"
45
+ "\\s*$"
41
46
  );
42
47
 
43
48
  /**
@@ -46,24 +51,67 @@ const sourceMappingURLRegex = new RegExp(
46
51
  * @returns {SourceMappingURL} source mapping information
47
52
  */
48
53
  function getSourceMappingURL(code) {
49
- const lines = code.split(/^/m);
50
- /** @type {RegExpMatchArray | null | undefined} */
51
- let match;
52
-
53
- for (let i = lines.length - 1; i >= 0; i--) {
54
- match = lines[i].match(sourceMappingURLRegex);
55
- if (match) {
54
+ let end = code.length;
55
+ let lineStart = end;
56
+ // ECMA-426's non-parsing extraction stops at code or ambiguous comments.
57
+ // Block comments also support CSS and existing JavaScript annotations.
58
+ while (end > 0) {
59
+ while (end > 0 && whitespaceRegex.test(code[end - 1])) end--;
60
+ if (end === 0) break;
61
+ // Consecutive block comments can share a long line of generated code.
62
+ if (lineStart >= end) {
63
+ lineStart = end;
64
+ while (lineStart > 0 && !lineBreakRegex.test(code[lineStart - 1])) {
65
+ lineStart--;
66
+ }
67
+ while (lineStart < end && whitespaceRegex.test(code[lineStart])) {
68
+ lineStart++;
69
+ }
70
+ }
71
+ let start = lineStart;
72
+ let comment;
73
+ if (code.startsWith("//", start)) {
74
+ comment = code.slice(start, end);
75
+ if (ambiguousCommentRegex.test(comment)) break;
76
+ } else if (code.endsWith("*/", end)) {
77
+ start = code.lastIndexOf("/*", end - 2);
78
+ if (start < 0) break;
79
+ comment = code.slice(start, end);
80
+ // An escaped opener or a character class can belong to a trailing RegExp.
81
+ if (
82
+ code[start - 1] === "\\" ||
83
+ ambiguousBlockCommentRegex.test(comment)
84
+ ) {
85
+ break;
86
+ }
87
+ // Comments cannot nest; an inner opener may only be comment text.
88
+ if (
89
+ start > 0 &&
90
+ code.lastIndexOf("/*", start - 1) > code.lastIndexOf("*/", start - 1)
91
+ ) {
92
+ break;
93
+ }
94
+ } else {
56
95
  break;
57
96
  }
97
+ const match = sourceMappingURLRegex.exec(comment);
98
+ if (match) {
99
+ const sourceMappingURL = match[1] || match[2] || "";
100
+ return {
101
+ sourceMappingURL: sourceMappingURL
102
+ ? decodeURI(sourceMappingURL)
103
+ : sourceMappingURL,
104
+ replacementString: comment,
105
+ replacementStart: start
106
+ };
107
+ }
108
+ end = start;
58
109
  }
59
110
 
60
- const sourceMappingURL = match ? match[1] || match[2] || "" : "";
61
-
62
111
  return {
63
- sourceMappingURL: sourceMappingURL
64
- ? decodeURI(sourceMappingURL)
65
- : sourceMappingURL,
66
- replacementString: match ? match[0] : ""
112
+ sourceMappingURL: "",
113
+ replacementString: "",
114
+ replacementStart: 0
67
115
  };
68
116
  }
69
117
 
@@ -229,7 +277,8 @@ async function extractSourceMap(stringOrBuffer, resourcePath, readResource) {
229
277
  source: stringOrBuffer,
230
278
  sourceMap: inputSourceMap
231
279
  };
232
- const { sourceMappingURL, replacementString } = getSourceMappingURL(input);
280
+ const { sourceMappingURL, replacementString, replacementStart } =
281
+ getSourceMappingURL(input);
233
282
 
234
283
  if (!sourceMappingURL) {
235
284
  return output;
@@ -310,8 +359,10 @@ async function extractSourceMap(stringOrBuffer, resourcePath, readResource) {
310
359
  }
311
360
 
312
361
  return {
313
- source: input.replace(replacementString, ""),
314
- sourceMap: /** @type {RawSourceMap} */ (newMap)
362
+ source:
363
+ input.slice(0, replacementStart) +
364
+ input.slice(replacementStart + replacementString.length),
365
+ sourceMap: newMap
315
366
  };
316
367
  }
317
368
 
@@ -68,7 +68,7 @@ const findCaseMismatch = (fs, missingPath, callback) => {
68
68
  }
69
69
  fs.readdir(next, (err, nextEntries) => {
70
70
  if (err || !nextEntries) return callback();
71
- walkDown(next, /** @type {string[]} */ (nextEntries), index + 1);
71
+ walkDown(next, nextEntries, index + 1);
72
72
  });
73
73
  };
74
74
 
@@ -89,7 +89,7 @@ const findCaseMismatch = (fs, missingPath, callback) => {
89
89
  return walkUp();
90
90
  }
91
91
  segments.reverse();
92
- walkDown(parent, /** @type {string[]} */ (entries), 0);
92
+ walkDown(parent, entries, 0);
93
93
  });
94
94
  };
95
95
 
@@ -9,7 +9,7 @@ const InitFragment = require("../template/InitFragment");
9
9
  const makeSerializable = require("../util/makeSerializable");
10
10
 
11
11
  /** @import { Source } from "webpack-sources" */
12
- /** @import { GenerateContext } from "../module/Generator" */
12
+ /** @import { ChunkRenderContext as GenerateContext } from "../javascript/JavascriptModulesPlugin" */
13
13
  /** @typedef {{ name: string, value?: string }[]} ArrayImportSpecifiers */
14
14
  /** @typedef {Set<string>} ImportSpecifier */
15
15
  /** @typedef {Map<string, ImportSpecifier>} ImportSpecifiers */
@@ -81,7 +81,7 @@ class ExternalModuleInitFragment extends InitFragment {
81
81
  * @param {GenerateContext} context context
82
82
  * @returns {string | Source | undefined} the source code that will be included as initialization code
83
83
  */
84
- getContent({ runtimeRequirements }) {
84
+ getContent(context) {
85
85
  /** @type {string[]} */
86
86
  const namedImports = [];
87
87
 
@@ -154,9 +154,7 @@ class ReadFileCompileAsyncWasmPlugin {
154
154
  chunk.runtime
155
155
  ) &&
156
156
  needsRuntimeFullHash(
157
- /** @type {string} */ (
158
- compilation.outputOptions.webassemblyModuleFilename
159
- )
157
+ compilation.outputOptions.webassemblyModuleFilename
160
158
  )
161
159
  ) {
162
160
  set.add(RuntimeGlobals.getFullHash);
@@ -165,9 +163,7 @@ class ReadFileCompileAsyncWasmPlugin {
165
163
  chunk,
166
164
  new AsyncWasmLoadingRuntimeModule({
167
165
  fullHashDigest: usesFullHashDigest(
168
- /** @type {string} */ (
169
- compilation.outputOptions.webassemblyModuleFilename
170
- )
166
+ compilation.outputOptions.webassemblyModuleFilename
171
167
  ),
172
168
  generateLoadBinaryCode,
173
169
  supportsStreaming: false
@@ -196,9 +192,7 @@ class ReadFileCompileAsyncWasmPlugin {
196
192
  chunk.runtime
197
193
  ) &&
198
194
  needsRuntimeFullHash(
199
- /** @type {string} */ (
200
- compilation.outputOptions.webassemblyModuleFilename
201
- )
195
+ compilation.outputOptions.webassemblyModuleFilename
202
196
  )
203
197
  ) {
204
198
  set.add(RuntimeGlobals.getFullHash);
@@ -207,9 +201,7 @@ class ReadFileCompileAsyncWasmPlugin {
207
201
  chunk,
208
202
  new AsyncWasmCompileRuntimeModule({
209
203
  fullHashDigest: usesFullHashDigest(
210
- /** @type {string} */ (
211
- compilation.outputOptions.webassemblyModuleFilename
212
- )
204
+ compilation.outputOptions.webassemblyModuleFilename
213
205
  ),
214
206
  generateLoadBinaryCode,
215
207
  supportsStreaming: false
@@ -137,9 +137,7 @@ class ReadFileCompileWasmPlugin {
137
137
  set.add(RuntimeGlobals.moduleCache);
138
138
  if (
139
139
  needsRuntimeFullHash(
140
- /** @type {string} */ (
141
- compilation.outputOptions.webassemblyModuleFilename
142
- )
140
+ compilation.outputOptions.webassemblyModuleFilename
143
141
  )
144
142
  ) {
145
143
  set.add(RuntimeGlobals.getFullHash);
@@ -148,9 +146,7 @@ class ReadFileCompileWasmPlugin {
148
146
  chunk,
149
147
  new WasmChunkLoadingRuntimeModule({
150
148
  fullHashDigest: usesFullHashDigest(
151
- /** @type {string} */ (
152
- compilation.outputOptions.webassemblyModuleFilename
153
- )
149
+ compilation.outputOptions.webassemblyModuleFilename
154
150
  ),
155
151
  generateLoadBinaryCode,
156
152
  supportsStreaming: false,
@@ -129,7 +129,8 @@ module.exports = ({ colors, appendOnly, stream, compiler }) => {
129
129
  };
130
130
 
131
131
  /**
132
- * @param {EXPECTED_ANY[]} statusMessage status message
132
+ * @template T
133
+ * @param {T[]} statusMessage status message
133
134
  * @returns {void}
134
135
  */
135
136
  const setStatusMessage = (statusMessage) => {
@@ -155,10 +156,10 @@ module.exports = ({ colors, appendOnly, stream, compiler }) => {
155
156
  writeStatusMessage();
156
157
  };
157
158
 
158
- /** @type {<T extends unknown[]>(...args: T) => void} */
159
+ /** @type {LoggerConsole["log"]} */
159
160
  const writeGroupMessage = writeColored("<-> ", (str) => c.bold(c.cyan(str)));
160
161
 
161
- /** @type {<T extends unknown[]>(...args: T) => void} */
162
+ /** @type {LoggerConsole["log"]} */
162
163
  const writeGroupCollapsedMessage = writeColored("<+> ", (str) =>
163
164
  c.bold(c.cyan(str))
164
165
  );
@@ -357,7 +357,7 @@ class CircularModulesPlugin {
357
357
  * @returns {number | undefined} bit flags from CircularKind
358
358
  */
359
359
  static getCircularKind(module) {
360
- const buildInfo = /** @type {BuildInfo | undefined} */ (module.buildInfo);
360
+ const buildInfo = module.buildInfo;
361
361
  if (!buildInfo || buildInfo.circularKind === undefined) return undefined;
362
362
  return buildInfo.circularKind;
363
363
  }
@@ -1471,8 +1471,6 @@ class ConcatenatedModule extends Module {
1471
1471
  if (runtimeCondition === false) continue;
1472
1472
  const nonDeferAccess = !defer;
1473
1473
  const module = connection.module;
1474
- if (targets === undefined) targets = [module];
1475
- else if (!targets.includes(module)) targets.push(module);
1476
1474
  const dependency = connection.dependency;
1477
1475
  if (
1478
1476
  dependency !== null &&
@@ -1483,6 +1481,8 @@ class ConcatenatedModule extends Module {
1483
1481
  }
1484
1482
  const entry = referencesMap.get(module);
1485
1483
  if (entry === undefined) {
1484
+ if (targets === undefined) targets = [module];
1485
+ else targets.push(module);
1486
1486
  referencesMap.set(module, {
1487
1487
  connection,
1488
1488
  runtimeCondition,
@@ -2342,9 +2342,7 @@ class ConcatenatedModule extends Module {
2342
2342
  newName &&
2343
2343
  concatenatedModuleInfo.call(
2344
2344
  {
2345
- rawExportMap: new Map([
2346
- [exportId, /** @type {string} */ (newName)]
2347
- ])
2345
+ rawExportMap: new Map([[exportId, newName]])
2348
2346
  },
2349
2347
  /** @type {ConcatenatedModuleInfo} */ (referencedInfo)
2350
2348
  );
@@ -2820,9 +2818,7 @@ ${defineGetters}`
2820
2818
  if (!info.wrapped) {
2821
2819
  const source = namespaceObjectSources.get(info);
2822
2820
  if (source) result.add(source);
2823
- const escapeSource = escapeNamespaceObjectSources.get(
2824
- /** @type {ConcatenatedModuleInfo} */ (info)
2825
- );
2821
+ const escapeSource = escapeNamespaceObjectSources.get(info);
2826
2822
  if (escapeSource) result.add(escapeSource);
2827
2823
  }
2828
2824
  } else if (info.type === "external" && info.deferred) {
@@ -39,7 +39,7 @@ const PLUGIN_LOGGER_NAME = `webpack.${PLUGIN_NAME}`;
39
39
  */
40
40
  const getProvidedExportsEtag = (module, lazyBarrels, hashFunction) => {
41
41
  const lazyRequests = lazyBarrels.getLazyRequests(module);
42
- const buildInfo = /** @type {BuildInfo | undefined} */ (module.buildInfo);
42
+ const buildInfo = module.buildInfo;
43
43
  const valueDependencies = buildInfo && buildInfo.valueDependencies;
44
44
  const hasValueDependencies =
45
45
  valueDependencies !== undefined && valueDependencies.size > 0;
@@ -145,9 +145,8 @@ class FlagDependencyExportsPlugin {
145
145
  exportsInfo.restoreProvided(memCacheEntry.data);
146
146
  return callback();
147
147
  }
148
- const buildHash = /** @type {string} */ (
149
- /** @type {BuildInfo} */ (module.buildInfo).hash
150
- );
148
+ const buildHash = /** @type {BuildInfo} */ (module.buildInfo)
149
+ .hash;
151
150
  cache.get(
152
151
  module.identifier(),
153
152
  etag ? `${buildHash}|${etag}` : buildHash,
@@ -391,10 +390,7 @@ class FlagDependencyExportsPlugin {
391
390
  if (exports) {
392
391
  const nestedExportsInfo =
393
392
  exportInfo.createNestedExportsInfo();
394
- mergeExports(
395
- /** @type {ExportsInfo} */ (nestedExportsInfo),
396
- exports
397
- );
393
+ mergeExports(nestedExportsInfo, exports);
398
394
  }
399
395
 
400
396
  if (
@@ -545,9 +541,8 @@ class FlagDependencyExportsPlugin {
545
541
  };
546
542
  memCache.set(this, entry);
547
543
  }
548
- const buildHash = /** @type {string} */ (
549
- /** @type {BuildInfo} */ (module.buildInfo).hash
550
- );
544
+ const buildHash = /** @type {BuildInfo} */ (module.buildInfo)
545
+ .hash;
551
546
  cache.store(
552
547
  module.identifier(),
553
548
  etag ? `${buildHash}|${etag}` : buildHash,
@@ -16,7 +16,7 @@ const TupleQueue = require("./TupleQueue");
16
16
 
17
17
  /** @import Compiler from "../Compiler" */
18
18
  /** @import DependenciesBlock from "../graph/DependenciesBlock" */
19
- /** @import { ReferencedExport, ReferencedExports } from "../graph/Dependency" */
19
+ /** @import { ExportInfoName, ReferencedExport, ReferencedExports } from "../graph/Dependency" */
20
20
  /** @import ExportsInfo from "../graph/ExportsInfo" */
21
21
  /** @import Module from "../module/Module" */
22
22
  /** @import { RuntimeSpec } from "../util/runtime" */
@@ -77,6 +77,25 @@ class FlagDependencyUsagePlugin {
77
77
  /** @type {Map<ExportsInfo, Module>} */
78
78
  const exportInfoToModuleMap = new Map();
79
79
 
80
+ /**
81
+ * Returns the module owning an export of a nested exports info. A
82
+ * nested exports info redirecting to a module's exports owns only
83
+ * its own names, the rest belong to the redirect target.
84
+ * @param {ExportsInfo} exportsInfo nested exports info
85
+ * @param {ExportInfoName} name export name
86
+ * @returns {Module | undefined} the module owning the export
87
+ */
88
+ const getExportOwnerModule = (exportsInfo, name) => {
89
+ /** @type {ExportsInfo | undefined} */
90
+ let current = exportsInfo;
91
+ while (current !== undefined) {
92
+ const owner = exportInfoToModuleMap.get(current);
93
+ if (owner !== undefined) return owner;
94
+ if (current._exports.has(name)) return;
95
+ current = current._redirectTo;
96
+ }
97
+ };
98
+
80
99
  /** @type {TupleQueue<[Module], RuntimeSpec>} */
81
100
  const queue = new TupleQueue();
82
101
 
@@ -179,7 +198,10 @@ class FlagDependencyUsagePlugin {
179
198
  const currentModule =
180
199
  currentExportsInfo === exportsInfo
181
200
  ? module
182
- : exportInfoToModuleMap.get(currentExportsInfo);
201
+ : getExportOwnerModule(
202
+ currentExportsInfo,
203
+ usedExport[i]
204
+ );
183
205
  if (currentModule) {
184
206
  queue.enqueue(currentModule, runtime);
185
207
  }
@@ -198,7 +220,10 @@ class FlagDependencyUsagePlugin {
198
220
  const currentModule =
199
221
  currentExportsInfo === exportsInfo
200
222
  ? module
201
- : exportInfoToModuleMap.get(currentExportsInfo);
223
+ : getExportOwnerModule(
224
+ currentExportsInfo,
225
+ usedExport[i]
226
+ );
202
227
  if (currentModule) {
203
228
  queue.enqueue(currentModule, runtime);
204
229
  }
@@ -170,11 +170,7 @@ const isInlineExportsEnabled = (moduleGraph) =>
170
170
  * @returns {boolean} true when inlining is enabled for this module
171
171
  */
172
172
  const isInlineEnabled = (module) =>
173
- Boolean(
174
- module.buildInfo &&
175
- /** @type {import("../module/Module").BuildInfo} */
176
- (module.buildInfo).inlineExports
177
- );
173
+ Boolean(module.buildInfo && module.buildInfo.inlineExports);
178
174
 
179
175
  /**
180
176
  * Check whether an import to `module` for `ids` resolves to an inlined value.
@@ -192,8 +188,9 @@ const isExportInlined = (moduleGraph, module, ids, runtime) => {
192
188
 
193
189
  /**
194
190
  * Whether a used name is an inlined value rather than a property name.
195
- * @param {unknown} value a used name
196
- * @returns {value is InlinedUsedName} true when the value is inlined
191
+ * @template V
192
+ * @param {V} value a used name
193
+ * @returns {value is Extract<V, InlinedUsedName>} true when the value is inlined
197
194
  */
198
195
  const isInlinedUsedName = (value) => value instanceof InlinedUsedName;
199
196
 
@@ -5,7 +5,9 @@
5
5
 
6
6
  "use strict";
7
7
 
8
+ const { ImportPhaseUtils } = require("../dependencies/core/ImportPhase");
8
9
  const Dependency = require("../graph/Dependency");
10
+ const isDependencyKind = require("../graph/isDependencyKind");
9
11
 
10
12
  /** @import Compilation, { DependencyConstructor } from "../Compilation" */
11
13
  /** @import ModuleGraph from "../graph/ModuleGraph" */
@@ -76,13 +78,23 @@ const getRequestKey = (dependency) => {
76
78
  };
77
79
 
78
80
  /**
79
- * Returns true if names were requested and none of them is a local export.
80
- * Only local exports use the import declarations, so the barrel can defer them until a local export is requested.
81
+ * Returns whether the re-export reads an `import defer` namespace, which `SideEffectsFlagPlugin` keeps on the barrel.
82
+ * @param {Dependency} dependency the re-export dependency
83
+ * @returns {boolean} true, when requesting it runs the barrel
84
+ */
85
+ const isDeferredReexport = (dependency) =>
86
+ isDependencyKind(dependency, "esm-import") &&
87
+ ImportPhaseUtils.isDefer(dependency.phase);
88
+
89
+ /**
90
+ * Returns true if names were requested and none of them runs the barrel.
91
+ * Only a running barrel uses the import declarations, so it can defer them until such a name is requested.
81
92
  * @param {Set<string> | true | undefined} forwardedIds names requested before classification
82
93
  * @param {Set<string>} localIds locally provided export names
94
+ * @param {Map<string, string> | undefined} deferredReexports names re-exported from an `import defer` namespace
83
95
  * @returns {boolean} true, when import declarations can be deferred
84
96
  */
85
- const canLazyImports = (forwardedIds, localIds) => {
97
+ const canLazyImports = (forwardedIds, localIds, deferredReexports) => {
86
98
  if (
87
99
  forwardedIds === undefined ||
88
100
  forwardedIds === true ||
@@ -92,6 +104,9 @@ const canLazyImports = (forwardedIds, localIds) => {
92
104
  }
93
105
  for (const id of forwardedIds) {
94
106
  if (localIds.has(id)) return false;
107
+ if (deferredReexports !== undefined && deferredReexports.has(id)) {
108
+ return false;
109
+ }
95
110
  }
96
111
  return true;
97
112
  };
@@ -319,6 +334,11 @@ class LazyBarrels {
319
334
  this._modules = new WeakMap();
320
335
  /** @type {WeakSet<Dependency>} */
321
336
  this._lazyDependencies = new WeakSet();
337
+ // without provided exports no import is redirected past the barrel, so it runs
338
+ /** @type {boolean} */
339
+ this._canLazyImports = Boolean(
340
+ compilation.options.optimization.providedExports
341
+ );
322
342
  lazyBarrelsByModuleGraph.set(compilation.moduleGraph, this);
323
343
  }
324
344
 
@@ -429,6 +449,8 @@ class LazyBarrels {
429
449
  /** @type {Set<string>} */
430
450
  const localIds = new Set();
431
451
  let hasFallbackStarReexport = false;
452
+ /** @type {Map<string, string> | undefined} export name -> request key */
453
+ let deferredReexports;
432
454
 
433
455
  // re-export starts deferred and this compilation's requests activate the ones it needs
434
456
  for (const dep of dependencies) {
@@ -451,7 +473,12 @@ class LazyBarrels {
451
473
  if (info === undefined) info = new LazyBarrelInfo(this._lazyDependencies);
452
474
  info.addLazy(requestKey, dep, factory);
453
475
  if (role === Dependency.LAZY_NAMED_REEXPORT) {
454
- info.addForwardId(/** @type {string} */ (dep.lazyName), requestKey);
476
+ const name = /** @type {string} */ (dep.lazyName);
477
+ info.addForwardId(name, requestKey);
478
+ if (isDeferredReexport(dep)) {
479
+ if (deferredReexports === undefined) deferredReexports = new Map();
480
+ deferredReexports.set(name, requestKey);
481
+ }
455
482
  } else if (role === Dependency.LAZY_STAR_REEXPORT) {
456
483
  info.addFallback(requestKey);
457
484
  hasFallbackStarReexport = true;
@@ -461,7 +488,10 @@ class LazyBarrels {
461
488
  // import declarations
462
489
  /** @type {Set<string> | undefined} */
463
490
  let importRequests;
464
- if (canLazyImports(forwardedIds, localIds)) {
491
+ if (
492
+ this._canLazyImports &&
493
+ canLazyImports(forwardedIds, localIds, deferredReexports)
494
+ ) {
465
495
  for (const dep of dependencies) {
466
496
  if (dep.lazyRole !== Dependency.LAZY_IMPORT_DECLARATION) continue;
467
497
  const factory = this._getDepFactory(dep);
@@ -478,6 +508,12 @@ class LazyBarrels {
478
508
 
479
509
  if (info === undefined) return;
480
510
 
511
+ if (importRequests !== undefined && deferredReexports !== undefined) {
512
+ for (const [id, requestKey] of deferredReexports) {
513
+ info.addForwardId(id, new Set([...importRequests, requestKey]));
514
+ }
515
+ }
516
+
481
517
  // local exports: known names, so they skip the star fallback but active the deferred imports
482
518
  if (hasFallbackStarReexport || importRequests !== undefined) {
483
519
  const requests =
@@ -32,6 +32,8 @@ const { STAGE_DEFAULT } = require("./OptimizationStages");
32
32
  /** @import { BuildInfo, BuildMeta } from "../module/Module" */
33
33
  /** @import RequestShortener from "../util/RequestShortener" */
34
34
  /** @import { RuntimeSpec } from "../util/runtime" */
35
+ /** @import { ResolverWithOptions } from "../resolve/ResolverFactory" */
36
+ /** @import { InputFileSystem } from "../fs/fs" */
35
37
 
36
38
  /** @typedef {Module | ((requestShortener: RequestShortener) => string)} Problem */
37
39
 
@@ -112,11 +114,6 @@ class ModuleConcatenationPlugin {
112
114
  apply(compiler) {
113
115
  const { _backCompat: backCompat } = compiler;
114
116
  compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
115
- if (compilation.moduleMemCaches) {
116
- throw new Error(
117
- "optimization.concatenateModules can't be used with cacheUnaffected as module concatenation is a global effect"
118
- );
119
- }
120
117
  const moduleGraph = compilation.moduleGraph;
121
118
  /** @type {Map<Module, string | ((requestShortener: RequestShortener) => string)>} */
122
119
  const bailoutReasonMap = new Map();
@@ -196,6 +193,7 @@ class ModuleConcatenationPlugin {
196
193
  stage: STAGE_DEFAULT
197
194
  },
198
195
  (allChunks, modules, callback) => {
196
+ // set once the modules are built, so not yet when the compilation starts
199
197
  const logger = compilation.getLogger(
200
198
  "webpack.ModuleConcatenationPlugin"
201
199
  );
@@ -523,10 +521,10 @@ class ModuleConcatenationPlugin {
523
521
  newModule.build(
524
522
  compilation.options,
525
523
  compilation,
526
- /** @type {EXPECTED_ANY} */
527
- (null),
528
- /** @type {EXPECTED_ANY} */
529
- (null),
524
+ /** @type {ResolverWithOptions} */
525
+ (/** @type {ResolverWithOptions | null} */ (null)),
526
+ /** @type {InputFileSystem} */
527
+ (/** @type {InputFileSystem | null} */ (null)),
530
528
  (err) => {
531
529
  if (err) {
532
530
  if (!err.module) {
@@ -1027,7 +1025,7 @@ class ModuleConcatenationPlugin {
1027
1025
  )} (expected runtime ${runtimeToString(
1028
1026
  runtime
1029
1027
  )}, module is only referenced in ${runtimeToString(
1030
- /** @type {RuntimeSpec} */ (runtimeCondition)
1028
+ runtimeCondition
1031
1029
  )})`
1032
1030
  ).join(", ")}`;
1033
1031
  return cacheFailure(
@@ -600,9 +600,7 @@ ${referencingAssets
600
600
  );
601
601
  }
602
602
  const digest = hash.digest(
603
- /** @type {HashDigest} */ (
604
- hashToDigest.get(oldHash) || this._hashDigest
605
- )
603
+ hashToDigest.get(oldHash) || this._hashDigest
606
604
  );
607
605
  hashToNewHash.set(oldHash, digest.slice(0, oldHash.length));
608
606
  }
@@ -650,9 +648,7 @@ ${referencingAssets
650
648
  for (const c of chunks) hash.update(c);
651
649
  }
652
650
  const digest = hash.digest(
653
- /** @type {HashDigest} */ (
654
- hashToDigest.get(oldHash) || this._hashDigest
655
- )
651
+ hashToDigest.get(oldHash) || this._hashDigest
656
652
  );
657
653
  newHash = digest.slice(0, oldHash.length);
658
654
  }