webpack 5.107.1 → 5.108.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 (360) hide show
  1. package/hot/dev-server.js +2 -0
  2. package/lib/APIPlugin.js +8 -4
  3. package/lib/CacheFacade.js +7 -0
  4. package/lib/Chunk.js +4 -0
  5. package/lib/ChunkGraph.js +30 -9
  6. package/lib/CircularModulesPlugin.js +190 -0
  7. package/lib/CleanPlugin.js +2 -1
  8. package/lib/Compilation.js +396 -65
  9. package/lib/Compiler.js +52 -14
  10. package/lib/ConcatenationScope.js +10 -3
  11. package/lib/ContextExclusionPlugin.js +1 -0
  12. package/lib/ContextModule.js +92 -47
  13. package/lib/ContextModuleFactory.js +45 -38
  14. package/lib/ContextReplacementPlugin.js +4 -0
  15. package/lib/DefinePlugin.js +89 -15
  16. package/lib/Dependency.js +111 -7
  17. package/lib/EntryOptionPlugin.js +39 -2
  18. package/lib/EntryPlugin.js +2 -0
  19. package/lib/ExportsInfo.js +314 -73
  20. package/lib/ExternalModule.js +82 -34
  21. package/lib/ExternalModuleFactoryPlugin.js +1 -0
  22. package/lib/ExternalsPlugin.js +1 -0
  23. package/lib/FileSystemInfo.js +173 -23
  24. package/lib/FlagAllModulesAsUsedPlugin.js +1 -5
  25. package/lib/FlagDependencyExportsPlugin.js +23 -0
  26. package/lib/FlagDependencyUsagePlugin.js +87 -5
  27. package/lib/FlagEntryExportAsUsedPlugin.js +2 -0
  28. package/lib/Generator.js +8 -0
  29. package/lib/HotModuleReplacementPlugin.js +65 -28
  30. package/lib/IgnorePlugin.js +1 -0
  31. package/lib/InitFragment.js +5 -0
  32. package/lib/JavascriptMetaInfoPlugin.js +7 -5
  33. package/lib/LazyBarrel.js +361 -0
  34. package/lib/LoaderTargetPlugin.js +1 -0
  35. package/lib/Module.js +21 -42
  36. package/lib/ModuleGraph.js +3 -2
  37. package/lib/ModuleProfile.js +27 -1
  38. package/lib/ModuleTemplate.js +2 -0
  39. package/lib/MultiCompiler.js +16 -0
  40. package/lib/MultiStats.js +1 -0
  41. package/lib/MultiWatching.js +2 -0
  42. package/lib/NodeStuffPlugin.js +22 -17
  43. package/lib/NormalModule.js +557 -77
  44. package/lib/NormalModuleReplacementPlugin.js +2 -0
  45. package/lib/PrefetchPlugin.js +2 -0
  46. package/lib/ProgressPlugin.js +9 -1
  47. package/lib/ProvidePlugin.js +1 -0
  48. package/lib/RawModule.js +20 -16
  49. package/lib/RecordIdsPlugin.js +1 -0
  50. package/lib/RuntimeGlobals.js +5 -0
  51. package/lib/RuntimePlugin.js +58 -26
  52. package/lib/RuntimeTemplate.js +278 -21
  53. package/lib/SelfModuleFactory.js +1 -0
  54. package/lib/SourceMapDevToolPlugin.js +105 -30
  55. package/lib/Stats.js +1 -0
  56. package/lib/Template.js +8 -2
  57. package/lib/TemplatedPathPlugin.js +473 -131
  58. package/lib/WarnCaseSensitiveModulesPlugin.js +1 -0
  59. package/lib/WarnDeprecatedOptionPlugin.js +4 -0
  60. package/lib/WarnNoModeSetPlugin.js +1 -0
  61. package/lib/WatchIgnorePlugin.js +1 -0
  62. package/lib/Watching.js +9 -0
  63. package/lib/WebpackOptionsApply.js +50 -5
  64. package/lib/asset/AssetBytesGenerator.js +11 -3
  65. package/lib/asset/AssetGenerator.js +47 -22
  66. package/lib/asset/AssetModule.js +47 -0
  67. package/lib/asset/AssetModulesPlugin.js +14 -14
  68. package/lib/asset/AssetParser.js +2 -2
  69. package/lib/asset/AssetSourceGenerator.js +8 -0
  70. package/lib/asset/RawDataUrlModule.js +12 -13
  71. package/lib/buildChunkGraph.js +88 -9
  72. package/lib/bun/BunTargetPlugin.js +48 -0
  73. package/lib/cache/AddBuildDependenciesPlugin.js +1 -0
  74. package/lib/cache/AddManagedPathsPlugin.js +3 -0
  75. package/lib/cache/IdleFileCachePlugin.js +4 -0
  76. package/lib/cache/MemoryWithGcCachePlugin.js +1 -0
  77. package/lib/cache/PackFileCacheStrategy.js +1 -0
  78. package/lib/cache/ResolverCachePlugin.js +2 -0
  79. package/lib/cache/getLazyHashedEtag.js +9 -2
  80. package/lib/cache/mergeEtags.js +2 -0
  81. package/lib/cli.js +109 -19
  82. package/lib/config/browserslistTargetHandler.js +99 -0
  83. package/lib/config/defaults.js +147 -7
  84. package/lib/config/defineConfig.js +31 -0
  85. package/lib/config/normalization.js +5 -0
  86. package/lib/config/target.js +181 -2
  87. package/lib/container/ContainerEntryModule.js +15 -14
  88. package/lib/container/ContainerExposedDependency.js +2 -2
  89. package/lib/container/ContainerReferencePlugin.js +1 -1
  90. package/lib/container/FallbackDependency.js +5 -7
  91. package/lib/container/FallbackModule.js +10 -9
  92. package/lib/container/RemoteModule.js +20 -10
  93. package/lib/container/RemoteRuntimeModule.js +14 -12
  94. package/lib/css/CssGenerator.js +353 -327
  95. package/lib/css/CssInjectStyleRuntimeModule.js +36 -8
  96. package/lib/css/CssLoadingRuntimeModule.js +118 -47
  97. package/lib/css/CssModule.js +52 -23
  98. package/lib/css/CssModulesPlugin.js +107 -124
  99. package/lib/css/CssParser.js +2759 -2205
  100. package/lib/css/syntax.js +2859 -0
  101. package/lib/debug/ProfilingPlugin.js +2 -0
  102. package/lib/deno/DenoTargetPlugin.js +47 -0
  103. package/lib/dependencies/AMDDefineDependency.js +22 -17
  104. package/lib/dependencies/AMDDefineDependencyParserPlugin.js +1 -0
  105. package/lib/dependencies/AMDRequireArrayDependency.js +7 -11
  106. package/lib/dependencies/AMDRequireContextDependency.js +7 -11
  107. package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +1 -0
  108. package/lib/dependencies/AMDRequireDependency.js +24 -20
  109. package/lib/dependencies/CachedConstDependency.js +3 -0
  110. package/lib/dependencies/CommonJsDependencyHelpers.js +1 -0
  111. package/lib/dependencies/CommonJsExportRequireDependency.js +79 -31
  112. package/lib/dependencies/CommonJsExportsDependency.js +21 -16
  113. package/lib/dependencies/CommonJsExportsParserPlugin.js +230 -7
  114. package/lib/dependencies/CommonJsFullRequireDependency.js +27 -19
  115. package/lib/dependencies/CommonJsImportsParserPlugin.js +51 -16
  116. package/lib/dependencies/CommonJsPlugin.js +1 -1
  117. package/lib/dependencies/CommonJsRequireContextDependency.js +10 -13
  118. package/lib/dependencies/CommonJsRequireDependency.js +42 -11
  119. package/lib/dependencies/CommonJsSelfReferenceDependency.js +22 -14
  120. package/lib/dependencies/ConstDependency.js +16 -11
  121. package/lib/dependencies/ContextDependency.js +4 -0
  122. package/lib/dependencies/ContextDependencyTemplateAsId.js +1 -1
  123. package/lib/dependencies/ContextElementDependency.js +23 -14
  124. package/lib/dependencies/CreateRequireParserPlugin.js +1 -0
  125. package/lib/dependencies/CreateScriptUrlDependency.js +5 -7
  126. package/lib/dependencies/CriticalDependencyWarning.js +1 -0
  127. package/lib/dependencies/CssIcssExportDependency.js +512 -574
  128. package/lib/dependencies/CssIcssImportDependency.js +9 -9
  129. package/lib/dependencies/CssIcssSymbolDependency.js +22 -15
  130. package/lib/dependencies/CssImportDependency.js +25 -1
  131. package/lib/dependencies/CssUrlDependency.js +23 -14
  132. package/lib/dependencies/DllEntryDependency.js +9 -13
  133. package/lib/dependencies/ExportBindingInitFragment.js +164 -0
  134. package/lib/dependencies/ExportsInfoDependency.js +12 -12
  135. package/lib/dependencies/ExternalModuleDependency.js +8 -5
  136. package/lib/dependencies/ExternalModuleInitFragment.js +7 -5
  137. package/lib/dependencies/ExternalModuleInitFragmentDependency.js +13 -10
  138. package/lib/dependencies/HarmonyAcceptDependency.js +11 -11
  139. package/lib/dependencies/HarmonyAcceptImportDependency.js +1 -0
  140. package/lib/dependencies/HarmonyDetectionParserPlugin.js +47 -22
  141. package/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js +28 -2
  142. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +46 -22
  143. package/lib/dependencies/HarmonyExportExpressionDependency.js +107 -30
  144. package/lib/dependencies/HarmonyExportHeaderDependency.js +7 -9
  145. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +109 -31
  146. package/lib/dependencies/HarmonyExportInitFragment.js +22 -20
  147. package/lib/dependencies/HarmonyExportSpecifierDependency.js +83 -18
  148. package/lib/dependencies/HarmonyExports.js +4 -1
  149. package/lib/dependencies/HarmonyImportDependency.js +24 -0
  150. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +110 -74
  151. package/lib/dependencies/HarmonyImportGuard.js +254 -0
  152. package/lib/dependencies/HarmonyImportSideEffectDependency.js +18 -1
  153. package/lib/dependencies/HarmonyImportSpecifierDependency.js +118 -14
  154. package/lib/dependencies/HarmonyLinkingError.js +1 -0
  155. package/lib/dependencies/HarmonyModulesPlugin.js +1 -0
  156. package/lib/dependencies/{HtmlScriptSrcDependency.js → HtmlEntryDependency.js} +267 -154
  157. package/lib/dependencies/HtmlInlineHtmlDependency.js +107 -0
  158. package/lib/dependencies/HtmlInlineScriptDependency.js +20 -27
  159. package/lib/dependencies/HtmlInlineStyleDependency.js +62 -11
  160. package/lib/dependencies/HtmlSourceDependency.js +18 -0
  161. package/lib/dependencies/ImportContextDependency.js +5 -9
  162. package/lib/dependencies/ImportDependency.js +44 -2
  163. package/lib/dependencies/ImportMetaHotAcceptDependency.js +1 -0
  164. package/lib/dependencies/ImportMetaHotDeclineDependency.js +1 -0
  165. package/lib/dependencies/ImportMetaPlugin.js +74 -30
  166. package/lib/dependencies/ImportParserPlugin.js +19 -0
  167. package/lib/dependencies/ImportWeakDependency.js +1 -0
  168. package/lib/dependencies/JsonExportsDependency.js +9 -9
  169. package/lib/dependencies/LoaderImportDependency.js +1 -0
  170. package/lib/dependencies/LocalModule.js +11 -12
  171. package/lib/dependencies/LocalModuleDependency.js +11 -13
  172. package/lib/dependencies/ModuleDecoratorDependency.js +9 -9
  173. package/lib/dependencies/ModuleDependency.js +7 -0
  174. package/lib/dependencies/ModuleHotAcceptDependency.js +1 -0
  175. package/lib/dependencies/ModuleHotDeclineDependency.js +1 -0
  176. package/lib/dependencies/ModuleInitFragmentDependency.js +15 -11
  177. package/lib/dependencies/ProvidedDependency.js +31 -27
  178. package/lib/dependencies/PureExpressionDependency.js +7 -9
  179. package/lib/dependencies/RequireEnsureDependency.js +12 -13
  180. package/lib/dependencies/RequireHeaderDependency.js +3 -4
  181. package/lib/dependencies/RequireIncludeDependencyParserPlugin.js +3 -0
  182. package/lib/dependencies/RequireResolveContextDependency.js +7 -11
  183. package/lib/dependencies/RequireResolveDependency.js +1 -0
  184. package/lib/dependencies/RequireResolveHeaderDependency.js +3 -5
  185. package/lib/dependencies/RuntimeRequirementsDependency.js +6 -7
  186. package/lib/dependencies/StaticExportsDependency.js +10 -10
  187. package/lib/dependencies/SystemPlugin.js +2 -0
  188. package/lib/dependencies/URLContextDependency.js +5 -7
  189. package/lib/dependencies/URLDependency.js +14 -16
  190. package/lib/dependencies/UnsupportedDependency.js +8 -13
  191. package/lib/dependencies/WebAssemblyExportImportedDependency.js +11 -16
  192. package/lib/dependencies/WebAssemblyImportDependency.js +14 -16
  193. package/lib/dependencies/WebpackIsIncludedDependency.js +1 -0
  194. package/lib/dependencies/WorkerDependency.js +19 -8
  195. package/lib/dependencies/WorkerPlugin.js +40 -10
  196. package/lib/dependencies/processExportInfo.js +13 -11
  197. package/lib/dll/DelegatedModule.js +29 -15
  198. package/lib/dll/DelegatedModuleFactoryPlugin.js +1 -0
  199. package/lib/dll/DelegatedPlugin.js +1 -0
  200. package/lib/dll/DllEntryPlugin.js +3 -0
  201. package/lib/dll/DllModule.js +3 -2
  202. package/lib/dll/DllPlugin.js +16 -0
  203. package/lib/dll/DllReferencePlugin.js +3 -0
  204. package/lib/dll/LibManifestPlugin.js +1 -0
  205. package/lib/electron/ElectronTargetPlugin.js +22 -4
  206. package/lib/errors/ConcurrentCompilationError.js +1 -0
  207. package/lib/errors/HookWebpackError.js +11 -13
  208. package/lib/errors/IgnoreErrorModuleFactory.js +1 -0
  209. package/lib/errors/InvalidDependenciesModuleWarning.js +2 -0
  210. package/lib/errors/JSONParseError.js +9 -8
  211. package/lib/errors/ModuleBuildError.js +16 -13
  212. package/lib/errors/ModuleDependencyError.js +8 -1
  213. package/lib/errors/ModuleDependencyWarning.js +8 -1
  214. package/lib/errors/ModuleError.js +5 -11
  215. package/lib/errors/ModuleHashingError.js +4 -0
  216. package/lib/errors/ModuleNotFoundError.js +3 -0
  217. package/lib/errors/ModuleParseError.js +5 -11
  218. package/lib/errors/ModuleRestoreError.js +2 -0
  219. package/lib/errors/ModuleStoreError.js +3 -0
  220. package/lib/errors/ModuleWarning.js +7 -11
  221. package/lib/errors/NodeStuffInWebError.js +1 -0
  222. package/lib/errors/NonErrorEmittedError.js +2 -0
  223. package/lib/errors/UnhandledSchemeError.js +1 -0
  224. package/lib/esm/ModuleChunkLoadingPlugin.js +0 -1
  225. package/lib/esm/ModuleChunkLoadingRuntimeModule.js +28 -23
  226. package/lib/hmr/HotModuleReplacement.runtime.js +175 -30
  227. package/lib/hmr/HotModuleReplacementRuntimeModule.js +7 -0
  228. package/lib/hmr/JavascriptHotModuleReplacement.runtime.js +173 -26
  229. package/lib/hmr/LazyCompilationPlugin.js +134 -6
  230. package/lib/html/HtmlGenerator.js +292 -50
  231. package/lib/html/HtmlModule.js +40 -0
  232. package/lib/html/HtmlModulesPlugin.js +287 -60
  233. package/lib/html/HtmlParser.js +1108 -1099
  234. package/lib/html/{walkHtmlTokens.js → syntax.js} +4701 -212
  235. package/lib/ids/IdHelpers.js +4 -2
  236. package/lib/index.js +19 -0
  237. package/lib/javascript/BasicEvaluatedExpression.js +1 -0
  238. package/lib/javascript/EnableChunkLoadingPlugin.js +1 -0
  239. package/lib/javascript/JavascriptGenerator.js +6 -2
  240. package/lib/javascript/JavascriptModule.js +54 -0
  241. package/lib/javascript/JavascriptModulesPlugin.js +257 -102
  242. package/lib/javascript/JavascriptParser.js +286 -159
  243. package/lib/json/JsonModule.js +40 -0
  244. package/lib/json/JsonModulesPlugin.js +7 -0
  245. package/lib/json/JsonParser.js +4 -2
  246. package/lib/library/AssignLibraryPlugin.js +5 -3
  247. package/lib/library/ExportPropertyLibraryPlugin.js +1 -0
  248. package/lib/library/FalseIIFEUmdWarning.js +1 -0
  249. package/lib/library/ModuleLibraryPlugin.js +54 -29
  250. package/lib/library/SystemLibraryPlugin.js +3 -3
  251. package/lib/node/NodeTargetPlugin.js +1 -0
  252. package/lib/node/NodeWatchFileSystem.js +38 -22
  253. package/lib/node/ReadFileChunkLoadingRuntimeModule.js +10 -9
  254. package/lib/node/RequireChunkLoadingRuntimeModule.js +18 -16
  255. package/lib/optimize/ConcatenatedModule.js +397 -78
  256. package/lib/optimize/ConstExportsPlugin.js +211 -0
  257. package/lib/optimize/InlineExports.js +178 -0
  258. package/lib/optimize/InnerGraph.js +372 -275
  259. package/lib/optimize/InnerGraphPlugin.js +196 -99
  260. package/lib/optimize/ModuleConcatenationPlugin.js +56 -25
  261. package/lib/optimize/RealContentHashPlugin.js +14 -1
  262. package/lib/optimize/SideEffectsFlagPlugin.js +234 -64
  263. package/lib/runtime/AsyncModuleRuntimeModule.js +32 -30
  264. package/lib/runtime/AutoPublicPathRuntimeModule.js +11 -5
  265. package/lib/runtime/CompatGetDefaultExportRuntimeModule.js +1 -1
  266. package/lib/runtime/CreateFakeNamespaceObjectRuntimeModule.js +6 -4
  267. package/lib/runtime/DefinePropertyGettersRuntimeModule.js +33 -4
  268. package/lib/runtime/GetChunkFilenameRuntimeModule.js +82 -3
  269. package/lib/runtime/GetTrustedTypesPolicyRuntimeModule.js +1 -1
  270. package/lib/runtime/HasOwnPropertyRuntimeModule.js +1 -1
  271. package/lib/runtime/LoadScriptRuntimeModule.js +18 -13
  272. package/lib/runtime/MakeDeferredNamespaceObjectRuntime.js +39 -26
  273. package/lib/runtime/MakeNamespaceObjectRuntimeModule.js +1 -1
  274. package/lib/runtime/OnChunksLoadedRuntimeModule.js +4 -4
  275. package/lib/runtime/RelativeUrlRuntimeModule.js +3 -2
  276. package/lib/runtime/StartupChunkDependenciesRuntimeModule.js +1 -1
  277. package/lib/runtime/StartupEntrypointRuntimeModule.js +4 -3
  278. package/lib/runtime/WorkerRuntimeModule.js +33 -0
  279. package/lib/schemes/HttpUriPlugin.js +3 -2
  280. package/lib/serialization/AggregateErrorSerializer.js +7 -6
  281. package/lib/serialization/ArraySerializer.js +4 -8
  282. package/lib/serialization/BinaryMiddleware.js +538 -468
  283. package/lib/serialization/DateObjectSerializer.js +2 -2
  284. package/lib/serialization/ErrorObjectSerializer.js +7 -6
  285. package/lib/serialization/MapObjectSerializer.js +5 -9
  286. package/lib/serialization/NullPrototypeObjectSerializer.js +7 -9
  287. package/lib/serialization/ObjectMiddleware.js +50 -13
  288. package/lib/serialization/PlainObjectSerializer.js +9 -8
  289. package/lib/serialization/RegExpObjectSerializer.js +2 -2
  290. package/lib/serialization/Serializer.js +1 -0
  291. package/lib/serialization/SetObjectSerializer.js +4 -8
  292. package/lib/sharing/ConsumeSharedModule.js +6 -7
  293. package/lib/sharing/ConsumeSharedPlugin.js +1 -0
  294. package/lib/sharing/ConsumeSharedRuntimeModule.js +46 -41
  295. package/lib/sharing/ProvideSharedDependency.js +30 -15
  296. package/lib/sharing/ProvideSharedModule.js +30 -11
  297. package/lib/sharing/ProvideSharedPlugin.js +4 -2
  298. package/lib/sharing/SharePlugin.js +3 -0
  299. package/lib/sharing/ShareRuntimeModule.js +18 -16
  300. package/lib/sharing/resolveMatchedConfigs.js +2 -1
  301. package/lib/sharing/utils.js +1 -1
  302. package/lib/stats/DefaultStatsFactoryPlugin.js +4 -6
  303. package/lib/stats/DefaultStatsPrinterPlugin.js +14 -14
  304. package/lib/stats/StatsFactory.js +11 -9
  305. package/lib/stats/StatsPrinter.js +9 -2
  306. package/lib/url/URLParserPlugin.js +5 -2
  307. package/lib/util/AsyncQueue.js +10 -0
  308. package/lib/util/LazyBucketSortedSet.js +5 -0
  309. package/lib/util/LazySet.js +6 -4
  310. package/lib/util/LocConverter.js +11 -1
  311. package/lib/util/Semaphore.js +1 -0
  312. package/lib/util/SourceProcessor.js +106 -0
  313. package/lib/util/TupleSet.js +1 -0
  314. package/lib/util/WeakTupleMap.js +27 -1
  315. package/lib/util/chainedImports.js +3 -3
  316. package/lib/util/comparators.js +2 -2
  317. package/lib/util/concatenate.js +31 -7
  318. package/lib/util/createHash.js +0 -1
  319. package/lib/util/deterministicGrouping.js +19 -12
  320. package/lib/util/extractSourceMap.js +1 -1
  321. package/lib/util/findGraphRoots.js +2 -0
  322. package/lib/util/fs.js +16 -6
  323. package/lib/util/hash/DebugHash.js +1 -0
  324. package/lib/util/hash/hash-digest.js +24 -15
  325. package/lib/util/identifier.js +7 -0
  326. package/lib/util/internalSerializables.js +11 -2
  327. package/lib/util/magicComment.js +42 -0
  328. package/lib/util/makeSerializable.js +1 -0
  329. package/lib/util/nonNumericOnlyHash.js +30 -1
  330. package/lib/util/property.js +7 -1
  331. package/lib/util/publicPathPlaceholder.js +64 -0
  332. package/lib/util/registerExternalSerializer.js +4 -4
  333. package/lib/wasm-async/AsyncWasmModule.js +77 -0
  334. package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +1 -1
  335. package/lib/wasm-async/AsyncWebAssemblyModulesPlugin.js +1 -96
  336. package/lib/wasm-async/AsyncWebAssemblyParser.js +1 -1
  337. package/lib/wasm-async/UniversalCompileAsyncWasmPlugin.js +1 -1
  338. package/lib/wasm-sync/SyncWasmModule.js +39 -0
  339. package/lib/wasm-sync/UnsupportedWebAssemblyFeatureError.js +1 -0
  340. package/lib/wasm-sync/WasmChunkLoadingRuntimeModule.js +6 -3
  341. package/lib/wasm-sync/WasmFinalizeExportsPlugin.js +2 -1
  342. package/lib/wasm-sync/WebAssemblyGenerator.js +1 -0
  343. package/lib/wasm-sync/WebAssemblyInInitialChunkError.js +2 -0
  344. package/lib/wasm-sync/WebAssemblyModulesPlugin.js +10 -0
  345. package/lib/wasm-sync/WebAssemblyParser.js +7 -3
  346. package/lib/web/JsonpChunkLoadingRuntimeModule.js +45 -39
  347. package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +11 -10
  348. package/package.json +36 -28
  349. package/schemas/WebpackOptions.check.js +1 -1
  350. package/schemas/WebpackOptions.json +212 -3
  351. package/schemas/plugins/HtmlGeneratorOptions.check.js +1 -1
  352. package/schemas/plugins/HtmlParserOptions.check.d.ts +7 -0
  353. package/schemas/plugins/HtmlParserOptions.check.js +6 -0
  354. package/schemas/plugins/HtmlParserOptions.json +3 -0
  355. package/schemas/plugins/css/CssAutoOrModuleParserOptions.check.js +1 -1
  356. package/schemas/plugins/css/CssModuleParserOptions.check.js +1 -1
  357. package/schemas/plugins/css/CssParserOptions.check.js +1 -1
  358. package/types.d.ts +1592 -314
  359. package/lib/css/walkCssTokens.js +0 -2020
  360. package/lib/util/AppendOnlyStackedSet.js +0 -93
@@ -9,8 +9,8 @@ const {
9
9
  CSS_TYPE,
10
10
  JAVASCRIPT_TYPE
11
11
  } = require("../ModuleSourceTypeConstants");
12
+ const HtmlGenerator = require("../html/HtmlGenerator");
12
13
  const makeSerializable = require("../util/makeSerializable");
13
- const CssUrlDependency = require("./CssUrlDependency");
14
14
  const ModuleDependency = require("./ModuleDependency");
15
15
 
16
16
  /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
@@ -20,21 +20,28 @@ const ModuleDependency = require("./ModuleDependency");
20
20
  /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
21
21
  /** @typedef {import("../Entrypoint")} Entrypoint */
22
22
  /** @typedef {import("../javascript/JavascriptParser").Range} Range */
23
- /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
24
- /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
23
+ /** @typedef {"script" | "script-module" | "modulepreload" | "stylesheet"} HtmlEntryElementKind */
25
24
 
26
- /** @typedef {"script-classic" | "script-module" | "modulepreload" | "stylesheet"} HtmlScriptElementKind */
25
+ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext<[string, string, HtmlEntryElementKind, number, number, boolean, string, number, boolean, number, (Range | null), (Range | null)]>} ObjectDeserializerContext */
26
+ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext<[string, string, HtmlEntryElementKind, number, number, boolean, string, number, boolean, number, (Range | null), (Range | null)]>} ObjectSerializerContext */
27
27
 
28
- class HtmlScriptSrcDependency extends ModuleDependency {
28
+ class HtmlEntryDependency extends ModuleDependency {
29
29
  /**
30
- * Creates an instance of HtmlScriptSrcDependency.
30
+ * Creates an instance of HtmlEntryDependency.
31
31
  * @param {string} request request
32
32
  * @param {Range} range range of the attribute value in the source
33
33
  * @param {string} entryName name of the entry this script src is bundled into
34
34
  * @param {string=} category dependency category used for resolving and grouping
35
- * @param {HtmlScriptElementKind=} elementKind shape of the originating HTML element; used when expanding sibling tags for split/runtime chunks
35
+ * @param {HtmlEntryElementKind=} elementKind shape of the originating HTML element; used when expanding sibling tags for split/runtime chunks
36
36
  * @param {number=} tagStart position of the opening `<` of the originating tag in the source; sibling tags emitted for additional entry chunks are inserted right before this
37
37
  * @param {number=} tagOpenEnd position of the character immediately after the opening tag's `>` in the source; combined with `tagStart` this lets the template clone the original opening tag verbatim (preserving attributes like `nonce`, `crossorigin`, `referrerpolicy`, `defer`, `async`) when generating sibling tags
38
+ * @param {boolean=} tagIsNative whether the originating element is the native tag for `elementKind` (`<script>` / `<link>`); decided at parse time from the tag name so the template needn't re-parse the source text. A custom element mapped to a `script`/`stylesheet` source `type` is non-native and gets a freshly synthesized sibling tag instead of a verbatim clone
39
+ * @param {string=} copyableAttrsText the originating tag's `nonce`/`crossorigin`/`referrerpolicy` attribute source spans (leading-space-prefixed, in that fixed order), captured at parse time so synthesized sibling `<link>`/`<script>` tags carry the same CSP/fetch policy without the template re-parsing the tag text; empty when none are present
40
+ * @param {number=} tagNameEnd position right after the originating tag's name (e.g. after `<script`), captured at parse time so the template can insert a `crossorigin` attribute without re-scanning the tag text for the name boundary
41
+ * @param {boolean=} hasOwnCrossOrigin whether the originating tag already carries a `crossorigin` attribute; when true the author's value wins and `output.crossOriginLoading` is not applied
42
+ * @param {number=} cssAnchor source offset of the first `<script>` tag; a later entry's injected stylesheet `<link>`s are inserted here so CSS loads before any script. -1 when the document has no script
43
+ * @param {Range | null=} integrityRange source span of the originating tag's `integrity` attribute (offsets relative to `tagStart`, leading whitespace and quotes included), captured at parse time so cloned sibling tags can drop it without re-parsing; null when absent
44
+ * @param {Range | null=} typeValueRange source span of the originating tag's `type` attribute value (offsets relative to `tagStart`), captured at parse time so `script-module` sibling clones can force `type="module"` in place; null when there is no value to rewrite (a `type="module"` is then inserted instead)
38
45
  */
39
46
  constructor(
40
47
  request,
@@ -43,23 +50,45 @@ class HtmlScriptSrcDependency extends ModuleDependency {
43
50
  category,
44
51
  elementKind,
45
52
  tagStart,
46
- tagOpenEnd
53
+ tagOpenEnd,
54
+ tagIsNative,
55
+ copyableAttrsText,
56
+ tagNameEnd,
57
+ hasOwnCrossOrigin,
58
+ cssAnchor,
59
+ integrityRange,
60
+ typeValueRange
47
61
  ) {
48
62
  super(request);
49
63
  this.range = range;
64
+ /** @type {string} */
50
65
  this.entryName = entryName;
51
66
  /** @type {string} */
52
67
  this._category = category || "commonjs";
53
- /** @type {HtmlScriptElementKind} */
54
- this.elementKind = elementKind || "script-classic";
68
+ /** @type {HtmlEntryElementKind} */
69
+ this.elementKind = elementKind || "script";
55
70
  /** @type {number} */
56
71
  this.tagStart = tagStart === undefined ? -1 : tagStart;
57
72
  /** @type {number} */
58
73
  this.tagOpenEnd = tagOpenEnd === undefined ? -1 : tagOpenEnd;
74
+ /** @type {boolean} */
75
+ this.tagIsNative = tagIsNative !== false;
76
+ /** @type {string} */
77
+ this.copyableAttrsText = copyableAttrsText || "";
78
+ /** @type {number} */
79
+ this.tagNameEnd = tagNameEnd === undefined ? -1 : tagNameEnd;
80
+ /** @type {boolean} */
81
+ this.hasOwnCrossOrigin = hasOwnCrossOrigin === true;
82
+ /** @type {number} */
83
+ this.cssAnchor = cssAnchor === undefined ? -1 : cssAnchor;
84
+ /** @type {Range | null} */
85
+ this.integrityRange = integrityRange || null;
86
+ /** @type {Range | null} */
87
+ this.typeValueRange = typeValueRange || null;
59
88
  }
60
89
 
61
90
  get type() {
62
- return "html script src";
91
+ return "html entry";
63
92
  }
64
93
 
65
94
  get category() {
@@ -71,12 +100,19 @@ class HtmlScriptSrcDependency extends ModuleDependency {
71
100
  * @param {ObjectSerializerContext} context context
72
101
  */
73
102
  serialize(context) {
74
- const { write } = context;
75
- write(this.entryName);
76
- write(this._category);
77
- write(this.elementKind);
78
- write(this.tagStart);
79
- write(this.tagOpenEnd);
103
+ context
104
+ .write(this.entryName)
105
+ .write(this._category)
106
+ .write(this.elementKind)
107
+ .write(this.tagStart)
108
+ .write(this.tagOpenEnd)
109
+ .write(this.tagIsNative)
110
+ .write(this.copyableAttrsText)
111
+ .write(this.tagNameEnd)
112
+ .write(this.hasOwnCrossOrigin)
113
+ .write(this.cssAnchor)
114
+ .write(this.integrityRange)
115
+ .write(this.typeValueRange);
80
116
  super.serialize(context);
81
117
  }
82
118
 
@@ -85,49 +121,33 @@ class HtmlScriptSrcDependency extends ModuleDependency {
85
121
  * @param {ObjectDeserializerContext} context context
86
122
  */
87
123
  deserialize(context) {
88
- const { read } = context;
89
- this.entryName = read();
90
- this._category = read();
91
- this.elementKind = read();
92
- this.tagStart = read();
93
- this.tagOpenEnd = read();
94
- super.deserialize(context);
124
+ this.entryName = context.read();
125
+ const c1 = context.rest;
126
+ this._category = c1.read();
127
+ const c2 = c1.rest;
128
+ this.elementKind = c2.read();
129
+ const c3 = c2.rest;
130
+ this.tagStart = c3.read();
131
+ const c4 = c3.rest;
132
+ this.tagOpenEnd = c4.read();
133
+ const c5 = c4.rest;
134
+ this.tagIsNative = c5.read();
135
+ const c6 = c5.rest;
136
+ this.copyableAttrsText = c6.read();
137
+ const c7 = c6.rest;
138
+ this.tagNameEnd = c7.read();
139
+ const c8 = c7.rest;
140
+ this.hasOwnCrossOrigin = c8.read();
141
+ const c9 = c8.rest;
142
+ this.cssAnchor = c9.read();
143
+ const c10 = c9.rest;
144
+ this.integrityRange = c10.read();
145
+ const c11 = c10.rest;
146
+ this.typeValueRange = c11.read();
147
+ super.deserialize(c11.rest);
95
148
  }
96
149
  }
97
150
 
98
- /**
99
- * @param {Chunk} chunk a chunk
100
- * @param {import("../Compilation")} compilation compilation
101
- * @param {"javascript" | "css"} contentHashType which content hash to plug into the filename template
102
- * @returns {string} chunk filename path (no public-path prefix)
103
- */
104
- const getChunkFilename = (chunk, compilation, contentHashType) => {
105
- const outputOptions = compilation.outputOptions;
106
- let filenameTemplate;
107
- if (contentHashType === "css") {
108
- // For a CSS-typed chunk, use the same template the CSS pipeline
109
- // will use when it actually emits the `.css` file, so the `<link
110
- // rel="stylesheet" href>` URL we write into the HTML matches the
111
- // asset on disk.
112
- filenameTemplate =
113
- require("../css/CssModulesPlugin").getChunkFilenameTemplate(
114
- chunk,
115
- outputOptions
116
- );
117
- } else {
118
- filenameTemplate =
119
- chunk.filenameTemplate ||
120
- (chunk.canBeInitial()
121
- ? outputOptions.filename
122
- : outputOptions.chunkFilename);
123
- }
124
-
125
- return compilation.getPath(filenameTemplate, {
126
- chunk,
127
- contentHashType
128
- });
129
- };
130
-
131
151
  /**
132
152
  * @param {Entrypoint} entrypoint entrypoint
133
153
  * @returns {Chunk[]} every chunk this entrypoint needs in load order: the
@@ -240,27 +260,25 @@ const chunkHasCss = (chunk, chunkGraph) =>
240
260
  );
241
261
 
242
262
  /**
243
- * Compare two chunks for a deterministic tie-break in CSS link ordering.
244
- * `chunk.name` and `chunk.id` are both stable strings (when present);
245
- * one of them is set for every chunk webpack emits. We can't rely on
246
- * `Array.prototype.sort` being stable — webpack still supports Node
247
- * 10.13 where V8's sort is not guaranteed stable for arrays larger
248
- * than ten elements so any time `firstCssModulePostOrderIndex`
249
- * returns the same value for two chunks (most commonly when several
250
- * chunks have no reachable CSS module in the entrypoint's dependency
251
- * walk and all map to `Infinity`) this comparator picks the canonical
252
- * order.
253
- * @param {Chunk} a first chunk
254
- * @param {Chunk} b second chunk
255
- * @returns {-1 | 0 | 1} ordering
263
+ * Deterministic tie-break key for CSS link ordering. `chunk.name` and
264
+ * `chunk.id` are both stable strings (when present); one of them is set for
265
+ * every chunk webpack emits. We can't rely on `Array.prototype.sort` being
266
+ * stable — webpack still supports Node 10.13 where V8's sort is not guaranteed
267
+ * stable for arrays larger than ten elements so any time
268
+ * `firstCssModulePostOrderIndex` returns the same value for two chunks (most
269
+ * commonly when several chunks have no reachable CSS module in the entrypoint's
270
+ * dependency walk and all map to `Infinity`) this key picks the canonical order.
271
+ * Computed once per chunk when building the sort array rather than per
272
+ * comparison.
273
+ * @param {Chunk} chunk chunk
274
+ * @returns {string} the tie-break key
256
275
  */
257
- const compareChunksForCssTieBreak = (a, b) => {
258
- const an = `${a.name || ""} ${a.id === null || a.id === undefined ? "" : a.id}`;
259
- const bn = `${b.name || ""} ${b.id === null || b.id === undefined ? "" : b.id}`;
260
- if (an < bn) return -1;
261
- if (an > bn) return 1;
262
- return 0;
263
- };
276
+ const cssChunkSortKey = (chunk) =>
277
+ `${chunk.name || ""} ${
278
+ chunk.id === null || chunk.id === undefined ? "" : chunk.id
279
+ }`;
280
+
281
+ const CSS_SOURCE_TYPES = [CSS_TYPE, CSS_IMPORT_TYPE];
264
282
 
265
283
  /**
266
284
  * Smallest post-order index among the CSS modules of a chunk, taken
@@ -285,7 +303,7 @@ const compareChunksForCssTieBreak = (a, b) => {
285
303
  */
286
304
  const firstCssModulePostOrderIndex = (chunk, entrypoint, chunkGraph) => {
287
305
  let min = Number.POSITIVE_INFINITY;
288
- for (const sourceType of [CSS_TYPE, CSS_IMPORT_TYPE]) {
306
+ for (const sourceType of CSS_SOURCE_TYPES) {
289
307
  const modules = chunkGraph.getChunkModulesIterableBySourceType(
290
308
  chunk,
291
309
  sourceType
@@ -299,33 +317,44 @@ const firstCssModulePostOrderIndex = (chunk, entrypoint, chunkGraph) => {
299
317
  return min;
300
318
  };
301
319
 
302
- const COPYABLE_LINK_ATTRS = ["nonce", "crossorigin", "referrerpolicy"];
303
-
304
320
  /**
305
321
  * Build a fresh `<link rel="stylesheet" href="…">` for a CSS chunk that
306
322
  * was pulled in by a `<script src>` entry — the originating tag was a
307
323
  * `<script>`, but the chunk is CSS so cloning the script tag verbatim
308
- * would produce nonsense (`<script src="…\.css">`). Copy
309
- * `nonce`/`crossorigin`/`referrerpolicy` from the original element so
310
- * the same CSP and fetch policy applies; `defer`/`async`/`type` have no
311
- * meaning on `<link>` and are dropped.
312
- * @param {string} originalTag the originating `<script>`/`<link>` tag's source text
324
+ * would produce nonsense (`<script src="…\.css">`). `extra` carries the
325
+ * `nonce`/`crossorigin`/`referrerpolicy` source spans captured from the
326
+ * original element at parse time so the same CSP and fetch policy applies;
327
+ * `defer`/`async`/`type` have no meaning on `<link>` and are dropped.
328
+ * @param {string} extra the copyable CSP/fetch attribute source text (leading-space-prefixed, or empty)
313
329
  * @param {string} href URL for the stylesheet
330
+ * @param {string} crossOrigin ` crossorigin="…"` to append from `output.crossOriginLoading`, or `""`
314
331
  * @returns {string} the sibling `<link>` tag's HTML
315
332
  */
316
- const buildStylesheetLink = (originalTag, href) => {
317
- let extra = "";
318
- for (const attr of COPYABLE_LINK_ATTRS) {
319
- // Match ` <attr>`, ` <attr>=value`, ` <attr>="value"`, ` <attr>='value'`.
320
- const re = new RegExp(
321
- `\\s${attr}(?:\\s*=\\s*(?:"[^"]*"|'[^']*'|[^\\s>]+))?(?=[\\s/>])`,
322
- "i"
323
- );
324
- const m = originalTag.match(re);
325
- if (m) extra += m[0];
326
- }
333
+ const buildStylesheetLink = (extra, href, crossOrigin) => {
327
334
  const safeHref = href.replace(/"/g, "&quot;");
328
- return `<link rel="stylesheet" href="${safeHref}"${extra}>`;
335
+ return `<link rel="stylesheet" href="${safeHref}"${extra}${crossOrigin}>`;
336
+ };
337
+
338
+ /**
339
+ * Build a fresh `<script src="…">` for a JS chunk pulled in by a custom
340
+ * (non-`<script>`) element that the user mapped to `type: "script"` /
341
+ * `"script-module"`. Cloning the custom element verbatim and appending
342
+ * `</script>` (as `cloneTagWithUrl` does for a real `<script>`) would emit
343
+ * mismatched, non-executing markup such as `<my-script src="…"></script>`,
344
+ * so synthesize a real script element instead — copying only the CSP/fetch
345
+ * attributes (`extra`, captured at parse time), since the custom element's
346
+ * other attributes have no defined meaning on a `<script>`.
347
+ * @param {string} extra the copyable CSP/fetch attribute source text (leading-space-prefixed, or empty)
348
+ * @param {string} src URL for the script
349
+ * @param {boolean} isModule whether to emit `type="module"`
350
+ * @param {string} crossOrigin ` crossorigin="…"` to append from `output.crossOriginLoading`, or `""`
351
+ * @returns {string} the sibling `<script>` tag's HTML
352
+ */
353
+ const buildScriptTag = (extra, src, isModule, crossOrigin) => {
354
+ const safeSrc = src.replace(/"/g, "&quot;");
355
+ return `<script${
356
+ isModule ? ' type="module"' : ""
357
+ } src="${safeSrc}"${extra}${crossOrigin}></script>`;
329
358
  };
330
359
 
331
360
  /**
@@ -337,12 +366,18 @@ const buildStylesheetLink = (originalTag, href) => {
337
366
  * content-specific. When the original tag was upgraded to a module script
338
367
  * (either by the author or by the `output.module` auto-upgrade in
339
368
  * `HtmlParser`), the sibling is forced to `type="module"` regardless of what
340
- * the source originally said.
369
+ * the source originally said. The `integrity`/`type` spans come from the
370
+ * parser (`HtmlParser` captured them off the AST); edits are applied
371
+ * right-to-left so earlier offsets stay valid — no re-parsing of the tag.
341
372
  * @param {string} originalTag the opening tag's source text including `>`
342
373
  * @param {number} srcStartInTag offset of the src/href value start within `originalTag`
343
374
  * @param {number} srcEndInTag offset of the src/href value end within `originalTag`
344
375
  * @param {string} newUrl URL to put into the cloned tag's src/href slot
345
- * @param {HtmlScriptElementKind} elementKind shape of the originating tag
376
+ * @param {HtmlEntryElementKind} elementKind shape of the originating tag
377
+ * @param {string} crossOrigin ` crossorigin="…"` to insert from `output.crossOriginLoading`, or `""`
378
+ * @param {number} tagNameEndInTag offset right after the tag name within `originalTag`, where `crossOrigin` is inserted
379
+ * @param {Range | null} integrityRange `integrity` attribute span within `originalTag` to drop, or null
380
+ * @param {Range | null} typeValueRange `type` value span within `originalTag` to overwrite with `module`, or null to insert `type="module"`
346
381
  * @returns {string} the sibling tag's HTML (including a closing `</script>` for script tags)
347
382
  */
348
383
  const cloneTagWithUrl = (
@@ -350,39 +385,60 @@ const cloneTagWithUrl = (
350
385
  srcStartInTag,
351
386
  srcEndInTag,
352
387
  newUrl,
353
- elementKind
388
+ elementKind,
389
+ crossOrigin,
390
+ tagNameEndInTag,
391
+ integrityRange,
392
+ typeValueRange
354
393
  ) => {
355
- let body =
356
- originalTag.slice(0, srcStartInTag) +
357
- newUrl +
358
- originalTag.slice(srcEndInTag);
359
-
360
- // Strip dangerous-to-copy attributes from the cloned tag — currently
361
- // just `integrity`. The match handles all three quoting styles
362
- // (`"…"`, `'…'`, unquoted) and the bare-attribute form.
363
- body = body.replace(
364
- /\s+integrity(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s>]+))?(?=[\s/>])/gi,
365
- ""
366
- );
394
+ const isLink =
395
+ elementKind === "modulepreload" || elementKind === "stylesheet";
367
396
 
397
+ /** @type {{ start: number, end: number, text: string }[]} */
398
+ const edits = [{ start: srcStartInTag, end: srcEndInTag, text: newUrl }];
399
+
400
+ // `integrity` is content-specific — drop it from the clone.
401
+ // TODO: emit a correct per-chunk `integrity` once a core SRI output
402
+ // option exists, instead of dropping it.
403
+ if (integrityRange) {
404
+ edits.push({ start: integrityRange[0], end: integrityRange[1], text: "" });
405
+ }
406
+
407
+ // Inserted right after the tag name: `crossorigin` from
408
+ // `output.crossOriginLoading`, then a forced `type="module"` when the tag
409
+ // has no `type` value to rewrite in place.
410
+ let afterName = crossOrigin;
368
411
  if (elementKind === "script-module") {
369
- if (/\stype\s*=/i.test(body)) {
370
- body = body.replace(
371
- /(\stype\s*=\s*)(?:"[^"]*"|'[^']*'|[^\s>]+)/i,
372
- '$1"module"'
373
- );
412
+ if (typeValueRange) {
413
+ edits.push({
414
+ start: typeValueRange[0],
415
+ end: typeValueRange[1],
416
+ text: "module"
417
+ });
374
418
  } else {
375
- body = body.replace(/^<script\b/i, '<script type="module"');
419
+ afterName += ' type="module"';
376
420
  }
377
421
  }
378
422
 
423
+ if (afterName) {
424
+ edits.push({
425
+ start: tagNameEndInTag,
426
+ end: tagNameEndInTag,
427
+ text: afterName
428
+ });
429
+ }
430
+
431
+ edits.sort((a, b) => b.start - a.start);
432
+ let body = originalTag;
433
+ for (const edit of edits) {
434
+ body = body.slice(0, edit.start) + edit.text + body.slice(edit.end);
435
+ }
436
+
379
437
  // `<link>` is a void element — no closing tag. `<script>` needs `</script>`.
380
- return elementKind === "modulepreload" || elementKind === "stylesheet"
381
- ? body
382
- : `${body}</script>`;
438
+ return isLink ? body : `${body}</script>`;
383
439
  };
384
440
 
385
- HtmlScriptSrcDependency.Template = class HtmlScriptSrcDependencyTemplate extends (
441
+ HtmlEntryDependency.Template = class HtmlEntryDependencyTemplate extends (
386
442
  ModuleDependency.Template
387
443
  ) {
388
444
  /**
@@ -394,9 +450,10 @@ HtmlScriptSrcDependency.Template = class HtmlScriptSrcDependencyTemplate extends
394
450
  */
395
451
  apply(dependency, source, templateContext) {
396
452
  const { runtimeTemplate } = templateContext;
397
- const dep = /** @type {HtmlScriptSrcDependency} */ (dependency);
453
+ const dep = /** @type {HtmlEntryDependency} */ (dependency);
398
454
  const compilation = runtimeTemplate.compilation;
399
455
  const { chunkGraph } = compilation;
456
+ const { crossOriginLoading } = compilation.outputOptions;
400
457
  const entrypoint = /** @type {Entrypoint | undefined} */ (
401
458
  compilation.entrypoints.get(dep.entryName)
402
459
  );
@@ -410,15 +467,13 @@ HtmlScriptSrcDependency.Template = class HtmlScriptSrcDependencyTemplate extends
410
467
  const entryChunk = orderedChunks[orderedChunks.length - 1];
411
468
  const isStylesheet = dep.elementKind === "stylesheet";
412
469
 
413
- // Rewrite the originating tag's src/href to the entry chunk's
414
- // primary asset for that element kind: `.css` for
415
- // `<link rel="stylesheet">`, `.js` for everything else.
470
+ // Rewrite src/href to a chunk-URL sentinel (resolved by renderManifest):
471
+ // `.css` for `<link rel="stylesheet">`, `.js` for everything else.
416
472
  const entryContentHashType = isStylesheet ? "css" : "javascript";
417
- const entryUrl = `${CssUrlDependency.PUBLIC_PATH_AUTO}${getChunkFilename(
473
+ const entryUrl = HtmlGenerator.makeChunkUrlSentinel(
418
474
  entryChunk,
419
- compilation,
420
475
  entryContentHashType
421
- )}`;
476
+ );
422
477
  source.replace(dep.range[0], dep.range[1] - 1, entryUrl);
423
478
 
424
479
  if (dep.tagStart < 0 || dep.tagOpenEnd <= dep.tagStart) {
@@ -439,6 +494,30 @@ HtmlScriptSrcDependency.Template = class HtmlScriptSrcDependencyTemplate extends
439
494
  const originalTag = originalContent.slice(dep.tagStart, dep.tagOpenEnd);
440
495
  const srcStartInTag = dep.range[0] - dep.tagStart;
441
496
  const srcEndInTag = dep.range[1] - dep.tagStart;
497
+ // Non-native originating elements (a custom element mapped to a
498
+ // `script`/`stylesheet` source `type`) can't be cloned verbatim — the
499
+ // clone would be invalid markup (e.g. `<my-script …></script>`), so a
500
+ // real native tag is synthesized instead. Decided at parse time from the
501
+ // tag name (`dep.tagIsNative`) rather than re-parsing the source text.
502
+ const tagIsNative = dep.tagIsNative;
503
+
504
+ // `crossorigin` to mirror `output.crossOriginLoading` onto every injected
505
+ // tag. Empty when the option is off or the originating tag already set
506
+ // `crossorigin` (author value wins, flagged at parse time). Mirrors
507
+ // webpack's runtime, which sets `crossOrigin` on chunk-loading scripts,
508
+ // and matches Vite, which emits it on every injected script/stylesheet.
509
+ const crossOrigin =
510
+ crossOriginLoading && !dep.hasOwnCrossOrigin
511
+ ? ` crossorigin="${crossOriginLoading}"`
512
+ : "";
513
+ const tagNameEndInTag = dep.tagNameEnd - dep.tagStart;
514
+
515
+ // Mirror it onto the entry tag too (siblings get it via the builders
516
+ // below), inserted right after the tag name — its parse-time offset — so
517
+ // it sits alongside any `type="module"` the parser injected.
518
+ if (tagIsNative && crossOrigin && dep.tagNameEnd >= 0) {
519
+ source.insert(dep.tagNameEnd, crossOrigin);
520
+ }
442
521
 
443
522
  /**
444
523
  * @param {Chunk} chunk chunk to emit a sibling tag for
@@ -446,23 +525,49 @@ HtmlScriptSrcDependency.Template = class HtmlScriptSrcDependencyTemplate extends
446
525
  * @returns {string} a single sibling tag's HTML
447
526
  */
448
527
  const buildSibling = (chunk, kind) => {
449
- const url = `${CssUrlDependency.PUBLIC_PATH_AUTO}${getChunkFilename(
450
- chunk,
451
- compilation,
452
- kind
453
- )}`;
454
- if (kind === "css" && !isStylesheet) {
455
- // Originating tag is `<script>` (or `<link rel=modulepreload>`)
456
- // but this chunk is CSS — emit a fresh `<link>` rather than
457
- // cloning the script.
458
- return buildStylesheetLink(originalTag, url);
528
+ const url = HtmlGenerator.makeChunkUrlSentinel(chunk, kind);
529
+ if (kind === "css") {
530
+ // A CSS chunk is always loaded via `<link rel="stylesheet">`.
531
+ // Clone only when the entry tag is itself a native `<link
532
+ // rel="stylesheet">` (so attributes like `media` carry over);
533
+ // a `<script>` entry that imported CSS, or a custom element,
534
+ // gets a freshly synthesized `<link>`.
535
+ if (isStylesheet && tagIsNative) {
536
+ return cloneTagWithUrl(
537
+ originalTag,
538
+ srcStartInTag,
539
+ srcEndInTag,
540
+ url,
541
+ dep.elementKind,
542
+ crossOrigin,
543
+ tagNameEndInTag,
544
+ dep.integrityRange,
545
+ dep.typeValueRange
546
+ );
547
+ }
548
+ return buildStylesheetLink(dep.copyableAttrsText, url, crossOrigin);
549
+ }
550
+ // A JS chunk is loaded via `<script>`. Clone a native `<script>`
551
+ // verbatim; synthesize a real `<script>` for custom elements that
552
+ // were mapped to `type: script`/`script-module`.
553
+ if (tagIsNative) {
554
+ return cloneTagWithUrl(
555
+ originalTag,
556
+ srcStartInTag,
557
+ srcEndInTag,
558
+ url,
559
+ dep.elementKind,
560
+ crossOrigin,
561
+ tagNameEndInTag,
562
+ dep.integrityRange,
563
+ dep.typeValueRange
564
+ );
459
565
  }
460
- return cloneTagWithUrl(
461
- originalTag,
462
- srcStartInTag,
463
- srcEndInTag,
566
+ return buildScriptTag(
567
+ dep.copyableAttrsText,
464
568
  url,
465
- dep.elementKind
569
+ dep.elementKind === "script-module",
570
+ crossOrigin
466
571
  );
467
572
  };
468
573
 
@@ -487,7 +592,7 @@ HtmlScriptSrcDependency.Template = class HtmlScriptSrcDependencyTemplate extends
487
592
  // re-derive the order from the entrypoint's module post-order
488
593
  // index, which mirrors the dependency walk and so reflects the
489
594
  // import order.
490
- /** @type {{ chunk: Chunk, index: number }[]} */
595
+ /** @type {{ chunk: Chunk, index: number, key: string }[]} */
491
596
  const cssChunkOrder = [];
492
597
  /** @type {Chunk[]} */
493
598
  const jsChunks = [];
@@ -498,7 +603,8 @@ HtmlScriptSrcDependency.Template = class HtmlScriptSrcDependencyTemplate extends
498
603
  if (hasCss) {
499
604
  cssChunkOrder.push({
500
605
  chunk,
501
- index: firstCssModulePostOrderIndex(chunk, entrypoint, chunkGraph)
606
+ index: firstCssModulePostOrderIndex(chunk, entrypoint, chunkGraph),
607
+ key: cssChunkSortKey(chunk)
502
608
  });
503
609
  }
504
610
  // Anything that isn't CSS-only stays on the JS lane, in the
@@ -520,7 +626,8 @@ HtmlScriptSrcDependency.Template = class HtmlScriptSrcDependencyTemplate extends
520
626
  entryChunk,
521
627
  entrypoint,
522
628
  chunkGraph
523
- )
629
+ ),
630
+ key: cssChunkSortKey(entryChunk)
524
631
  });
525
632
  }
526
633
  cssChunkOrder.sort((a, b) => {
@@ -533,11 +640,17 @@ HtmlScriptSrcDependency.Template = class HtmlScriptSrcDependencyTemplate extends
533
640
  // including the `Infinity === Infinity` case.
534
641
  if (a.index < b.index) return -1;
535
642
  if (a.index > b.index) return 1;
536
- return compareChunksForCssTieBreak(a.chunk, b.chunk);
643
+ if (a.key < b.key) return -1;
644
+ if (a.key > b.key) return 1;
645
+ return 0;
537
646
  });
538
- for (const { chunk } of cssChunkOrder) {
539
- siblings.push(buildSibling(chunk, "css"));
540
- }
647
+ // Insert CSS before the first script (not just before this tag) so a
648
+ // later entry's stylesheet still loads ahead of every script.
649
+ const cssAt = dep.cssAnchor === -1 ? dep.tagStart : dep.cssAnchor;
650
+ const cssLinks = cssChunkOrder.map(({ chunk }) =>
651
+ buildSibling(chunk, "css")
652
+ );
653
+ if (cssLinks.length > 0) source.insert(cssAt, cssLinks.join(""));
541
654
  for (const chunk of jsChunks) {
542
655
  siblings.push(buildSibling(chunk, "javascript"));
543
656
  }
@@ -550,8 +663,8 @@ HtmlScriptSrcDependency.Template = class HtmlScriptSrcDependencyTemplate extends
550
663
  };
551
664
 
552
665
  makeSerializable(
553
- HtmlScriptSrcDependency,
554
- "webpack/lib/dependencies/HtmlScriptSrcDependency"
666
+ HtmlEntryDependency,
667
+ "webpack/lib/dependencies/HtmlEntryDependency"
555
668
  );
556
669
 
557
- module.exports = HtmlScriptSrcDependency;
670
+ module.exports = HtmlEntryDependency;