Skip to content

Commit 04c47e8

Browse files
fix(html): end a run at an ignored script, and keep page names apart
Three findings from review, and one snapshot CI could not reproduce. A `<!-- webpackIgnore: true -->` script returns before the run boundary is read, so a run spanned it — and it stays in the document, running where it stands, while the bodies around it merged into one chunk that runs earlier. It ends a run now, like every other `<script>`. Two pages whose paths differ only by a separator sanitized to one entry name: `sub/page.html` and `sub-page.html` both read `sub-page`, which `Compilation.addEntry` merges, so each page would load the other's code. The mapping is reversible now — a `-` in the path doubles, and anything else outside a name is written as its code point. `output.html.inline`'s pattern form is caller-supplied, so a global one carries `lastIndex` from chunk to chunk and intermittently misses; it is reset before each test. Its schema text now says the pattern matches the filename template, which is what it reads. The inline-script chunk snapshot went to the run's leader, which carries the runtime — whose `hasOwnProperty` shorthand follows the Node the case runs under, so it differed on the 12.x and 14.x jobs. That chunk's content is asserted instead.
1 parent 7fdc588 commit 04c47e8

13 files changed

Lines changed: 108 additions & 258 deletions

File tree

‎lib/html/HtmlGenerator.js‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -609,14 +609,17 @@ class HtmlGenerator extends Generator {
609609
if (inlineOption === "script") return contentType === "javascript";
610610
if (inlineOption === "style") return contentType === "css";
611611
// An extracted inline `<script>` is named after its page rather than its
612-
// entry, so the name it emits under is matched as well.
612+
// entry, so the template it emits under is matched as well. A pattern the
613+
// caller made global carries `lastIndex` between chunks, so it is reset.
613614
const name = chunk.name || String(chunk.id);
614615
const { filenameTemplate } = chunk;
615-
return inlineOption.some(
616-
(re) =>
617-
re.test(name) ||
618-
(typeof filenameTemplate === "string" && re.test(filenameTemplate))
619-
);
616+
return inlineOption.some((re) => {
617+
re.lastIndex = 0;
618+
if (re.test(name)) return true;
619+
if (typeof filenameTemplate !== "string") return false;
620+
re.lastIndex = 0;
621+
return re.test(filenameTemplate);
622+
});
620623
}
621624

622625
/**

‎lib/html/HtmlParser.js‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,9 @@ const isEmptyRunGap = (source, start, end) => {
186186
return true;
187187
};
188188

189-
// Everything an entry name keeps from a page's path; the rest becomes one `-`.
190-
const ENTRY_NAME_SEPARATOR_REGEXP = /[^\w.-]+/g;
189+
// What an entry name keeps from a page's path. The mapping is reversible, so
190+
// two pages of one compilation can never sanitize to the same name.
191+
const ENTRY_NAME_KEPT_REGEXP = /[^\w.]/g;
191192
const ENTRY_NAME_EXTENSION_REGEXP = /\.[^./\\]*$/;
192193
const ENTRY_NAME_TRIM_REGEXP = /^[-.]+|[-.]+$/g;
193194

@@ -209,7 +210,11 @@ const entryNamePrefix = (pagePath, hashFunction) => {
209210
}
210211
return pagePath
211212
.replace(ENTRY_NAME_EXTENSION_REGEXP, "")
212-
.replace(ENTRY_NAME_SEPARATOR_REGEXP, "-")
213+
.replace(ENTRY_NAME_KEPT_REGEXP, (character) => {
214+
if (character === "-") return "--";
215+
if (character === "/" || character === "\\") return "-";
216+
return `_${character.charCodeAt(0).toString(16)}_`;
217+
})
213218
.replace(ENTRY_NAME_TRIM_REGEXP, "");
214219
};
215220

@@ -1371,6 +1376,12 @@ class HtmlParser extends Parser {
13711376
}
13721377

13731378
if (ignore) {
1379+
// It stays in the document and runs where it stands, so a run
1380+
// may not span it either.
1381+
if (path.tagName() === "script") {
1382+
inlineRunEntryName = undefined;
1383+
inlineRunLeaderDependency = undefined;
1384+
}
13741385
return;
13751386
}
13761387

‎schemas/WebpackOptions.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5416,7 +5416,7 @@
54165416
"added": "5.109.0"
54175417
},
54185418
"inline": {
5419-
"description": "Inline the content of matching chunks directly into the HTML instead of emitting a separate `<script>`/`<link>` tag. `true` inlines every chunk; `\"script\"` inlines only JavaScript, `\"style\"` only CSS; an array of `RegExp` patterns matches against the chunk name and the name it is emitted under. A chunk another entry loads its runtime from by url stays a separate file.",
5419+
"description": "Inline the content of matching chunks directly into the HTML instead of emitting a separate `<script>`/`<link>` tag. `true` inlines every chunk; `\"script\"` inlines only JavaScript, `\"style\"` only CSS; an array of `RegExp` patterns matches against the chunk name and the filename template it is emitted under. A chunk another entry loads its runtime from by url stays a separate file.",
54205420
"anyOf": [
54215421
{
54225422
"type": "array",

‎test/__snapshots__/Cli.basictest.js.snap‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14247,7 +14247,7 @@ Object {
1424714247
"type": "RegExp",
1424814248
},
1424914249
Object {
14250-
"description": "Inline the content of matching chunks directly into the HTML instead of emitting a separate \`<script>\`/\`<link>\` tag. \`true\` inlines every chunk; \`\\"script\\"\` inlines only JavaScript, \`\\"style\\"\` only CSS; an array of \`RegExp\` patterns matches against the chunk name and the name it is emitted under. A chunk another entry loads its runtime from by url stays a separate file.",
14250+
"description": "Inline the content of matching chunks directly into the HTML instead of emitting a separate \`<script>\`/\`<link>\` tag. \`true\` inlines every chunk; \`\\"script\\"\` inlines only JavaScript, \`\\"style\\"\` only CSS; an array of \`RegExp\` patterns matches against the chunk name and the filename template it is emitted under. A chunk another entry loads its runtime from by url stays a separate file.",
1425114251
"multiple": false,
1425214252
"path": "output.html.inline",
1425314253
"type": "enum",
@@ -14257,26 +14257,26 @@ Object {
1425714257
],
1425814258
},
1425914259
Object {
14260-
"description": "Inline the content of matching chunks directly into the HTML instead of emitting a separate \`<script>\`/\`<link>\` tag. \`true\` inlines every chunk; \`\\"script\\"\` inlines only JavaScript, \`\\"style\\"\` only CSS; an array of \`RegExp\` patterns matches against the chunk name and the name it is emitted under. A chunk another entry loads its runtime from by url stays a separate file.",
14260+
"description": "Inline the content of matching chunks directly into the HTML instead of emitting a separate \`<script>\`/\`<link>\` tag. \`true\` inlines every chunk; \`\\"script\\"\` inlines only JavaScript, \`\\"style\\"\` only CSS; an array of \`RegExp\` patterns matches against the chunk name and the filename template it is emitted under. A chunk another entry loads its runtime from by url stays a separate file.",
1426114261
"multiple": false,
1426214262
"path": "output.html.inline",
1426314263
"type": "boolean",
1426414264
},
1426514265
],
14266-
"description": "A regular expression matched against the chunk name; matching chunks are inlined. Inline the content of matching chunks directly into the HTML instead of emitting a separate \`<script>\`/\`<link>\` tag. \`true\` inlines every chunk; \`\\"script\\"\` inlines only JavaScript, \`\\"style\\"\` only CSS; an array of \`RegExp\` patterns matches against the chunk name and the name it is emitted under. A chunk another entry loads its runtime from by url stays a separate file.",
14266+
"description": "A regular expression matched against the chunk name; matching chunks are inlined. Inline the content of matching chunks directly into the HTML instead of emitting a separate \`<script>\`/\`<link>\` tag. \`true\` inlines every chunk; \`\\"script\\"\` inlines only JavaScript, \`\\"style\\"\` only CSS; an array of \`RegExp\` patterns matches against the chunk name and the filename template it is emitted under. A chunk another entry loads its runtime from by url stays a separate file.",
1426714267
"multiple": true,
1426814268
"simpleType": "string",
1426914269
},
1427014270
"output-html-inline-reset": Object {
1427114271
"configs": Array [
1427214272
Object {
14273-
"description": "Clear all items provided in 'output.html.inline' configuration. Inline the content of matching chunks directly into the HTML instead of emitting a separate \`<script>\`/\`<link>\` tag. \`true\` inlines every chunk; \`\\"script\\"\` inlines only JavaScript, \`\\"style\\"\` only CSS; an array of \`RegExp\` patterns matches against the chunk name and the name it is emitted under. A chunk another entry loads its runtime from by url stays a separate file.",
14273+
"description": "Clear all items provided in 'output.html.inline' configuration. Inline the content of matching chunks directly into the HTML instead of emitting a separate \`<script>\`/\`<link>\` tag. \`true\` inlines every chunk; \`\\"script\\"\` inlines only JavaScript, \`\\"style\\"\` only CSS; an array of \`RegExp\` patterns matches against the chunk name and the filename template it is emitted under. A chunk another entry loads its runtime from by url stays a separate file.",
1427414274
"multiple": false,
1427514275
"path": "output.html.inline",
1427614276
"type": "reset",
1427714277
},
1427814278
],
14279-
"description": "Clear all items provided in 'output.html.inline' configuration. Inline the content of matching chunks directly into the HTML instead of emitting a separate \`<script>\`/\`<link>\` tag. \`true\` inlines every chunk; \`\\"script\\"\` inlines only JavaScript, \`\\"style\\"\` only CSS; an array of \`RegExp\` patterns matches against the chunk name and the name it is emitted under. A chunk another entry loads its runtime from by url stays a separate file.",
14279+
"description": "Clear all items provided in 'output.html.inline' configuration. Inline the content of matching chunks directly into the HTML instead of emitting a separate \`<script>\`/\`<link>\` tag. \`true\` inlines every chunk; \`\\"script\\"\` inlines only JavaScript, \`\\"style\\"\` only CSS; an array of \`RegExp\` patterns matches against the chunk name and the filename template it is emitted under. A chunk another entry loads its runtime from by url stays a separate file.",
1428014280
"multiple": false,
1428114281
"simpleType": "boolean",
1428214282
},

‎test/configCases/html/inline-module-merge/__snapshots__/ConfigCacheTest.snap‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,15 @@ exports[`ConfigCacheTestCases html inline-module-merge exported tests should dro
3333
<script src=\\"unclosed.mjs\\" type=\\"module\\"></script>
3434
"
3535
`;
36+
37+
exports[`ConfigCacheTestCases html inline-module-merge exported tests should end a run at an ignored script 1`] = `
38+
"<!DOCTYPE html>
39+
<title>Ignored</title>
40+
<script src=\\"ignored.mjs\\" type=\\"module\\"></script>
41+
<!-- webpackIgnore: true -->
42+
<script type=\\"module\\">
43+
globalThis.__ignoredBody = \\"stays inline\\";
44+
</script>
45+
<script src=\\"ignored1.mjs\\" type=\\"module\\"></script>
46+
"
47+
`;

‎test/configCases/html/inline-module-merge/__snapshots__/ConfigTest.snap‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,15 @@ exports[`ConfigTestCases html inline-module-merge exported tests should drop a r
3333
<script src=\\"unclosed.mjs\\" type=\\"module\\"></script>
3434
"
3535
`;
36+
37+
exports[`ConfigTestCases html inline-module-merge exported tests should end a run at an ignored script 1`] = `
38+
"<!DOCTYPE html>
39+
<title>Ignored</title>
40+
<script src=\\"ignored.mjs\\" type=\\"module\\"></script>
41+
<!-- webpackIgnore: true -->
42+
<script type=\\"module\\">
43+
globalThis.__ignoredBody = \\"stays inline\\";
44+
</script>
45+
<script src=\\"ignored1.mjs\\" type=\\"module\\"></script>
46+
"
47+
`;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<title>Ignored</title>
3+
<script type="module">
4+
import { push } from "./shared.js";
5+
push("before-ignored");
6+
</script>
7+
<!-- webpackIgnore: true -->
8+
<script type="module">
9+
globalThis.__ignoredBody = "stays inline";
10+
</script>
11+
<script type="module">
12+
import { push } from "./shared.js";
13+
push("after-ignored");
14+
</script>

‎test/configCases/html/inline-module-merge/index.js‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import page from "./page.html";
66
import unclosed from "./unclosed.html";
77
import subPage from "./sub/page.html";
88
import classicPage from "./classic.html";
9+
import ignoredPage from "./ignored.html";
10+
import dashPage from "./sub-page.html";
911

1012
const here = path.dirname(fileURLToPath(import.meta.url));
1113

@@ -26,6 +28,8 @@ const pageContent = typeof page === "string" ? page : "";
2628
const unclosedContent = typeof unclosed === "string" ? unclosed : "";
2729
const subContent = typeof subPage === "string" ? subPage : "";
2830
const classicContent = typeof classicPage === "string" ? classicPage : "";
31+
const ignoredContent = typeof ignoredPage === "string" ? ignoredPage : "";
32+
const dashContent = typeof dashPage === "string" ? dashPage : "";
2933

3034
// Document-order list of every inline-script chunk url left in the page.
3135
const scriptChunkUrls = collectMatches(
@@ -118,3 +122,30 @@ it("should bundle a run of classic inline scripts into one chunk", () => {
118122
classicChunk.indexOf("classic-2")
119123
);
120124
});
125+
126+
it("should end a run at an ignored script", () => {
127+
expect(ignoredContent).toMatchSnapshot();
128+
129+
// The ignored tag stays in the document and runs where it stands, so the
130+
// bodies on either side of it cannot share a chunk.
131+
expect(ignoredContent).toContain("stays inline");
132+
const urls = collectMatches(
133+
ignoredContent,
134+
/<script[^>]*\bsrc="(ignored\d*\.mjs)"/g
135+
);
136+
expect(urls).toHaveLength(2);
137+
expect(readChunk(urls[0])).toContain("before-ignored");
138+
expect(readChunk(urls[1])).toContain("after-ignored");
139+
});
140+
141+
it("should keep pages apart whose paths differ only by a separator", () => {
142+
// `sub/page.html` and `sub-page.html` must not sanitize to one entry name,
143+
// or each page would load the other's code.
144+
const dashUrl = dashContent.match(/<script[^>]*\bsrc="([\w.-]+\.mjs)"/)[1];
145+
const subUrl = subContent.match(/<script[^>]*\bsrc="([\w.-]+\.mjs)"/)[1];
146+
expect(dashUrl).not.toBe(subUrl);
147+
expect(readChunk(dashUrl)).toContain("dash-named-page");
148+
expect(readChunk(dashUrl)).not.toContain("sub-module");
149+
expect(readChunk(subUrl)).toContain("sub-module");
150+
expect(readChunk(subUrl)).not.toContain("dash-named-page");
151+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!DOCTYPE html>
2+
<title>Sibling of sub/page.html</title>
3+
<script type="module">
4+
import { push } from "./shared.js";
5+
push("dash-named-page");
6+
</script>

‎test/configCases/html/inline-script/__snapshots__/ConfigCacheTest.snap‎

Lines changed: 0 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,5 @@
11
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

3-
exports[`ConfigCacheTestCases html inline-script exported tests should bundle inline <script type=module> as an ES-module chunk 1`] = `
4-
"/******/ var __webpack_modules__ = ({});
5-
/************************************************************************/
6-
/******/ // The module cache
7-
/******/ const __webpack_module_cache__ = {};
8-
/******/
9-
/******/ // The require function
10-
/******/ function __webpack_require__(moduleId) {
11-
/******/ // Check if module is in cache
12-
/******/ const cachedModule = __webpack_module_cache__[moduleId];
13-
/******/ if (cachedModule !== undefined) {
14-
/******/ return cachedModule.exports;
15-
/******/ }
16-
/******/ // Create a new module (and put it into the cache)
17-
/******/ const module = __webpack_module_cache__[moduleId] = {
18-
/******/ // no module.id needed
19-
/******/ // no module.loaded needed
20-
/******/ exports: {}
21-
/******/ };
22-
/******/
23-
/******/ // Execute the module function
24-
/******/ if (!(moduleId in __webpack_modules__)) {
25-
/******/ delete __webpack_module_cache__[moduleId];
26-
/******/ const e = new Error(\\"Cannot find module '\\" + moduleId + \\"'\\");
27-
/******/ e.code = 'MODULE_NOT_FOUND';
28-
/******/ throw e;
29-
/******/ }
30-
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
31-
/******/
32-
/******/ // Return the exports of the module
33-
/******/ return module.exports;
34-
/******/ }
35-
/******/
36-
/******/ // expose the modules object (__webpack_modules__)
37-
/******/ __webpack_require__.m = __webpack_modules__;
38-
/******/
39-
/************************************************************************/
40-
/******/ /* webpack/runtime/hasOwnProperty shorthand */
41-
/******/ __webpack_require__.o = (obj, prop) => (Object.hasOwn(obj, prop));
42-
/******/
43-
/******/ /* webpack/runtime/export webpack runtime */
44-
/******/ export { __webpack_require__ };
45-
/******/
46-
/******/ /* webpack/runtime/import chunk loading */
47-
/******/ (() => {
48-
/******/ // no baseURI
49-
/******/
50-
/******/ // object to store loaded and loading chunks
51-
/******/ // undefined = chunk not loaded, null = chunk preloaded/prefetched
52-
/******/ // [resolve, Promise] = chunk loading, 0 = chunk loaded
53-
/******/ const installedChunks = {
54-
/******/ \\"page-0\\": 0
55-
/******/ };
56-
/******/
57-
/******/ const installChunk = (data) => {
58-
/******/ let {__webpack_esm_ids__, __webpack_esm_modules__, __webpack_esm_runtime__} = data;
59-
/******/ // add \\"modules\\" to the modules object,
60-
/******/ // then flag all \\"ids\\" as loaded and fire callback
61-
/******/ var moduleId, chunkId, i = 0;
62-
/******/ for(moduleId in __webpack_esm_modules__) {
63-
/******/ if(__webpack_require__.o(__webpack_esm_modules__, moduleId)) {
64-
/******/ __webpack_require__.m[moduleId] = __webpack_esm_modules__[moduleId];
65-
/******/ }
66-
/******/ }
67-
/******/ if(__webpack_esm_runtime__) __webpack_esm_runtime__(__webpack_require__);
68-
/******/ for(;i < __webpack_esm_ids__.length; i++) {
69-
/******/ chunkId = __webpack_esm_ids__[i];
70-
/******/ if(__webpack_require__.o(installedChunks, chunkId) && installedChunks[chunkId]) {
71-
/******/ installedChunks[chunkId][0]();
72-
/******/ }
73-
/******/ installedChunks[chunkId] = 0;
74-
/******/ }
75-
/******/
76-
/******/ }
77-
/******/
78-
/******/ // no chunk on demand loading
79-
/******/
80-
/******/ // no prefetching
81-
/******/
82-
/******/ // no preloaded
83-
/******/
84-
/******/ __webpack_require__.C = installChunk;
85-
/******/
86-
/******/ // no on chunks loaded
87-
/******/ // no HMR
88-
/******/
89-
/******/ // no HMR manifest
90-
/******/ })();
91-
/******/
92-
/************************************************************************/
93-
/*!********************************************************************************************************!*\\\\
94-
!*** data:text/javascript;base64,CnZhciBncmVldGluZyA9ICJoaSI7CmNvbnNvbGUubG9nKCI8Yj5oZWxsbzwvYj4iKTsK ***!
95-
\\\\********************************************************************************************************/
96-
97-
var greeting = \\"hi\\";
98-
console.log(\\"<b>hello</b>\\");
99-
100-
/*!****************************************************************************************************************************************************************************************************************************************************************************************************************!*\\\\
101-
!*** data:text/javascript;base64,CmNvbnN0IGFyciA9IFsxLCAyLCAzXTsKY29uc3Qgc3VtID0gYXJyLnJlZHVjZSgoYSwgYikgPT4gYSArIGIsIDApOwp3aW5kb3cuX19pbmxpbmVNb2R1bGVTdW0gPSBzdW07Ci8vIE5vbi1BU0NJSSBzb3VyY2Ugc3Vydml2ZXMgdGhlIGRhdGEgVVJJIHJvdW5kLXRyaXAuCndpbmRvdy5fX2lubGluZU1vZHVsZVVuaWNvZGUgPSAi5pel5pys6KqeIM+AIjsK ***!
102-
\\\\****************************************************************************************************************************************************************************************************************************************************************************************************************/
103-
104-
const arr = [1, 2, 3];
105-
const sum = arr.reduce((a, b) => a + b, 0);
106-
window.__inlineModuleSum = sum;
107-
// Non-ASCII source survives the data URI round-trip.
108-
window.__inlineModuleUnicode = \\"日本語 π\\";
109-
110-
/*!********************************************************************************************************************************************************************************************************************************************************************!*\\\\
111-
!*** data:text/javascript;base64,Ci8vIGA8YCBjaGFyYWN0ZXJzIGluc2lkZSBKUyBtdXN0IE5PVCBiZSByZXBhcnNlZCBhcyBIVE1MLgp2YXIgY291bnRlciA9IDA7CmZ1bmN0aW9uIGx0KGEsIGIpIHsgcmV0dXJuIGEgPCBiOyB9CnZhciBodG1sID0gIjxkaXY+PHNwYW4+bmVzdGVkPC9zcGFuPjwvZGl2PiI7CmNvdW50ZXIrKzsK ***!
112-
\\\\********************************************************************************************************************************************************************************************************************************************************************/
113-
114-
// \`<\` characters inside JS must NOT be reparsed as HTML.
115-
var counter = 0;
116-
function lt(a, b) { return a < b; }
117-
var html = \\"<div><span>nested</span></div>\\";
118-
counter++;
119-
120-
"
121-
`;
122-
1233
exports[`ConfigCacheTestCases html inline-script exported tests should bundle inline <script> bodies as entry chunks and rewrite their tags to \`<script src>\` 1`] = `
1244
"<!DOCTYPE html>
1255
<html>

0 commit comments

Comments
 (0)