Skip to content

Commit 5630f96

Browse files
fix(html): drop the JS asset an extracted HTML entry leaves behind
An `.html` file used as an entry emitted a JS chunk holding the page as a string export. Nothing loads it: the emitted page references the `<script src>` entries the parser split out of it, never the entry chunk. The file was dead weight that duplicated the page's markup, one per page, and it took the entrypoint's primary asset slot in stats and the manifest while the page itself was listed as auxiliary. The cleanup pass that already removes the chunk of an `output.html` wrapper entry now also covers a page extracted from a real `.html` entry — the same reasoning, and the same guard: a file another asset still references by URL is kept. A data URL keeps its current meaning, counting as a wrapper only for the `data:text/html` webpack writes itself, and an entry asked for by name through `output.library` keeps its JS. For comparison, a CSS entry has never emitted a JS asset: CssGenerator exposes the JavaScript type only when a JS module imports the stylesheet. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01NuWDbcF2jvq7q1r8aEY5V9
1 parent b2815d2 commit 5630f96

11 files changed

Lines changed: 203 additions & 11 deletions

File tree

‎lib/html/HtmlModulesPlugin.js‎

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -733,24 +733,45 @@ class HtmlModulesPlugin {
733733
// a content-hashed filename makes a substring hit a real reference.
734734
/** @type {Set<string>} */
735735
const deletionCandidates = new Set(inlinedFiles);
736-
// A wrapper entry's chunk only rebuilds the page's own markup; the
737-
// page loads the `<script src>` entries the parser split out of it.
736+
// A page-only entry's chunk rebuilds markup that was already
737+
// written out as the `.html` asset; the page loads the
738+
// `<script src>` entries the parser split out of it, never this
739+
// chunk. That covers the `data:text/html` wrapper entry
740+
// `output.html` adds, and an `.html` file used as an entry, whose
741+
// JS side is just the page as a string export.
738742
for (const entrypoint of compilation.entrypoints.values()) {
739743
const chunk = entrypoint.getEntrypointChunk();
740744
if (!chunk) continue;
741-
let wrapperOnly = false;
745+
let pageOnly = false;
746+
let pageFile = false;
742747
for (const entryModule of compilation.chunkGraph.getChunkEntryModulesIterable(
743748
chunk
744749
)) {
745-
const { resource } = /** @type {import("../NormalModule")} */ (
746-
entryModule
747-
);
748-
wrapperOnly =
749-
typeof resource === "string" &&
750-
HTML_DATA_URL_RE.test(resource);
751-
if (!wrapperOnly) break;
750+
const { resource } = /** @type {NormalModule} */ (entryModule);
751+
// A data URL counts only as the wrapper `output.html`
752+
// writes — another `text/html`-ish media type is an entry
753+
// the user asked for, and keeps its chunk.
754+
const dataUrl =
755+
typeof resource === "string" && resource.startsWith("data:");
756+
const wrapper = dataUrl && HTML_DATA_URL_RE.test(resource);
757+
const extractedPage =
758+
!dataUrl &&
759+
entryModule instanceof HtmlModule &&
760+
entryModule.getSourceTypes().has(HTML_TYPE);
761+
if (extractedPage) pageFile = true;
762+
pageOnly = wrapper || extractedPage;
763+
if (!pageOnly) break;
764+
}
765+
if (!pageOnly) continue;
766+
// A page the user pointed `entry` at can still be asked for by
767+
// name through `output.library` — then its JS is the output.
768+
if (
769+
pageFile &&
770+
(entrypoint.options.library ||
771+
compilation.outputOptions.library)
772+
) {
773+
continue;
752774
}
753-
if (!wrapperOnly) continue;
754775
for (const file of chunk.files) deletionCandidates.add(file);
755776
}
756777
// Materialize each other asset's source once (it can be expensive to
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>HTML entry exposed as a library</title>
5+
<script src="./script.js"></script>
6+
</head>
7+
<body>
8+
</body>
9+
</html>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "html-entry-script";
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
3+
// Assertions live in the emitted `test.js`; the entry's own JS asset is the
4+
// library export, not a test bundle.
5+
module.exports = {
6+
findBundle() {
7+
return ["./test.js"];
8+
}
9+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
4+
it("should keep the JS asset of an HTML entry exposed as a library", () => {
5+
const files = fs.readdirSync(__dirname);
6+
7+
expect(files).toContain("page.html");
8+
expect(files).toContain("page.js");
9+
10+
const html = fs.readFileSync(path.resolve(__dirname, "page.html"), "utf-8");
11+
expect(html).toMatch(/<script src="__html_[a-f0-9]+_0\.chunk\.js">/);
12+
13+
// The library hands back the page markup.
14+
expect(require("./page.js")).toContain("<title>HTML entry exposed as a library</title>");
15+
});
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"use strict";
2+
3+
// An `.html` entry whose JS side is asked for by name through `output.library`.
4+
// The page is still emitted, and the entry's JS asset is kept: the library is
5+
// what the user configured the build to hand back.
6+
7+
const fs = require("fs");
8+
const path = require("path");
9+
const webpack = require("../../../../");
10+
11+
/** @type {import("../../../../").Configuration} */
12+
module.exports = {
13+
entry: {
14+
page: "./page.html"
15+
},
16+
output: {
17+
filename: "[name].js",
18+
chunkFilename: "[name].chunk.js",
19+
library: {
20+
type: "commonjs2"
21+
}
22+
},
23+
optimization: {
24+
chunkIds: "named"
25+
},
26+
experiments: {
27+
html: true
28+
},
29+
plugins: [
30+
{
31+
apply(compiler) {
32+
// Provide a free-standing test runner; the html entry's JS asset
33+
// exports the page markup and carries no `it()` calls of its own.
34+
compiler.hooks.compilation.tap("Test", (compilation) => {
35+
compilation.hooks.processAssets.tap(
36+
{
37+
name: "copy-test",
38+
stage:
39+
compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL
40+
},
41+
() => {
42+
const data = fs.readFileSync(path.resolve(__dirname, "test.js"));
43+
compilation.emitAsset(
44+
"test.js",
45+
new webpack.sources.RawSource(data)
46+
);
47+
}
48+
);
49+
});
50+
}
51+
}
52+
]
53+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>HTML entry without a JS asset</title>
5+
<script src="./script.js"></script>
6+
</head>
7+
<body>
8+
</body>
9+
</html>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "html-entry-script";
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
3+
// The HTML entry has no JS asset at all — assertions live in the emitted
4+
// `test.js`, which the harness loads directly.
5+
module.exports = {
6+
findBundle() {
7+
return ["./test.js"];
8+
}
9+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
4+
it("should not emit a JS asset for an HTML entry", () => {
5+
const files = fs.readdirSync(__dirname);
6+
7+
expect(files).toContain("page.html");
8+
expect(files).not.toContain("page.js");
9+
10+
const html = fs.readFileSync(path.resolve(__dirname, "page.html"), "utf-8");
11+
// The page loads the script the parser split out of it, not the entry chunk.
12+
expect(html).toMatch(/<script src="__html_[a-f0-9]+_0\.chunk\.js">/);
13+
expect(html).not.toContain("page.js");
14+
});

0 commit comments

Comments
 (0)