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 — feat(html): add `module.generator.html.extract` option by alexander-akait · Pull Request #20979 · webpack/webpack Conversation
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
🦋 Changeset detectedLatest commit: 4b3a7e5 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 (45a1bab). Install it locally:
npm i -D webpack@https://pkg.pr.new/webpack@45a1bab
yarn add -D webpack@https://pkg.pr.new/webpack@45a1bab
pnpm add -D webpack@https://pkg.pr.new/webpack@45a1bab |
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.
Adds HTML module extraction support and expands HTML script/modulepreload rewriting so extracted HTML can reference all chunks needed by referenced entries.
Changes:
extract, htmlFilename, and htmlChunkFilename..html assets.Copilot reviewed 39 out of 47 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
.changeset/html-extract-option.md |
Documents the minor feature addition. |
declarations/WebpackOptions.d.ts |
Adds generated option declarations. |
types.d.ts |
Adds generated public typings. |
schemas/WebpackOptions.json |
Adds schema definitions for HTML generator/output options. |
schemas/plugins/HtmlGeneratorOptions.json |
Adds plugin schema reference. |
schemas/plugins/HtmlGeneratorOptions.check.js |
Adds generated validator. |
schemas/plugins/HtmlGeneratorOptions.check.d.ts |
Adds generated validator typing. |
lib/config/defaults.js |
Adds HTML generator/output defaults. |
lib/config/normalization.js |
Normalizes new output fields. |
lib/html/HtmlGenerator.js |
Adds extraction-aware HTML source generation. |
lib/html/HtmlModulesPlugin.js |
Validates options and emits extracted HTML assets. |
lib/html/HtmlParser.js |
Passes tag metadata for script/link chunk expansion. |
lib/dependencies/HtmlScriptSrcDependency.js |
Emits sibling tags for runtime/split chunks. |
test/configCases/html/html-entry-point/* |
Adds HTML entry-point extraction fixture and assertions. |
test/configCases/html/extract/* |
Adds imported HTML extraction fixture and assertions. |
test/configCases/html/extract-split-chunks/* |
Adds split-chunk script reference fixture. |
test/configCases/html/extract-runtime-chunk/* |
Adds runtime-chunk script reference fixture. |
test/configCases/html/extract-custom-filename/* |
Adds custom HTML filename fixture. |
💡 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 generated = this._renderHtml(module, generateContext); | ||
|
|
||
| if (generateContext.type === HTML_TYPE) { | ||
| return new RawSource(generated); |
| chunkGraph, | ||
| contentHash, | ||
| contentHashType: HTML_TYPE, | ||
| filename: sourceFilename |
| switch (dep.elementKind) { | ||
| case "modulepreload": | ||
| siblings.push(`<link rel="modulepreload" href="${url}">`); | ||
| break; | ||
| case "script-module": | ||
| siblings.push(`<script type="module" src="${url}"></script>`); | ||
| break; | ||
| default: | ||
| siblings.push(`<script src="${url}"></script>`); |
| const push = (/** @type {Chunk | null | undefined} */ chunk) => { | ||
| if (!chunk || seen.has(chunk) || chunk === entryChunk) return; | ||
| if (parentEntryChunks.has(chunk)) return; | ||
| seen.add(chunk); | ||
| ordered.push(chunk); | ||
| }; | ||
| if (runtimeChunk !== entryChunk) { | ||
| push(runtimeChunk); | ||
| } |
| const scriptSrcMatches = [ | ||
| ...extracted.matchAll(/<script src="([^"]+)">/g) | ||
| ].map((m) => m[1]); |
| const scriptSrcMatches = [ | ||
| ...extracted.matchAll(/<script src="([^"]+)">/g) | ||
| ].map((m) => m[1]); |
There was an error while loading. Please reload this page.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Add `module.generator.html.extract` (default `false`) and `module.generator.html.filename` (default `[name].html`). When `extract` is on, the parsed and URL-rewritten HTML is emitted as a standalone `.html` output file alongside the module's JavaScript export, in preparation for first-class HTML entry points. Also fix `<script src>` / `<link rel="modulepreload">` references inside HTML modules to load every chunk in the referenced entry's chunk group — including the runtime chunk split off by `optimization.runtimeChunk` and shared chunks created by `optimization.splitChunks` — by inserting sibling tags before the original tag in document order.
Mirror the CSS pipeline: filename templates for extracted `.html` files now live on `output.htmlFilename` and `output.htmlChunkFilename` (defaults derived from `output.filename` / `output.chunkFilename` with `.js` swapped for `.html`) instead of being a per-generator option. `module.generator.html.filename` is dropped in favor of the output-level options. `[contenthash]` in the template is supported via a per-module content hash computed from the rewritten HTML. Adds a `extract-custom-filename` configCase exercising a custom `output.htmlFilename` with `[contenthash]`.
When `module.generator.html.extract` is left unset, default it to `true`
for HTML modules reached as compilation entries (HTML-as-entry-point) and
`false` for HTML modules imported from JavaScript. `extract: true` /
`extract: false` continue to override unconditionally.
HtmlGenerator now receives the module graph and detects entry modules by
the absence of an `originModule` on any of their incoming connections,
matching the EntryDependency shape.
Adds a `html-entry-point` configCase that uses `entry: { page: "./page.html" }`
with no explicit `extract` setting and asserts the URL-rewritten page is
still emitted to disk.
Remove `target`, `node.__dirname`, `node.__filename`, and `externalsPresets.node` from the new extract configCase webpack configs — those options aren't related to what these tests are actually exercising and just add noise.
Copilot review fixes: - Resolve \`[webpack/auto]\` placeholders in extracted HTML against an undo path computed from the emitted \`.html\` filename. Without this, \`output.htmlFilename: \"pages/[name].html\"\` would leave asset/chunk URLs root-relative inside the page, so the browser would resolve them under the subdirectory instead of \`output.path\`. The generator now leaves placeholders in the HTML source type for the plugin to resolve at \`renderManifest\` time. - Forward the compilation \`hash\` into \`getPathWithInfo\` so \`[fullhash]\` / \`[hash]\` work in user-supplied \`output.htmlFilename\` templates. - Preserve safe attributes (\`nonce\`, \`crossorigin\`, \`referrerpolicy\`, \`defer\`, \`async\`, etc.) on sibling tags emitted for additional entry chunks by cloning the original tag's opening source text and only swapping the \`src\`/\`href\` value. \`integrity\` is stripped because it's content-specific to the original entry chunk; copying it would cause the browser to reject the sibling chunks. Sibling tags for \`script-module\` are forced to \`type=\"module\"\` regardless of the original tag's \`type\` attribute. - Skip the parent entrypoint's runtime chunk in addition to its entry chunk when walking \`dependOn\` ancestors. Otherwise, with \`optimization.runtimeChunk: \"single\"\` plus chained HTML entries, every dependant script would re-emit the shared runtime chunk even though the leader's tag already loaded it. - Use a \`RegExp#exec\` loop instead of \`String.prototype.matchAll\` in the runtime-chunk / split-chunks configCases for legacy Node.js compatibility. Rebase fallout: - Defaults snapshot updates (\`htmlFilename\` / \`htmlChunkFilename\` defaults, empty \`module.generator.html\` slot).
270ab4e to
13349e6
Compare
There was an error while loading. Please reload this page.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@ ## main #20979 +/- ## ========================================== + Coverage 90.91% 90.93% +0.01% ========================================== Files 573 573 Lines 58639 58853 +214 Branches 15774 15850 +76 ========================================== + Hits 53312 53516 +204 - Misses 5327 5337 +10
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.
Copilot reviewed 41 out of 49 changed files in this pull request and generated 2 comments.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| compilation.hooks.renderManifest.tap( | ||
| PLUGIN_NAME, | ||
| (result, { chunk, codeGenerationResults, hash: compilationHash }) => { | ||
| const { chunkGraph } = compilation; | ||
| const modules = | ||
| chunkGraph.getOrderedChunkModulesIterableBySourceType( | ||
| chunk, | ||
| HTML_TYPE, | ||
| compareModulesByFullName(compilation.compiler) | ||
| ); |
| "webpack": minor | ||
| --- | ||
|
|
||
| Add `module.generator.html.extract` for HTML modules and the matching `output.htmlFilename` / `output.htmlChunkFilename` filename templates (defaults derived from `output.filename` / `output.chunkFilename` with `.js` swapped for `.html`, mirroring the CSS pipeline). When extraction is on, the parsed and URL-rewritten HTML is emitted as a standalone `.html` output file alongside the module's JavaScript export. |
`module-generator-html-extract`, `output-html-filename`, and `output-html-chunk-filename` are derived from the schema additions and appear in webpack-cli's flag list.
`compilation.hooks.renderManifest` also fires for `HotUpdateChunk`s in HMR mode, where `chunk.canBeInitial()` is `false`. Without this guard we'd emit a stray hot-update `.html` file (using the `output.htmlChunkFilename` template) for every HMR build. Mirror `CssModulesPlugin`'s early-return so extraction only runs for real output chunks. Also extends the `extract-runtime-chunk` configCase with a second classic `<script src>` that chains via `dependOn` to the first, and asserts the shared runtime chunk is loaded exactly once in the extracted HTML — guarding the dependOn parent-runtime-chunk de-duplication path that was added earlier.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Copilot reviewed 43 out of 51 changed files in this pull request and generated 4 comments.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| */ | ||
| updateHash(hash, updateHashContext) { | ||
| hash.update("html"); | ||
| if (this.options.extract) { |
|
|
||
| result.push({ | ||
| render: () => finalSource, | ||
| filename, | ||
| info, | ||
| auxiliary: true, | ||
| identifier: `htmlModule${chunkGraph.getModuleId(module)}`, | ||
| hash: fullContentHash |
| // Emit extracted `.html` files for any HTML module whose | ||
| // generator has `extract: true`. The HTML content is read from | ||
| // the generator's secondary `"html"` source type (see | ||
| // HtmlGenerator#generate). The filename template comes from | ||
| // `output.htmlFilename` (initial chunks) or | ||
| // `output.htmlChunkFilename` (non-initial chunks), mirroring | ||
| // the CSS pipeline. Path data follows the asset-module pattern — |
| F(output, "htmlFilename", () => { | ||
| const filename = | ||
| /** @type {NonNullable<Output["htmlFilename"]>} */ | ||
| (output.filename); | ||
| if (typeof filename !== "function") { | ||
| return filename.replace(/\.[mc]?js(\?|$)/, ".html$1"); | ||
| } | ||
| return "[id].html"; | ||
| }); | ||
| F(output, "htmlChunkFilename", () => { | ||
| const chunkFilename = | ||
| /** @type {NonNullable<Output["htmlChunkFilename"]>} */ | ||
| (output.chunkFilename); | ||
| if (typeof chunkFilename !== "function") { | ||
| return chunkFilename.replace(/\.[mc]?js(\?|$)/, ".html$1"); | ||
| } | ||
| return "[id].html"; |
When `experiments.css` is enabled, `<link rel="stylesheet" href="…">` inside an HTML module is now bundled as a CSS entry chunk instead of being copied through as a plain asset URL. The CSS goes through `CssModulesPlugin`, so `url()` references inside it resolve into asset modules and the rewritten HTML `<link>` points at the emitted `.css` chunk. Implementation notes: - `HtmlParser` upgrades `<link rel="stylesheet">` to an entry (gated on `experiments.css`). The synthetic entry uses dependency category `css-import` to bypass the `dependency: "url"` → asset rule so the default `.css` rule (`type: CSS_MODULE_TYPE_AUTO`) wins. - CSS entries skip the `dependOn` chaining used for script entries — mixing a CSS stylesheet with a JS runtime in the same chunk would produce broken output. - Sibling-tag emission (`HtmlScriptSrcDependency.Template`) recognises the new `stylesheet` element kind so additional chunks of a CSS entry group are emitted as extra `<link>` tags (no `</script>` close). - `HtmlModulesPlugin` collects stylesheet entry names per compilation and, in `afterChunks`, sets each entry chunk's `cssFilenameTemplate` to `output.cssChunkFilename`. Using `cssChunkFilename` (rather than `cssFilename`) guarantees `[id].` is in the template when `output.filename` is a literal like `bundle0.js`, so multiple stylesheet entries don't collide on the same emitted `.css` filename. - The dependency template's `getChunkFilename` defers to `CssModulesPlugin.getChunkFilenameTemplate` for CSS chunks so the `<link href>` URL written into the HTML matches the file that `CssModulesPlugin` emits. Adds: - `ConfigCacheTest.snap` snapshots for the existing `extract`, `extract-runtime-chunk`, `extract-split-chunks`, and `html-entry-point` configCases (they need parallel snapshots for the filesystem-cache run; this is what made the basic CI job fail). - A new `html-entry-point-css` configCase: HTML entry with `<link rel="stylesheet">` and `url()` in the CSS; asserts the link is rewritten to the emitted CSS chunk and the `url()` inside is rewritten to the hashed asset.
Four review comments addressed: - `HtmlGenerator#updateHash` now hashes the *effective* extraction state (`_shouldExtract(module)`) instead of the raw `options.extract`. Under the `extract: undefined` default, extraction toggles on whether the module is a compilation entry — the module's source-type set changes with it, so any cached HTML-type codegen result must be invalidated. - `HtmlModulesPlugin#renderManifest` cache keys now reflect the emitted variant. The asset identifier includes the final filename, and the hash is computed from the post-undo-path final content, so the same HTML module landing in chunks with different `output.htmlFilename`/`htmlChunkFilename` shapes can't reuse one variant's bytes under another variant's URL. - `output.htmlFilename` / `output.htmlChunkFilename` defaults now fall back to `[name].html` when the template derived from `output.filename` / `output.chunkFilename` has no per-module placeholder. With `output.filename: "bundle.js"`, the naive `.js → .html` swap would give `bundle.html` and two extracted HTML modules in the same compilation would collide at emit time. This mirrors the `chunkFilename` uniqueness-injection logic in spirit. - Updated the `renderManifest` opt-in comment to document the actual rule: `extract: true` always extracts, `false` never, and unset extracts iff the HTML module is a compilation entry — instead of the stale "any HTML module whose generator has extract: true". Defaults snapshot updated for the new `htmlFilename` fallback.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Copilot reviewed 56 out of 65 changed files in this pull request and generated 3 comments.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| // stylesheet href stays a plain asset URL. | ||
| const isStylesheetEntry = | ||
| this.css && | ||
| elementName === "link" && |
| */ | ||
| generateError(error, module, generateContext) { | ||
| if (generateContext.type === HTML_TYPE) { | ||
| return new RawSource(`<!-- ${error.message} -->`); |
| const finalContentHash = nonNumericOnlyHash( | ||
| /** @type {string} */ ( | ||
| createHash(outputOptions.hashFunction) | ||
| .update( | ||
| outputOptions.hashSalt | ||
| ? `${outputOptions.hashSalt}|${finalContent}` | ||
| : finalContent | ||
| ) | ||
| .digest(outputOptions.hashDigest) |
Three new review comments addressed:
- `HtmlParser`: scope `<link rel="stylesheet">` → CSS entry promotion
to the `href` attribute only. The parser loop runs per-attribute
(`<link>` also exposes `imagesrcset`), so an `imagesrcset` URL on a
stylesheet link would previously be treated as a CSS entry chunk
instead of a regular asset reference.
- `HtmlGenerator#generateError`: when emitting an error placeholder
into the extracted `.html`, sanitise the message — strip `<`/`>` and
collapse `--` runs — so a crafted error message can't close the HTML
comment with `-->` and inject markup into the served page.
- `HtmlModulesPlugin#renderManifest`: fold `outputOptions.hashSalt` via
a separate `hash.update()` call instead of string-concatenating
`${salt}|${content}`. Matches the salt-then-content scheme used
elsewhere in webpack and avoids edge cases with salts that contain
separators.
Also update `Validation.test.js`'s ecmaVersion inline snapshot to
include the new `htmlChunkFilename?` / `htmlFilename?` output
properties — those tests fail when valid-property lists shift.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Copilot reviewed 57 out of 66 changed files in this pull request and generated 1 comment.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| /** @typedef {import("../Compiler")} Compiler */ | ||
| /** @typedef {{ request: string, entryName: string, kind: "classic" | "esm-script" | "modulepreload" }} EntryScriptInfo */ | ||
|
|
After `HtmlParser` started emitting a fourth `kind: "stylesheet"` entry group (when `<link rel="stylesheet">` is routed through the CSS pipeline under `experiments.css`), the local typedef in `HtmlModulesPlugin` still only listed `classic | esm-script | modulepreload`. The runtime shape of `buildInfo.htmlEntryScripts` already includes the stylesheet group, so the typedef was understating it and tripping JSDoc type hints.
Types CoverageCoverage after merging claude/html-extract-option-9Rwvz into main will be
Coverage Report
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
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.