GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — fix: emit `<link rel=stylesheet>` for CSS chunks reachable from `<script src>` in HTML entries by alexander-akait · Pull Request #21002 · webpack/webpack Conversation
Member
In the experimental HTML-entry pipeline, a
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions…ipt src>` in HTML entries In the experimental HTML-entry pipeline, a `<script src>` whose bundled JS imports CSS produced a `.css` file on disk that was never referenced from the extracted HTML — `CssLoadingRuntimeModule` marks initial CSS chunks as already loaded and expects the HTML to inject the `<link>` itself, so the stylesheet was effectively lost. With `splitChunks` separating CSS into sibling chunks the symptom escalated to cloned `<script src="style.js">` tags pointing at non-existent JS filenames. CSS sibling chunks (and the entry chunk's own CSS, when present) are now emitted as `<link rel="stylesheet">` tags with the correct `.css` URL, ordered by the entrypoint's module post-order index so the cascade matches source import order — addressing the ordering issue from html-webpack-plugin#1838 / mini-css-extract-plugin#959 for HTML-entry builds. `nonce`/`crossorigin`/`referrerpolicy` are copied from the originating tag onto the emitted `<link>`. …ntry The earlier fix only had per-path coverage (link-only or script-imported-CSS-only). This adds a config case where a single HTML entry uses both at once: a `<link rel="stylesheet">` for one stylesheet and a `<script src>` whose JS imports two more CSS files, one of which chains to a third via a plain CSS `@import`. Verifies that the extracted HTML has one `<link>` per emitted CSS chunk in source order (linked sheet before script-injected sheet, both before the `<script>`), that the `@import`-pulled module merges into the same chunk as its importer rather than producing a stray third `<link>`, and that the cascade `color` outcome matches what the source authored.
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was an error while loading. Please reload this page.
🦋 Changeset detectedLatest commit: 5bd4234 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
This PR is packaged and the instant preview is available (e3ba2ff). Install it locally:
npm i -D webpack@https://pkg.pr.new/webpack@e3ba2ff
yarn add -D webpack@https://pkg.pr.new/webpack@e3ba2ff
pnpm add -D webpack@https://pkg.pr.new/webpack@e3ba2ff |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Codecov Report❌ Patch coverage is
❌ Your patch check has failed because the patch coverage (84.05%) is below the target coverage (90.00%). You can increase the patch coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@ ## main #21002 +/- ## ========================================== + Coverage 90.92% 90.93% +0.01% ========================================== Files 573 573 Lines 58980 59046 +66 Branches 15894 15916 +22 ========================================== + Hits 53629 53695 +66 Misses 5351 5351
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Fixes the experimental HTML-entry pipeline (experiments.html + experiments.css) so that CSS assets reachable from an HTML <script src> entry are actually referenced from the extracted HTML (as <link rel="stylesheet">), with deterministic ordering intended to match CSS import order and with selected security/fetch attributes propagated.
Changes:
HtmlScriptSrcDependency to emit <link rel="stylesheet"> tags for CSS chunks reachable from script-based HTML entries and to order CSS sibling chunks by entrypoint module post-order index.<link> with JS-imported CSS (plus snapshots).Copilot reviewed 27 out of 27 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| lib/dependencies/HtmlScriptSrcDependency.js | Emit stylesheet <link> tags for CSS chunks reachable from <script src> entries; attempt to preserve ordering/attributes. |
| .changeset/html-entry-css-chunks-link-tags.md | Changeset describing the HTML-entry CSS linking fix and ordering/attribute behavior. |
| test/configCases/html/css-imported-from-js/webpack.config.js | New config case: HTML <script src> entry whose bundled JS imports CSS (no splitChunks). |
| test/configCases/html/css-imported-from-js/test.js | Asserts extracted HTML contains both <link rel="stylesheet"> and <script> and referenced assets exist. |
| test/configCases/html/css-imported-from-js/test.config.js | Config-case harness entry pointing to the test asset. |
| test/configCases/html/css-imported-from-js/style.css | CSS fixture for JS-imported CSS test. |
| test/configCases/html/css-imported-from-js/page.html | HTML fixture containing <script src="./entry.js">. |
| test/configCases/html/css-imported-from-js/entry.js | JS fixture importing CSS. |
| test/configCases/html/css-imported-from-js/snapshots/ConfigTest.snap | Snapshot for extracted HTML output of JS-imported CSS test. |
| test/configCases/html/css-split-chunks-order/webpack.config.js | New config case: splitChunks extracts multiple CSS chunks from a script entry to verify <link> emission and ordering. |
| test/configCases/html/css-split-chunks-order/test.js | Asserts two stylesheet links (in import order) and one script tag; validates referenced files exist and ordering relative to script. |
| test/configCases/html/css-split-chunks-order/test.config.js | Config-case harness entry pointing to the test asset. |
| test/configCases/html/css-split-chunks-order/style1.css | CSS fixture 1 (intended to load before style2). |
| test/configCases/html/css-split-chunks-order/style2.css | CSS fixture 2 (intended to override style1). |
| test/configCases/html/css-split-chunks-order/page.html | HTML fixture containing <script src="./entry.js">. |
| test/configCases/html/css-split-chunks-order/entry.js | JS fixture importing the two CSS files in order. |
| test/configCases/html/css-split-chunks-order/snapshots/ConfigTest.snap | Snapshot for extracted HTML output of split CSS chunks ordering test. |
| test/configCases/html/css-mixed-link-and-js-import/webpack.config.js | New config case: authored <link rel="stylesheet"> plus <script src> whose bundle imports additional CSS (including CSS @import). |
| test/configCases/html/css-mixed-link-and-js-import/test.js | Asserts coexistence and ordering of authored link CSS vs script-imported CSS; validates cascade assumptions and file existence. |
| test/configCases/html/css-mixed-link-and-js-import/test.config.js | Config-case harness entry pointing to the test asset. |
| test/configCases/html/css-mixed-link-and-js-import/shared.css | CSS fixture for @import chain. |
| test/configCases/html/css-mixed-link-and-js-import/page.html | HTML fixture containing both <link rel="stylesheet" ...> and <script src="./entry.js">. |
| test/configCases/html/css-mixed-link-and-js-import/linked.css | CSS fixture referenced directly by authored <link>. |
| test/configCases/html/css-mixed-link-and-js-import/imports-shared.css | CSS fixture importing shared.css via CSS @import. |
| test/configCases/html/css-mixed-link-and-js-import/imported.css | CSS fixture imported from JS. |
| test/configCases/html/css-mixed-link-and-js-import/entry.js | JS fixture importing CSS fixtures. |
| test/configCases/html/css-mixed-link-and-js-import/snapshots/ConfigTest.snap | Snapshot for extracted HTML output of mixed link + JS-imported CSS test. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| const chunkHasCss = (chunk, chunkGraph) => | ||
| // Mirrors `CssModulesPlugin.chunkHasCss` (we don't reuse it directly | ||
| // to avoid a hard dep from the dependency template on the css plugin | ||
| // for non-css builds). | ||
| Boolean(chunkGraph.getChunkModulesIterableBySourceType(chunk, CSS_TYPE)); |
| * @returns {boolean} true if the chunk has any JavaScript modules | ||
| */ | ||
| const chunkHasJs = (chunk, chunkGraph) => |
| for (let i = 0; i < orderedChunks.length - 1; i++) { | ||
| const chunk = orderedChunks[i]; | ||
| const hasCss = chunkHasCss(chunk, chunkGraph); | ||
| const hasJs = chunkHasJs(chunk, chunkGraph); | ||
| if (hasCss) { | ||
| cssChunkOrder.push({ | ||
| chunk, | ||
| index: firstCssModulePostOrderIndex(chunk, entrypoint, chunkGraph) | ||
| }); | ||
| } | ||
| if (hasJs) jsChunks.push(chunk); | ||
| if (!hasCss && !hasJs) { | ||
| // Empty chunk (no JS, no CSS — unusual, e.g. wasm-only or | ||
| // asset-only). Fall back to the legacy clone so we at | ||
| // least reference *something*; this preserves prior | ||
| // behavior for users who relied on it. | ||
| otherChunks.push(chunk); |
| ) | ||
| }); | ||
| } | ||
| cssChunkOrder.sort((a, b) => a.index - b.index); |
| } else { | ||
| // CSS chunks are emitted before JS chunks so the cascade is set | ||
| // up before any script runs. Within CSS the order needs to match | ||
| // the source's import order — `entrypoint.chunks` alone doesn't | ||
| // give us that for arbitrary splitChunks layouts (splitChunks | ||
| // inserts each new chunk before the entry chunk via | ||
| // `insertChunk(_, before)`, so split CSS siblings end up in | ||
| // *reverse* of the order they were processed — exactly the | ||
| // html-webpack-plugin#1838 / mini-css-extract#959 symptom). We | ||
| // re-derive the order from the entrypoint's module post-order | ||
| // index, which mirrors the dependency walk and so reflects the | ||
| // import order. |
There was an error while loading. Please reload this page.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
…>`/`<script>` siblings for HTML entries Addresses Copilot review on #21002: 1. `chunkHasJs` now also returns true for runtime chunks (`chunk.hasRuntime()`) and for chunks whose entry modules report a JavaScript source type. `RuntimeModule`s live in a separate `runtimeModules` set and are not surfaced by `getChunkModulesIterableBySourceType(JAVASCRIPT_TYPE)`, so the prior check classified a split-off `optimization.runtimeChunk` as "neither JS nor CSS" and the fallback path emitted it AFTER the chunks that depend on it — yielding `__webpack_require__ is not defined` at load time. With the fix runtime chunks stay in their original `getEntrypointChunksInLoadOrder` slot (first). 2. `chunkHasCss` now matches `CssModulesPlugin.chunkHasCss` exactly — it accepts either `CSS_TYPE` or `CSS_IMPORT_TYPE` modules, so a chunk made up only of `@import` placeholder modules still gets a `<link>` tag. 3. The CSS sibling sort uses a deterministic chunk-name/id tie-break in addition to the post-order index, so `<link>` ordering is stable when two chunks share an index (most commonly when both map to `Infinity` because the entrypoint's walk never reached either's CSS modules). Webpack still supports Node 10.13, which predates V8's guaranteed stable `Array#sort`. 4. Removed the separate `otherChunks` lane: anything that isn't CSS-only stays on the JS lane in `orderedChunks` order, preserving the runtime-first / vendor-before-entry invariant. New regression test `test/configCases/html/css-runtime-and-split-chunks/` covers `optimization.runtimeChunk` + `optimization.splitChunks` + JS-imported CSS in one HTML entry and pins the runtime → vendor → entry script ordering plus the leading `<link>`.
…onfig case Every `test/configCases/**` case is executed twice — once via `ConfigTestCases.basictest.js` (fresh build) and once via `ConfigCacheTestCases.basictest.js` (build from persistent cache). The basic test was failing in CI because the local snapshot-write only captured the first invocation, and `--ci` mode refuses to write the missing cache-mode snapshots.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Copilot reviewed 40 out of 40 changed files in this pull request and generated 1 comment.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| const modules = chunkGraph.getChunkModulesIterableBySourceType( | ||
| chunk, | ||
| CSS_TYPE | ||
| ); | ||
| if (!modules) return min; | ||
| for (const module of modules) { | ||
| const idx = entrypoint.getModulePostOrderIndex(module); | ||
| if (idx !== undefined && idx < min) min = idx; |
…ering key Addresses follow-up Copilot comment on #21002. `chunkHasCss` was already extended to accept either `CSS_TYPE` or `CSS_IMPORT_TYPE`, but `firstCssModulePostOrderIndex` still scanned only `CSS_TYPE`, so a chunk made up exclusively of `@import` placeholder modules (e.g. when splitChunks separates them from their target CSS) collapsed to `Infinity` and fell back on the chunk-name tie-breaker for ordering. The post-order walk now considers both source types, matching the `chunkHasCss` check and giving such chunks their true source-position index for the cascade.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Copilot reviewed 40 out of 40 changed files in this pull request and generated 2 comments.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| const d = a.index - b.index; | ||
| if (d !== 0) return d; |
| const COPYABLE_LINK_ATTRS = ["nonce", "crossorigin", "referrerpolicy"]; | ||
|
|
||
| /** | ||
| * Build a fresh `<link rel="stylesheet" href="…">` for a CSS chunk that | ||
| * was pulled in by a `<script src>` entry — the originating tag was a | ||
| * `<script>`, but the chunk is CSS so cloning the script tag verbatim | ||
| * would produce nonsense (`<script src="…\.css">`). Copy | ||
| * `nonce`/`crossorigin`/`referrerpolicy` from the original element so | ||
| * the same CSP and fetch policy applies; `defer`/`async`/`type` have no | ||
| * meaning on `<link>` and are dropped. | ||
| * @param {string} originalTag the originating `<script>`/`<link>` tag's source text | ||
| * @param {string} href URL for the stylesheet | ||
| * @returns {string} the sibling `<link>` tag's HTML | ||
| */ | ||
| const buildStylesheetLink = (originalTag, href) => { | ||
| let extra = ""; | ||
| for (const attr of COPYABLE_LINK_ATTRS) { | ||
| // Match ` <attr>`, ` <attr>=value`, ` <attr>="value"`, ` <attr>='value'`. | ||
| const re = new RegExp( | ||
| `\\s${attr}(?:\\s*=\\s*(?:"[^"]*"|'[^']*'|[^\\s>]+))?(?=[\\s/>])`, | ||
| "i" | ||
| ); | ||
| const m = originalTag.match(re); | ||
| if (m) extra += m[0]; | ||
| } | ||
| const safeHref = href.replace(/"/g, """); | ||
| return `<link rel="stylesheet" href="${safeHref}"${extra}>`; | ||
| }; |
…ity`, and cover CSP attribute propagation Addresses two follow-up Copilot comments on #21002: 1. `cssChunkOrder.sort` subtracted post-order indices to compute the primary ordering. When two chunks both map to `Number.POSITIVE_INFINITY` (the documented fallback for chunks the entrypoint's walk never reaches), `Infinity - Infinity` is `NaN` and the comparator returned `NaN`, bypassing the `compareChunksForCssTieBreak` fallback. On the Node 10.13 targets this repo still supports, `Array#sort` doesn't promise stable ordering, so emitted `<link>` order became non-deterministic in that case. The comparator now uses explicit `<`/`>` comparisons so the tie-break runs whenever indices match — `Infinity` included. 2. `nonce`/`crossorigin`/`referrerpolicy` propagation from the originating `<script>` onto the emitted `<link rel="stylesheet">` was implemented but had no test coverage. The css-imported-from-js config case's `<script>` tag now carries `nonce`, `crossorigin`, `referrerpolicy`, `defer`, and `integrity` attributes, and the test asserts that all three CSP/fetch attributes end up on the emitted stylesheet `<link>` while `defer`/`integrity` (script-only or content-specific) do not bleed onto it.
Types CoverageCoverage after merging claude/check-html-entry-webpack-2ot8p into main will be
Coverage Report
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Copilot reviewed 40 out of 40 changed files in this pull request and generated no new comments.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
…ipt src>` in HTML entries (webpack#21002)
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.