Skip to content

Commit 57f8dcf

Browse files
authored
test(html): cover parser tree-construction edge cases and generator fallbacks (#21631)
* test(html): cover CDATA sections, foreign-content break-out and template insertion-mode reset Eleven tree-construction and tokenizer lines codecov reports as never run, reached through a real build: the three CDATA section-end paths and EOF in one, the `</p>` break-out and any-other-end-tag walk in foreign content (with the MathML/SVG arms of isSpecial), and resetting the insertion mode to the cell or caption a template closes in. * test(html): cover the foster-parented text insert with a non-text previous sibling * test(html): cover the HMR concatenation bailout and the inline chunk-hash fallback * test(html): keep the foster fixture on round-trippable input and drop regexes from the inline assertions * test(html): assert the HMR concatenation bailout reason instead of only the rendered content
1 parent 57f2f2e commit 57f8dcf

26 files changed

Lines changed: 372 additions & 0 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import html from "./page.html";
2+
3+
it("should report why the HTML module cannot be concatenated", () => {
4+
const module = __STATS__.modules.find((m) => m.name.includes("page.html"));
5+
expect(module.optimizationBailout).toContainEqual(
6+
expect.stringContaining("HTML module needs its own module scope for HMR")
7+
);
8+
});
9+
10+
it("should still render the HTML module's content", () => {
11+
expect(html).toContain("<p>hmr page</p>");
12+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<!DOCTYPE html>
2+
<html><body><p>hmr page</p></body></html>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"use strict";
2+
3+
const { HotModuleReplacementPlugin } = require("../../../../");
4+
5+
/** @type {import("../../../../").Configuration} */
6+
module.exports = {
7+
target: "web",
8+
mode: "production",
9+
module: {
10+
rules: [
11+
{
12+
test: /\.html$/,
13+
type: "html"
14+
}
15+
]
16+
},
17+
optimization: {
18+
minimize: false,
19+
// HMR wires self-accept against a per-module `module.hot`, so an HTML
20+
// module merged into a shared scope would patch the wrong module id —
21+
// the generator has to refuse concatenation for it.
22+
concatenateModules: true
23+
},
24+
plugins: [new HotModuleReplacementPlugin()],
25+
stats: {
26+
optimizationBailout: true
27+
},
28+
experiments: {
29+
html: true
30+
}
31+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import "./style.css";
2+
3+
console.log("inline chunk hash");
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
body { color: red; }
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
3+
module.exports = {
4+
// The entry bundle is inlined into the page, so the assertions run from an
5+
// asset emitted beside it.
6+
findBundle() {
7+
return ["test.js"];
8+
}
9+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
4+
it("should resolve an inlined chunk through the chunk hash when no content hash is set", () => {
5+
const html = fs
6+
.readFileSync(path.resolve(__dirname, "main.html"))
7+
.toString("utf-8");
8+
// Every sentinel resolved: none is left in the emitted page.
9+
expect(html).not.toContain("__WEBPACK_HTML_INLINE__");
10+
expect(html).toContain("<style>");
11+
expect(html).toContain("body { color: red; }");
12+
expect(html).toContain("<script>");
13+
});
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"use strict";
2+
3+
const fs = require("fs");
4+
const path = require("path");
5+
const webpack = require("../../../../");
6+
7+
/** @type {import("../../../../").WebpackPluginInstance} */
8+
const copyTest = {
9+
apply(compiler) {
10+
compiler.hooks.compilation.tap("Test", (compilation) => {
11+
compilation.hooks.processAssets.tap(
12+
{
13+
name: "copy-test",
14+
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL
15+
},
16+
() => {
17+
compilation.emitAsset(
18+
"test.js",
19+
new webpack.sources.RawSource(
20+
fs.readFileSync(path.resolve(__dirname, "test.js"))
21+
)
22+
);
23+
}
24+
);
25+
});
26+
}
27+
};
28+
29+
/** @type {import("../../../../").Configuration} */
30+
module.exports = {
31+
target: "web",
32+
mode: "development",
33+
entry: { main: "./src.js" },
34+
output: {
35+
filename: "[name].js",
36+
htmlFilename: "main.html",
37+
// Inlining resolves each chunk through a sentinel carrying its hash; in
38+
// development the chunk carries no content hash, so the fallback runs.
39+
html: { inline: true }
40+
},
41+
experiments: {
42+
html: true,
43+
css: true
44+
},
45+
plugins: [copyTest]
46+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`ConfigCacheTestCases html parser-foreign-content parser-foreign-content should compile 1`] = `
4+
"<!doctype html><html><body>
5+
<svg>a]b</svg>
6+
<svg>c]</svg>
7+
<svg>d]]e</svg>
8+
<svg><circle/></svg><p></p>after-p
9+
<div><svg><g>after-end-tag</g></svg></div>
10+
<svg>unterminated
11+
</svg></body></html>"
12+
`;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`ConfigTestCases html parser-foreign-content parser-foreign-content should compile 1`] = `
4+
"<!doctype html><html><body>
5+
<svg>a]b</svg>
6+
<svg>c]</svg>
7+
<svg>d]]e</svg>
8+
<svg><circle/></svg><p></p>after-p
9+
<div><svg><g>after-end-tag</g></svg></div>
10+
<svg>unterminated
11+
</svg></body></html>"
12+
`;

0 commit comments

Comments
 (0)