@depup/webpack 5.106.1-depup.0 → 5.109.2-depup.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (669) hide show
  1. package/README.md +9 -11
  2. package/bin/webpack.js +12 -0
  3. package/changes.json +16 -24
  4. package/hot/dev-server.js +2 -0
  5. package/lib/APIPlugin.js +60 -35
  6. package/lib/AsyncDependenciesBlock.js +3 -0
  7. package/lib/AutomaticPrefetchPlugin.js +6 -1
  8. package/lib/BannerPlugin.js +13 -5
  9. package/lib/Cache.js +27 -5
  10. package/lib/CacheFacade.js +41 -0
  11. package/lib/Chunk.js +71 -25
  12. package/lib/ChunkGraph.js +153 -19
  13. package/lib/ChunkGroup.js +115 -28
  14. package/lib/ChunkTemplate.js +9 -0
  15. package/lib/CircularModulesPlugin.js +190 -0
  16. package/lib/CleanPlugin.js +27 -28
  17. package/lib/CodeGenerationResults.js +19 -0
  18. package/lib/CompatibilityPlugin.js +85 -37
  19. package/lib/Compilation.js +656 -112
  20. package/lib/Compiler.js +109 -35
  21. package/lib/ConcatenationScope.js +34 -4
  22. package/lib/ConditionalInitFragment.js +6 -0
  23. package/lib/ConstPlugin.js +15 -12
  24. package/lib/ContextExclusionPlugin.js +3 -1
  25. package/lib/ContextModule.js +345 -55
  26. package/lib/ContextModuleFactory.js +193 -51
  27. package/lib/ContextReplacementPlugin.js +8 -1
  28. package/lib/DefinePlugin.js +537 -139
  29. package/lib/DependenciesBlock.js +6 -1
  30. package/lib/Dependency.js +159 -11
  31. package/lib/DependencyTemplate.js +7 -1
  32. package/lib/DependencyTemplates.js +4 -0
  33. package/lib/DotenvPlugin.js +3 -0
  34. package/lib/DynamicEntryPlugin.js +3 -1
  35. package/lib/EntryOptionPlugin.js +42 -2
  36. package/lib/EntryPlugin.js +4 -1
  37. package/lib/Entrypoint.js +5 -1
  38. package/lib/EnvironmentPlugin.js +3 -2
  39. package/lib/ErrorHelpers.js +7 -0
  40. package/lib/EvalDevToolModulePlugin.js +3 -1
  41. package/lib/EvalSourceMapDevToolPlugin.js +11 -11
  42. package/lib/ExportsInfo.js +421 -113
  43. package/lib/ExportsInfoApiPlugin.js +4 -3
  44. package/lib/ExternalModule.js +381 -80
  45. package/lib/ExternalModuleFactoryPlugin.js +75 -3
  46. package/lib/ExternalsPlugin.js +3 -1
  47. package/lib/FileSystemInfo.js +701 -175
  48. package/lib/FlagAllModulesAsUsedPlugin.js +3 -6
  49. package/lib/FlagDependencyExportsPlugin.js +82 -9
  50. package/lib/FlagDependencyUsagePlugin.js +115 -8
  51. package/lib/FlagEntryExportAsUsedPlugin.js +4 -1
  52. package/lib/Generator.js +24 -3
  53. package/lib/HotModuleReplacementPlugin.js +110 -44
  54. package/lib/IgnorePlugin.js +5 -2
  55. package/lib/IgnoreWarningsPlugin.js +2 -1
  56. package/lib/InitFragment.js +33 -30
  57. package/lib/JavascriptMetaInfoPlugin.js +9 -6
  58. package/lib/LazyBarrel.js +389 -0
  59. package/lib/LibraryTemplatePlugin.js +2 -1
  60. package/lib/LoaderOptionsPlugin.js +3 -1
  61. package/lib/LoaderTargetPlugin.js +3 -1
  62. package/lib/MainTemplate.js +15 -0
  63. package/lib/ManifestPlugin.js +9 -2
  64. package/lib/Module.js +121 -57
  65. package/lib/ModuleFactory.js +6 -1
  66. package/lib/ModuleFilenameHelpers.js +18 -4
  67. package/lib/ModuleGraph.js +69 -2
  68. package/lib/ModuleGraphConnection.js +9 -0
  69. package/lib/ModuleInfoHeaderPlugin.js +5 -0
  70. package/lib/ModuleNotFoundError.js +3 -83
  71. package/lib/ModuleProfile.js +27 -1
  72. package/lib/ModuleSourceTypeConstants.js +52 -19
  73. package/lib/ModuleTemplate.js +10 -0
  74. package/lib/ModuleTypeConstants.js +20 -4
  75. package/lib/MultiCompiler.js +59 -4
  76. package/lib/MultiStats.js +9 -0
  77. package/lib/MultiWatching.js +6 -0
  78. package/lib/NoEmitOnErrorsPlugin.js +1 -1
  79. package/lib/NodeStuffPlugin.js +78 -46
  80. package/lib/NormalModule.js +519 -206
  81. package/lib/NormalModuleFactory.js +156 -31
  82. package/lib/NormalModuleReplacementPlugin.js +3 -1
  83. package/lib/NullFactory.js +1 -0
  84. package/lib/OptionsApply.js +1 -0
  85. package/lib/Parser.js +3 -1
  86. package/lib/PlatformPlugin.js +2 -1
  87. package/lib/PrefetchPlugin.js +4 -1
  88. package/lib/ProgressPlugin.js +351 -161
  89. package/lib/ProvidePlugin.js +6 -3
  90. package/lib/RawModule.js +32 -16
  91. package/lib/RecordIdsPlugin.js +9 -0
  92. package/lib/RequestShortener.js +8 -0
  93. package/lib/ResolverFactory.js +5 -0
  94. package/lib/RuntimeGlobals.js +58 -5
  95. package/lib/RuntimeModule.js +22 -7
  96. package/lib/RuntimePlugin.js +123 -118
  97. package/lib/RuntimeTemplate.js +616 -37
  98. package/lib/SelfModuleFactory.js +3 -0
  99. package/lib/SourceMapDevToolModuleOptionsPlugin.js +2 -0
  100. package/lib/SourceMapDevToolPlugin.js +341 -63
  101. package/lib/Stats.js +6 -0
  102. package/lib/Template.js +52 -6
  103. package/lib/TemplatedPathPlugin.js +505 -138
  104. package/lib/UseStrictPlugin.js +3 -2
  105. package/lib/WarnCaseSensitiveModulesPlugin.js +72 -3
  106. package/lib/WarnDeprecatedOptionPlugin.js +6 -2
  107. package/lib/WarnNoModeSetPlugin.js +18 -2
  108. package/lib/WatchIgnorePlugin.js +5 -1
  109. package/lib/Watching.js +25 -3
  110. package/lib/WebpackError.js +3 -74
  111. package/lib/WebpackIsIncludedPlugin.js +4 -2
  112. package/lib/WebpackOptionsApply.js +88 -25
  113. package/lib/WebpackOptionsDefaulter.js +1 -0
  114. package/lib/asset/AssetBytesGenerator.js +28 -10
  115. package/lib/asset/AssetBytesParser.js +1 -0
  116. package/lib/asset/AssetGenerator.js +199 -60
  117. package/lib/asset/AssetModule.js +47 -0
  118. package/lib/asset/AssetModulesPlugin.js +115 -24
  119. package/lib/asset/AssetParser.js +4 -2
  120. package/lib/asset/AssetSourceGenerator.js +26 -8
  121. package/lib/asset/AssetSourceParser.js +1 -0
  122. package/lib/asset/RawDataUrlModule.js +23 -13
  123. package/lib/asset/WebManifestGenerator.js +164 -0
  124. package/lib/asset/WebManifestParser.js +130 -0
  125. package/lib/async-modules/AsyncModuleHelpers.js +1 -0
  126. package/lib/async-modules/AwaitDependenciesInitFragment.js +18 -4
  127. package/lib/async-modules/InferAsyncModulesPlugin.js +1 -1
  128. package/lib/async-modules/isGeneratorLowered.js +26 -0
  129. package/lib/buildChunkGraph.js +107 -15
  130. package/lib/bun/BunTargetPlugin.js +42 -0
  131. package/lib/cache/AddBuildDependenciesPlugin.js +3 -1
  132. package/lib/cache/AddManagedPathsPlugin.js +5 -1
  133. package/lib/cache/IdleFileCachePlugin.js +6 -1
  134. package/lib/cache/MemoryCachePlugin.js +1 -1
  135. package/lib/cache/MemoryWithGcCachePlugin.js +4 -1
  136. package/lib/cache/PackFileCacheStrategy.js +302 -12
  137. package/lib/cache/ResolverCachePlugin.js +12 -1
  138. package/lib/cache/getLazyHashedEtag.js +13 -2
  139. package/lib/cache/mergeEtags.js +4 -0
  140. package/lib/cli.js +142 -20
  141. package/lib/config/browserslistTargetHandler.js +141 -0
  142. package/lib/config/defaults.js +612 -43
  143. package/lib/config/defineConfig.js +31 -0
  144. package/lib/config/normalization.js +46 -1
  145. package/lib/config/target.js +200 -2
  146. package/lib/container/ContainerEntryDependency.js +2 -0
  147. package/lib/container/ContainerEntryModule.js +27 -14
  148. package/lib/container/ContainerEntryModuleFactory.js +1 -0
  149. package/lib/container/ContainerExposedDependency.js +6 -2
  150. package/lib/container/ContainerPlugin.js +2 -1
  151. package/lib/container/ContainerReferencePlugin.js +3 -2
  152. package/lib/container/FallbackDependency.js +9 -7
  153. package/lib/container/FallbackItemDependency.js +1 -0
  154. package/lib/container/FallbackModule.js +22 -9
  155. package/lib/container/FallbackModuleFactory.js +1 -0
  156. package/lib/container/HoistContainerReferencesPlugin.js +47 -55
  157. package/lib/container/ModuleFederationPlugin.js +21 -29
  158. package/lib/container/RemoteModule.js +33 -10
  159. package/lib/container/RemoteRuntimeModule.js +15 -12
  160. package/lib/container/RemoteToExternalDependency.js +1 -0
  161. package/lib/container/options.js +7 -0
  162. package/lib/css/CssGenerator.js +540 -272
  163. package/lib/css/CssInjectStyleRuntimeModule.js +86 -70
  164. package/lib/css/CssLoadingRuntimeModule.js +173 -81
  165. package/lib/{CssModule.js → css/CssModule.js} +72 -37
  166. package/lib/css/CssModulesPlugin.js +371 -208
  167. package/lib/css/CssParser.js +3538 -1963
  168. package/lib/css/syntax.js +4155 -0
  169. package/lib/debug/ProfilingPlugin.js +33 -2
  170. package/lib/deno/DenoTargetPlugin.js +41 -0
  171. package/lib/dependencies/AMDDefineDependency.js +29 -17
  172. package/lib/dependencies/AMDDefineDependencyParserPlugin.js +19 -6
  173. package/lib/dependencies/AMDPlugin.js +6 -3
  174. package/lib/dependencies/AMDRequireArrayDependency.js +13 -11
  175. package/lib/dependencies/AMDRequireContextDependency.js +10 -11
  176. package/lib/dependencies/AMDRequireDependenciesBlock.js +1 -0
  177. package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +28 -14
  178. package/lib/dependencies/AMDRequireDependency.js +28 -20
  179. package/lib/dependencies/AMDRequireItemDependency.js +1 -0
  180. package/lib/dependencies/AMDRuntimeModules.js +3 -0
  181. package/lib/dependencies/CachedConstDependency.js +9 -1
  182. package/lib/dependencies/CommonJsDependencyHelpers.js +104 -0
  183. package/lib/dependencies/CommonJsExportRequireDependency.js +157 -38
  184. package/lib/dependencies/CommonJsExportsDependency.js +74 -20
  185. package/lib/dependencies/CommonJsExportsParserPlugin.js +246 -15
  186. package/lib/dependencies/CommonJsFullRequireDependency.js +91 -27
  187. package/lib/dependencies/CommonJsImportsParserPlugin.js +189 -24
  188. package/lib/dependencies/CommonJsPlugin.js +7 -4
  189. package/lib/dependencies/CommonJsRequireContextDependency.js +13 -13
  190. package/lib/dependencies/CommonJsRequireDependency.js +135 -13
  191. package/lib/dependencies/CommonJsSelfReferenceDependency.js +60 -16
  192. package/lib/dependencies/ConstDependency.js +22 -12
  193. package/lib/dependencies/ContextDependency.js +13 -2
  194. package/lib/dependencies/ContextDependencyHelpers.js +7 -4
  195. package/lib/dependencies/ContextDependencyTemplateAsId.js +2 -1
  196. package/lib/dependencies/ContextDependencyTemplateAsRequireCall.js +1 -0
  197. package/lib/dependencies/ContextElementDependency.js +27 -14
  198. package/lib/dependencies/CreateRequireParserPlugin.js +14 -7
  199. package/lib/dependencies/CreateScriptUrlDependency.js +9 -7
  200. package/lib/dependencies/CriticalDependencyWarning.js +3 -1
  201. package/lib/dependencies/CssIcssExportDependency.js +681 -367
  202. package/lib/dependencies/CssIcssImportDependency.js +62 -17
  203. package/lib/dependencies/CssIcssSymbolDependency.js +36 -17
  204. package/lib/dependencies/CssImportDependency.js +37 -1
  205. package/lib/dependencies/CssUrlDependency.js +87 -14
  206. package/lib/dependencies/DelegatedSourceDependency.js +1 -0
  207. package/lib/dependencies/DllEntryDependency.js +12 -13
  208. package/lib/dependencies/DynamicExports.js +5 -0
  209. package/lib/dependencies/EntryDependency.js +1 -0
  210. package/lib/dependencies/ExportBindingInitFragment.js +164 -0
  211. package/lib/dependencies/ExportsInfoDependency.js +18 -12
  212. package/lib/dependencies/ExternalModuleDependency.js +13 -5
  213. package/lib/dependencies/ExternalModuleInitFragment.js +10 -5
  214. package/lib/dependencies/ExternalModuleInitFragmentDependency.js +19 -12
  215. package/lib/dependencies/HarmonyAcceptDependency.js +24 -26
  216. package/lib/dependencies/HarmonyAcceptImportDependency.js +2 -0
  217. package/lib/dependencies/HarmonyCompatibilityDependency.js +33 -14
  218. package/lib/dependencies/HarmonyDetectionParserPlugin.js +108 -30
  219. package/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js +34 -3
  220. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +70 -33
  221. package/lib/dependencies/HarmonyExportExpressionDependency.js +133 -43
  222. package/lib/dependencies/HarmonyExportHeaderDependency.js +11 -9
  223. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +268 -44
  224. package/lib/dependencies/HarmonyExportInitFragment.js +28 -20
  225. package/lib/dependencies/HarmonyExportSpecifierDependency.js +88 -18
  226. package/lib/dependencies/HarmonyExports.js +6 -1
  227. package/lib/dependencies/HarmonyImportDependency.js +60 -4
  228. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +139 -146
  229. package/lib/dependencies/HarmonyImportGuard.js +427 -0
  230. package/lib/dependencies/HarmonyImportSideEffectDependency.js +22 -1
  231. package/lib/dependencies/HarmonyImportSpecifierDependency.js +146 -24
  232. package/lib/{HarmonyLinkingError.js → dependencies/HarmonyLinkingError.js} +6 -3
  233. package/lib/dependencies/HarmonyModulesPlugin.js +11 -1
  234. package/lib/dependencies/HarmonyTopLevelThisParserPlugin.js +2 -1
  235. package/lib/dependencies/HtmlEntryDependency.js +1295 -0
  236. package/lib/dependencies/HtmlInlineHtmlDependency.js +107 -0
  237. package/lib/dependencies/HtmlInlineScriptDependency.js +126 -0
  238. package/lib/dependencies/HtmlInlineStyleDependency.js +152 -0
  239. package/lib/dependencies/HtmlSourceDependency.js +172 -0
  240. package/lib/dependencies/ImportContextDependency.js +9 -9
  241. package/lib/dependencies/ImportDependency.js +92 -3
  242. package/lib/dependencies/ImportEagerDependency.js +2 -0
  243. package/lib/dependencies/ImportMetaContextDependency.js +1 -0
  244. package/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +35 -19
  245. package/lib/dependencies/ImportMetaContextPlugin.js +40 -8
  246. package/lib/dependencies/ImportMetaGlobDependency.js +81 -0
  247. package/lib/dependencies/ImportMetaGlobDependencyParserPlugin.js +78 -0
  248. package/lib/dependencies/ImportMetaGlobHelpers.js +660 -0
  249. package/lib/dependencies/ImportMetaHotAcceptDependency.js +2 -0
  250. package/lib/dependencies/ImportMetaHotDeclineDependency.js +2 -0
  251. package/lib/dependencies/ImportMetaPlugin.js +595 -209
  252. package/lib/dependencies/ImportMetaResolveDependency.js +90 -0
  253. package/lib/dependencies/ImportParserPlugin.js +114 -27
  254. package/lib/dependencies/ImportPhase.js +17 -11
  255. package/lib/dependencies/ImportPlugin.js +2 -1
  256. package/lib/dependencies/ImportWeakDependency.js +3 -0
  257. package/lib/dependencies/JsonExportsDependency.js +15 -10
  258. package/lib/dependencies/LoaderDependency.js +2 -0
  259. package/lib/dependencies/LoaderImportDependency.js +3 -0
  260. package/lib/dependencies/LoaderPlugin.js +9 -2
  261. package/lib/dependencies/LocalModule.js +15 -12
  262. package/lib/dependencies/LocalModuleDependency.js +15 -13
  263. package/lib/dependencies/LocalModulesHelpers.js +3 -0
  264. package/lib/dependencies/ModuleDecoratorDependency.js +16 -10
  265. package/lib/dependencies/ModuleDependency.js +14 -0
  266. package/lib/dependencies/ModuleDependencyTemplateAsId.js +1 -0
  267. package/lib/dependencies/ModuleDependencyTemplateAsRequireId.js +1 -0
  268. package/lib/dependencies/ModuleHotAcceptDependency.js +2 -0
  269. package/lib/dependencies/ModuleHotDeclineDependency.js +2 -0
  270. package/lib/dependencies/ModuleInitFragmentDependency.js +19 -11
  271. package/lib/dependencies/NullDependency.js +2 -0
  272. package/lib/dependencies/PrefetchDependency.js +1 -0
  273. package/lib/dependencies/ProvidedDependency.js +36 -27
  274. package/lib/dependencies/PureExpressionDependency.js +14 -10
  275. package/lib/dependencies/RequireContextDependency.js +1 -0
  276. package/lib/dependencies/RequireContextDependencyParserPlugin.js +2 -1
  277. package/lib/dependencies/RequireContextPlugin.js +2 -1
  278. package/lib/dependencies/RequireEnsureDependenciesBlock.js +1 -0
  279. package/lib/dependencies/RequireEnsureDependenciesBlockParserPlugin.js +4 -6
  280. package/lib/dependencies/RequireEnsureDependency.js +16 -13
  281. package/lib/dependencies/RequireEnsureItemDependency.js +1 -0
  282. package/lib/dependencies/RequireEnsurePlugin.js +2 -1
  283. package/lib/dependencies/RequireHeaderDependency.js +7 -4
  284. package/lib/dependencies/RequireIncludeDependency.js +2 -0
  285. package/lib/dependencies/RequireIncludeDependencyParserPlugin.js +11 -12
  286. package/lib/dependencies/RequireIncludePlugin.js +2 -1
  287. package/lib/{RequireJsStuffPlugin.js → dependencies/RequireJsStuffPlugin.js} +9 -8
  288. package/lib/dependencies/RequireResolveContextDependency.js +10 -11
  289. package/lib/dependencies/RequireResolveDependency.js +2 -0
  290. package/lib/dependencies/RequireResolveHeaderDependency.js +8 -5
  291. package/lib/dependencies/RuntimeRequirementsDependency.js +11 -8
  292. package/lib/dependencies/StaticExportsDependency.js +13 -10
  293. package/lib/dependencies/SystemPlugin.js +9 -6
  294. package/lib/dependencies/SystemRuntimeModule.js +1 -0
  295. package/lib/dependencies/TopLevelAwaitDependency.js +93 -0
  296. package/lib/dependencies/URLContextDependency.js +8 -7
  297. package/lib/dependencies/URLDependency.js +157 -33
  298. package/lib/dependencies/URLPlugin.js +2 -0
  299. package/lib/dependencies/UnsupportedDependency.js +12 -13
  300. package/lib/dependencies/WebAssemblyExportImportedDependency.js +15 -16
  301. package/lib/dependencies/WebAssemblyImportDependency.js +19 -18
  302. package/lib/dependencies/WebpackIsIncludedDependency.js +3 -0
  303. package/lib/dependencies/{WorkerPlugin.js → WorkerAndWorkletPlugin.js} +364 -114
  304. package/lib/dependencies/WorkerDependency.js +111 -27
  305. package/lib/dependencies/WorkletDependency.js +278 -0
  306. package/lib/dependencies/getFunctionExpression.js +1 -0
  307. package/lib/dependencies/processExportInfo.js +14 -11
  308. package/lib/{DelegatedModule.js → dll/DelegatedModule.js} +71 -44
  309. package/lib/{DelegatedModuleFactoryPlugin.js → dll/DelegatedModuleFactoryPlugin.js} +27 -4
  310. package/lib/{DelegatedPlugin.js → dll/DelegatedPlugin.js} +5 -3
  311. package/lib/{DllEntryPlugin.js → dll/DllEntryPlugin.js} +9 -5
  312. package/lib/{DllModule.js → dll/DllModule.js} +36 -24
  313. package/lib/{DllModuleFactory.js → dll/DllModuleFactory.js} +5 -4
  314. package/lib/{DllPlugin.js → dll/DllPlugin.js} +24 -6
  315. package/lib/{DllReferencePlugin.js → dll/DllReferencePlugin.js} +26 -18
  316. package/lib/{LibManifestPlugin.js → dll/LibManifestPlugin.js} +14 -10
  317. package/lib/electron/ElectronTargetPlugin.js +24 -5
  318. package/lib/{AbstractMethodError.js → errors/AbstractMethodError.js} +10 -1
  319. package/lib/{AsyncDependencyToInitialChunkError.js → errors/AsyncDependencyToInitialChunkError.js} +8 -3
  320. package/lib/errors/BuildCycleError.js +1 -1
  321. package/lib/{ChunkRenderError.js → errors/ChunkRenderError.js} +1 -1
  322. package/lib/{CodeGenerationError.js → errors/CodeGenerationError.js} +1 -1
  323. package/lib/{CommentCompilationWarning.js → errors/CommentCompilationWarning.js} +9 -3
  324. package/lib/{ConcurrentCompilationError.js → errors/ConcurrentCompilationError.js} +5 -2
  325. package/lib/{EnvironmentNotSupportAsyncWarning.js → errors/EnvironmentNotSupportAsyncWarning.js} +5 -5
  326. package/lib/{HookWebpackError.js → errors/HookWebpackError.js} +20 -16
  327. package/lib/{IgnoreErrorModuleFactory.js → errors/IgnoreErrorModuleFactory.js} +7 -4
  328. package/lib/{InvalidDependenciesModuleWarning.js → errors/InvalidDependenciesModuleWarning.js} +6 -3
  329. package/lib/errors/JSONParseError.js +115 -0
  330. package/lib/errors/LoaderLoadingError.js +20 -0
  331. package/lib/{ModuleBuildError.js → errors/ModuleBuildError.js} +22 -16
  332. package/lib/{ModuleDependencyError.js → errors/ModuleDependencyError.js} +10 -3
  333. package/lib/{ModuleDependencyWarning.js → errors/ModuleDependencyWarning.js} +13 -5
  334. package/lib/{ModuleError.js → errors/ModuleError.js} +10 -14
  335. package/lib/{ModuleHashingError.js → errors/ModuleHashingError.js} +5 -1
  336. package/lib/errors/ModuleNotFoundError.js +94 -0
  337. package/lib/errors/ModuleParseError.js +141 -0
  338. package/lib/{ModuleRestoreError.js → errors/ModuleRestoreError.js} +4 -1
  339. package/lib/{ModuleStoreError.js → errors/ModuleStoreError.js} +5 -1
  340. package/lib/{ModuleWarning.js → errors/ModuleWarning.js} +13 -14
  341. package/lib/{NodeStuffInWebError.js → errors/NodeStuffInWebError.js} +7 -5
  342. package/lib/errors/NonErrorEmittedError.js +30 -0
  343. package/lib/{UnhandledSchemeError.js → errors/UnhandledSchemeError.js} +9 -2
  344. package/lib/{UnsupportedFeatureWarning.js → errors/UnsupportedFeatureWarning.js} +4 -3
  345. package/lib/errors/WebpackError.js +84 -0
  346. package/lib/esm/ExportWebpackRequireRuntimeModule.js +2 -0
  347. package/lib/esm/ModuleChunkFormatPlugin.js +52 -15
  348. package/lib/esm/ModuleChunkLoadingPlugin.js +11 -3
  349. package/lib/esm/ModuleChunkLoadingRuntimeModule.js +80 -52
  350. package/lib/hmr/HotModuleReplacement.runtime.js +175 -30
  351. package/lib/hmr/HotModuleReplacementRuntimeModule.js +8 -0
  352. package/lib/hmr/JavascriptHotModuleReplacement.runtime.js +173 -26
  353. package/lib/hmr/JavascriptHotModuleReplacementHelper.js +1 -0
  354. package/lib/hmr/LazyCompilationPlugin.js +154 -7
  355. package/lib/hmr/lazyCompilationBackend.js +53 -12
  356. package/lib/html/HtmlGenerator.js +1490 -0
  357. package/lib/html/HtmlModule.js +41 -0
  358. package/lib/html/HtmlModulesPlugin.js +1129 -0
  359. package/lib/html/HtmlParser.js +1970 -0
  360. package/lib/html/favicon.svg +1 -0
  361. package/lib/html/syntax.js +9106 -0
  362. package/lib/ids/ChunkModuleIdRangePlugin.js +3 -1
  363. package/lib/ids/DeterministicChunkIdsPlugin.js +3 -1
  364. package/lib/ids/DeterministicModuleIdsPlugin.js +3 -1
  365. package/lib/ids/HashedModuleIdsPlugin.js +2 -1
  366. package/lib/ids/IdHelpers.js +49 -12
  367. package/lib/ids/NamedChunkIdsPlugin.js +3 -1
  368. package/lib/ids/NamedModuleIdsPlugin.js +3 -1
  369. package/lib/ids/NaturalChunkIdsPlugin.js +1 -1
  370. package/lib/ids/NaturalModuleIdsPlugin.js +1 -1
  371. package/lib/ids/OccurrenceChunkIdsPlugin.js +2 -1
  372. package/lib/ids/OccurrenceModuleIdsPlugin.js +4 -1
  373. package/lib/ids/SyncModuleIdsPlugin.js +3 -1
  374. package/lib/index.js +66 -15
  375. package/lib/javascript/ArrayPushCallbackChunkFormatPlugin.js +5 -6
  376. package/lib/javascript/BasicEvaluatedExpression.js +370 -49
  377. package/lib/javascript/ChunkFormatHelpers.js +2 -1
  378. package/lib/javascript/ChunkHelpers.js +1 -0
  379. package/lib/javascript/CommonJsChunkFormatPlugin.js +1 -1
  380. package/lib/javascript/EnableChunkLoadingPlugin.js +6 -1
  381. package/lib/javascript/JavascriptGenerator.js +213 -51
  382. package/lib/javascript/JavascriptModule.js +54 -0
  383. package/lib/javascript/JavascriptModulesPlugin.js +628 -198
  384. package/lib/javascript/JavascriptParser.js +1334 -471
  385. package/lib/javascript/JavascriptParserHelpers.js +10 -7
  386. package/lib/javascript/StartupHelpers.js +5 -0
  387. package/lib/javascript/syntax.js +5037 -0
  388. package/lib/json/JsonData.js +5 -0
  389. package/lib/json/JsonGenerator.js +21 -0
  390. package/lib/json/JsonModule.js +40 -0
  391. package/lib/json/JsonModulesPlugin.js +8 -1
  392. package/lib/json/JsonParser.js +18 -27
  393. package/lib/library/AbstractLibraryPlugin.js +17 -2
  394. package/lib/library/AmdLibraryPlugin.js +8 -0
  395. package/lib/library/AssignLibraryPlugin.js +21 -3
  396. package/lib/library/EnableLibraryPlugin.js +8 -2
  397. package/lib/library/ExportPropertyLibraryPlugin.js +10 -0
  398. package/lib/{FalseIIFEUmdWarning.js → library/FalseIIFEUmdWarning.js} +2 -1
  399. package/lib/library/JsonpLibraryPlugin.js +8 -0
  400. package/lib/library/ModuleLibraryPlugin.js +154 -7
  401. package/lib/library/SystemLibraryPlugin.js +11 -3
  402. package/lib/library/UmdLibraryPlugin.js +16 -0
  403. package/lib/loaders/LoaderRunner.js +714 -0
  404. package/lib/loaders/loadLoader.js +100 -0
  405. package/lib/logging/Logger.js +17 -0
  406. package/lib/logging/createConsoleLogger.js +7 -0
  407. package/lib/logging/runtime.js +2 -0
  408. package/lib/logging/truncateArgs.js +2 -0
  409. package/lib/node/CommonJsChunkLoadingPlugin.js +5 -1
  410. package/lib/node/NodeEnvironmentPlugin.js +7 -3
  411. package/lib/node/NodeSourcePlugin.js +1 -1
  412. package/lib/node/NodeTargetPlugin.js +4 -63
  413. package/lib/node/NodeTemplatePlugin.js +3 -1
  414. package/lib/node/NodeWatchFileSystem.js +40 -22
  415. package/lib/node/ReadFileChunkLoadingRuntimeModule.js +13 -9
  416. package/lib/node/ReadFileCompileAsyncWasmPlugin.js +33 -3
  417. package/lib/node/ReadFileCompileWasmPlugin.js +28 -3
  418. package/lib/node/RequireChunkLoadingRuntimeModule.js +21 -16
  419. package/lib/node/nodeBuiltins.js +80 -0
  420. package/lib/node/nodeConsole.js +116 -64
  421. package/lib/optimize/AggressiveMergingPlugin.js +22 -16
  422. package/lib/optimize/AggressiveSplittingPlugin.js +6 -1
  423. package/lib/optimize/ConcatenatedModule.js +603 -130
  424. package/lib/optimize/ConstExportsPlugin.js +211 -0
  425. package/lib/optimize/EnsureChunkConditionsPlugin.js +2 -1
  426. package/lib/optimize/FlagIncludedChunksPlugin.js +15 -4
  427. package/lib/optimize/InlineExports.js +178 -0
  428. package/lib/optimize/InnerGraph.js +374 -262
  429. package/lib/optimize/InnerGraphPlugin.js +246 -117
  430. package/lib/optimize/LimitChunkCountPlugin.js +9 -0
  431. package/lib/optimize/MangleExportsPlugin.js +5 -1
  432. package/lib/optimize/MergeDuplicateChunksPlugin.js +2 -0
  433. package/lib/optimize/MinChunkSizePlugin.js +2 -1
  434. package/lib/optimize/MinMaxSizeWarning.js +5 -4
  435. package/lib/optimize/ModuleConcatenationPlugin.js +229 -55
  436. package/lib/optimize/RealContentHashPlugin.js +130 -52
  437. package/lib/optimize/RemoveEmptyChunksPlugin.js +2 -1
  438. package/lib/optimize/RemoveParentModulesPlugin.js +3 -1
  439. package/lib/optimize/RuntimeChunkPlugin.js +2 -1
  440. package/lib/optimize/SideEffectsFlagPlugin.js +381 -55
  441. package/lib/optimize/SplitChunksPlugin.js +200 -50
  442. package/lib/performance/AssetsOverSizeLimitWarning.js +3 -2
  443. package/lib/performance/EntrypointsOverSizeLimitWarning.js +3 -2
  444. package/lib/performance/NoAsyncChunksWarning.js +5 -3
  445. package/lib/performance/SizeLimitsPlugin.js +8 -2
  446. package/lib/prefetch/ChunkPrefetchFunctionRuntimeModule.js +1 -0
  447. package/lib/prefetch/ChunkPrefetchPreloadPlugin.js +21 -0
  448. package/lib/prefetch/ChunkPrefetchStartupRuntimeModule.js +1 -0
  449. package/lib/prefetch/ChunkPrefetchTriggerRuntimeModule.js +5 -1
  450. package/lib/prefetch/ChunkPreloadTriggerRuntimeModule.js +25 -13
  451. package/lib/prefetch/ResourceHintPlugin.js +687 -0
  452. package/lib/prefetch/ResourceHintRuntimeModule.js +149 -0
  453. package/lib/prefetch/StartupAssetHintRuntimeModule.js +210 -0
  454. package/lib/prefetch/parseResourceHintOptions.js +103 -0
  455. package/lib/rules/BasicEffectRulePlugin.js +3 -0
  456. package/lib/rules/BasicMatcherRulePlugin.js +3 -0
  457. package/lib/rules/ObjectMatcherRulePlugin.js +3 -0
  458. package/lib/rules/RuleSetCompiler.js +132 -0
  459. package/lib/rules/UseEffectRulePlugin.js +10 -3
  460. package/lib/runtime/AsyncModuleGeneratorRuntimeModule.js +56 -0
  461. package/lib/runtime/AsyncModuleRuntimeModule.js +52 -33
  462. package/lib/runtime/AutoPublicPathRuntimeModule.js +22 -9
  463. package/lib/runtime/BaseUriRuntimeModule.js +1 -0
  464. package/lib/runtime/ChunkNameRuntimeModule.js +1 -0
  465. package/lib/runtime/CommonJsWrapRuntimeModule.js +37 -0
  466. package/lib/runtime/CompatGetDefaultExportRuntimeModule.js +2 -1
  467. package/lib/runtime/CompatRuntimeModule.js +2 -0
  468. package/lib/runtime/CreateFakeNamespaceObjectRuntimeModule.js +7 -4
  469. package/lib/runtime/CreateScriptRuntimeModule.js +1 -0
  470. package/lib/runtime/CreateScriptUrlRuntimeModule.js +1 -0
  471. package/lib/runtime/DefinePropertyGettersRuntimeModule.js +34 -4
  472. package/lib/runtime/EnsureChunkRuntimeModule.js +1 -0
  473. package/lib/runtime/GetChunkFilenameRuntimeModule.js +88 -8
  474. package/lib/runtime/GetFullHashRuntimeModule.js +1 -0
  475. package/lib/runtime/GetMainFilenameRuntimeModule.js +1 -0
  476. package/lib/runtime/GetTrustedTypesPolicyRuntimeModule.js +2 -1
  477. package/lib/runtime/GetWorkletBootstrapRuntimeModule.js +59 -0
  478. package/lib/runtime/GlobalRuntimeModule.js +1 -0
  479. package/lib/runtime/HasOwnPropertyRuntimeModule.js +2 -1
  480. package/lib/runtime/HelperRuntimeModule.js +5 -0
  481. package/lib/runtime/LoadScriptRuntimeModule.js +28 -37
  482. package/lib/runtime/MakeDeferredNamespaceObjectRuntime.js +354 -37
  483. package/lib/runtime/MakeNamespaceObjectRuntimeModule.js +2 -1
  484. package/lib/runtime/NonceRuntimeModule.js +1 -0
  485. package/lib/runtime/OnChunksLoadedRuntimeModule.js +5 -4
  486. package/lib/runtime/PublicPathRuntimeModule.js +1 -0
  487. package/lib/runtime/RelativeUrlRuntimeModule.js +4 -2
  488. package/lib/runtime/RuntimeIdRuntimeModule.js +1 -0
  489. package/lib/runtime/SetAnonymousDefaultNameRuntimeModule.js +38 -0
  490. package/lib/runtime/StartupChunkDependenciesPlugin.js +13 -1
  491. package/lib/runtime/StartupChunkDependenciesRuntimeModule.js +2 -1
  492. package/lib/runtime/StartupEntrypointRuntimeModule.js +5 -3
  493. package/lib/runtime/SystemContextRuntimeModule.js +1 -0
  494. package/lib/runtime/ToBinaryRuntimeModule.js +1 -0
  495. package/lib/runtime/WorkerRuntimeModule.js +33 -0
  496. package/lib/schemes/DataUriPlugin.js +14 -2
  497. package/lib/schemes/FileUriPlugin.js +1 -1
  498. package/lib/schemes/HttpUriPlugin.js +86 -4
  499. package/lib/schemes/VirtualUrlPlugin.js +8 -4
  500. package/lib/serialization/AggregateErrorSerializer.js +9 -6
  501. package/lib/serialization/ArraySerializer.js +6 -8
  502. package/lib/serialization/BinaryMiddleware.js +427 -1006
  503. package/lib/serialization/DateObjectSerializer.js +4 -2
  504. package/lib/serialization/ErrorObjectSerializer.js +10 -6
  505. package/lib/serialization/FileMiddleware.js +345 -59
  506. package/lib/serialization/MapObjectSerializer.js +7 -9
  507. package/lib/serialization/NullPrototypeObjectSerializer.js +9 -9
  508. package/lib/serialization/ObjectMiddleware.js +73 -13
  509. package/lib/serialization/PlainObjectSerializer.js +16 -8
  510. package/lib/serialization/RegExpObjectSerializer.js +4 -2
  511. package/lib/serialization/Serializer.js +6 -0
  512. package/lib/serialization/SerializerMiddleware.js +14 -2
  513. package/lib/serialization/SetObjectSerializer.js +6 -8
  514. package/lib/serialization/SingleItemMiddleware.js +3 -0
  515. package/lib/sharing/ConsumeSharedFallbackDependency.js +1 -0
  516. package/lib/sharing/ConsumeSharedModule.js +21 -7
  517. package/lib/sharing/ConsumeSharedPlugin.js +11 -11
  518. package/lib/sharing/ConsumeSharedRuntimeModule.js +55 -45
  519. package/lib/sharing/ProvideForSharedDependency.js +1 -0
  520. package/lib/sharing/ProvideSharedDependency.js +34 -15
  521. package/lib/sharing/ProvideSharedModule.js +42 -12
  522. package/lib/sharing/ProvideSharedModuleFactory.js +1 -0
  523. package/lib/sharing/ProvideSharedPlugin.js +13 -8
  524. package/lib/sharing/SharePlugin.js +5 -1
  525. package/lib/sharing/ShareRuntimeModule.js +19 -16
  526. package/lib/sharing/resolveMatchedConfigs.js +6 -2
  527. package/lib/sharing/utils.js +9 -1
  528. package/lib/stats/DefaultStatsFactoryPlugin.js +289 -201
  529. package/lib/stats/DefaultStatsPresetPlugin.js +14 -2
  530. package/lib/stats/DefaultStatsPrinterPlugin.js +74 -170
  531. package/lib/stats/StatsFactory.js +70 -27
  532. package/lib/stats/StatsPrinter.js +16 -2
  533. package/lib/typescript/TypeScriptPlugin.js +210 -0
  534. package/lib/url/URLParserPlugin.js +111 -23
  535. package/lib/util/ArrayHelpers.js +1 -0
  536. package/lib/util/ArrayQueue.js +10 -5
  537. package/lib/util/AsyncQueue.js +154 -74
  538. package/lib/util/Hash.js +2 -2
  539. package/lib/util/IterableHelpers.js +3 -0
  540. package/lib/util/LazyBucketSortedSet.js +26 -0
  541. package/lib/util/LazySet.js +45 -4
  542. package/lib/util/LocConverter.js +64 -0
  543. package/lib/util/ParallelismFactorCalculator.js +1 -0
  544. package/lib/util/Queue.js +6 -3
  545. package/lib/util/Semaphore.js +15 -1
  546. package/lib/util/SetHelpers.js +4 -1
  547. package/lib/util/SortableSet.js +7 -1
  548. package/lib/util/SourceProcessor.js +119 -0
  549. package/lib/util/StackedCacheMap.js +20 -3
  550. package/lib/util/StackedMap.js +162 -43
  551. package/lib/util/StringXor.js +1 -1
  552. package/lib/util/TupleQueue.js +7 -3
  553. package/lib/util/TupleSet.js +14 -0
  554. package/lib/util/URLAbsoluteSpecifier.js +1 -0
  555. package/lib/util/WeakTupleMap.js +60 -1
  556. package/lib/util/binarySearchBounds.js +3 -2
  557. package/lib/util/chainedImports.js +3 -3
  558. package/lib/util/cleverMerge.js +19 -2
  559. package/lib/util/comparators.js +35 -4
  560. package/lib/util/compileBooleanMatcher.js +9 -0
  561. package/lib/util/concatenate.js +80 -24
  562. package/lib/util/conventions.js +46 -1
  563. package/lib/util/createHash.js +0 -1
  564. package/lib/util/createHooksRegistry.js +43 -0
  565. package/lib/util/createMappings.js +118 -0
  566. package/lib/util/dataURL.js +3 -2
  567. package/lib/util/deprecation.js +19 -0
  568. package/lib/util/deterministicGrouping.js +46 -17
  569. package/lib/util/extractSourceMap.js +2 -1
  570. package/lib/util/extractUrlAndGlobal.js +1 -0
  571. package/lib/util/findGraphRoots.js +7 -0
  572. package/lib/{formatLocation.js → util/formatLocation.js} +4 -2
  573. package/lib/{SizeFormatHelpers.js → util/formatSize.js} +10 -3
  574. package/lib/util/fs.js +87 -14
  575. package/lib/util/generateDebugId.js +1 -0
  576. package/lib/util/globUtils.js +710 -0
  577. package/lib/util/hash/BatchedHash.js +1 -0
  578. package/lib/util/hash/BulkUpdateHash.js +1 -0
  579. package/lib/util/hash/DebugHash.js +1 -0
  580. package/lib/util/hash/hash-digest.js +32 -15
  581. package/lib/util/hash/md4.js +1 -1
  582. package/lib/util/hash/wasm-hash.js +5 -0
  583. package/lib/util/hash/xxhash64.js +1 -1
  584. package/lib/util/identifier.js +118 -7
  585. package/lib/util/implicitTypeLoaderFallback.js +62 -0
  586. package/lib/util/internalSerializables.js +102 -60
  587. package/lib/util/magicComment.js +149 -7
  588. package/lib/util/makeSerializable.js +7 -0
  589. package/lib/util/memoize.js +2 -0
  590. package/lib/util/mimeTypes.js +176 -0
  591. package/lib/util/nonNumericOnlyHash.js +31 -1
  592. package/lib/util/numberHash.js +2 -2
  593. package/lib/util/parseJson.js +41 -0
  594. package/lib/util/processAsyncTree.js +8 -0
  595. package/lib/util/property.js +9 -2
  596. package/lib/util/publicPathPlaceholder.js +64 -0
  597. package/lib/util/registerExternalSerializer.js +24 -4
  598. package/lib/util/removeBOM.js +1 -0
  599. package/lib/util/runtime.js +32 -0
  600. package/lib/util/semver.js +15 -0
  601. package/lib/util/serialization.js +2 -0
  602. package/lib/util/smartGrouping.js +8 -0
  603. package/lib/util/source.js +23 -0
  604. package/lib/util/topologicalSort.js +69 -0
  605. package/lib/validateSchema.js +1 -0
  606. package/lib/wasm/EnableWasmLoadingPlugin.js +15 -1
  607. package/lib/wasm-async/AsyncWasmCompileRuntimeModule.js +25 -18
  608. package/lib/wasm-async/AsyncWasmLoadingRuntimeModule.js +34 -22
  609. package/lib/wasm-async/AsyncWasmModule.js +77 -0
  610. package/lib/wasm-async/AsyncWebAssemblyGenerator.js +6 -0
  611. package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +6 -1
  612. package/lib/wasm-async/AsyncWebAssemblyModulesPlugin.js +22 -126
  613. package/lib/wasm-async/AsyncWebAssemblyParser.js +3 -2
  614. package/lib/wasm-async/UniversalCompileAsyncWasmPlugin.js +13 -2
  615. package/lib/wasm-sync/SyncWasmModule.js +39 -0
  616. package/lib/wasm-sync/UnsupportedWebAssemblyFeatureError.js +7 -3
  617. package/lib/wasm-sync/WasmChunkLoadingRuntimeModule.js +86 -19
  618. package/lib/wasm-sync/WasmFinalizeExportsPlugin.js +4 -3
  619. package/lib/wasm-sync/WebAssemblyGenerator.js +27 -1
  620. package/lib/wasm-sync/WebAssemblyInInitialChunkError.js +9 -3
  621. package/lib/wasm-sync/WebAssemblyJavascriptGenerator.js +4 -0
  622. package/lib/wasm-sync/WebAssemblyModulesPlugin.js +20 -1
  623. package/lib/wasm-sync/WebAssemblyParser.js +9 -3
  624. package/lib/wasm-sync/WebAssemblyUtils.js +2 -0
  625. package/lib/web/FetchCompileAsyncWasmPlugin.js +29 -1
  626. package/lib/web/FetchCompileWasmPlugin.js +27 -1
  627. package/lib/web/JsonpChunkLoadingPlugin.js +11 -1
  628. package/lib/web/JsonpChunkLoadingRuntimeModule.js +58 -64
  629. package/lib/web/JsonpTemplatePlugin.js +2 -1
  630. package/lib/webpack.js +78 -1
  631. package/lib/webworker/ImportScriptsChunkLoadingPlugin.js +10 -1
  632. package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +15 -10
  633. package/lib/webworker/WebWorkerTemplatePlugin.js +1 -1
  634. package/module.d.ts +21 -0
  635. package/package.json +79 -77
  636. package/schemas/WebpackOptions.check.js +1 -1
  637. package/schemas/WebpackOptions.json +1173 -80
  638. package/schemas/plugins/{DllPlugin.check.d.ts → HtmlGeneratorOptions.check.d.ts} +1 -1
  639. package/schemas/plugins/HtmlGeneratorOptions.check.js +6 -0
  640. package/schemas/plugins/HtmlGeneratorOptions.json +3 -0
  641. package/schemas/plugins/{DllReferencePlugin.check.d.ts → HtmlParserOptions.check.d.ts} +1 -1
  642. package/schemas/plugins/HtmlParserOptions.check.js +6 -0
  643. package/schemas/plugins/HtmlParserOptions.json +3 -0
  644. package/schemas/plugins/ProgressPlugin.check.js +1 -1
  645. package/schemas/plugins/ProgressPlugin.json +38 -0
  646. package/schemas/plugins/container/ContainerReferencePlugin.check.js +1 -1
  647. package/schemas/plugins/container/ContainerReferencePlugin.json +2 -0
  648. package/schemas/plugins/container/ExternalsType.check.js +1 -1
  649. package/schemas/plugins/container/ModuleFederationPlugin.check.js +1 -1
  650. package/schemas/plugins/container/ModuleFederationPlugin.json +2 -0
  651. package/schemas/plugins/css/CssAutoOrModuleParserOptions.check.d.ts +7 -0
  652. package/schemas/plugins/css/CssAutoOrModuleParserOptions.check.js +6 -0
  653. package/schemas/plugins/css/CssAutoOrModuleParserOptions.json +3 -0
  654. package/schemas/plugins/css/CssModuleParserOptions.check.js +1 -1
  655. package/schemas/plugins/css/CssParserOptions.check.js +1 -1
  656. package/schemas/plugins/dll/DllPlugin.check.d.ts +7 -0
  657. package/schemas/plugins/dll/DllReferencePlugin.check.d.ts +7 -0
  658. package/types.d.ts +11053 -3019
  659. package/lib/CaseSensitiveModulesWarning.js +0 -72
  660. package/lib/GraphHelpers.js +0 -46
  661. package/lib/ModuleParseError.js +0 -125
  662. package/lib/NoModeWarning.js +0 -23
  663. package/lib/css/CssMergeStyleSheetsRuntimeModule.js +0 -56
  664. package/lib/css/walkCssTokens.js +0 -1843
  665. package/lib/util/AppendOnlyStackedSet.js +0 -78
  666. /package/schemas/plugins/{DllPlugin.check.js → dll/DllPlugin.check.js} +0 -0
  667. /package/schemas/plugins/{DllPlugin.json → dll/DllPlugin.json} +0 -0
  668. /package/schemas/plugins/{DllReferencePlugin.check.js → dll/DllReferencePlugin.check.js} +0 -0
  669. /package/schemas/plugins/{DllReferencePlugin.json → dll/DllReferencePlugin.json} +0 -0
@@ -0,0 +1,687 @@
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ */
4
+
5
+ "use strict";
6
+
7
+ const { RawSource } = require("webpack-sources");
8
+ const ModuleFilenameHelpers = require("../ModuleFilenameHelpers");
9
+ const RuntimeGlobals = require("../RuntimeGlobals");
10
+ const CssUrlDependency = require("../dependencies/CssUrlDependency");
11
+ const HtmlSourceDependency = require("../dependencies/HtmlSourceDependency");
12
+ const URLDependency = require("../dependencies/URLDependency");
13
+ const ResourceHintRuntimeModule = require("./ResourceHintRuntimeModule");
14
+ const StartupAssetHintRuntimeModule = require("./StartupAssetHintRuntimeModule");
15
+ const parseResourceHintOptions = require("./parseResourceHintOptions");
16
+
17
+ /** @typedef {import("../Compilation")} Compilation */
18
+ /** @typedef {import("../Compiler")} Compiler */
19
+ /** @typedef {import("../Module")} Module */
20
+ /** @typedef {import("../../declarations/WebpackOptions").ResourceHintsInitial} ResourceHintsConfig */
21
+ /** @typedef {import("../../declarations/WebpackOptions").ResourceHintsOptions} ResourceHintsOptions */
22
+ /** @typedef {import("../../declarations/WebpackOptions").UrlHintRule} UrlHintRule */
23
+ /** @typedef {import("../dependencies/HtmlEntryDependency").HtmlResourceHint} HtmlResourceHint */
24
+ /** @typedef {import("../dependencies/HtmlEntryDependency").HtmlResourceHintContext} HtmlResourceHintContext */
25
+ /** @typedef {import("./parseResourceHintOptions").ResourceHintOptions} ResourceHintOptions */
26
+
27
+ /**
28
+ * @typedef {object} ResolvedResourceHints
29
+ * @property {boolean=} prefetch project-wide default for `webpackPrefetch`
30
+ * @property {boolean=} preload project-wide default for `webpackPreload`
31
+ * @property {("low" | "high" | "auto" | false)=} fetchPriority project-wide default for `webpackFetchPriority`
32
+ * @property {string=} as project-wide default for `webpackAs`
33
+ * @property {string=} type project-wide default for `webpackType`
34
+ * @property {string=} media project-wide default for `webpackMedia`
35
+ */
36
+
37
+ /**
38
+ * A URL-referenced-asset dependency that can carry resource-hint state.
39
+ * `URLDependency`, `CssUrlDependency` and `HtmlSourceDependency` all share
40
+ * these field names — the `applyResourceHints` statics mutate them in place.
41
+ * @typedef {object} ResourceHintDep
42
+ * @property {true | undefined} prefetch
43
+ * @property {true | undefined} preload
44
+ * @property {("low" | "high" | "auto" | undefined)} fetchPriority
45
+ * @property {string | undefined} asAttribute
46
+ * @property {string | undefined} typeAttribute
47
+ * @property {string | undefined} mediaAttribute
48
+ */
49
+
50
+ /**
51
+ * A URL-referenced asset an HTML entry should emit as `<link>` in its `<head>`.
52
+ * @typedef {object} HtmlHintedAsset
53
+ * @property {Module} module the asset module (source-of-truth for the URL)
54
+ * @property {ResourceHintDep} dep the URL/CSS/HTML source dep carrying the hint flags
55
+ */
56
+
57
+ /**
58
+ * A resolved resource-hint descriptor emitted for an entrypoint — the shape
59
+ * consumed by `stats.entrypoints[name].resourceHints` and by SSR frameworks
60
+ * that render the initial HTML themselves.
61
+ * @typedef {object} EntrypointHint
62
+ * @property {"preload" | "prefetch" | "modulepreload" | "preconnect"} rel
63
+ * @property {string} href emitted URL (public path applied)
64
+ * @property {string=} as
65
+ * @property {string=} type
66
+ * @property {string=} media
67
+ * @property {("low" | "high" | "auto")=} fetchPriority
68
+ * @property {(boolean | "anonymous" | "use-credentials")=} crossorigin
69
+ * @property {string[]=} hostChunks names of the entrypoint chunks this hint originates from (Vite's `hostId`) — lets the callback rewrite per referencing chunk
70
+ */
71
+
72
+ /**
73
+ * Origin of `output.publicPath` when it's an absolute cross-origin URL, for
74
+ * `output.autoPreconnect`. `undefined` for relative / `"auto"` public paths.
75
+ * @param {Compilation} compilation compilation
76
+ * @returns {string | undefined} `scheme://host[:port]` or undefined
77
+ */
78
+ const getPublicPathOrigin = (compilation) => {
79
+ const publicPath = compilation.outputOptions.publicPath;
80
+ if (typeof publicPath !== "string") return undefined;
81
+ const match = /^(https?:)?\/\/[^/?#]+/i.exec(publicPath);
82
+ return match ? match[0] : undefined;
83
+ };
84
+
85
+ /**
86
+ * @typedef {object} CompilationResolver
87
+ * @property {ResourceHintsConfig | undefined} hints the effective `output.resourceHints` value (`undefined` when unset)
88
+ * @property {(entryName: string) => HtmlHintedAsset[]} getHtmlHinted URL asset descriptors reachable from an HTML entrypoint's initial chunks — `HtmlEntryDependency` template consumes this list to emit `<link>` tags into the extracted HTML `<head>`
89
+ * @property {(assetModule: Module) => boolean} isHtmlHinted true when `assetModule` appears in *any* HTML entry's hinted list — the JS chunk-startup runtime skips it so the DOM never carries two tags for one URL
90
+ * @property {(entryName: string) => EntrypointHint[]} getEntrypointHints resolved `<link>`-shaped descriptors for the given entry — auto initial-graph hints plus URL asset hints, then filtered / rewritten through the user function (when `output.resourceHints` is a function). Reads via `stats.entrypoints[name].resourceHints`.
91
+ */
92
+
93
+ const PLUGIN_NAME = "ResourceHintPlugin";
94
+
95
+ /** @type {WeakMap<Compilation, CompilationResolver>} */
96
+ const compilationResolvers = new WeakMap();
97
+
98
+ // Sane default-exclude for URL hint rules: manifests, PDFs, plain text are
99
+ // almost never wanted as `<link rel="preload/prefetch">` targets. Explicit
100
+ // magic comments still work — they route through `applyParsedHints`.
101
+ const DEFAULT_ASSETS_EXCLUDE_REGEXP = /\.(?:webmanifest|pdf|txt)(?:\?.*)?$/i;
102
+
103
+ // Initial-graph auto hints target JS chunk output files only.
104
+ const JS_CHUNK_FILE_REGEXP = /\.m?jsx?$/i;
105
+
106
+ /**
107
+ * True for the URL-referenced-asset deps that can carry resource-hint flags.
108
+ * Type guard so callers keep the narrowed union without re-listing the classes.
109
+ * @param {import("../Dependency")} dep dependency
110
+ * @returns {dep is URLDependency | CssUrlDependency | HtmlSourceDependency} whether it is a URL asset dep
111
+ */
112
+ const isUrlAssetDep = (dep) =>
113
+ dep instanceof URLDependency ||
114
+ dep instanceof CssUrlDependency ||
115
+ dep instanceof HtmlSourceDependency;
116
+
117
+ /**
118
+ * True for the CSS / HTML URL-asset deps whose templates don't run for JS
119
+ * output — the plugin lifts their hint flags into JS runtime requirements
120
+ * itself. JS `URLDependency` lifts its own, so it is excluded here.
121
+ * @param {import("../Dependency")} dep dependency
122
+ * @returns {dep is CssUrlDependency | HtmlSourceDependency} whether it is a CSS/HTML URL asset dep
123
+ */
124
+ const isCssOrHtmlUrlAssetDep = (dep) =>
125
+ dep instanceof CssUrlDependency || dep instanceof HtmlSourceDependency;
126
+
127
+ /**
128
+ * Entry chunk name for a hint's `hostChunks` (Vite's `hostId`); the id is a
129
+ * stable fallback for unnamed chunks.
130
+ * @param {import("../Chunk")} chunk chunk
131
+ * @returns {string} name or stringified id
132
+ */
133
+ const chunkHostName = (chunk) => chunk.name || String(chunk.id);
134
+
135
+ /**
136
+ * Walk `chunks` × their modules × dependencies and yield each distinct
137
+ * URL-asset module carrying a prefetch/preload flag, deduping targets within a
138
+ * single call. One shared walk for the entrypoint-hint and HTML-hint passes,
139
+ * which would otherwise each re-implement the same 4-level nesting.
140
+ * @param {Compilation} compilation compilation
141
+ * @param {Iterable<import("../Chunk")>} chunks chunks to scan
142
+ * @returns {IterableIterator<{ dep: URLDependency | CssUrlDependency | HtmlSourceDependency, target: Module, chunk: import("../Chunk") }>} hinted assets
143
+ */
144
+ function* iterateHintedUrlAssets(compilation, chunks) {
145
+ const { chunkGraph, moduleGraph } = compilation;
146
+ /** @type {WeakSet<Module>} */
147
+ const seen = new WeakSet();
148
+ for (const chunk of chunks) {
149
+ for (const module of chunkGraph.getChunkModulesIterable(chunk)) {
150
+ const deps = module.dependencies;
151
+ if (!deps) continue;
152
+ for (const dep of deps) {
153
+ if (!isUrlAssetDep(dep)) continue;
154
+ if (!dep.prefetch && !dep.preload) continue;
155
+ const target = moduleGraph.getModule(dep);
156
+ if (!target || seen.has(target)) continue;
157
+ seen.add(target);
158
+ yield { dep, target, chunk };
159
+ }
160
+ }
161
+ }
162
+ }
163
+
164
+ /**
165
+ * Merge the matching `UrlHintRule`s for a request. Rules match by
166
+ * `test`/`include`/`exclude` (omit all three → matches everything); later
167
+ * matches override earlier ones for defined fields.
168
+ * @param {UrlHintRule[] | undefined} rules parser-scoped `urlHints`
169
+ * @param {string} request request URL
170
+ * @returns {ResolvedResourceHints} merged defaults
171
+ */
172
+ const matchUrlHints = (rules, request) => {
173
+ if (!rules || rules.length === 0) return {};
174
+ if (DEFAULT_ASSETS_EXCLUDE_REGEXP.test(request)) return {};
175
+ /** @type {ResolvedResourceHints} */
176
+ const merged = {};
177
+ for (const rule of rules) {
178
+ if (
179
+ (rule.test !== undefined ||
180
+ rule.include !== undefined ||
181
+ rule.exclude !== undefined) &&
182
+ !ModuleFilenameHelpers.matchObject(
183
+ /** @type {EXPECTED_ANY} */ ({
184
+ test: rule.test,
185
+ include: rule.include,
186
+ exclude: rule.exclude
187
+ }),
188
+ request
189
+ )
190
+ ) {
191
+ continue;
192
+ }
193
+ if (rule.prefetch !== undefined) merged.prefetch = rule.prefetch;
194
+ if (rule.preload !== undefined) merged.preload = rule.preload;
195
+ if (rule.fetchPriority !== undefined) {
196
+ merged.fetchPriority = rule.fetchPriority;
197
+ }
198
+ if (rule.as !== undefined) merged.as = rule.as;
199
+ if (rule.type !== undefined) merged.type = rule.type;
200
+ if (rule.media !== undefined) merged.media = rule.media;
201
+ }
202
+ return merged;
203
+ };
204
+
205
+ /**
206
+ * When `output.resourceHints` is a function, invoke it and return its
207
+ * descriptors; otherwise pass through. Callback signature — see `ResourceHints`
208
+ * schema entry.
209
+ * @param {import("../Entrypoint")} entrypoint entrypoint
210
+ * @param {ResourceHintsConfig | undefined} hints top-level config
211
+ * @param {string} entryName entry name
212
+ * @param {"html" | "js"} hostType page type
213
+ * @param {Compilation} compilation compilation
214
+ * @param {EntrypointHint[]} defaultHints computed default descriptors
215
+ * @returns {EntrypointHint[]} descriptors after the user hook (or the defaults untouched)
216
+ */
217
+ const applyUserHook = (
218
+ entrypoint,
219
+ hints,
220
+ entryName,
221
+ hostType,
222
+ compilation,
223
+ defaultHints
224
+ ) => {
225
+ if (typeof hints !== "function") return defaultHints;
226
+ const out = hints({
227
+ entryName,
228
+ entrypoint,
229
+ hostType,
230
+ compilation,
231
+ // `collectEntrypointHints` always sets `hostChunks`; the callback type
232
+ // declares it required, so assert it here.
233
+ defaultHints:
234
+ /** @type {(HtmlResourceHint & { hostChunks: string[] })[]} */ (
235
+ defaultHints
236
+ )
237
+ });
238
+ return Array.isArray(out) ? /** @type {EntrypointHint[]} */ (out) : [];
239
+ };
240
+
241
+ /**
242
+ * Collect the resolved `<link>`-shaped hint descriptors for an entrypoint —
243
+ * combining the auto initial-graph hints (from a truthy `output.resourceHints`)
244
+ * with the URL-referenced-asset hints (fonts, images, workers) carried on
245
+ * `URLDependency` / `CssUrlDependency` / `HtmlSourceDependency`. Backs
246
+ * `stats.entrypoints[name].resourceHints`. Works for any entrypoint, HTML or
247
+ * JS-only — SSR frameworks read this to inject `<link>` server-side without a
248
+ * separate manifest.
249
+ * @param {import("../Compilation")} compilation compilation
250
+ * @param {string} entryName entrypoint name
251
+ * @param {ResourceHintsConfig | undefined} hints `output.resourceHints`
252
+ * @returns {EntrypointHint[]} descriptors
253
+ */
254
+ const collectEntrypointHints = (compilation, entryName, hints) => {
255
+ // `"none"` is a hard off switch — no hints anywhere (stats / manifest / DOM).
256
+ if (hints === "none") return [];
257
+ const entrypoint = compilation.entrypoints.get(entryName);
258
+ if (!entrypoint) return [];
259
+ /** @type {EntrypointHint[]} */
260
+ const out = [];
261
+ /** @type {Set<string>} */
262
+ const seenKeys = new Set();
263
+ const push = (/** @type {EntrypointHint} */ h) => {
264
+ const key = `${h.rel}\0${h.href}`;
265
+ if (seenKeys.has(key)) return;
266
+ seenKeys.add(key);
267
+ out.push(h);
268
+ };
269
+ const publicPath =
270
+ typeof compilation.outputOptions.publicPath === "string" &&
271
+ compilation.outputOptions.publicPath !== "auto"
272
+ ? compilation.outputOptions.publicPath
273
+ : "";
274
+ // `resourceHints.preconnect`: warm the connection to a cross-origin
275
+ // publicPath (the origin every chunk / asset is fetched from), emitted first.
276
+ const outputResourceHints = compilation.outputOptions.resourceHints;
277
+ if (outputResourceHints && outputResourceHints.preconnect) {
278
+ const origin = getPublicPathOrigin(compilation);
279
+ if (origin) {
280
+ /** @type {EntrypointHint} */
281
+ const h = { rel: "preconnect", href: origin };
282
+ const crossOrigin = compilation.outputOptions.crossOriginLoading;
283
+ if (crossOrigin) h.crossorigin = crossOrigin;
284
+ push(h);
285
+ }
286
+ }
287
+ // Auto initial-graph hints — computed for `true`, `"prefetch"`, or a
288
+ // function (the callback receives them as `defaultHints`). The array
289
+ // form supplies its own list; nothing is auto-emitted here for it.
290
+ if (hints === true || hints === "prefetch" || typeof hints === "function") {
291
+ const isModuleOutput = compilation.outputOptions.module === true;
292
+ const prefetch = hints === "prefetch";
293
+ const rel = prefetch
294
+ ? "prefetch"
295
+ : isModuleOutput
296
+ ? "modulepreload"
297
+ : "preload";
298
+ const entryChunk = entrypoint.getEntrypointChunk();
299
+ for (const chunk of entrypoint.chunks) {
300
+ if (chunk === entryChunk) continue;
301
+ for (const file of chunk.files) {
302
+ if (!JS_CHUNK_FILE_REGEXP.test(file)) continue;
303
+ const href = publicPath + file;
304
+ /** @type {EntrypointHint} */
305
+ const h = {
306
+ rel,
307
+ href,
308
+ hostChunks: [chunkHostName(chunk)]
309
+ };
310
+ if (rel === "preload") h.as = "script";
311
+ push(h);
312
+ }
313
+ }
314
+ }
315
+ // URL-referenced deps carrying prefetch/preload. Walk entrypoint's initial
316
+ // chunks × modules × deps (async chunks are handled by the on-demand
317
+ // runtime via `dynamicImportPrefetch/Preload` parser options).
318
+ /** @type {Set<import("../Chunk")>} */
319
+ const chunkSet = new Set(entrypoint.chunks);
320
+ const runtimeChunk = entrypoint.getRuntimeChunk();
321
+ if (runtimeChunk) chunkSet.add(runtimeChunk);
322
+ for (const { dep, target, chunk } of iterateHintedUrlAssets(
323
+ compilation,
324
+ chunkSet
325
+ )) {
326
+ const buildInfo =
327
+ /** @type {{ filename?: string }} */
328
+ (target.buildInfo);
329
+ if (!buildInfo || !buildInfo.filename) continue;
330
+ const asAttribute =
331
+ dep.asAttribute ||
332
+ ResourceHintRuntimeModule.guessAsAttribute(dep.request);
333
+ /** @type {EntrypointHint} */
334
+ const h = {
335
+ rel: dep.preload ? "preload" : "prefetch",
336
+ href: publicPath + buildInfo.filename,
337
+ hostChunks: [chunkHostName(chunk)]
338
+ };
339
+ if (asAttribute) h.as = asAttribute;
340
+ if (dep.typeAttribute) h.type = dep.typeAttribute;
341
+ if (dep.mediaAttribute) h.media = dep.mediaAttribute;
342
+ if (dep.fetchPriority) h.fetchPriority = dep.fetchPriority;
343
+ push(h);
344
+ }
345
+ // `hostType`: `"html"` iff the entrypoint has an extracted HTML page
346
+ // (any `HtmlEntryDependency` with elementKind `script`/`script-module` on
347
+ // any HTML module points at this entryName); SSR frameworks reading
348
+ // `stats.entrypoints[name].resourceHints` see `"js"`.
349
+ let hostType = /** @type {"html" | "js"} */ ("js");
350
+
351
+ const HtmlEntryDependency = require("../dependencies/HtmlEntryDependency");
352
+
353
+ outer: for (const module of compilation.modules) {
354
+ if (!module.getSourceTypes || !module.getSourceTypes().has("html")) {
355
+ continue;
356
+ }
357
+ const presDeps = module.presentationalDependencies;
358
+ if (!presDeps) continue;
359
+ for (const dep of presDeps) {
360
+ if (
361
+ dep instanceof HtmlEntryDependency &&
362
+ dep.entryName === entryName &&
363
+ (dep.elementKind === "script" || dep.elementKind === "script-module")
364
+ ) {
365
+ hostType = "html";
366
+ break outer;
367
+ }
368
+ }
369
+ }
370
+ return applyUserHook(
371
+ entrypoint,
372
+ hints,
373
+ entryName,
374
+ hostType,
375
+ compilation,
376
+ out
377
+ );
378
+ };
379
+
380
+ /**
381
+ * Adds runtime support for `__webpack_require__.PA` / `__webpack_require__.LA`,
382
+ * the helpers that inject `<link rel="prefetch">` / `<link rel="preload">`
383
+ * tags for asset modules referenced via `new URL(..., import.meta.url)`, CSS
384
+ * `url(...)`, and HTML `<img src>` / `<link href>`. Also stores the top-level
385
+ * `output.resourceHints` value so `HtmlEntryDependency` can emit its `<link>`
386
+ * tags into the extracted HTML `<head>`.
387
+ */
388
+ class ResourceHintPlugin {
389
+ /**
390
+ * @param {ResourceHintsOptions=} options normalized `output.resourceHints`
391
+ */
392
+ constructor(options) {
393
+ /** @type {ResourceHintsConfig | undefined} */
394
+ this._hints = options ? options.initial : undefined;
395
+ }
396
+
397
+ /**
398
+ * Returns the per-compilation resolver. `.hints` is the effective
399
+ * `output.resourceHints` value (used by `HtmlEntryDependency` template);
400
+ * `.isHtmlHinted(assetModule)` skips the JS chunk-startup `<link>` when the
401
+ * HTML `<head>` already emits it.
402
+ * @param {Compilation} compilation compilation
403
+ * @returns {CompilationResolver} resolver
404
+ */
405
+ static getCompilationResolver(compilation) {
406
+ const entry = compilationResolvers.get(compilation);
407
+ if (entry) return entry;
408
+ return {
409
+ hints: undefined,
410
+ getHtmlHinted: () => [],
411
+ isHtmlHinted: () => false,
412
+ getEntrypointHints: () => []
413
+ };
414
+ }
415
+
416
+ /**
417
+ * Match `parser.<type>.urlHints` rules for a request. Returns the merged
418
+ * defaults; the caller passes them to `applyDefaults`. Exposed so parsers
419
+ * that own their own URL dep creation (JS/CSS/HTML) can share one matcher.
420
+ * @param {UrlHintRule[] | undefined} rules rules array
421
+ * @param {string} request request URL
422
+ * @returns {ResolvedResourceHints} matched defaults
423
+ */
424
+ static matchUrlHints(rules, request) {
425
+ return matchUrlHints(rules, request);
426
+ }
427
+
428
+ /**
429
+ * Apply `urlHints` rule defaults to a URL asset dep. Used from URL-emitting
430
+ * parsers when they have per-request defaults but no comment options to
431
+ * parse (e.g. the HTML parser's `pendingHints` flow, where the comment was
432
+ * parsed earlier).
433
+ * @param {ResourceHintDep} dep dep to mutate
434
+ * @param {ResolvedResourceHints} defaults `matchUrlHints(rules, request)` result
435
+ * @returns {void}
436
+ */
437
+ static applyDefaults(dep, defaults) {
438
+ if (defaults.prefetch) dep.prefetch = true;
439
+ if (defaults.preload) dep.preload = true;
440
+ if (defaults.fetchPriority) dep.fetchPriority = defaults.fetchPriority;
441
+ if (defaults.as !== undefined) dep.asAttribute = defaults.as;
442
+ if (defaults.type !== undefined) dep.typeAttribute = defaults.type;
443
+ if (defaults.media !== undefined) dep.mediaAttribute = defaults.media;
444
+ }
445
+
446
+ /**
447
+ * Apply already-parsed `webpackPrefetch` / `webpackPreload` /
448
+ * `webpackFetchPriority` / `webpackAs` / `webpackType` / `webpackMedia`
449
+ * overrides to a URL asset dep. Each field wins over the project-wide
450
+ * default only when it's explicitly set on the magic comment.
451
+ * @param {ResourceHintDep} dep dep to mutate
452
+ * @param {ResourceHintOptions} hints parsed hint options
453
+ * @returns {void}
454
+ */
455
+ static applyParsedHints(dep, hints) {
456
+ if (hints.prefetch !== undefined) dep.prefetch = hints.prefetch;
457
+ if (hints.preload !== undefined) dep.preload = hints.preload;
458
+ if (hints.fetchPriority !== undefined) {
459
+ dep.fetchPriority = hints.fetchPriority;
460
+ }
461
+ if (hints.as !== undefined) dep.asAttribute = hints.as;
462
+ if (hints.type !== undefined) dep.typeAttribute = hints.type;
463
+ if (hints.media !== undefined) dep.mediaAttribute = hints.media;
464
+ }
465
+
466
+ /**
467
+ * Match `urlHints` for `request` + apply per-URL magic-comment overrides
468
+ * to a URL asset dep. Called from every URL-emitting parser (JS
469
+ * `new URL(...)`, CSS `url(...)`, HTML `<img src>` / `<link href>`,
470
+ * `new Worker(new URL(...))`) so all sources share the same precedence:
471
+ * rule defaults first, magic comments win.
472
+ * @param {ResourceHintDep} dep dep to mutate in place
473
+ * @param {UrlHintRule[] | undefined} rules parser-scoped `urlHints` rules
474
+ * @param {string} request the asset request (for rule matching)
475
+ * @param {Record<string, EXPECTED_ANY> | null | undefined} commentOptions parsed magic-comment options (`null` / `undefined` skips override phase)
476
+ * @param {import("../NormalModule")} module module for emitting warnings on invalid comments
477
+ * @param {import("../Dependency").DependencyLocation} loc loc for warnings
478
+ * @returns {void}
479
+ */
480
+ static applyResourceHints(dep, rules, request, commentOptions, module, loc) {
481
+ ResourceHintPlugin.applyDefaults(dep, matchUrlHints(rules, request));
482
+ if (!commentOptions) return;
483
+ ResourceHintPlugin.applyParsedHints(
484
+ dep,
485
+ parseResourceHintOptions(commentOptions, module, loc)
486
+ );
487
+ }
488
+
489
+ /**
490
+ * @param {Compiler} compiler the compiler
491
+ * @returns {void}
492
+ */
493
+ apply(compiler) {
494
+ const hints = this._hints;
495
+
496
+ const HtmlEntryDependency = require("../dependencies/HtmlEntryDependency");
497
+
498
+ compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
499
+ // Lazy-computed on first access — walk every HTML module's
500
+ // `HtmlEntryDependency`s, resolve each named entry, and collect
501
+ // every URL asset dep target reachable from its ordered chunks.
502
+ // One pass produces two views: a per-entry list (consumed by
503
+ // `HtmlEntryDependency.Template` to emit `<link>` tags) and a
504
+ // global `WeakSet` (consumed by `StartupAssetHintRuntimeModule`
505
+ // to skip the JS runtime `<link>` for the same asset).
506
+ // Deterministic (module graph is fixed by seal time) and cheap
507
+ // (only fires when a JS chunk actually produces hints).
508
+ /** @type {Map<string, HtmlHintedAsset[]> | undefined} */
509
+ let perEntry;
510
+ /** @type {WeakSet<Module> | undefined} */
511
+ let anyHtmlHinted;
512
+ const build = () => {
513
+ perEntry = new Map();
514
+ anyHtmlHinted = new WeakSet();
515
+ for (const module of compilation.modules) {
516
+ if (!module.getSourceTypes || !module.getSourceTypes().has("html")) {
517
+ continue;
518
+ }
519
+ // HtmlEntry deps live on `presentationalDependencies` (added
520
+ // by the HTML parser via `addPresentationalDependency`) —
521
+ // they don't affect module resolution, only rendering.
522
+ const presDeps = module.presentationalDependencies;
523
+ if (!presDeps) continue;
524
+ for (const dep of presDeps) {
525
+ if (
526
+ !(dep instanceof HtmlEntryDependency) ||
527
+ (dep.elementKind !== "script" &&
528
+ dep.elementKind !== "script-module")
529
+ ) {
530
+ continue;
531
+ }
532
+ const entrypoint = compilation.entrypoints.get(dep.entryName);
533
+ if (!entrypoint) continue;
534
+ // Mirror `getEntrypointChunksInLoadOrder`: entry chunk +
535
+ // runtime chunk + all initial siblings (`entrypoint.chunks`
536
+ // includes splitChunks output). Async `import()` chunks
537
+ // stay on the on-demand runtime and are NOT HTML-hinted.
538
+ /** @type {Set<import("../Chunk")>} */
539
+ const chunks = new Set(entrypoint.chunks);
540
+ const entry = entrypoint.getEntrypointChunk();
541
+ if (entry) chunks.add(entry);
542
+ const runtimeChunk = entrypoint.getRuntimeChunk();
543
+ if (runtimeChunk) chunks.add(runtimeChunk);
544
+ /** @type {HtmlHintedAsset[]} */
545
+ const hinted = [];
546
+ for (const { dep: assetDep, target } of iterateHintedUrlAssets(
547
+ compilation,
548
+ chunks
549
+ )) {
550
+ anyHtmlHinted.add(target);
551
+ hinted.push({ module: target, dep: assetDep });
552
+ }
553
+ // Multiple HtmlEntryDeps may share an entryName (e.g. a
554
+ // `<script>` and a `<link rel="modulepreload">` for the
555
+ // same chunk). Merge lists so no asset is duplicated.
556
+ const existing = perEntry.get(dep.entryName);
557
+ if (existing) {
558
+ for (const h of hinted) existing.push(h);
559
+ } else {
560
+ perEntry.set(dep.entryName, hinted);
561
+ }
562
+ }
563
+ }
564
+ };
565
+ compilationResolvers.set(compilation, {
566
+ hints,
567
+ getHtmlHinted: (entryName) => {
568
+ if (hints === "none") return [];
569
+ if (!perEntry) build();
570
+ return (
571
+ /** @type {Map<string, HtmlHintedAsset[]>} */ (perEntry).get(
572
+ entryName
573
+ ) || []
574
+ );
575
+ },
576
+ isHtmlHinted: (assetModule) => {
577
+ if (hints === "none") return false;
578
+ if (!anyHtmlHinted) build();
579
+ return /** @type {WeakSet<Module>} */ (anyHtmlHinted).has(
580
+ assetModule
581
+ );
582
+ },
583
+ getEntrypointHints: (entryName) =>
584
+ collectEntrypointHints(compilation, entryName, hints)
585
+ });
586
+ // CSS `url(...)` and HTML `<img src>` / `<link href>` deps store
587
+ // hint flags but their dep templates don't run for the JS
588
+ // output, so we need to lift those flags into the JS chunk's
589
+ // runtime requirements ourselves. Iterate only css/html modules
590
+ // (pure-JS chunks skip the walk entirely).
591
+ compilation.hooks.additionalChunkRuntimeRequirements.tap(
592
+ PLUGIN_NAME,
593
+ (chunk, set, { chunkGraph }) => {
594
+ if (hints === "none") return;
595
+ let hasPrefetch = false;
596
+ let hasPreload = false;
597
+ for (const module of chunkGraph.getChunkModulesIterable(chunk)) {
598
+ const deps =
599
+ /** @type {{ dependencies?: import("../Dependency")[] }} */
600
+ (module).dependencies;
601
+ if (!deps) continue;
602
+ for (const dep of deps) {
603
+ if (!isCssOrHtmlUrlAssetDep(dep)) continue;
604
+ if (dep.prefetch) hasPrefetch = true;
605
+ if (dep.preload) hasPreload = true;
606
+ if (hasPrefetch && hasPreload) break;
607
+ }
608
+ if (hasPrefetch && hasPreload) break;
609
+ }
610
+ // Attach the startup-hint runtime module to *this* chunk
611
+ // (covering async chunks too) — `runtimeRequirementInTree`
612
+ // alone would only attach it to the runtime chunk, so a
613
+ // hinted `new URL(...)` in an async chunk would never
614
+ // fire its `<link>` injection. We add it whenever this
615
+ // chunk already pulls in `startupAssetHints` (via JS
616
+ // `URLDependency` template) or whenever CSS / HTML lifts
617
+ // it in here.
618
+ if (
619
+ hasPrefetch ||
620
+ hasPreload ||
621
+ set.has(RuntimeGlobals.startupAssetHints)
622
+ ) {
623
+ set.add(RuntimeGlobals.startupAssetHints);
624
+ // CSS-only asset modules aren't in JS `__webpack_modules__`,
625
+ // so the startup-hint runtime falls back to
626
+ // `__webpack_require__.p + "filename"`. Make sure
627
+ // `publicPath` is in this chunk's runtime.
628
+ set.add(RuntimeGlobals.publicPath);
629
+ if (hasPrefetch) set.add(RuntimeGlobals.prefetchAsset);
630
+ if (hasPreload) set.add(RuntimeGlobals.preloadAsset);
631
+ compilation.addRuntimeModule(
632
+ chunk,
633
+ new StartupAssetHintRuntimeModule()
634
+ );
635
+ }
636
+ }
637
+ );
638
+ for (const [rel, runtimeGlobal] of /** @type {const} */ ([
639
+ ["prefetch", RuntimeGlobals.prefetchAsset],
640
+ ["preload", RuntimeGlobals.preloadAsset]
641
+ ])) {
642
+ compilation.hooks.runtimeRequirementInTree
643
+ .for(runtimeGlobal)
644
+ .tap(PLUGIN_NAME, (chunk) => {
645
+ compilation.addRuntimeModule(
646
+ chunk,
647
+ new ResourceHintRuntimeModule(rel)
648
+ );
649
+ });
650
+ }
651
+
652
+ // SSR manifest: serialize each entrypoint's resolved hints to a JSON
653
+ // asset (the same descriptors as `stats.entrypoints[].resourceHints`)
654
+ // so an SSR server can inject the `<link>` tags itself. `"none"`
655
+ // yields empty lists (via `collectEntrypointHints`).
656
+ const rhOptions = compilation.outputOptions.resourceHints;
657
+ const manifestPath = rhOptions && rhOptions.manifest;
658
+ if (manifestPath) {
659
+ const Compilation = require("../Compilation");
660
+
661
+ compilation.hooks.processAssets.tap(
662
+ {
663
+ name: PLUGIN_NAME,
664
+ stage: Compilation.PROCESS_ASSETS_STAGE_REPORT
665
+ },
666
+ () => {
667
+ /** @type {Record<string, EntrypointHint[]>} */
668
+ const manifest = {};
669
+ for (const name of compilation.entrypoints.keys()) {
670
+ manifest[name] = collectEntrypointHints(compilation, name, hints);
671
+ }
672
+ const source = new RawSource(
673
+ `${JSON.stringify(manifest, null, 2)}\n`
674
+ );
675
+ if (compilation.getAsset(manifestPath)) {
676
+ compilation.updateAsset(manifestPath, source);
677
+ } else {
678
+ compilation.emitAsset(manifestPath, source);
679
+ }
680
+ }
681
+ );
682
+ }
683
+ });
684
+ }
685
+ }
686
+
687
+ module.exports = ResourceHintPlugin;