webpack 5.97.1 → 5.104.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 (610) hide show
  1. package/README.md +368 -434
  2. package/bin/webpack.js +15 -9
  3. package/hot/dev-server.js +18 -3
  4. package/hot/emitter-event-target.js +7 -0
  5. package/hot/lazy-compilation-node.js +45 -29
  6. package/hot/lazy-compilation-universal.js +18 -0
  7. package/hot/lazy-compilation-web.js +15 -5
  8. package/hot/load-http.js +7 -0
  9. package/hot/log.js +15 -15
  10. package/hot/only-dev-server.js +19 -4
  11. package/hot/poll.js +1 -0
  12. package/lib/APIPlugin.js +48 -50
  13. package/lib/AbstractMethodError.js +1 -0
  14. package/lib/AsyncDependenciesBlock.js +4 -5
  15. package/lib/AutomaticPrefetchPlugin.js +22 -22
  16. package/lib/BannerPlugin.js +35 -40
  17. package/lib/Cache.js +12 -9
  18. package/lib/CacheFacade.js +13 -13
  19. package/lib/CaseSensitiveModulesWarning.js +6 -6
  20. package/lib/Chunk.js +68 -47
  21. package/lib/ChunkGraph.js +174 -95
  22. package/lib/ChunkGroup.js +32 -24
  23. package/lib/ChunkTemplate.js +8 -8
  24. package/lib/CleanPlugin.js +90 -42
  25. package/lib/CodeGenerationResults.js +12 -9
  26. package/lib/CompatibilityPlugin.js +70 -21
  27. package/lib/Compilation.js +827 -550
  28. package/lib/Compiler.js +137 -109
  29. package/lib/ConcatenationScope.js +45 -8
  30. package/lib/ConditionalInitFragment.js +1 -1
  31. package/lib/ConstPlugin.js +89 -59
  32. package/lib/ContextExclusionPlugin.js +5 -4
  33. package/lib/ContextModule.js +66 -51
  34. package/lib/ContextModuleFactory.js +51 -30
  35. package/lib/ContextReplacementPlugin.js +89 -34
  36. package/lib/CssModule.js +8 -4
  37. package/lib/DefinePlugin.js +209 -57
  38. package/lib/DelegatedModule.js +34 -23
  39. package/lib/DelegatedModuleFactoryPlugin.js +27 -24
  40. package/lib/DelegatedPlugin.js +4 -2
  41. package/lib/DependenciesBlock.js +0 -2
  42. package/lib/Dependency.js +37 -19
  43. package/lib/DependencyTemplate.js +2 -0
  44. package/lib/DependencyTemplates.js +5 -6
  45. package/lib/DllEntryPlugin.js +8 -4
  46. package/lib/DllModule.js +11 -12
  47. package/lib/DllModuleFactory.js +2 -2
  48. package/lib/DllPlugin.js +9 -6
  49. package/lib/DllReferencePlugin.js +60 -65
  50. package/lib/DotenvPlugin.js +462 -0
  51. package/lib/DynamicEntryPlugin.js +8 -8
  52. package/lib/EntryOptionPlugin.js +8 -6
  53. package/lib/EntryPlugin.js +5 -3
  54. package/lib/Entrypoint.js +19 -0
  55. package/lib/EnvironmentNotSupportAsyncWarning.js +0 -3
  56. package/lib/EnvironmentPlugin.js +26 -19
  57. package/lib/ErrorHelpers.js +6 -6
  58. package/lib/EvalDevToolModulePlugin.js +17 -14
  59. package/lib/EvalSourceMapDevToolPlugin.js +156 -142
  60. package/lib/ExportsInfo.js +180 -115
  61. package/lib/ExportsInfoApiPlugin.js +2 -2
  62. package/lib/ExternalModule.js +256 -102
  63. package/lib/ExternalModuleFactoryPlugin.js +66 -29
  64. package/lib/ExternalsPlugin.js +57 -2
  65. package/lib/FileSystemInfo.js +313 -247
  66. package/lib/FlagAllModulesAsUsedPlugin.js +2 -2
  67. package/lib/FlagDependencyExportsPlugin.js +29 -19
  68. package/lib/FlagDependencyUsagePlugin.js +9 -10
  69. package/lib/FlagEntryExportAsUsedPlugin.js +1 -1
  70. package/lib/Generator.js +43 -10
  71. package/lib/GraphHelpers.js +11 -3
  72. package/lib/HookWebpackError.js +35 -7
  73. package/lib/HotModuleReplacementPlugin.js +149 -108
  74. package/lib/HotUpdateChunk.js +0 -3
  75. package/lib/IgnoreErrorModuleFactory.js +2 -2
  76. package/lib/IgnorePlugin.js +16 -13
  77. package/lib/IgnoreWarningsPlugin.js +6 -4
  78. package/lib/InitFragment.js +41 -23
  79. package/lib/InvalidDependenciesModuleWarning.js +2 -3
  80. package/lib/JavascriptMetaInfoPlugin.js +2 -4
  81. package/lib/LibManifestPlugin.js +12 -13
  82. package/lib/LoaderOptionsPlugin.js +12 -14
  83. package/lib/LoaderTargetPlugin.js +5 -3
  84. package/lib/MainTemplate.js +18 -29
  85. package/lib/ManifestPlugin.js +235 -0
  86. package/lib/Module.js +138 -60
  87. package/lib/ModuleBuildError.js +4 -2
  88. package/lib/ModuleDependencyError.js +4 -3
  89. package/lib/ModuleDependencyWarning.js +4 -3
  90. package/lib/ModuleError.js +1 -1
  91. package/lib/ModuleFactory.js +10 -3
  92. package/lib/ModuleFilenameHelpers.js +63 -60
  93. package/lib/ModuleGraph.js +195 -70
  94. package/lib/ModuleGraphConnection.js +14 -20
  95. package/lib/ModuleInfoHeaderPlugin.js +15 -16
  96. package/lib/ModuleNotFoundError.js +1 -1
  97. package/lib/ModuleParseError.js +8 -4
  98. package/lib/ModuleSourceTypeConstants.js +189 -0
  99. package/lib/ModuleTemplate.js +7 -8
  100. package/lib/ModuleTypeConstants.js +37 -15
  101. package/lib/ModuleWarning.js +1 -1
  102. package/lib/MultiCompiler.js +64 -49
  103. package/lib/MultiStats.js +19 -14
  104. package/lib/MultiWatching.js +7 -11
  105. package/lib/NoEmitOnErrorsPlugin.js +5 -3
  106. package/lib/NodeStuffPlugin.js +438 -126
  107. package/lib/NormalModule.js +322 -201
  108. package/lib/NormalModuleFactory.js +230 -117
  109. package/lib/NormalModuleReplacementPlugin.js +37 -39
  110. package/lib/NullFactory.js +3 -2
  111. package/lib/OptimizationStages.js +1 -1
  112. package/lib/OptionsApply.js +1 -1
  113. package/lib/Parser.js +5 -3
  114. package/lib/PlatformPlugin.js +3 -1
  115. package/lib/PrefetchPlugin.js +6 -4
  116. package/lib/ProgressPlugin.js +48 -49
  117. package/lib/ProvidePlugin.js +7 -5
  118. package/lib/RawModule.js +26 -12
  119. package/lib/RecordIdsPlugin.js +88 -110
  120. package/lib/RequestShortener.js +3 -1
  121. package/lib/ResolverFactory.js +15 -14
  122. package/lib/RuntimeGlobals.js +216 -153
  123. package/lib/RuntimeModule.js +7 -6
  124. package/lib/RuntimePlugin.js +115 -62
  125. package/lib/RuntimeTemplate.js +248 -76
  126. package/lib/SelfModuleFactory.js +2 -2
  127. package/lib/SizeFormatHelpers.js +2 -2
  128. package/lib/SourceMapDevToolModuleOptionsPlugin.js +17 -27
  129. package/lib/SourceMapDevToolPlugin.js +72 -59
  130. package/lib/Stats.js +2 -3
  131. package/lib/Template.js +31 -24
  132. package/lib/TemplatedPathPlugin.js +24 -21
  133. package/lib/UseStrictPlugin.js +1 -1
  134. package/lib/WarnCaseSensitiveModulesPlugin.js +36 -37
  135. package/lib/WarnDeprecatedOptionPlugin.js +7 -8
  136. package/lib/WarnNoModeSetPlugin.js +3 -1
  137. package/lib/WatchIgnorePlugin.js +11 -9
  138. package/lib/Watching.js +27 -29
  139. package/lib/WebpackError.js +10 -3
  140. package/lib/WebpackIsIncludedPlugin.js +4 -5
  141. package/lib/WebpackOptionsApply.js +263 -135
  142. package/lib/asset/AssetBytesGenerator.js +171 -0
  143. package/lib/asset/AssetBytesParser.js +37 -0
  144. package/lib/asset/AssetGenerator.js +285 -223
  145. package/lib/asset/AssetModulesPlugin.js +103 -41
  146. package/lib/asset/AssetParser.js +7 -3
  147. package/lib/asset/AssetSourceGenerator.js +41 -17
  148. package/lib/asset/RawDataUrlModule.js +17 -9
  149. package/lib/async-modules/AsyncModuleHelpers.js +52 -0
  150. package/lib/async-modules/AwaitDependenciesInitFragment.js +39 -24
  151. package/lib/async-modules/InferAsyncModulesPlugin.js +25 -26
  152. package/lib/buildChunkGraph.js +36 -17
  153. package/lib/cache/AddBuildDependenciesPlugin.js +5 -6
  154. package/lib/cache/IdleFileCachePlugin.js +22 -20
  155. package/lib/cache/MemoryCachePlugin.js +3 -3
  156. package/lib/cache/MemoryWithGcCachePlugin.js +17 -11
  157. package/lib/cache/PackFileCacheStrategy.js +198 -170
  158. package/lib/cache/ResolverCachePlugin.js +58 -48
  159. package/lib/cache/getLazyHashedEtag.js +5 -4
  160. package/lib/cli.js +197 -39
  161. package/lib/config/browserslistTargetHandler.js +110 -85
  162. package/lib/config/defaults.js +514 -113
  163. package/lib/config/normalization.js +91 -64
  164. package/lib/config/target.js +30 -18
  165. package/lib/container/ContainerEntryDependency.js +0 -1
  166. package/lib/container/ContainerEntryModule.js +16 -15
  167. package/lib/container/ContainerEntryModuleFactory.js +2 -2
  168. package/lib/container/ContainerPlugin.js +5 -6
  169. package/lib/container/ContainerReferencePlugin.js +27 -30
  170. package/lib/container/FallbackDependency.js +2 -1
  171. package/lib/container/FallbackModule.js +19 -14
  172. package/lib/container/FallbackModuleFactory.js +2 -2
  173. package/lib/container/HoistContainerReferencesPlugin.js +7 -7
  174. package/lib/container/ModuleFederationPlugin.js +4 -4
  175. package/lib/container/RemoteModule.js +14 -14
  176. package/lib/container/RemoteRuntimeModule.js +2 -2
  177. package/lib/container/options.js +9 -9
  178. package/lib/css/CssGenerator.js +366 -74
  179. package/lib/css/CssLoadingRuntimeModule.js +26 -16
  180. package/lib/css/CssMergeStyleSheetsRuntimeModule.js +56 -0
  181. package/lib/css/CssModulesPlugin.js +191 -142
  182. package/lib/css/CssParser.js +1756 -740
  183. package/lib/css/walkCssTokens.js +180 -67
  184. package/lib/debug/ProfilingPlugin.js +135 -51
  185. package/lib/dependencies/AMDDefineDependencyParserPlugin.js +29 -28
  186. package/lib/dependencies/AMDPlugin.js +18 -11
  187. package/lib/dependencies/AMDRequireArrayDependency.js +5 -4
  188. package/lib/dependencies/AMDRequireContextDependency.js +2 -1
  189. package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +35 -26
  190. package/lib/dependencies/AMDRuntimeModules.js +3 -1
  191. package/lib/dependencies/CachedConstDependency.js +24 -18
  192. package/lib/dependencies/CommonJsExportRequireDependency.js +31 -24
  193. package/lib/dependencies/CommonJsExportsDependency.js +2 -1
  194. package/lib/dependencies/CommonJsExportsParserPlugin.js +50 -36
  195. package/lib/dependencies/CommonJsFullRequireDependency.js +7 -13
  196. package/lib/dependencies/CommonJsImportsParserPlugin.js +146 -127
  197. package/lib/dependencies/CommonJsPlugin.js +29 -18
  198. package/lib/dependencies/CommonJsRequireContextDependency.js +4 -3
  199. package/lib/dependencies/CommonJsSelfReferenceDependency.js +4 -4
  200. package/lib/dependencies/ConstDependency.js +2 -2
  201. package/lib/dependencies/ContextDependency.js +10 -5
  202. package/lib/dependencies/ContextDependencyHelpers.js +21 -13
  203. package/lib/dependencies/ContextDependencyTemplateAsId.js +10 -9
  204. package/lib/dependencies/ContextDependencyTemplateAsRequireCall.js +13 -10
  205. package/lib/dependencies/ContextElementDependency.js +23 -12
  206. package/lib/dependencies/CssIcssExportDependency.js +402 -22
  207. package/lib/dependencies/CssIcssImportDependency.js +116 -51
  208. package/lib/dependencies/CssIcssSymbolDependency.js +33 -35
  209. package/lib/dependencies/CssImportDependency.js +17 -14
  210. package/lib/dependencies/CssUrlDependency.js +8 -13
  211. package/lib/dependencies/DynamicExports.js +19 -19
  212. package/lib/dependencies/ExportsInfoDependency.js +13 -10
  213. package/lib/dependencies/ExternalModuleDependency.js +7 -7
  214. package/lib/dependencies/ExternalModuleInitFragment.js +3 -2
  215. package/lib/dependencies/ExternalModuleInitFragmentDependency.js +96 -0
  216. package/lib/dependencies/HarmonyAcceptDependency.js +96 -5
  217. package/lib/dependencies/HarmonyAcceptImportDependency.js +2 -5
  218. package/lib/dependencies/HarmonyCompatibilityDependency.js +0 -1
  219. package/lib/dependencies/HarmonyDetectionParserPlugin.js +10 -30
  220. package/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js +19 -8
  221. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +136 -99
  222. package/lib/dependencies/HarmonyExportExpressionDependency.js +1 -1
  223. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +152 -61
  224. package/lib/dependencies/HarmonyExportInitFragment.js +2 -2
  225. package/lib/dependencies/HarmonyExportSpecifierDependency.js +2 -2
  226. package/lib/dependencies/HarmonyExports.js +5 -5
  227. package/lib/dependencies/HarmonyImportDependency.js +92 -45
  228. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +201 -163
  229. package/lib/dependencies/HarmonyImportSideEffectDependency.js +5 -6
  230. package/lib/dependencies/HarmonyImportSpecifierDependency.js +69 -42
  231. package/lib/dependencies/HarmonyModulesPlugin.js +16 -11
  232. package/lib/dependencies/HarmonyTopLevelThisParserPlugin.js +15 -15
  233. package/lib/dependencies/ImportContextDependency.js +15 -1
  234. package/lib/dependencies/ImportDependency.js +25 -7
  235. package/lib/dependencies/ImportEagerDependency.js +7 -5
  236. package/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +23 -13
  237. package/lib/dependencies/ImportMetaContextPlugin.js +2 -2
  238. package/lib/dependencies/ImportMetaPlugin.js +172 -20
  239. package/lib/dependencies/ImportParserPlugin.js +327 -49
  240. package/lib/dependencies/ImportPhase.js +121 -0
  241. package/lib/dependencies/ImportPlugin.js +3 -1
  242. package/lib/dependencies/ImportWeakDependency.js +7 -5
  243. package/lib/dependencies/JsonExportsDependency.js +54 -28
  244. package/lib/dependencies/LoaderDependency.js +0 -3
  245. package/lib/dependencies/LoaderImportDependency.js +0 -3
  246. package/lib/dependencies/LoaderPlugin.js +24 -25
  247. package/lib/dependencies/LocalModulesHelpers.js +4 -4
  248. package/lib/dependencies/ModuleDecoratorDependency.js +2 -4
  249. package/lib/dependencies/ModuleDependency.js +14 -13
  250. package/lib/dependencies/ModuleDependencyTemplateAsRequireId.js +1 -0
  251. package/lib/dependencies/ModuleHotAcceptDependency.js +1 -1
  252. package/lib/dependencies/NullDependency.js +2 -0
  253. package/lib/dependencies/ProvidedDependency.js +8 -10
  254. package/lib/dependencies/PureExpressionDependency.js +1 -2
  255. package/lib/dependencies/RequireContextDependency.js +2 -1
  256. package/lib/dependencies/RequireContextDependencyParserPlugin.js +47 -44
  257. package/lib/dependencies/RequireContextPlugin.js +5 -2
  258. package/lib/dependencies/RequireEnsureDependenciesBlock.js +3 -3
  259. package/lib/dependencies/RequireEnsureDependenciesBlockParserPlugin.js +101 -99
  260. package/lib/dependencies/RequireEnsurePlugin.js +6 -6
  261. package/lib/dependencies/RequireIncludeDependency.js +2 -2
  262. package/lib/dependencies/RequireIncludeDependencyParserPlugin.js +33 -34
  263. package/lib/dependencies/RequireIncludePlugin.js +1 -0
  264. package/lib/dependencies/RequireResolveContextDependency.js +1 -1
  265. package/lib/dependencies/RequireResolveDependency.js +3 -3
  266. package/lib/dependencies/RuntimeRequirementsDependency.js +3 -4
  267. package/lib/dependencies/StaticExportsDependency.js +3 -5
  268. package/lib/dependencies/SystemPlugin.js +4 -4
  269. package/lib/dependencies/URLContextDependency.js +65 -0
  270. package/lib/dependencies/URLDependency.js +3 -8
  271. package/lib/dependencies/URLPlugin.js +18 -159
  272. package/lib/dependencies/WebAssemblyExportImportedDependency.js +3 -3
  273. package/lib/dependencies/WebAssemblyImportDependency.js +2 -2
  274. package/lib/dependencies/WebpackIsIncludedDependency.js +2 -3
  275. package/lib/dependencies/WorkerDependency.js +8 -6
  276. package/lib/dependencies/WorkerPlugin.js +131 -67
  277. package/lib/dependencies/getFunctionExpression.js +2 -2
  278. package/lib/dependencies/processExportInfo.js +4 -4
  279. package/lib/esm/ExportWebpackRequireRuntimeModule.js +1 -1
  280. package/lib/esm/ModuleChunkFormatPlugin.js +232 -179
  281. package/lib/esm/ModuleChunkLoadingPlugin.js +112 -57
  282. package/lib/esm/ModuleChunkLoadingRuntimeModule.js +102 -28
  283. package/lib/formatLocation.js +2 -2
  284. package/lib/hmr/HotModuleReplacement.runtime.js +41 -29
  285. package/lib/hmr/HotModuleReplacementRuntimeModule.js +1 -1
  286. package/lib/hmr/JavascriptHotModuleReplacement.runtime.js +43 -33
  287. package/lib/hmr/JavascriptHotModuleReplacementHelper.js +37 -0
  288. package/lib/hmr/LazyCompilationPlugin.js +67 -54
  289. package/lib/hmr/lazyCompilationBackend.js +23 -18
  290. package/lib/ids/ChunkModuleIdRangePlugin.js +14 -11
  291. package/lib/ids/DeterministicChunkIdsPlugin.js +32 -37
  292. package/lib/ids/DeterministicModuleIdsPlugin.js +52 -52
  293. package/lib/ids/HashedModuleIdsPlugin.js +15 -13
  294. package/lib/ids/IdHelpers.js +66 -49
  295. package/lib/ids/NamedChunkIdsPlugin.js +14 -17
  296. package/lib/ids/NamedModuleIdsPlugin.js +12 -14
  297. package/lib/ids/NaturalChunkIdsPlugin.js +6 -4
  298. package/lib/ids/NaturalModuleIdsPlugin.js +4 -3
  299. package/lib/ids/OccurrenceChunkIdsPlugin.js +7 -5
  300. package/lib/ids/OccurrenceModuleIdsPlugin.js +8 -7
  301. package/lib/ids/SyncModuleIdsPlugin.js +18 -13
  302. package/lib/index.js +54 -14
  303. package/lib/javascript/ArrayPushCallbackChunkFormatPlugin.js +116 -119
  304. package/lib/javascript/BasicEvaluatedExpression.js +26 -18
  305. package/lib/javascript/ChunkFormatHelpers.js +70 -0
  306. package/lib/javascript/ChunkHelpers.js +17 -5
  307. package/lib/javascript/CommonJsChunkFormatPlugin.js +114 -141
  308. package/lib/javascript/EnableChunkLoadingPlugin.js +7 -4
  309. package/lib/javascript/JavascriptGenerator.js +122 -100
  310. package/lib/javascript/JavascriptModulesPlugin.js +332 -152
  311. package/lib/javascript/JavascriptParser.js +1488 -1052
  312. package/lib/javascript/JavascriptParserHelpers.js +48 -47
  313. package/lib/javascript/StartupHelpers.js +23 -22
  314. package/lib/json/JsonData.js +4 -4
  315. package/lib/json/JsonGenerator.js +66 -32
  316. package/lib/json/JsonModulesPlugin.js +16 -6
  317. package/lib/json/JsonParser.js +23 -8
  318. package/lib/library/AbstractLibraryPlugin.js +45 -10
  319. package/lib/library/AmdLibraryPlugin.js +7 -5
  320. package/lib/library/AssignLibraryPlugin.js +71 -17
  321. package/lib/library/EnableLibraryPlugin.js +51 -25
  322. package/lib/library/ExportPropertyLibraryPlugin.js +12 -18
  323. package/lib/library/JsonpLibraryPlugin.js +5 -2
  324. package/lib/library/ModuleLibraryPlugin.js +230 -18
  325. package/lib/library/SystemLibraryPlugin.js +29 -11
  326. package/lib/library/UmdLibraryPlugin.js +35 -32
  327. package/lib/logging/Logger.js +18 -15
  328. package/lib/logging/createConsoleLogger.js +27 -27
  329. package/lib/logging/runtime.js +11 -11
  330. package/lib/logging/truncateArgs.js +5 -5
  331. package/lib/node/CommonJsChunkLoadingPlugin.js +72 -75
  332. package/lib/node/NodeEnvironmentPlugin.js +9 -3
  333. package/lib/node/NodeTargetPlugin.js +9 -1
  334. package/lib/node/NodeTemplatePlugin.js +2 -2
  335. package/lib/node/NodeWatchFileSystem.js +4 -4
  336. package/lib/node/ReadFileChunkLoadingRuntimeModule.js +22 -38
  337. package/lib/node/ReadFileCompileAsyncWasmPlugin.js +10 -11
  338. package/lib/node/ReadFileCompileWasmPlugin.js +11 -14
  339. package/lib/node/RequireChunkLoadingRuntimeModule.js +22 -30
  340. package/lib/node/nodeConsole.js +12 -7
  341. package/lib/optimize/AggressiveMergingPlugin.js +46 -47
  342. package/lib/optimize/AggressiveSplittingPlugin.js +233 -238
  343. package/lib/optimize/ConcatenatedModule.js +620 -251
  344. package/lib/optimize/EnsureChunkConditionsPlugin.js +58 -58
  345. package/lib/optimize/FlagIncludedChunksPlugin.js +93 -96
  346. package/lib/optimize/InnerGraph.js +139 -124
  347. package/lib/optimize/InnerGraphPlugin.js +29 -24
  348. package/lib/optimize/LimitChunkCountPlugin.js +32 -8
  349. package/lib/optimize/MangleExportsPlugin.js +22 -22
  350. package/lib/optimize/MergeDuplicateChunksPlugin.js +80 -81
  351. package/lib/optimize/MinChunkSizePlugin.js +12 -7
  352. package/lib/optimize/ModuleConcatenationPlugin.js +92 -71
  353. package/lib/optimize/RealContentHashPlugin.js +58 -44
  354. package/lib/optimize/RemoveEmptyChunksPlugin.js +8 -5
  355. package/lib/optimize/RemoveParentModulesPlugin.js +7 -4
  356. package/lib/optimize/RuntimeChunkPlugin.js +22 -25
  357. package/lib/optimize/SideEffectsFlagPlugin.js +44 -26
  358. package/lib/optimize/SplitChunksPlugin.js +209 -158
  359. package/lib/performance/AssetsOverSizeLimitWarning.js +1 -1
  360. package/lib/performance/EntrypointsOverSizeLimitWarning.js +2 -2
  361. package/lib/performance/SizeLimitsPlugin.js +8 -5
  362. package/lib/prefetch/ChunkPrefetchFunctionRuntimeModule.js +5 -7
  363. package/lib/prefetch/ChunkPrefetchPreloadPlugin.js +61 -64
  364. package/lib/prefetch/ChunkPrefetchStartupRuntimeModule.js +3 -4
  365. package/lib/prefetch/ChunkPrefetchTriggerRuntimeModule.js +2 -2
  366. package/lib/prefetch/ChunkPreloadTriggerRuntimeModule.js +2 -2
  367. package/lib/rules/BasicEffectRulePlugin.js +14 -5
  368. package/lib/rules/BasicMatcherRulePlugin.js +18 -7
  369. package/lib/rules/ObjectMatcherRulePlugin.js +17 -6
  370. package/lib/rules/RuleSetCompiler.js +73 -32
  371. package/lib/rules/UseEffectRulePlugin.js +61 -25
  372. package/lib/runtime/AsyncModuleRuntimeModule.js +74 -9
  373. package/lib/runtime/AutoPublicPathRuntimeModule.js +9 -4
  374. package/lib/runtime/BaseUriRuntimeModule.js +2 -2
  375. package/lib/runtime/CompatRuntimeModule.js +0 -1
  376. package/lib/runtime/CreateFakeNamespaceObjectRuntimeModule.js +1 -1
  377. package/lib/runtime/GetChunkFilenameRuntimeModule.js +34 -35
  378. package/lib/runtime/GetMainFilenameRuntimeModule.js +1 -1
  379. package/lib/runtime/GetTrustedTypesPolicyRuntimeModule.js +1 -1
  380. package/lib/runtime/LoadScriptRuntimeModule.js +0 -2
  381. package/lib/runtime/MakeDeferredNamespaceObjectRuntime.js +248 -0
  382. package/lib/runtime/PublicPathRuntimeModule.js +2 -2
  383. package/lib/runtime/RuntimeIdRuntimeModule.js +2 -1
  384. package/lib/runtime/StartupChunkDependenciesPlugin.js +39 -42
  385. package/lib/runtime/StartupChunkDependenciesRuntimeModule.js +10 -9
  386. package/lib/runtime/StartupEntrypointRuntimeModule.js +0 -1
  387. package/lib/runtime/SystemContextRuntimeModule.js +0 -2
  388. package/lib/runtime/ToBinaryRuntimeModule.js +64 -0
  389. package/lib/schemes/DataUriPlugin.js +9 -31
  390. package/lib/schemes/FileUriPlugin.js +11 -6
  391. package/lib/schemes/HttpUriPlugin.js +381 -261
  392. package/lib/schemes/VirtualUrlPlugin.js +222 -0
  393. package/lib/serialization/AggregateErrorSerializer.js +41 -0
  394. package/lib/serialization/BinaryMiddleware.js +51 -33
  395. package/lib/serialization/ErrorObjectSerializer.js +7 -2
  396. package/lib/serialization/FileMiddleware.js +101 -71
  397. package/lib/serialization/NullPrototypeObjectSerializer.js +5 -3
  398. package/lib/serialization/ObjectMiddleware.js +130 -54
  399. package/lib/serialization/PlainObjectSerializer.js +1 -1
  400. package/lib/serialization/Serializer.js +38 -20
  401. package/lib/serialization/SerializerMiddleware.js +121 -49
  402. package/lib/serialization/SingleItemMiddleware.js +9 -7
  403. package/lib/serialization/types.js +1 -1
  404. package/lib/sharing/ConsumeSharedModule.js +9 -9
  405. package/lib/sharing/ConsumeSharedPlugin.js +14 -14
  406. package/lib/sharing/ConsumeSharedRuntimeModule.js +11 -8
  407. package/lib/sharing/ProvideSharedModule.js +9 -11
  408. package/lib/sharing/ProvideSharedModuleFactory.js +5 -3
  409. package/lib/sharing/ProvideSharedPlugin.js +15 -10
  410. package/lib/sharing/SharePlugin.js +3 -4
  411. package/lib/sharing/ShareRuntimeModule.js +7 -6
  412. package/lib/sharing/resolveMatchedConfigs.js +27 -13
  413. package/lib/sharing/utils.js +37 -43
  414. package/lib/stats/DefaultStatsFactoryPlugin.js +455 -286
  415. package/lib/stats/DefaultStatsPresetPlugin.js +77 -46
  416. package/lib/stats/DefaultStatsPrinterPlugin.js +583 -419
  417. package/lib/stats/StatsFactory.js +75 -32
  418. package/lib/stats/StatsPrinter.js +71 -51
  419. package/lib/url/URLParserPlugin.js +264 -0
  420. package/lib/util/ArrayHelpers.js +6 -8
  421. package/lib/util/ArrayQueue.js +1 -1
  422. package/lib/util/AsyncQueue.js +11 -10
  423. package/lib/util/Hash.js +37 -5
  424. package/lib/util/IterableHelpers.js +4 -4
  425. package/lib/util/LazyBucketSortedSet.js +46 -27
  426. package/lib/util/LazySet.js +16 -10
  427. package/lib/util/MapHelpers.js +1 -1
  428. package/lib/util/ParallelismFactorCalculator.js +2 -2
  429. package/lib/util/Semaphore.js +3 -3
  430. package/lib/util/SetHelpers.js +6 -6
  431. package/lib/util/SortableSet.js +11 -9
  432. package/lib/util/StackedCacheMap.js +2 -2
  433. package/lib/util/StackedMap.js +3 -3
  434. package/lib/util/StringXor.js +2 -1
  435. package/lib/util/TupleQueue.js +11 -8
  436. package/lib/util/TupleSet.js +44 -19
  437. package/lib/util/URLAbsoluteSpecifier.js +3 -4
  438. package/lib/util/WeakTupleMap.js +56 -42
  439. package/lib/util/binarySearchBounds.js +4 -3
  440. package/lib/util/chainedImports.js +3 -1
  441. package/lib/util/cleverMerge.js +171 -107
  442. package/lib/util/comparators.js +265 -162
  443. package/lib/util/compileBooleanMatcher.js +120 -41
  444. package/lib/util/concatenate.js +20 -16
  445. package/lib/util/conventions.js +73 -74
  446. package/lib/util/create-schema-validation.js +8 -8
  447. package/lib/util/createHash.js +32 -135
  448. package/lib/util/dataURL.js +39 -0
  449. package/lib/util/deprecation.js +119 -116
  450. package/lib/util/deterministicGrouping.js +56 -47
  451. package/lib/util/extractSourceMap.js +319 -0
  452. package/lib/util/extractUrlAndGlobal.js +1 -1
  453. package/lib/util/findGraphRoots.js +19 -9
  454. package/lib/util/fs.js +169 -141
  455. package/lib/util/hash/BatchedHash.js +50 -10
  456. package/lib/util/hash/BulkUpdateHash.js +138 -0
  457. package/lib/util/hash/DebugHash.js +75 -0
  458. package/lib/util/hash/hash-digest.js +216 -0
  459. package/lib/util/hash/md4.js +2 -2
  460. package/lib/util/hash/wasm-hash.js +59 -17
  461. package/lib/util/identifier.js +124 -61
  462. package/lib/util/internalSerializables.js +4 -6
  463. package/lib/util/magicComment.js +9 -5
  464. package/lib/util/makeSerializable.js +2 -2
  465. package/lib/util/memoize.js +5 -2
  466. package/lib/util/objectToMap.js +3 -2
  467. package/lib/util/processAsyncTree.js +5 -5
  468. package/lib/util/propertyAccess.js +1 -1
  469. package/lib/util/propertyName.js +2 -3
  470. package/lib/util/registerExternalSerializer.js +16 -20
  471. package/lib/util/removeBOM.js +25 -0
  472. package/lib/util/runtime.js +72 -56
  473. package/lib/util/semver.js +44 -33
  474. package/lib/util/serialization.js +56 -56
  475. package/lib/util/smartGrouping.js +42 -27
  476. package/lib/util/source.js +5 -4
  477. package/lib/util/traverseDestructuringAssignmentProperties.js +45 -0
  478. package/lib/validateSchema.js +7 -9
  479. package/lib/wasm/EnableWasmLoadingPlugin.js +22 -12
  480. package/lib/wasm-async/AsyncWasmLoadingRuntimeModule.js +9 -5
  481. package/lib/wasm-async/AsyncWebAssemblyGenerator.js +15 -3
  482. package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +28 -22
  483. package/lib/wasm-async/AsyncWebAssemblyModulesPlugin.js +47 -57
  484. package/lib/wasm-async/AsyncWebAssemblyParser.js +2 -10
  485. package/lib/wasm-async/UniversalCompileAsyncWasmPlugin.js +8 -4
  486. package/lib/wasm-sync/WasmChunkLoadingRuntimeModule.js +23 -17
  487. package/lib/wasm-sync/WasmFinalizeExportsPlugin.js +55 -57
  488. package/lib/wasm-sync/WebAssemblyGenerator.js +56 -41
  489. package/lib/wasm-sync/WebAssemblyInInitialChunkError.js +6 -3
  490. package/lib/wasm-sync/WebAssemblyJavascriptGenerator.js +25 -10
  491. package/lib/wasm-sync/WebAssemblyModulesPlugin.js +11 -12
  492. package/lib/wasm-sync/WebAssemblyParser.js +10 -17
  493. package/lib/wasm-sync/WebAssemblyUtils.js +1 -1
  494. package/lib/web/FetchCompileAsyncWasmPlugin.js +4 -4
  495. package/lib/web/FetchCompileWasmPlugin.js +6 -8
  496. package/lib/web/JsonpChunkLoadingPlugin.js +74 -74
  497. package/lib/web/JsonpChunkLoadingRuntimeModule.js +10 -26
  498. package/lib/web/JsonpTemplatePlugin.js +0 -1
  499. package/lib/webpack.js +107 -87
  500. package/lib/webworker/ImportScriptsChunkLoadingPlugin.js +79 -75
  501. package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +38 -51
  502. package/lib/webworker/WebWorkerTemplatePlugin.js +1 -0
  503. package/module.d.ts +5 -0
  504. package/package.json +149 -129
  505. package/schemas/WebpackOptions.check.d.ts +1 -1
  506. package/schemas/WebpackOptions.check.js +2 -2
  507. package/schemas/WebpackOptions.json +543 -190
  508. package/schemas/plugins/BannerPlugin.check.d.ts +1 -1
  509. package/schemas/plugins/BannerPlugin.check.js +2 -2
  510. package/schemas/plugins/BannerPlugin.json +4 -0
  511. package/schemas/plugins/DllPlugin.check.d.ts +1 -1
  512. package/schemas/plugins/DllPlugin.check.js +1 -1
  513. package/schemas/plugins/DllReferencePlugin.check.d.ts +1 -1
  514. package/schemas/plugins/DllReferencePlugin.check.js +1 -1
  515. package/schemas/plugins/IgnorePlugin.check.d.ts +1 -1
  516. package/schemas/plugins/IgnorePlugin.check.js +1 -1
  517. package/schemas/plugins/IgnorePlugin.json +1 -1
  518. package/schemas/plugins/LoaderOptionsPlugin.check.d.ts +1 -1
  519. package/schemas/plugins/LoaderOptionsPlugin.check.js +1 -1
  520. package/schemas/plugins/{HashedModuleIdsPlugin.check.d.ts → ManifestPlugin.check.d.ts} +2 -2
  521. package/schemas/plugins/ManifestPlugin.check.js +6 -0
  522. package/schemas/plugins/ManifestPlugin.json +98 -0
  523. package/schemas/plugins/ProgressPlugin.check.d.ts +1 -1
  524. package/schemas/plugins/ProgressPlugin.check.js +1 -1
  525. package/schemas/plugins/ProgressPlugin.json +1 -1
  526. package/schemas/plugins/SourceMapDevToolPlugin.check.d.ts +1 -1
  527. package/schemas/plugins/SourceMapDevToolPlugin.check.js +2 -2
  528. package/schemas/plugins/SourceMapDevToolPlugin.json +23 -6
  529. package/schemas/plugins/WatchIgnorePlugin.check.d.ts +1 -1
  530. package/schemas/plugins/WatchIgnorePlugin.check.js +1 -1
  531. package/schemas/plugins/asset/AssetGeneratorOptions.check.d.ts +1 -1
  532. package/schemas/plugins/asset/AssetGeneratorOptions.check.js +1 -1
  533. package/schemas/plugins/asset/AssetInlineGeneratorOptions.check.d.ts +1 -1
  534. package/schemas/plugins/asset/AssetInlineGeneratorOptions.check.js +1 -1
  535. package/schemas/plugins/asset/AssetParserOptions.check.d.ts +1 -1
  536. package/schemas/plugins/asset/AssetParserOptions.check.js +1 -1
  537. package/schemas/plugins/asset/AssetResourceGeneratorOptions.check.d.ts +1 -1
  538. package/schemas/plugins/asset/AssetResourceGeneratorOptions.check.js +1 -1
  539. package/schemas/plugins/container/ContainerPlugin.check.d.ts +1 -1
  540. package/schemas/plugins/container/ContainerPlugin.check.js +1 -1
  541. package/schemas/plugins/container/ContainerReferencePlugin.check.d.ts +1 -1
  542. package/schemas/plugins/container/ContainerReferencePlugin.check.js +2 -2
  543. package/schemas/plugins/container/ContainerReferencePlugin.json +4 -1
  544. package/schemas/plugins/container/ExternalsType.check.d.ts +1 -1
  545. package/schemas/plugins/container/ExternalsType.check.js +2 -2
  546. package/schemas/plugins/container/ModuleFederationPlugin.check.d.ts +1 -1
  547. package/schemas/plugins/container/ModuleFederationPlugin.check.js +2 -2
  548. package/schemas/plugins/container/ModuleFederationPlugin.json +4 -1
  549. package/schemas/plugins/css/CssGeneratorOptions.check.d.ts +1 -1
  550. package/schemas/plugins/css/CssGeneratorOptions.check.js +1 -1
  551. package/schemas/plugins/css/CssModuleGeneratorOptions.check.d.ts +1 -1
  552. package/schemas/plugins/css/CssModuleGeneratorOptions.check.js +2 -2
  553. package/schemas/plugins/css/CssModuleParserOptions.check.d.ts +1 -1
  554. package/schemas/plugins/css/CssModuleParserOptions.check.js +2 -2
  555. package/schemas/plugins/css/CssParserOptions.check.d.ts +1 -1
  556. package/schemas/plugins/css/CssParserOptions.check.js +2 -2
  557. package/schemas/plugins/debug/ProfilingPlugin.check.d.ts +1 -1
  558. package/schemas/plugins/debug/ProfilingPlugin.check.js +1 -1
  559. package/schemas/plugins/ids/HashedModuleIdsPlugin.check.d.ts +7 -0
  560. package/schemas/plugins/ids/HashedModuleIdsPlugin.check.js +6 -0
  561. package/schemas/plugins/{HashedModuleIdsPlugin.json → ids/HashedModuleIdsPlugin.json} +15 -2
  562. package/schemas/plugins/ids/OccurrenceChunkIdsPlugin.check.d.ts +1 -1
  563. package/schemas/plugins/ids/OccurrenceChunkIdsPlugin.check.js +1 -1
  564. package/schemas/plugins/ids/OccurrenceModuleIdsPlugin.check.d.ts +1 -1
  565. package/schemas/plugins/ids/OccurrenceModuleIdsPlugin.check.js +1 -1
  566. package/schemas/plugins/{css/CssGlobalParserOptions.check.d.ts → json/JsonModulesPluginGenerator.check.d.ts} +1 -1
  567. package/schemas/plugins/json/JsonModulesPluginGenerator.check.js +6 -0
  568. package/schemas/plugins/json/JsonModulesPluginGenerator.json +3 -0
  569. package/schemas/plugins/{css/CssGlobalGeneratorOptions.check.d.ts → json/JsonModulesPluginParser.check.d.ts} +1 -1
  570. package/schemas/plugins/json/JsonModulesPluginParser.check.js +6 -0
  571. package/schemas/plugins/json/JsonModulesPluginParser.json +3 -0
  572. package/schemas/plugins/optimize/AggressiveSplittingPlugin.check.d.ts +1 -1
  573. package/schemas/plugins/optimize/AggressiveSplittingPlugin.check.js +1 -1
  574. package/schemas/plugins/optimize/LimitChunkCountPlugin.check.d.ts +1 -1
  575. package/schemas/plugins/optimize/LimitChunkCountPlugin.check.js +1 -1
  576. package/schemas/plugins/optimize/MergeDuplicateChunksPlugin.check.d.ts +1 -1
  577. package/schemas/plugins/optimize/MergeDuplicateChunksPlugin.check.js +1 -1
  578. package/schemas/plugins/optimize/MinChunkSizePlugin.check.d.ts +1 -1
  579. package/schemas/plugins/optimize/MinChunkSizePlugin.check.js +1 -1
  580. package/schemas/plugins/schemes/HttpUriPlugin.check.d.ts +1 -1
  581. package/schemas/plugins/schemes/HttpUriPlugin.check.js +1 -1
  582. package/schemas/plugins/schemes/VirtualUrlPlugin.check.d.ts +7 -0
  583. package/schemas/plugins/schemes/VirtualUrlPlugin.check.js +6 -0
  584. package/schemas/plugins/schemes/VirtualUrlPlugin.json +77 -0
  585. package/schemas/plugins/sharing/ConsumeSharedPlugin.check.d.ts +1 -1
  586. package/schemas/plugins/sharing/ConsumeSharedPlugin.check.js +1 -1
  587. package/schemas/plugins/sharing/ProvideSharedPlugin.check.d.ts +1 -1
  588. package/schemas/plugins/sharing/ProvideSharedPlugin.check.js +1 -1
  589. package/schemas/plugins/sharing/SharePlugin.check.d.ts +1 -1
  590. package/schemas/plugins/sharing/SharePlugin.check.js +1 -1
  591. package/types.d.ts +5115 -1863
  592. package/SECURITY.md +0 -9
  593. package/lib/ModuleSourceTypesConstants.js +0 -112
  594. package/lib/dependencies/CssLocalIdentifierDependency.js +0 -250
  595. package/lib/dependencies/CssSelfLocalIdentifierDependency.js +0 -111
  596. package/lib/library/ModernModuleLibraryPlugin.js +0 -144
  597. package/schemas/plugins/HashedModuleIdsPlugin.check.js +0 -6
  598. package/schemas/plugins/JsonModulesPluginParser.check.d.ts +0 -7
  599. package/schemas/plugins/JsonModulesPluginParser.check.js +0 -6
  600. package/schemas/plugins/JsonModulesPluginParser.json +0 -12
  601. package/schemas/plugins/css/CssAutoGeneratorOptions.check.d.ts +0 -7
  602. package/schemas/plugins/css/CssAutoGeneratorOptions.check.js +0 -6
  603. package/schemas/plugins/css/CssAutoGeneratorOptions.json +0 -3
  604. package/schemas/plugins/css/CssAutoParserOptions.check.d.ts +0 -7
  605. package/schemas/plugins/css/CssAutoParserOptions.check.js +0 -6
  606. package/schemas/plugins/css/CssAutoParserOptions.json +0 -3
  607. package/schemas/plugins/css/CssGlobalGeneratorOptions.check.js +0 -6
  608. package/schemas/plugins/css/CssGlobalGeneratorOptions.json +0 -3
  609. package/schemas/plugins/css/CssGlobalParserOptions.check.js +0 -6
  610. package/schemas/plugins/css/CssGlobalParserOptions.json +0 -3
@@ -16,19 +16,28 @@ const {
16
16
  const ConcatenationScope = require("../ConcatenationScope");
17
17
  const { UsageState } = require("../ExportsInfo");
18
18
  const Module = require("../Module");
19
- const { JS_TYPES } = require("../ModuleSourceTypesConstants");
19
+ const {
20
+ JAVASCRIPT_TYPE,
21
+ JAVASCRIPT_TYPES
22
+ } = require("../ModuleSourceTypeConstants");
20
23
  const { JAVASCRIPT_MODULE_TYPE_ESM } = require("../ModuleTypeConstants");
21
24
  const RuntimeGlobals = require("../RuntimeGlobals");
22
25
  const Template = require("../Template");
26
+ const { DEFAULTS } = require("../config/defaults");
23
27
  const HarmonyImportDependency = require("../dependencies/HarmonyImportDependency");
28
+ const { ImportPhaseUtils } = require("../dependencies/ImportPhase");
24
29
  const JavascriptParser = require("../javascript/JavascriptParser");
30
+ const {
31
+ getMakeDeferredNamespaceModeFromExportsType,
32
+ getOptimizedDeferredModule
33
+ } = require("../runtime/MakeDeferredNamespaceObjectRuntime");
25
34
  const { equals } = require("../util/ArrayHelpers");
26
35
  const LazySet = require("../util/LazySet");
27
36
  const { concatComparators } = require("../util/comparators");
28
37
  const {
29
38
  RESERVED_NAMES,
30
- findNewName,
31
39
  addScopeSymbols,
40
+ findNewName,
32
41
  getAllReferences,
33
42
  getPathInAst,
34
43
  getUsedNamesInScopeInfo
@@ -51,41 +60,41 @@ const {
51
60
  /** @typedef {import("eslint-scope").Scope} Scope */
52
61
  /** @typedef {import("eslint-scope").Variable} Variable */
53
62
  /** @typedef {import("webpack-sources").Source} Source */
54
- /** @typedef {import("../../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
63
+ /** @typedef {import("../config/defaults").WebpackOptionsNormalizedWithDefaults} WebpackOptions */
55
64
  /** @typedef {import("../ChunkGraph")} ChunkGraph */
56
65
  /** @typedef {import("../CodeGenerationResults")} CodeGenerationResults */
57
66
  /** @typedef {import("../Compilation")} Compilation */
58
- /** @typedef {import("../Dependency")} Dependency */
59
67
  /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
60
- /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
61
68
  /** @typedef {import("../DependencyTemplates")} DependencyTemplates */
62
69
  /** @typedef {import("../ExportsInfo").ExportInfo} ExportInfo */
70
+ /** @typedef {import("../Module").BuildCallback} BuildCallback */
63
71
  /** @typedef {import("../Module").BuildInfo} BuildInfo */
72
+ /** @typedef {import("../Module").FileSystemDependencies} FileSystemDependencies */
64
73
  /** @typedef {import("../Module").BuildMeta} BuildMeta */
65
74
  /** @typedef {import("../Module").CodeGenerationContext} CodeGenerationContext */
66
75
  /** @typedef {import("../Module").CodeGenerationResult} CodeGenerationResult */
67
76
  /** @typedef {import("../Module").LibIdentOptions} LibIdentOptions */
77
+ /** @typedef {import("../Module").LibIdent} LibIdent */
78
+ /** @typedef {import("../Module").NameForCondition} NameForCondition */
68
79
  /** @typedef {import("../Module").ReadOnlyRuntimeRequirements} ReadOnlyRuntimeRequirements */
69
80
  /** @typedef {import("../Module").RuntimeRequirements} RuntimeRequirements */
70
81
  /** @typedef {import("../Module").SourceTypes} SourceTypes */
71
82
  /** @typedef {import("../ModuleGraph")} ModuleGraph */
72
83
  /** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */
73
84
  /** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */
74
- /** @typedef {import("../ModuleParseError")} ModuleParseError */
75
85
  /** @typedef {import("../RequestShortener")} RequestShortener */
76
86
  /** @typedef {import("../ResolverFactory").ResolverWithOptions} ResolverWithOptions */
77
87
  /** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
78
- /** @typedef {import("../WebpackError")} WebpackError */
79
88
  /** @typedef {import("../javascript/JavascriptModulesPlugin").ChunkRenderContext} ChunkRenderContext */
80
89
  /** @typedef {import("../javascript/JavascriptParser").Program} Program */
81
90
  /** @typedef {import("../javascript/JavascriptParser").Range} Range */
82
91
  /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
83
92
  /** @typedef {import("../util/Hash")} Hash */
84
93
  /** @typedef {typeof import("../util/Hash")} HashConstructor */
85
- /** @typedef {import("../util/concatenate").UsedNames} UsedNames */
94
+ /** @typedef {import("../util/concatenate").ScopeInfo} ScopeInfo */
86
95
  /** @typedef {import("../util/fs").InputFileSystem} InputFileSystem */
96
+ /** @typedef {import("../util/identifier").AssociatedObjectForCache} AssociatedObjectForCache */
87
97
  /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
88
-
89
98
  /**
90
99
  * @template T
91
100
  * @typedef {import("../InitFragment")<T>} InitFragment
@@ -98,27 +107,23 @@ const {
98
107
 
99
108
  // fix eslint-scope to support class properties correctly
100
109
  // cspell:word Referencer
101
- const ReferencerClass = /** @type {any} */ (Referencer);
110
+ const ReferencerClass = Referencer;
102
111
  if (!ReferencerClass.prototype.PropertyDefinition) {
103
112
  ReferencerClass.prototype.PropertyDefinition =
104
113
  ReferencerClass.prototype.Property;
105
114
  }
106
115
 
107
- /**
108
- * @typedef {object} ReexportInfo
109
- * @property {Module} module
110
- * @property {string[]} export
111
- */
112
-
113
116
  /** @typedef {RawBinding | SymbolBinding} Binding */
114
117
 
118
+ /** @typedef {string[]} ExportName */
119
+
115
120
  /**
116
121
  * @typedef {object} RawBinding
117
122
  * @property {ModuleInfo} info
118
123
  * @property {string} rawName
119
124
  * @property {string=} comment
120
- * @property {string[]} ids
121
- * @property {string[]} exportName
125
+ * @property {ExportName} ids
126
+ * @property {ExportName} exportName
122
127
  */
123
128
 
124
129
  /**
@@ -126,12 +131,14 @@ if (!ReferencerClass.prototype.PropertyDefinition) {
126
131
  * @property {ConcatenatedModuleInfo} info
127
132
  * @property {string} name
128
133
  * @property {string=} comment
129
- * @property {string[]} ids
130
- * @property {string[]} exportName
134
+ * @property {ExportName} ids
135
+ * @property {ExportName} exportName
131
136
  */
132
137
 
133
- /** @typedef {ConcatenatedModuleInfo | ExternalModuleInfo } ModuleInfo */
134
- /** @typedef {ConcatenatedModuleInfo | ExternalModuleInfo | ReferenceToModuleInfo } ModuleInfoOrReference */
138
+ /** @typedef {ConcatenatedModuleInfo | ExternalModuleInfo} ModuleInfo */
139
+ /** @typedef {ConcatenatedModuleInfo | ExternalModuleInfo | ReferenceToModuleInfo} ModuleInfoOrReference */
140
+
141
+ /** @typedef {Map<string, string>} ExportMap */
135
142
 
136
143
  /**
137
144
  * @typedef {object} ConcatenatedModuleInfo
@@ -146,16 +153,17 @@ if (!ReferencerClass.prototype.PropertyDefinition) {
146
153
  * @property {Scope | undefined} globalScope
147
154
  * @property {Scope | undefined} moduleScope
148
155
  * @property {Map<string, string>} internalNames
149
- * @property {Map<string, string> | undefined} exportMap
150
- * @property {Map<string, string> | undefined} rawExportMap
156
+ * @property {ExportMap | undefined} exportMap
157
+ * @property {ExportMap | undefined} rawExportMap
151
158
  * @property {string=} namespaceExportSymbol
152
159
  * @property {string | undefined} namespaceObjectName
153
- * @property {boolean} interopNamespaceObjectUsed
154
- * @property {string | undefined} interopNamespaceObjectName
155
- * @property {boolean} interopNamespaceObject2Used
156
- * @property {string | undefined} interopNamespaceObject2Name
157
- * @property {boolean} interopDefaultAccessUsed
158
- * @property {string | undefined} interopDefaultAccessName
160
+ * @property {ConcatenationScope | undefined} concatenationScope
161
+ * @property {boolean} interopNamespaceObjectUsed "default-with-named" namespace
162
+ * @property {string | undefined} interopNamespaceObjectName "default-with-named" namespace
163
+ * @property {boolean} interopNamespaceObject2Used "default-only" namespace
164
+ * @property {string | undefined} interopNamespaceObject2Name "default-only" namespace
165
+ * @property {boolean} interopDefaultAccessUsed runtime namespace object that detects "__esModule"
166
+ * @property {string | undefined} interopDefaultAccessName runtime namespace object that detects "__esModule"
159
167
  */
160
168
 
161
169
  /**
@@ -163,27 +171,33 @@ if (!ReferencerClass.prototype.PropertyDefinition) {
163
171
  * @property {"external"} type
164
172
  * @property {Module} module
165
173
  * @property {RuntimeSpec | boolean} runtimeCondition
174
+ * @property {NonDeferAccess} nonDeferAccess
166
175
  * @property {number} index
167
- * @property {string | undefined} name
168
- * @property {boolean} interopNamespaceObjectUsed
169
- * @property {string | undefined} interopNamespaceObjectName
170
- * @property {boolean} interopNamespaceObject2Used
171
- * @property {string | undefined} interopNamespaceObject2Name
172
- * @property {boolean} interopDefaultAccessUsed
173
- * @property {string | undefined} interopDefaultAccessName
176
+ * @property {string | undefined} name module.exports / harmony namespace object
177
+ * @property {string | undefined} deferredName deferred module.exports / harmony namespace object
178
+ * @property {boolean} deferred the module is deferred at least once
179
+ * @property {boolean} deferredNamespaceObjectUsed deferred namespace object that being used in a not-analyzable way so it must be materialized
180
+ * @property {string | undefined} deferredNamespaceObjectName deferred namespace object that being used in a not-analyzable way so it must be materialized
181
+ * @property {boolean} interopNamespaceObjectUsed "default-with-named" namespace
182
+ * @property {string | undefined} interopNamespaceObjectName "default-with-named" namespace
183
+ * @property {boolean} interopNamespaceObject2Used "default-only" namespace
184
+ * @property {string | undefined} interopNamespaceObject2Name "default-only" namespace
185
+ * @property {boolean} interopDefaultAccessUsed runtime namespace object that detects "__esModule"
186
+ * @property {string | undefined} interopDefaultAccessName runtime namespace object that detects "__esModule"
174
187
  */
175
188
 
176
189
  /**
177
190
  * @typedef {object} ReferenceToModuleInfo
178
191
  * @property {"reference"} type
179
192
  * @property {RuntimeSpec | boolean} runtimeCondition
193
+ * @property {NonDeferAccess} nonDeferAccess
180
194
  * @property {ModuleInfo} target
181
195
  */
182
196
 
183
197
  /**
184
198
  * @template T
185
199
  * @param {string} property property
186
- * @param {function(T[keyof T], T[keyof T]): 0 | 1 | -1} comparator comparator
200
+ * @param {(a: T[keyof T], b: T[keyof T]) => 0 | 1 | -1} comparator comparator
187
201
  * @returns {Comparator<T>} comparator
188
202
  */
189
203
 
@@ -220,7 +234,7 @@ const byRangeStart = createComparator("rangeStart", compareNumbers);
220
234
  * @param {Iterable<string>} iterable iterable object
221
235
  * @returns {string} joined iterable object
222
236
  */
223
- const joinIterableWithComma = iterable => {
237
+ const joinIterableWithComma = (iterable) => {
224
238
  // This is more performant than Array.from().join(", ")
225
239
  // as it doesn't create an array
226
240
  let str = "";
@@ -236,23 +250,45 @@ const joinIterableWithComma = iterable => {
236
250
  return str;
237
251
  };
238
252
 
253
+ /** @typedef {boolean} NonDeferAccess */
254
+
255
+ /**
256
+ * @param {NonDeferAccess} a a
257
+ * @param {NonDeferAccess} b b
258
+ * @returns {NonDeferAccess} merged
259
+ */
260
+ const mergeNonDeferAccess = (a, b) => a || b;
261
+
262
+ /**
263
+ * @param {NonDeferAccess} a first
264
+ * @param {NonDeferAccess} b second
265
+ * @returns {NonDeferAccess} first - second
266
+ */
267
+ const subtractNonDeferAccess = (a, b) => a && !b;
268
+
239
269
  /**
240
270
  * @typedef {object} ConcatenationEntry
241
271
  * @property {"concatenated" | "external"} type
242
272
  * @property {Module} module
243
273
  * @property {RuntimeSpec | boolean} runtimeCondition
274
+ * @property {NonDeferAccess} nonDeferAccess
244
275
  */
245
276
 
277
+ /** @typedef {Set<ConcatenatedModuleInfo>} NeededNamespaceObjects */
278
+
279
+ /** @typedef {Map<Module, ModuleInfo>} ModuleToInfoMap */
280
+
246
281
  /**
247
282
  * @param {ModuleGraph} moduleGraph the module graph
248
283
  * @param {ModuleInfo} info module info
249
- * @param {string[]} exportName exportName
250
- * @param {Map<Module, ModuleInfo>} moduleToInfoMap moduleToInfoMap
284
+ * @param {ExportName} exportName exportName
285
+ * @param {ModuleToInfoMap} moduleToInfoMap moduleToInfoMap
251
286
  * @param {RuntimeSpec} runtime for which runtime
252
287
  * @param {RequestShortener} requestShortener the request shortener
253
288
  * @param {RuntimeTemplate} runtimeTemplate the runtime template
254
- * @param {Set<ConcatenatedModuleInfo>} neededNamespaceObjects modules for which a namespace object should be generated
289
+ * @param {NeededNamespaceObjects} neededNamespaceObjects modules for which a namespace object should be generated
255
290
  * @param {boolean} asCall asCall
291
+ * @param {boolean} depDeferred the dependency is deferred
256
292
  * @param {boolean | undefined} strictHarmonyModule strictHarmonyModule
257
293
  * @param {boolean | undefined} asiSafe asiSafe
258
294
  * @param {Set<ExportInfo>} alreadyVisited alreadyVisited
@@ -268,6 +304,7 @@ const getFinalBinding = (
268
304
  runtimeTemplate,
269
305
  neededNamespaceObjects,
270
306
  asCall,
307
+ depDeferred,
271
308
  strictHarmonyModule,
272
309
  asiSafe,
273
310
  alreadyVisited = new Set()
@@ -276,21 +313,36 @@ const getFinalBinding = (
276
313
  moduleGraph,
277
314
  strictHarmonyModule
278
315
  );
316
+ const moduleDeferred =
317
+ info.type === "external" &&
318
+ info.deferred &&
319
+ !(/** @type {BuildMeta} */ (info.module.buildMeta).async);
320
+ const deferred = depDeferred && moduleDeferred;
279
321
  if (exportName.length === 0) {
280
322
  switch (exportsType) {
281
323
  case "default-only":
282
- info.interopNamespaceObject2Used = true;
324
+ if (deferred) info.deferredNamespaceObjectUsed = true;
325
+ else info.interopNamespaceObject2Used = true;
283
326
  return {
284
327
  info,
285
- rawName: /** @type {string} */ (info.interopNamespaceObject2Name),
328
+ rawName: /** @type {string} */ (
329
+ deferred
330
+ ? info.deferredNamespaceObjectName
331
+ : info.interopNamespaceObject2Name
332
+ ),
286
333
  ids: exportName,
287
334
  exportName
288
335
  };
289
336
  case "default-with-named":
290
- info.interopNamespaceObjectUsed = true;
337
+ if (deferred) info.deferredNamespaceObjectUsed = true;
338
+ else info.interopNamespaceObjectUsed = true;
291
339
  return {
292
340
  info,
293
- rawName: /** @type {string} */ (info.interopNamespaceObjectName),
341
+ rawName: /** @type {string} */ (
342
+ deferred
343
+ ? info.deferredNamespaceObjectName
344
+ : info.interopNamespaceObjectName
345
+ ),
294
346
  ids: exportName,
295
347
  exportName
296
348
  };
@@ -344,6 +396,22 @@ const getFinalBinding = (
344
396
  switch (exportName[0]) {
345
397
  case "default": {
346
398
  exportName = exportName.slice(1);
399
+ if (deferred) {
400
+ return {
401
+ info,
402
+ rawName: `${info.deferredName}.a`,
403
+ ids: exportName,
404
+ exportName
405
+ };
406
+ }
407
+ if (moduleDeferred) {
408
+ return {
409
+ info,
410
+ rawName: /** @type {string} */ (info.name),
411
+ ids: exportName,
412
+ exportName
413
+ };
414
+ }
347
415
  info.interopDefaultAccessUsed = true;
348
416
  const defaultExport = asCall
349
417
  ? `${info.interopDefaultAccessName}()`
@@ -385,6 +453,15 @@ const getFinalBinding = (
385
453
  exportName
386
454
  };
387
455
  case "external":
456
+ if (deferred) {
457
+ info.deferredNamespaceObjectUsed = true;
458
+ return {
459
+ info,
460
+ rawName: /** @type {string} */ (info.deferredNamespaceObjectName),
461
+ ids: exportName,
462
+ exportName
463
+ };
464
+ }
388
465
  return {
389
466
  info,
390
467
  rawName:
@@ -421,7 +498,7 @@ const getFinalBinding = (
421
498
  }
422
499
  const directExport = info.exportMap && info.exportMap.get(exportId);
423
500
  if (directExport) {
424
- const usedName = /** @type {string[]} */ (
501
+ const usedName = /** @type {ExportName} */ (
425
502
  exportsInfo.getUsedName(exportName, runtime)
426
503
  );
427
504
  if (!usedName) {
@@ -448,7 +525,7 @@ const getFinalBinding = (
448
525
  exportName
449
526
  };
450
527
  }
451
- const reexport = exportInfo.findTarget(moduleGraph, module =>
528
+ const reexport = exportInfo.findTarget(moduleGraph, (module) =>
452
529
  moduleToInfoMap.has(module)
453
530
  );
454
531
  if (reexport === false) {
@@ -476,6 +553,7 @@ const getFinalBinding = (
476
553
  runtimeTemplate,
477
554
  neededNamespaceObjects,
478
555
  asCall,
556
+ reexport.deferred,
479
557
  /** @type {BuildMeta} */
480
558
  (info.module.buildMeta).strictHarmonyModule,
481
559
  asiSafe,
@@ -483,7 +561,7 @@ const getFinalBinding = (
483
561
  );
484
562
  }
485
563
  if (info.namespaceExportSymbol) {
486
- const usedName = /** @type {string[]} */ (
564
+ const usedName = /** @type {ExportName} */ (
487
565
  exportsInfo.getUsedName(exportName, runtime)
488
566
  );
489
567
  return {
@@ -501,7 +579,7 @@ const getFinalBinding = (
501
579
  }
502
580
 
503
581
  case "external": {
504
- const used = /** @type {string[]} */ (
582
+ const used = /** @type {ExportName} */ (
505
583
  exportsInfo.getUsedName(exportName, runtime)
506
584
  );
507
585
  if (!used) {
@@ -515,7 +593,15 @@ const getFinalBinding = (
515
593
  const comment = equals(used, exportName)
516
594
  ? ""
517
595
  : Template.toNormalComment(`${exportName.join(".")}`);
518
- return { info, rawName: info.name + comment, ids: used, exportName };
596
+ return {
597
+ info,
598
+ rawName:
599
+ (deferred ? info.deferredName : info.name) +
600
+ (deferred ? ".a" : "") +
601
+ comment,
602
+ ids: used,
603
+ exportName
604
+ };
519
605
  }
520
606
  }
521
607
  };
@@ -523,13 +609,14 @@ const getFinalBinding = (
523
609
  /**
524
610
  * @param {ModuleGraph} moduleGraph the module graph
525
611
  * @param {ModuleInfo} info module info
526
- * @param {string[]} exportName exportName
527
- * @param {Map<Module, ModuleInfo>} moduleToInfoMap moduleToInfoMap
612
+ * @param {ExportName} exportName exportName
613
+ * @param {ModuleToInfoMap} moduleToInfoMap moduleToInfoMap
528
614
  * @param {RuntimeSpec} runtime for which runtime
529
615
  * @param {RequestShortener} requestShortener the request shortener
530
616
  * @param {RuntimeTemplate} runtimeTemplate the runtime template
531
- * @param {Set<ConcatenatedModuleInfo>} neededNamespaceObjects modules for which a namespace object should be generated
617
+ * @param {NeededNamespaceObjects} neededNamespaceObjects modules for which a namespace object should be generated
532
618
  * @param {boolean} asCall asCall
619
+ * @param {boolean} depDeferred the dependency is deferred
533
620
  * @param {boolean | undefined} callContext callContext
534
621
  * @param {boolean | undefined} strictHarmonyModule strictHarmonyModule
535
622
  * @param {boolean | undefined} asiSafe asiSafe
@@ -545,6 +632,7 @@ const getFinalName = (
545
632
  runtimeTemplate,
546
633
  neededNamespaceObjects,
547
634
  asCall,
635
+ depDeferred,
548
636
  callContext,
549
637
  strictHarmonyModule,
550
638
  asiSafe
@@ -559,6 +647,7 @@ const getFinalName = (
559
647
  runtimeTemplate,
560
648
  neededNamespaceObjects,
561
649
  asCall,
650
+ depDeferred,
562
651
  strictHarmonyModule,
563
652
  asiSafe
564
653
  );
@@ -600,9 +689,12 @@ const getFinalName = (
600
689
 
601
690
  /**
602
691
  * @typedef {object} ConcatenateModuleHooks
603
- * @property {SyncBailHook<[Record<string, string>], boolean | void>} exportsDefinitions
692
+ * @property {SyncBailHook<[ConcatenatedModule, RuntimeSpec[], string, Record<string, string>], boolean>} onDemandExportsGeneration
693
+ * @property {SyncBailHook<[Partial<ConcatenatedModuleInfo>, ConcatenatedModuleInfo], boolean | void>} concatenatedModuleInfo
604
694
  */
605
695
 
696
+ /** @typedef {BuildInfo["topLevelDeclarations"]} TopLevelDeclarations */
697
+
606
698
  /** @type {WeakMap<Compilation, ConcatenateModuleHooks>} */
607
699
  const compilationHooksMap = new WeakMap();
608
700
 
@@ -612,7 +704,7 @@ class ConcatenatedModule extends Module {
612
704
  * @param {Set<Module>} modules all modules in the concatenation (including the root module)
613
705
  * @param {RuntimeSpec} runtime the runtime
614
706
  * @param {Compilation} compilation the compilation
615
- * @param {object=} associatedObjectForCache object for caching
707
+ * @param {AssociatedObjectForCache=} associatedObjectForCache object for caching
616
708
  * @param {string | HashConstructor=} hashFunction hash function to use
617
709
  * @returns {ConcatenatedModule} the module
618
710
  */
@@ -622,7 +714,7 @@ class ConcatenatedModule extends Module {
622
714
  runtime,
623
715
  compilation,
624
716
  associatedObjectForCache,
625
- hashFunction = "md4"
717
+ hashFunction = DEFAULTS.HASH_FUNCTION
626
718
  ) {
627
719
  const identifier = ConcatenatedModule._createIdentifier(
628
720
  rootModule,
@@ -647,7 +739,16 @@ class ConcatenatedModule extends Module {
647
739
  let hooks = compilationHooksMap.get(compilation);
648
740
  if (hooks === undefined) {
649
741
  hooks = {
650
- exportsDefinitions: new SyncBailHook(["definitions"])
742
+ onDemandExportsGeneration: new SyncBailHook([
743
+ "module",
744
+ "runtimes",
745
+ "exportsFinalName",
746
+ "exportsSource"
747
+ ]),
748
+ concatenatedModuleInfo: new SyncBailHook([
749
+ "updatedInfo",
750
+ "concatenatedModuleInfo"
751
+ ])
651
752
  };
652
753
  compilationHooksMap.set(compilation, hooks);
653
754
  }
@@ -674,7 +775,7 @@ class ConcatenatedModule extends Module {
674
775
  this._modules = modules;
675
776
  this._runtime = runtime;
676
777
  this.factoryMeta = rootModule && rootModule.factoryMeta;
677
- /** @type {Compilation | undefined} */
778
+ /** @type {Compilation} */
678
779
  this.compilation = compilation;
679
780
  }
680
781
 
@@ -693,11 +794,11 @@ class ConcatenatedModule extends Module {
693
794
  * @returns {SourceTypes} types available (do not mutate)
694
795
  */
695
796
  getSourceTypes() {
696
- return JS_TYPES;
797
+ return JAVASCRIPT_TYPES;
697
798
  }
698
799
 
699
800
  get modules() {
700
- return Array.from(this._modules);
801
+ return [...this._modules];
701
802
  }
702
803
 
703
804
  /**
@@ -719,14 +820,14 @@ class ConcatenatedModule extends Module {
719
820
 
720
821
  /**
721
822
  * @param {LibIdentOptions} options options
722
- * @returns {string | null} an identifier for library inclusion
823
+ * @returns {LibIdent | null} an identifier for library inclusion
723
824
  */
724
825
  libIdent(options) {
725
826
  return this.rootModule.libIdent(options);
726
827
  }
727
828
 
728
829
  /**
729
- * @returns {string | null} absolute path which should be used for condition matching (usually the resource path)
830
+ * @returns {NameForCondition | null} absolute path which should be used for condition matching (usually the resource path)
730
831
  */
731
832
  nameForCondition() {
732
833
  return this.rootModule.nameForCondition();
@@ -745,7 +846,7 @@ class ConcatenatedModule extends Module {
745
846
  * @param {Compilation} compilation the compilation
746
847
  * @param {ResolverWithOptions} resolver the resolver
747
848
  * @param {InputFileSystem} fs the file system
748
- * @param {function(WebpackError=): void} callback callback function
849
+ * @param {BuildCallback} callback callback function
749
850
  * @returns {void}
750
851
  */
751
852
  build(options, compilation, resolver, fs, callback) {
@@ -771,15 +872,17 @@ class ConcatenatedModule extends Module {
771
872
  for (const m of this._modules) {
772
873
  // populate cacheable
773
874
  if (!(/** @type {BuildInfo} */ (m.buildInfo).cacheable)) {
774
- this.buildInfo.cacheable = false;
875
+ /** @type {BuildInfo} */
876
+ (this.buildInfo).cacheable = false;
775
877
  }
776
878
 
777
879
  // populate dependencies
778
880
  for (const d of m.dependencies.filter(
779
- dep =>
881
+ (dep) =>
780
882
  !(dep instanceof HarmonyImportDependency) ||
781
883
  !this._modules.has(
782
- /** @type {Module} */ (compilation.moduleGraph.getModule(dep))
884
+ /** @type {Module} */
885
+ (compilation.moduleGraph.getModule(dep))
783
886
  )
784
887
  )) {
785
888
  this.dependencies.push(d);
@@ -805,41 +908,45 @@ class ConcatenatedModule extends Module {
805
908
  }
806
909
  }
807
910
 
808
- const { assets, assetsInfo, topLevelDeclarations } =
911
+ const { assets, assetsInfo, topLevelDeclarations, needCreateRequire } =
809
912
  /** @type {BuildInfo} */ (m.buildInfo);
810
913
 
914
+ const buildInfo = /** @type {BuildInfo} */ (this.buildInfo);
915
+
811
916
  // populate topLevelDeclarations
812
917
  if (topLevelDeclarations) {
813
- const topLevelDeclarations = this.buildInfo.topLevelDeclarations;
918
+ const topLevelDeclarations = buildInfo.topLevelDeclarations;
814
919
  if (topLevelDeclarations !== undefined) {
815
920
  for (const decl of topLevelDeclarations) {
816
921
  topLevelDeclarations.add(decl);
817
922
  }
818
923
  }
819
924
  } else {
820
- this.buildInfo.topLevelDeclarations = undefined;
925
+ buildInfo.topLevelDeclarations = undefined;
926
+ }
927
+
928
+ // populate needCreateRequire
929
+ if (needCreateRequire) {
930
+ this.buildInfo.needCreateRequire = true;
821
931
  }
822
932
 
823
933
  // populate assets
824
934
  if (assets) {
825
- if (this.buildInfo.assets === undefined) {
826
- this.buildInfo.assets = Object.create(null);
935
+ if (buildInfo.assets === undefined) {
936
+ buildInfo.assets = Object.create(null);
827
937
  }
828
938
  Object.assign(
829
939
  /** @type {NonNullable<BuildInfo["assets"]>} */
830
- (
831
- /** @type {BuildInfo} */
832
- (this.buildInfo).assets
833
- ),
940
+ (buildInfo.assets),
834
941
  assets
835
942
  );
836
943
  }
837
944
  if (assetsInfo) {
838
- if (this.buildInfo.assetsInfo === undefined) {
839
- this.buildInfo.assetsInfo = new Map();
945
+ if (buildInfo.assetsInfo === undefined) {
946
+ buildInfo.assetsInfo = new Map();
840
947
  }
841
948
  for (const [key, value] of assetsInfo) {
842
- this.buildInfo.assetsInfo.set(key, value);
949
+ buildInfo.assetsInfo.set(key, value);
843
950
  }
844
951
  }
845
952
  }
@@ -870,28 +977,28 @@ class ConcatenatedModule extends Module {
870
977
  _createConcatenationList(rootModule, modulesSet, runtime, moduleGraph) {
871
978
  /** @type {ConcatenationEntry[]} */
872
979
  const list = [];
873
- /** @type {Map<Module, RuntimeSpec | true>} */
980
+ /** @type {Map<Module, { runtimeCondition: RuntimeSpec | true, nonDeferAccess: NonDeferAccess }>} */
874
981
  const existingEntries = new Map();
875
982
 
876
983
  /**
877
984
  * @param {Module} module a module
878
- * @returns {Iterable<{ connection: ModuleGraphConnection, runtimeCondition: RuntimeSpec | true }>} imported modules in order
985
+ * @returns {Iterable<{ connection: ModuleGraphConnection, runtimeCondition: RuntimeSpec | true, nonDeferAccess: NonDeferAccess }>} imported modules in order
879
986
  */
880
- const getConcatenatedImports = module => {
881
- const connections = Array.from(
882
- moduleGraph.getOutgoingConnections(module)
883
- );
987
+ const getConcatenatedImports = (module) => {
988
+ const connections = [...moduleGraph.getOutgoingConnections(module)];
884
989
  if (module === rootModule) {
885
- for (const c of moduleGraph.getOutgoingConnections(this))
990
+ for (const c of moduleGraph.getOutgoingConnections(this)) {
886
991
  connections.push(c);
992
+ }
887
993
  }
888
994
  /**
889
- * @type {Array<{ connection: ModuleGraphConnection, sourceOrder: number, rangeStart: number }>}
995
+ * @type {{ connection: ModuleGraphConnection, sourceOrder: number, rangeStart: number | undefined, defer?: boolean }[]}
890
996
  */
891
997
  const references = connections
892
- .filter(connection => {
893
- if (!(connection.dependency instanceof HarmonyImportDependency))
998
+ .filter((connection) => {
999
+ if (!(connection.dependency instanceof HarmonyImportDependency)) {
894
1000
  return false;
1001
+ }
895
1002
  return (
896
1003
  connection &&
897
1004
  connection.resolvedOriginModule === module &&
@@ -899,14 +1006,15 @@ class ConcatenatedModule extends Module {
899
1006
  connection.isTargetActive(runtime)
900
1007
  );
901
1008
  })
902
- .map(connection => {
903
- const dep = /** @type {HarmonyImportDependency} */ (
904
- connection.dependency
905
- );
1009
+ .map((connection) => {
1010
+ const dep =
1011
+ /** @type {HarmonyImportDependency} */
1012
+ (connection.dependency);
906
1013
  return {
907
1014
  connection,
908
- sourceOrder: dep.sourceOrder,
909
- rangeStart: dep.range && dep.range[0]
1015
+ sourceOrder: /** @type {number} */ (dep.sourceOrder),
1016
+ rangeStart: dep.range && dep.range[0],
1017
+ defer: ImportPhaseUtils.isDefer(dep.phase)
910
1018
  };
911
1019
  });
912
1020
  /**
@@ -926,17 +1034,22 @@ class ConcatenatedModule extends Module {
926
1034
  * If there is side-effects-free reexport, we can get simple deterministic result with range start comparison.
927
1035
  */
928
1036
  references.sort(concatComparators(bySourceOrder, byRangeStart));
929
- /** @type {Map<Module, { connection: ModuleGraphConnection, runtimeCondition: RuntimeSpec | true }>} */
1037
+ /** @type {Map<Module, { connection: ModuleGraphConnection, runtimeCondition: RuntimeSpec | true, nonDeferAccess: NonDeferAccess }>} */
930
1038
  const referencesMap = new Map();
931
- for (const { connection } of references) {
932
- const runtimeCondition = filterRuntime(runtime, r =>
1039
+ for (const { connection, defer } of references) {
1040
+ const runtimeCondition = filterRuntime(runtime, (r) =>
933
1041
  connection.isTargetActive(r)
934
1042
  );
935
1043
  if (runtimeCondition === false) continue;
1044
+ const nonDeferAccess = !defer;
936
1045
  const module = connection.module;
937
1046
  const entry = referencesMap.get(module);
938
1047
  if (entry === undefined) {
939
- referencesMap.set(module, { connection, runtimeCondition });
1048
+ referencesMap.set(module, {
1049
+ connection,
1050
+ runtimeCondition,
1051
+ nonDeferAccess
1052
+ });
940
1053
  continue;
941
1054
  }
942
1055
  entry.runtimeCondition = mergeRuntimeConditionNonFalse(
@@ -944,6 +1057,10 @@ class ConcatenatedModule extends Module {
944
1057
  runtimeCondition,
945
1058
  runtime
946
1059
  );
1060
+ entry.nonDeferAccess = mergeNonDeferAccess(
1061
+ entry.nonDeferAccess,
1062
+ nonDeferAccess
1063
+ );
947
1064
  }
948
1065
  return referencesMap.values();
949
1066
  };
@@ -951,17 +1068,25 @@ class ConcatenatedModule extends Module {
951
1068
  /**
952
1069
  * @param {ModuleGraphConnection} connection graph connection
953
1070
  * @param {RuntimeSpec | true} runtimeCondition runtime condition
1071
+ * @param {NonDeferAccess} nonDeferAccess non-defer access
954
1072
  * @returns {void}
955
1073
  */
956
- const enterModule = (connection, runtimeCondition) => {
1074
+ const enterModule = (connection, runtimeCondition, nonDeferAccess) => {
957
1075
  const module = connection.module;
958
1076
  if (!module) return;
959
1077
  const existingEntry = existingEntries.get(module);
960
- if (existingEntry === true) {
1078
+ if (
1079
+ existingEntry &&
1080
+ existingEntry.runtimeCondition === true &&
1081
+ existingEntry.nonDeferAccess === true
1082
+ ) {
961
1083
  return;
962
1084
  }
963
1085
  if (modulesSet.has(module)) {
964
- existingEntries.set(module, true);
1086
+ existingEntries.set(module, {
1087
+ runtimeCondition: true,
1088
+ nonDeferAccess: true
1089
+ });
965
1090
  if (runtimeCondition !== true) {
966
1091
  throw new Error(
967
1092
  `Cannot runtime-conditional concatenate a module (${module.identifier()} in ${this.rootModule.identifier()}, ${runtimeConditionToString(
@@ -969,33 +1094,66 @@ class ConcatenatedModule extends Module {
969
1094
  )}). This should not happen.`
970
1095
  );
971
1096
  }
1097
+ if (nonDeferAccess !== true) {
1098
+ throw new Error(
1099
+ `Cannot deferred concatenate a module (${module.identifier()} in ${this.rootModule.identifier()}. This should not happen.`
1100
+ );
1101
+ }
972
1102
  const imports = getConcatenatedImports(module);
973
- for (const { connection, runtimeCondition } of imports)
974
- enterModule(connection, runtimeCondition);
1103
+ for (const {
1104
+ connection,
1105
+ runtimeCondition,
1106
+ nonDeferAccess
1107
+ } of imports) {
1108
+ enterModule(connection, runtimeCondition, nonDeferAccess);
1109
+ }
975
1110
  list.push({
976
1111
  type: "concatenated",
977
1112
  module: connection.module,
978
- runtimeCondition
1113
+ runtimeCondition,
1114
+ nonDeferAccess
979
1115
  });
980
1116
  } else {
1117
+ /** @type {RuntimeSpec | boolean} */
1118
+ let reducedRuntimeCondition;
1119
+ /** @type {NonDeferAccess} */
1120
+ let reducedNonDeferAccess;
981
1121
  if (existingEntry !== undefined) {
982
- const reducedRuntimeCondition = subtractRuntimeCondition(
1122
+ reducedRuntimeCondition = subtractRuntimeCondition(
983
1123
  runtimeCondition,
984
- existingEntry,
1124
+ existingEntry.runtimeCondition,
985
1125
  runtime
986
1126
  );
987
- if (reducedRuntimeCondition === false) return;
988
- runtimeCondition = reducedRuntimeCondition;
989
- existingEntries.set(
990
- connection.module,
991
- mergeRuntimeConditionNonFalse(
992
- existingEntry,
993
- runtimeCondition,
994
- runtime
995
- )
1127
+ reducedNonDeferAccess = subtractNonDeferAccess(
1128
+ nonDeferAccess,
1129
+ existingEntry.nonDeferAccess
996
1130
  );
1131
+ if (
1132
+ reducedRuntimeCondition === false &&
1133
+ reducedNonDeferAccess === false
1134
+ ) {
1135
+ return;
1136
+ }
1137
+ if (reducedRuntimeCondition !== false) {
1138
+ existingEntry.runtimeCondition = mergeRuntimeConditionNonFalse(
1139
+ existingEntry.runtimeCondition,
1140
+ reducedRuntimeCondition,
1141
+ runtime
1142
+ );
1143
+ }
1144
+ if (reducedNonDeferAccess !== false) {
1145
+ existingEntry.nonDeferAccess = mergeNonDeferAccess(
1146
+ existingEntry.nonDeferAccess,
1147
+ reducedNonDeferAccess
1148
+ );
1149
+ }
997
1150
  } else {
998
- existingEntries.set(connection.module, runtimeCondition);
1151
+ reducedRuntimeCondition = runtimeCondition;
1152
+ reducedNonDeferAccess = nonDeferAccess;
1153
+ existingEntries.set(connection.module, {
1154
+ runtimeCondition,
1155
+ nonDeferAccess
1156
+ });
999
1157
  }
1000
1158
  if (list.length > 0) {
1001
1159
  const lastItem = list[list.length - 1];
@@ -1005,9 +1163,13 @@ class ConcatenatedModule extends Module {
1005
1163
  ) {
1006
1164
  lastItem.runtimeCondition = mergeRuntimeCondition(
1007
1165
  lastItem.runtimeCondition,
1008
- runtimeCondition,
1166
+ reducedRuntimeCondition,
1009
1167
  runtime
1010
1168
  );
1169
+ lastItem.nonDeferAccess = mergeNonDeferAccess(
1170
+ lastItem.nonDeferAccess,
1171
+ reducedNonDeferAccess
1172
+ );
1011
1173
  return;
1012
1174
  }
1013
1175
  }
@@ -1019,19 +1181,25 @@ class ConcatenatedModule extends Module {
1019
1181
  // concatenated module)
1020
1182
  return connection.module;
1021
1183
  },
1022
- runtimeCondition
1184
+ runtimeCondition: reducedRuntimeCondition,
1185
+ nonDeferAccess: reducedNonDeferAccess
1023
1186
  });
1024
1187
  }
1025
1188
  };
1026
1189
 
1027
- existingEntries.set(rootModule, true);
1190
+ existingEntries.set(rootModule, {
1191
+ runtimeCondition: true,
1192
+ nonDeferAccess: true
1193
+ });
1028
1194
  const imports = getConcatenatedImports(rootModule);
1029
- for (const { connection, runtimeCondition } of imports)
1030
- enterModule(connection, runtimeCondition);
1195
+ for (const { connection, runtimeCondition, nonDeferAccess } of imports) {
1196
+ enterModule(connection, runtimeCondition, nonDeferAccess);
1197
+ }
1031
1198
  list.push({
1032
1199
  type: "concatenated",
1033
1200
  module: rootModule,
1034
- runtimeCondition: true
1201
+ runtimeCondition: true,
1202
+ nonDeferAccess: true
1035
1203
  });
1036
1204
 
1037
1205
  return list;
@@ -1040,7 +1208,7 @@ class ConcatenatedModule extends Module {
1040
1208
  /**
1041
1209
  * @param {Module} rootModule the root module of the concatenation
1042
1210
  * @param {Set<Module>} modules all modules in the concatenation (including the root module)
1043
- * @param {object=} associatedObjectForCache object for caching
1211
+ * @param {AssociatedObjectForCache=} associatedObjectForCache object for caching
1044
1212
  * @param {string | HashConstructor=} hashFunction hash function to use
1045
1213
  * @returns {string} the identifier
1046
1214
  */
@@ -1048,7 +1216,7 @@ class ConcatenatedModule extends Module {
1048
1216
  rootModule,
1049
1217
  modules,
1050
1218
  associatedObjectForCache,
1051
- hashFunction = "md4"
1219
+ hashFunction = DEFAULTS.HASH_FUNCTION
1052
1220
  ) {
1053
1221
  const cachedMakePathsRelative = makePathsRelative.bindContextCache(
1054
1222
  /** @type {string} */ (rootModule.context),
@@ -1065,10 +1233,10 @@ class ConcatenatedModule extends Module {
1065
1233
  }
1066
1234
 
1067
1235
  /**
1068
- * @param {LazySet<string>} fileDependencies set where file dependencies are added to
1069
- * @param {LazySet<string>} contextDependencies set where context dependencies are added to
1070
- * @param {LazySet<string>} missingDependencies set where missing dependencies are added to
1071
- * @param {LazySet<string>} buildDependencies set where build dependencies are added to
1236
+ * @param {FileSystemDependencies} fileDependencies set where file dependencies are added to
1237
+ * @param {FileSystemDependencies} contextDependencies set where context dependencies are added to
1238
+ * @param {FileSystemDependencies} missingDependencies set where missing dependencies are added to
1239
+ * @param {FileSystemDependencies} buildDependencies set where build dependencies are added to
1072
1240
  */
1073
1241
  addCacheDependencies(
1074
1242
  fileDependencies,
@@ -1096,8 +1264,13 @@ class ConcatenatedModule extends Module {
1096
1264
  moduleGraph,
1097
1265
  chunkGraph,
1098
1266
  runtime: generationRuntime,
1267
+ runtimes,
1099
1268
  codeGenerationResults
1100
1269
  }) {
1270
+ const { concatenatedModuleInfo } = ConcatenatedModule.getCompilationHooks(
1271
+ this.compilation
1272
+ );
1273
+
1101
1274
  /** @type {RuntimeRequirements} */
1102
1275
  const runtimeRequirements = new Set();
1103
1276
  const runtime = intersectRuntime(generationRuntime, this._runtime);
@@ -1110,9 +1283,12 @@ class ConcatenatedModule extends Module {
1110
1283
  );
1111
1284
 
1112
1285
  // Set with modules that need a generated namespace object
1113
- /** @type {Set<ConcatenatedModuleInfo>} */
1286
+ /** @type {NeededNamespaceObjects} */
1114
1287
  const neededNamespaceObjects = new Set();
1115
1288
 
1289
+ // List of all used names to avoid conflicts
1290
+ const allUsedNames = new Set(RESERVED_NAMES);
1291
+
1116
1292
  // Generate source code and analyse scopes
1117
1293
  // Prepare a ReplaceSource for the final source
1118
1294
  for (const info of moduleToInfoMap.values()) {
@@ -1124,24 +1300,20 @@ class ConcatenatedModule extends Module {
1124
1300
  moduleGraph,
1125
1301
  chunkGraph,
1126
1302
  runtime,
1303
+ runtimes,
1127
1304
  /** @type {CodeGenerationResults} */
1128
- (codeGenerationResults)
1305
+ (codeGenerationResults),
1306
+ allUsedNames
1129
1307
  );
1130
1308
  }
1131
1309
 
1132
- // List of all used names to avoid conflicts
1133
- const allUsedNames = new Set(RESERVED_NAMES);
1134
1310
  // Updated Top level declarations are created by renaming
1311
+ /** @type {TopLevelDeclarations} */
1135
1312
  const topLevelDeclarations = new Set();
1136
1313
 
1137
1314
  // List of additional names in scope for module references
1138
- /** @type {Map<string, { usedNames: UsedNames, alreadyCheckedScopes: Set<TODO> }>} */
1315
+ /** @type {Map<string, ScopeInfo>} */
1139
1316
  const usedNamesInScopeInfo = new Map();
1140
- /**
1141
- * @param {string} module module identifier
1142
- * @param {string} id export id
1143
- * @returns {{ usedNames: UsedNames, alreadyCheckedScopes: Set<TODO> }} info
1144
- */
1145
1317
 
1146
1318
  // Set of already checked scopes
1147
1319
  const ignoredScopes = new Set();
@@ -1160,9 +1332,9 @@ class ConcatenatedModule extends Module {
1160
1332
  const superClassCache = new WeakMap();
1161
1333
  /**
1162
1334
  * @param {Scope} scope scope
1163
- * @returns {TODO} result
1335
+ * @returns {{ range: Range, variables: Variable[] }[]} result
1164
1336
  */
1165
- const getSuperClassExpressions = scope => {
1337
+ const getSuperClassExpressions = (scope) => {
1166
1338
  const cacheEntry = superClassCache.get(scope);
1167
1339
  if (cacheEntry !== undefined) return cacheEntry;
1168
1340
  const superClassExpressions = [];
@@ -1175,7 +1347,7 @@ class ConcatenatedModule extends Module {
1175
1347
  block.superClass
1176
1348
  ) {
1177
1349
  superClassExpressions.push({
1178
- range: block.superClass.range,
1350
+ range: /** @type {Range} */ (block.superClass.range),
1179
1351
  variables: childScope.variables
1180
1352
  });
1181
1353
  }
@@ -1192,8 +1364,9 @@ class ConcatenatedModule extends Module {
1192
1364
  const match = ConcatenationScope.matchModuleReference(name);
1193
1365
  if (!match) continue;
1194
1366
  const referencedInfo = modulesWithInfo[match.index];
1195
- if (referencedInfo.type === "reference")
1367
+ if (referencedInfo.type === "reference") {
1196
1368
  throw new Error("Module reference can't point to a reference");
1369
+ }
1197
1370
  const binding = getFinalBinding(
1198
1371
  moduleGraph,
1199
1372
  referencedInfo,
@@ -1204,6 +1377,7 @@ class ConcatenatedModule extends Module {
1204
1377
  runtimeTemplate,
1205
1378
  neededNamespaceObjects,
1206
1379
  false,
1380
+ match.deferredImport,
1207
1381
  /** @type {BuildMeta} */
1208
1382
  (info.module.buildMeta).strictHarmonyModule,
1209
1383
  true
@@ -1241,6 +1415,75 @@ class ConcatenatedModule extends Module {
1241
1415
  }
1242
1416
  }
1243
1417
 
1418
+ /**
1419
+ * @param {string} name the name to find a new name for
1420
+ * @param {ConcatenatedModuleInfo} info the info of the module
1421
+ * @param {Reference[]} references the references to the name
1422
+ * @returns {string | undefined} the new name or undefined if the name is not found
1423
+ */
1424
+ const _findNewName = (name, info, references) => {
1425
+ const { usedNames, alreadyCheckedScopes } = getUsedNamesInScopeInfo(
1426
+ usedNamesInScopeInfo,
1427
+ info.module.identifier(),
1428
+ name
1429
+ );
1430
+ if (allUsedNames.has(name) || usedNames.has(name)) {
1431
+ for (const ref of references) {
1432
+ addScopeSymbols(
1433
+ ref.from,
1434
+ usedNames,
1435
+ alreadyCheckedScopes,
1436
+ ignoredScopes
1437
+ );
1438
+ }
1439
+ const newName = findNewName(
1440
+ name,
1441
+ allUsedNames,
1442
+ usedNames,
1443
+ info.module.readableIdentifier(requestShortener)
1444
+ );
1445
+ allUsedNames.add(newName);
1446
+ info.internalNames.set(name, newName);
1447
+ topLevelDeclarations.add(newName);
1448
+ return newName;
1449
+ }
1450
+ };
1451
+
1452
+ /**
1453
+ * @param {string} name the name to find a new name for
1454
+ * @param {ConcatenatedModuleInfo} info the info of the module
1455
+ * @param {Reference[]} references the references to the name
1456
+ * @returns {string | undefined} the new name or undefined if the name is not found
1457
+ */
1458
+ const _findNewNameForSpecifier = (name, info, references) => {
1459
+ const { usedNames: moduleUsedNames, alreadyCheckedScopes } =
1460
+ getUsedNamesInScopeInfo(
1461
+ usedNamesInScopeInfo,
1462
+ info.module.identifier(),
1463
+ name
1464
+ );
1465
+ const referencesUsedNames = new Set();
1466
+ for (const ref of references) {
1467
+ addScopeSymbols(
1468
+ ref.from,
1469
+ referencesUsedNames,
1470
+ alreadyCheckedScopes,
1471
+ ignoredScopes
1472
+ );
1473
+ }
1474
+ if (moduleUsedNames.has(name) || referencesUsedNames.has(name)) {
1475
+ const newName = findNewName(
1476
+ name,
1477
+ allUsedNames,
1478
+ new Set([...moduleUsedNames, ...referencesUsedNames]),
1479
+ info.module.readableIdentifier(requestShortener)
1480
+ );
1481
+ allUsedNames.add(newName);
1482
+ topLevelDeclarations.add(newName);
1483
+ return newName;
1484
+ }
1485
+ };
1486
+
1244
1487
  // generate names for symbols
1245
1488
  for (const info of moduleToInfoMap.values()) {
1246
1489
  const { usedNames: namespaceObjectUsedNames } = getUsedNamesInScopeInfo(
@@ -1253,34 +1496,14 @@ class ConcatenatedModule extends Module {
1253
1496
  const variables = /** @type {Scope} */ (info.moduleScope).variables;
1254
1497
  for (const variable of variables) {
1255
1498
  const name = variable.name;
1256
- const { usedNames, alreadyCheckedScopes } = getUsedNamesInScopeInfo(
1257
- usedNamesInScopeInfo,
1258
- info.module.identifier(),
1259
- name
1260
- );
1261
- if (allUsedNames.has(name) || usedNames.has(name)) {
1262
- const references = getAllReferences(variable);
1263
- for (const ref of references) {
1264
- addScopeSymbols(
1265
- ref.from,
1266
- usedNames,
1267
- alreadyCheckedScopes,
1268
- ignoredScopes
1269
- );
1270
- }
1271
- const newName = findNewName(
1272
- name,
1273
- allUsedNames,
1274
- usedNames,
1275
- info.module.readableIdentifier(requestShortener)
1276
- );
1277
- allUsedNames.add(newName);
1278
- info.internalNames.set(name, newName);
1279
- topLevelDeclarations.add(newName);
1499
+ const references = getAllReferences(variable);
1500
+ const newName = _findNewName(name, info, references);
1501
+ if (newName) {
1280
1502
  const source = /** @type {ReplaceSource} */ (info.source);
1281
- const allIdentifiers = new Set(
1282
- references.map(r => r.identifier).concat(variable.identifiers)
1283
- );
1503
+ const allIdentifiers = new Set([
1504
+ ...references.map((r) => r.identifier),
1505
+ ...variable.identifiers
1506
+ ]);
1284
1507
  for (const identifier of allIdentifiers) {
1285
1508
  const r = /** @type {Range} */ (identifier.range);
1286
1509
  const path = getPathInAst(
@@ -1327,7 +1550,10 @@ class ConcatenatedModule extends Module {
1327
1550
  info.namespaceObjectName =
1328
1551
  /** @type {string} */
1329
1552
  (namespaceObjectName);
1330
- topLevelDeclarations.add(namespaceObjectName);
1553
+ topLevelDeclarations.add(
1554
+ /** @type {string} */
1555
+ (namespaceObjectName)
1556
+ );
1331
1557
  break;
1332
1558
  }
1333
1559
  case "external": {
@@ -1340,6 +1566,28 @@ class ConcatenatedModule extends Module {
1340
1566
  allUsedNames.add(externalName);
1341
1567
  info.name = externalName;
1342
1568
  topLevelDeclarations.add(externalName);
1569
+
1570
+ if (info.deferred) {
1571
+ const externalName = findNewName(
1572
+ "deferred",
1573
+ allUsedNames,
1574
+ namespaceObjectUsedNames,
1575
+ info.module.readableIdentifier(requestShortener)
1576
+ );
1577
+ allUsedNames.add(externalName);
1578
+ info.deferredName = externalName;
1579
+ topLevelDeclarations.add(externalName);
1580
+
1581
+ const externalNameInterop = findNewName(
1582
+ "deferredNamespaceObject",
1583
+ allUsedNames,
1584
+ namespaceObjectUsedNames,
1585
+ info.module.readableIdentifier(requestShortener)
1586
+ );
1587
+ allUsedNames.add(externalNameInterop);
1588
+ info.deferredNamespaceObjectName = externalNameInterop;
1589
+ topLevelDeclarations.add(externalNameInterop);
1590
+ }
1343
1591
  break;
1344
1592
  }
1345
1593
  }
@@ -1357,7 +1605,8 @@ class ConcatenatedModule extends Module {
1357
1605
  }
1358
1606
  if (
1359
1607
  buildMeta.exportsType === "default" &&
1360
- buildMeta.defaultObject !== "redirect"
1608
+ buildMeta.defaultObject !== "redirect" &&
1609
+ info.interopNamespaceObject2Used
1361
1610
  ) {
1362
1611
  const externalNameInterop = findNewName(
1363
1612
  "namespaceObject2",
@@ -1386,13 +1635,48 @@ class ConcatenatedModule extends Module {
1386
1635
  for (const info of moduleToInfoMap.values()) {
1387
1636
  if (info.type === "concatenated") {
1388
1637
  const globalScope = /** @type {Scope} */ (info.globalScope);
1638
+ // group references by name
1639
+ const referencesByName = new Map();
1389
1640
  for (const reference of globalScope.through) {
1390
1641
  const name = reference.identifier.name;
1642
+ if (!referencesByName.has(name)) {
1643
+ referencesByName.set(name, []);
1644
+ }
1645
+ referencesByName.get(name).push(reference);
1646
+ }
1647
+ for (const [name, references] of referencesByName) {
1391
1648
  const match = ConcatenationScope.matchModuleReference(name);
1392
1649
  if (match) {
1393
1650
  const referencedInfo = modulesWithInfo[match.index];
1394
- if (referencedInfo.type === "reference")
1651
+ if (referencedInfo.type === "reference") {
1395
1652
  throw new Error("Module reference can't point to a reference");
1653
+ }
1654
+ const concatenationScope = /** @type {ConcatenatedModuleInfo} */ (
1655
+ referencedInfo
1656
+ ).concatenationScope;
1657
+ const exportId = match.ids[0];
1658
+ const specifier =
1659
+ concatenationScope && concatenationScope.getRawExport(exportId);
1660
+ if (specifier) {
1661
+ const newName = _findNewNameForSpecifier(
1662
+ specifier,
1663
+ info,
1664
+ references
1665
+ );
1666
+ const initFragmentChanged =
1667
+ newName &&
1668
+ concatenatedModuleInfo.call(
1669
+ {
1670
+ rawExportMap: new Map([
1671
+ [exportId, /** @type {string} */ (newName)]
1672
+ ])
1673
+ },
1674
+ /** @type {ConcatenatedModuleInfo} */ (referencedInfo)
1675
+ );
1676
+ if (initFragmentChanged) {
1677
+ concatenationScope.setRawExportMap(exportId, newName);
1678
+ }
1679
+ }
1396
1680
  const finalName = getFinalName(
1397
1681
  moduleGraph,
1398
1682
  referencedInfo,
@@ -1403,31 +1687,35 @@ class ConcatenatedModule extends Module {
1403
1687
  runtimeTemplate,
1404
1688
  neededNamespaceObjects,
1405
1689
  match.call,
1690
+ match.deferredImport,
1406
1691
  !match.directImport,
1407
1692
  /** @type {BuildMeta} */
1408
1693
  (info.module.buildMeta).strictHarmonyModule,
1409
1694
  match.asiSafe
1410
1695
  );
1411
- const r = /** @type {Range} */ (reference.identifier.range);
1412
- const source = /** @type {ReplaceSource} */ (info.source);
1413
- // range is extended by 2 chars to cover the appended "._"
1414
- source.replace(r[0], r[1] + 1, finalName);
1696
+
1697
+ for (const reference of references) {
1698
+ const r = /** @type {Range} */ (reference.identifier.range);
1699
+ const source = /** @type {ReplaceSource} */ (info.source);
1700
+ // range is extended by 2 chars to cover the appended "._"
1701
+ source.replace(r[0], r[1] + 1, finalName);
1702
+ }
1415
1703
  }
1416
1704
  }
1417
1705
  }
1418
1706
  }
1419
1707
 
1420
1708
  // Map with all root exposed used exports
1421
- /** @type {Map<string, function(RequestShortener): string>} */
1709
+ /** @type {Map<string, (requestShortener: RequestShortener) => string>} */
1422
1710
  const exportsMap = new Map();
1423
1711
 
1424
1712
  // Set with all root exposed unused exports
1425
1713
  /** @type {Set<string>} */
1426
1714
  const unusedExports = new Set();
1427
1715
 
1428
- const rootInfo = /** @type {ConcatenatedModuleInfo} */ (
1429
- moduleToInfoMap.get(this.rootModule)
1430
- );
1716
+ const rootInfo =
1717
+ /** @type {ConcatenatedModuleInfo} */
1718
+ (moduleToInfoMap.get(this.rootModule));
1431
1719
  const strictHarmonyModule =
1432
1720
  /** @type {BuildMeta} */
1433
1721
  (rootInfo.module.buildMeta).strictHarmonyModule;
@@ -1442,7 +1730,7 @@ class ConcatenatedModule extends Module {
1442
1730
  unusedExports.add(name);
1443
1731
  continue;
1444
1732
  }
1445
- exportsMap.set(used, requestShortener => {
1733
+ exportsMap.set(used, (requestShortener) => {
1446
1734
  try {
1447
1735
  const finalName = getFinalName(
1448
1736
  moduleGraph,
@@ -1455,6 +1743,7 @@ class ConcatenatedModule extends Module {
1455
1743
  neededNamespaceObjects,
1456
1744
  false,
1457
1745
  false,
1746
+ false,
1458
1747
  strictHarmonyModule,
1459
1748
  true
1460
1749
  );
@@ -1475,19 +1764,17 @@ class ConcatenatedModule extends Module {
1475
1764
 
1476
1765
  // add harmony compatibility flag (must be first because of possible circular dependencies)
1477
1766
  let shouldAddHarmonyFlag = false;
1767
+ const rootExportsInfo = moduleGraph.getExportsInfo(this);
1478
1768
  if (
1479
- moduleGraph.getExportsInfo(this).otherExportsInfo.getUsed(runtime) !==
1480
- UsageState.Unused
1769
+ rootExportsInfo.otherExportsInfo.getUsed(runtime) !== UsageState.Unused ||
1770
+ rootExportsInfo.getReadOnlyExportInfo("__esModule").getUsed(runtime) !==
1771
+ UsageState.Unused
1481
1772
  ) {
1482
1773
  shouldAddHarmonyFlag = true;
1483
1774
  }
1484
1775
 
1485
1776
  // define exports
1486
1777
  if (exportsMap.size > 0) {
1487
- const { exportsDefinitions } = ConcatenatedModule.getCompilationHooks(
1488
- /** @type {Compilation} */ (this.compilation)
1489
- );
1490
-
1491
1778
  const definitions = [];
1492
1779
  for (const [key, value] of exportsMap) {
1493
1780
  definitions.push(
@@ -1496,32 +1783,38 @@ class ConcatenatedModule extends Module {
1496
1783
  )}`
1497
1784
  );
1498
1785
  }
1499
- const shouldSkipRenderDefinitions =
1500
- exportsDefinitions.call(exportsFinalName);
1501
-
1502
- if (!shouldSkipRenderDefinitions) {
1503
- runtimeRequirements.add(RuntimeGlobals.exports);
1504
- runtimeRequirements.add(RuntimeGlobals.definePropertyGetters);
1505
1786
 
1506
- if (shouldAddHarmonyFlag) {
1507
- result.add("// ESM COMPAT FLAG\n");
1508
- result.add(
1509
- runtimeTemplate.defineEsModuleFlagStatement({
1510
- exportsArgument: this.exportsArgument,
1511
- runtimeRequirements
1512
- })
1513
- );
1514
- }
1787
+ runtimeRequirements.add(RuntimeGlobals.exports);
1788
+ runtimeRequirements.add(RuntimeGlobals.definePropertyGetters);
1515
1789
 
1516
- result.add("\n// EXPORTS\n");
1790
+ if (shouldAddHarmonyFlag) {
1791
+ result.add("// ESM COMPAT FLAG\n");
1517
1792
  result.add(
1518
- `${RuntimeGlobals.definePropertyGetters}(${
1519
- this.exportsArgument
1520
- }, {${definitions.join(",")}\n});\n`
1793
+ runtimeTemplate.defineEsModuleFlagStatement({
1794
+ exportsArgument: this.exportsArgument,
1795
+ runtimeRequirements
1796
+ })
1521
1797
  );
1522
- } else {
1523
- /** @type {BuildMeta} */
1524
- (this.buildMeta).exportsFinalName = exportsFinalName;
1798
+ }
1799
+
1800
+ const exportsSource =
1801
+ "\n// EXPORTS\n" +
1802
+ `${RuntimeGlobals.definePropertyGetters}(${this.exportsArgument}, {${definitions.join(
1803
+ ","
1804
+ )}\n});\n`;
1805
+
1806
+ const { onDemandExportsGeneration } =
1807
+ ConcatenatedModule.getCompilationHooks(this.compilation);
1808
+
1809
+ if (
1810
+ !onDemandExportsGeneration.call(
1811
+ this,
1812
+ runtimes,
1813
+ exportsSource,
1814
+ exportsFinalName
1815
+ )
1816
+ ) {
1817
+ result.add(exportsSource);
1525
1818
  }
1526
1819
  }
1527
1820
 
@@ -1552,6 +1845,7 @@ class ConcatenatedModule extends Module {
1552
1845
  runtimeTemplate,
1553
1846
  neededNamespaceObjects,
1554
1847
  false,
1848
+ false,
1555
1849
  undefined,
1556
1850
  /** @type {BuildMeta} */
1557
1851
  (info.module.buildMeta).strictHarmonyModule,
@@ -1571,8 +1865,9 @@ class ConcatenatedModule extends Module {
1571
1865
  ","
1572
1866
  )}\n});\n`
1573
1867
  : "";
1574
- if (nsObj.length > 0)
1868
+ if (nsObj.length > 0) {
1575
1869
  runtimeRequirements.add(RuntimeGlobals.definePropertyGetters);
1870
+ }
1576
1871
  namespaceObjectSources.set(
1577
1872
  info,
1578
1873
  `
@@ -1591,8 +1886,40 @@ ${defineGetters}`
1591
1886
  if (!source) continue;
1592
1887
  result.add(source);
1593
1888
  }
1889
+
1890
+ if (info.type === "external" && info.deferred) {
1891
+ const moduleId = JSON.stringify(chunkGraph.getModuleId(info.module));
1892
+ const loader = getOptimizedDeferredModule(
1893
+ moduleId,
1894
+ info.module.getExportsType(
1895
+ moduleGraph,
1896
+ /** @type {BuildMeta} */
1897
+ (this.rootModule.buildMeta).strictHarmonyModule
1898
+ ),
1899
+ // an async module will opt-out of the concat module optimization.
1900
+ [],
1901
+ runtimeRequirements
1902
+ );
1903
+ runtimeRequirements.add(RuntimeGlobals.require);
1904
+ result.add(
1905
+ `\n// DEFERRED EXTERNAL MODULE: ${info.module.readableIdentifier(requestShortener)}\nvar ${info.deferredName} = ${loader};`
1906
+ );
1907
+ if (info.deferredNamespaceObjectUsed) {
1908
+ runtimeRequirements.add(RuntimeGlobals.makeDeferredNamespaceObject);
1909
+ result.add(
1910
+ `\nvar ${info.deferredNamespaceObjectName} = /*#__PURE__*/${
1911
+ RuntimeGlobals.makeDeferredNamespaceObject
1912
+ }(${JSON.stringify(
1913
+ chunkGraph.getModuleId(info.module)
1914
+ )}, ${getMakeDeferredNamespaceModeFromExportsType(
1915
+ info.module.getExportsType(moduleGraph, strictHarmonyModule)
1916
+ )});`
1917
+ );
1918
+ }
1919
+ }
1594
1920
  }
1595
1921
 
1922
+ /** @type {InitFragment<ChunkRenderContext>[]} */
1596
1923
  const chunkInitFragments = [];
1597
1924
 
1598
1925
  // evaluate modules in order
@@ -1618,30 +1945,44 @@ ${defineGetters}`
1618
1945
  break;
1619
1946
  }
1620
1947
  case "external": {
1621
- result.add(
1622
- `\n// EXTERNAL MODULE: ${info.module.readableIdentifier(
1623
- requestShortener
1624
- )}\n`
1625
- );
1626
- runtimeRequirements.add(RuntimeGlobals.require);
1627
- const { runtimeCondition } =
1628
- /** @type {ExternalModuleInfo | ReferenceToModuleInfo} */ (rawInfo);
1629
- const condition = runtimeTemplate.runtimeConditionExpression({
1630
- chunkGraph,
1631
- runtimeCondition,
1632
- runtime,
1633
- runtimeRequirements
1634
- });
1635
- if (condition !== "true") {
1636
- isConditional = true;
1637
- result.add(`if (${condition}) {\n`);
1638
- }
1639
- result.add(
1640
- `var ${info.name} = ${RuntimeGlobals.require}(${JSON.stringify(
1948
+ // deferred case is handled in the "const info of modulesWithInfo" loop above
1949
+ if (!info.deferred) {
1950
+ result.add(
1951
+ `\n// EXTERNAL MODULE: ${info.module.readableIdentifier(
1952
+ requestShortener
1953
+ )}\n`
1954
+ );
1955
+ runtimeRequirements.add(RuntimeGlobals.require);
1956
+ const { runtimeCondition } =
1957
+ /** @type {ExternalModuleInfo | ReferenceToModuleInfo} */
1958
+ (rawInfo);
1959
+ const condition = runtimeTemplate.runtimeConditionExpression({
1960
+ chunkGraph,
1961
+ runtimeCondition,
1962
+ runtime,
1963
+ runtimeRequirements
1964
+ });
1965
+ if (condition !== "true") {
1966
+ isConditional = true;
1967
+ result.add(`if (${condition}) {\n`);
1968
+ }
1969
+ const moduleId = JSON.stringify(
1641
1970
  chunkGraph.getModuleId(info.module)
1642
- )});`
1643
- );
1644
- name = info.name;
1971
+ );
1972
+ result.add(`var ${info.name} = __webpack_require__(${moduleId});`);
1973
+ name = info.name;
1974
+ }
1975
+ // If a module is deferred in other places, but used as non-deferred here,
1976
+ // the module itself will be emitted as mod_deferred (in the case "external"),
1977
+ // we need to emit an extra import declaration to evaluate it in order.
1978
+ const { nonDeferAccess } =
1979
+ /** @type {ExternalModuleInfo | ReferenceToModuleInfo} */
1980
+ (rawInfo);
1981
+ if (info.deferred && nonDeferAccess) {
1982
+ result.add(
1983
+ `\n// non-deferred import to a deferred module (${info.module.readableIdentifier(requestShortener)})\nvar ${info.name} = ${info.deferredName}.a;`
1984
+ );
1985
+ }
1645
1986
  break;
1646
1987
  }
1647
1988
  default:
@@ -1672,13 +2013,14 @@ ${defineGetters}`
1672
2013
  }
1673
2014
 
1674
2015
  const data = new Map();
1675
- if (chunkInitFragments.length > 0)
2016
+ if (chunkInitFragments.length > 0) {
1676
2017
  data.set("chunkInitFragments", chunkInitFragments);
2018
+ }
1677
2019
  data.set("topLevelDeclarations", topLevelDeclarations);
1678
2020
 
1679
2021
  /** @type {CodeGenerationResult} */
1680
2022
  const resultEntry = {
1681
- sources: new Map([["javascript", new CachedSource(result)]]),
2023
+ sources: new Map([[JAVASCRIPT_TYPE, new CachedSource(result)]]),
1682
2024
  data,
1683
2025
  runtimeRequirements
1684
2026
  };
@@ -1687,14 +2029,16 @@ ${defineGetters}`
1687
2029
  }
1688
2030
 
1689
2031
  /**
1690
- * @param {Map<Module, ModuleInfo>} modulesMap modulesMap
2032
+ * @param {ModuleToInfoMap} modulesMap modulesMap
1691
2033
  * @param {ModuleInfo} info info
1692
2034
  * @param {DependencyTemplates} dependencyTemplates dependencyTemplates
1693
2035
  * @param {RuntimeTemplate} runtimeTemplate runtimeTemplate
1694
2036
  * @param {ModuleGraph} moduleGraph moduleGraph
1695
2037
  * @param {ChunkGraph} chunkGraph chunkGraph
1696
2038
  * @param {RuntimeSpec} runtime runtime
2039
+ * @param {RuntimeSpec[]} runtimes runtimes
1697
2040
  * @param {CodeGenerationResults} codeGenerationResults codeGenerationResults
2041
+ * @param {Set<string>} usedNames used names
1698
2042
  */
1699
2043
  _analyseModule(
1700
2044
  modulesMap,
@@ -1704,13 +2048,19 @@ ${defineGetters}`
1704
2048
  moduleGraph,
1705
2049
  chunkGraph,
1706
2050
  runtime,
1707
- codeGenerationResults
2051
+ runtimes,
2052
+ codeGenerationResults,
2053
+ usedNames
1708
2054
  ) {
1709
2055
  if (info.type === "concatenated") {
1710
2056
  const m = info.module;
1711
2057
  try {
1712
2058
  // Create a concatenation scope to track and capture information
1713
- const concatenationScope = new ConcatenationScope(modulesMap, info);
2059
+ const concatenationScope = new ConcatenationScope(
2060
+ modulesMap,
2061
+ info,
2062
+ usedNames
2063
+ );
1714
2064
 
1715
2065
  // TODO cache codeGeneration results
1716
2066
  const codeGenResult = m.codeGeneration({
@@ -1719,23 +2069,34 @@ ${defineGetters}`
1719
2069
  moduleGraph,
1720
2070
  chunkGraph,
1721
2071
  runtime,
2072
+ runtimes,
1722
2073
  concatenationScope,
1723
2074
  codeGenerationResults,
1724
- sourceTypes: JS_TYPES
2075
+ sourceTypes: JAVASCRIPT_TYPES
1725
2076
  });
1726
- const source = /** @type {Source} */ (
1727
- codeGenResult.sources.get("javascript")
1728
- );
2077
+ const source =
2078
+ /** @type {Source} */
2079
+ (codeGenResult.sources.get(JAVASCRIPT_TYPE));
1729
2080
  const data = codeGenResult.data;
1730
2081
  const chunkInitFragments = data && data.get("chunkInitFragments");
1731
2082
  const code = source.source().toString();
2083
+
2084
+ /** @type {Program} */
1732
2085
  let ast;
2086
+
1733
2087
  try {
1734
- ast = JavascriptParser._parse(code, {
1735
- sourceType: "module"
1736
- });
2088
+ ({ ast } = JavascriptParser._parse(
2089
+ code,
2090
+ {
2091
+ sourceType: "module",
2092
+ ranges: true
2093
+ },
2094
+ JavascriptParser._getModuleParseFunction(this.compilation, m)
2095
+ ));
1737
2096
  } catch (_err) {
1738
- const err = /** @type {TODO} */ (_err);
2097
+ const err =
2098
+ /** @type {Error & { loc?: { line: number, column: number } }} */
2099
+ (_err);
1739
2100
  if (
1740
2101
  err.loc &&
1741
2102
  typeof err.loc === "object" &&
@@ -1768,6 +2129,7 @@ ${defineGetters}`
1768
2129
  info.chunkInitFragments = chunkInitFragments;
1769
2130
  info.globalScope = globalScope;
1770
2131
  info.moduleScope = moduleScope;
2132
+ info.concatenationScope = concatenationScope;
1771
2133
  } catch (err) {
1772
2134
  /** @type {Error} */
1773
2135
  (err).message +=
@@ -1780,7 +2142,7 @@ ${defineGetters}`
1780
2142
  /**
1781
2143
  * @param {ModuleGraph} moduleGraph the module graph
1782
2144
  * @param {RuntimeSpec} runtime the runtime
1783
- * @returns {[ModuleInfoOrReference[], Map<Module, ModuleInfo>]} module info items
2145
+ * @returns {[ModuleInfoOrReference[], ModuleToInfoMap]} module info items
1784
2146
  */
1785
2147
  _getModulesWithInfo(moduleGraph, runtime) {
1786
2148
  const orderedConcatenationList = this._createConcatenationList(
@@ -1789,7 +2151,7 @@ ${defineGetters}`
1789
2151
  runtime,
1790
2152
  moduleGraph
1791
2153
  );
1792
- /** @type {Map<Module, ModuleInfo>} */
2154
+ /** @type {ModuleToInfoMap} */
1793
2155
  const map = new Map();
1794
2156
  const list = orderedConcatenationList.map((info, index) => {
1795
2157
  let item = map.get(info.module);
@@ -1816,7 +2178,8 @@ ${defineGetters}`
1816
2178
  interopNamespaceObject2Used: false,
1817
2179
  interopNamespaceObject2Name: undefined,
1818
2180
  interopDefaultAccessUsed: false,
1819
- interopDefaultAccessName: undefined
2181
+ interopDefaultAccessName: undefined,
2182
+ concatenationScope: undefined
1820
2183
  };
1821
2184
  break;
1822
2185
  case "external":
@@ -1824,14 +2187,19 @@ ${defineGetters}`
1824
2187
  type: "external",
1825
2188
  module: info.module,
1826
2189
  runtimeCondition: info.runtimeCondition,
2190
+ nonDeferAccess: info.nonDeferAccess,
1827
2191
  index,
1828
2192
  name: undefined,
2193
+ deferredName: undefined,
1829
2194
  interopNamespaceObjectUsed: false,
1830
2195
  interopNamespaceObjectName: undefined,
1831
2196
  interopNamespaceObject2Used: false,
1832
2197
  interopNamespaceObject2Name: undefined,
1833
2198
  interopDefaultAccessUsed: false,
1834
- interopDefaultAccessName: undefined
2199
+ interopDefaultAccessName: undefined,
2200
+ deferred: moduleGraph.isDeferred(info.module),
2201
+ deferredNamespaceObjectName: undefined,
2202
+ deferredNamespaceObjectUsed: false
1835
2203
  };
1836
2204
  break;
1837
2205
  default:
@@ -1849,6 +2217,7 @@ ${defineGetters}`
1849
2217
  const ref = {
1850
2218
  type: "reference",
1851
2219
  runtimeCondition: info.runtimeCondition,
2220
+ nonDeferAccess: info.nonDeferAccess,
1852
2221
  target: item
1853
2222
  };
1854
2223
  return ref;