Skip to content

Commit cc7e10c

Browse files
alexander-akaitclaude
authored andcommitted
fix(html): regenerate the module header above an inline style body
`output.pathinfo`, which development turns on, heads a sheet with the module it was read as. A `<style>` body is read back on the next build of the page, so that header came in as part of the sheet and the new one went above it: the page grew a header per pass, each restating in base64 the body plus every header before it. The body sheds the header it arrives with, so the module this build reads is the sheet the author wrote and the header it writes is the only one. A page already carrying a stack of them sheds the stack. Only a header webpack wrote is taken: the shape is `generateHeader`'s, it has to be at the front, and an author's own `/*!` banner is left alone.
1 parent 9e82414 commit cc7e10c

9 files changed

Lines changed: 162 additions & 5 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Regenerate the module header above an inline `<style>` sheet instead of heading
6+
the one a previous build wrote, so rebuilding a page webpack emitted keeps one
7+
header per sheet rather than a stack of them.

‎lib/html/HtmlParser.js‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const WebpackError = require("../errors/WebpackError");
1515
const Parser = require("../module/Parser");
1616
const ResourceHintPlugin = require("../prefetch/ResourceHintPlugin");
1717
const parseResourceHintOptions = require("../prefetch/parseResourceHintOptions");
18+
const ModuleInfoHeaderPlugin = require("../template/ModuleInfoHeaderPlugin");
1819
const LazySet = require("../util/LazySet");
1920
const LocConverter = require("../util/LocConverter");
2021
const createHash = require("../util/createHash");
@@ -1620,11 +1621,16 @@ class HtmlParser extends Parser {
16201621
// to be decoded going in and escaped coming back out, which is
16211622
// what `characterData` tells the write-back.
16221623
const characterData = content && path.namespace() !== NS_HTML;
1623-
const cssText = content
1624-
? characterData
1625-
? decodeEntities(value, false)
1626-
: value
1627-
: decodeEntities(value, true);
1624+
// A page webpack emitted is one it can be pointed at again,
1625+
// where a sheet arrives headed by the module the last build
1626+
// read it as. That header was that build's; this writes its own.
1627+
const cssText = ModuleInfoHeaderPlugin.stripHeaders(
1628+
content
1629+
? characterData
1630+
? decodeEntities(value, false)
1631+
: value
1632+
: decodeEntities(value, true)
1633+
);
16281634
if (cssText.trim() === "") break;
16291635
const dep = new HtmlInlineStyleDependency(
16301636
dataUri("text/css", cssText),

‎lib/template/ModuleInfoHeaderPlugin.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ const printExportsInfoToSource = (
146146
}
147147
};
148148

149+
// `generateHeader`'s shape, as it reads back. A header names the module of the
150+
// build that wrote it, so a source webpack reads its own output from sheds the
151+
// one it arrives with and is headed again — see `stripHeaders`.
152+
const HEADER_REGEXP =
153+
/^\/\*!\*+!\*\\\r?\n {2}!\*\*\* .* \*\*\*!\r?\n {2}\\\*+\/\r?\n/;
154+
149155
/** @typedef {{ header: RawSource | undefined, full: WeakMap<Source, CachedSource> }} CacheEntry */
150156
/** @type {WeakMap<RequestShortener, WeakMap<Module, CacheEntry>>} */
151157
const caches = new WeakMap();
@@ -309,6 +315,23 @@ class ModuleInfoHeaderPlugin {
309315
* @param {RequestShortener} requestShortener request shortener
310316
* @returns {RawSource} the header
311317
*/
318+
/**
319+
* Drop the headers a build wrote at the top of a source that is being read
320+
* back in, so this build's own header heads the module rather than the last
321+
* build's — a page whose inline `<style>` webpack rewrites is read again on
322+
* every rebuild, and a header left in place would be headed once more.
323+
* @param {string} source source that may have been written by a build
324+
* @returns {string} the source with any header of ours taken off the front
325+
*/
326+
static stripHeaders(source) {
327+
let stripped = source;
328+
// A page built before this stripped may carry a stack of them.
329+
while (HEADER_REGEXP.test(stripped)) {
330+
stripped = stripped.replace(HEADER_REGEXP, "");
331+
}
332+
return stripped;
333+
}
334+
312335
generateHeader(module, requestShortener) {
313336
const req = module.readableIdentifier(requestShortener);
314337
const reqStr = req.replace(/\*\//g, "*_/");
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
4+
// The header `output.pathinfo` writes above a sheet, and the module it names.
5+
const HEADER_REGEXP = /^\/\*!\*+!\*\\\n {2}!\*\*\* (.*) \*\*\*!\n {2}\\\*+\/\n/;
6+
7+
// The emitted page's one `<style>`, split into the header a build wrote and
8+
// what it heads.
9+
module.exports = (stats) => {
10+
const asset = stats.assets.find((a) => /\.html$/.test(a.name));
11+
const html = fs.readFileSync(path.join(__dirname, asset.name), "utf8");
12+
const body = /<style>([\s\S]*?)<\/style>/.exec(html)[1];
13+
const header = HEADER_REGEXP.exec(body);
14+
return {
15+
html,
16+
// Every header in the page, to count them.
17+
headers: html.match(/!\*\*\* css /g) || [],
18+
// The sheet the header says it was read as.
19+
names:
20+
header &&
21+
Buffer.from(/base64,([\w+/=]+)/.exec(header[1])[1], "base64").toString(
22+
"utf8"
23+
),
24+
// The body under the header, which is the sheet as the page holds it.
25+
heads: header && body.slice(header[0].length)
26+
};
27+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import "./page.html";
2+
3+
const readStyle = require("./helper");
4+
5+
const SHEET = ".a { color: red; }\n";
6+
7+
it("should head the sheet with the module it read, not the one the page came with", () => {
8+
const style = readStyle(STATS_JSON);
9+
10+
// The page arrives with a header of its own, as a page an earlier build
11+
// emitted does. That one was the build before's, so this build sheds it and
12+
// writes its own — one header, not two.
13+
expect(style.headers).toHaveLength(1);
14+
expect(style.html).not.toContain(".stale");
15+
16+
// The module it names is the sheet the author wrote, so what the build read
17+
// is the sheet alone and not the sheet under a header.
18+
expect(style.names).toBe(SHEET);
19+
20+
// And the sheet comes back out as the page holds it: a newline appended
21+
// under it would be a line the body gains on every build of the page.
22+
expect(style.heads).toBe(SHEET);
23+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>style module header</title>
5+
<style>/*!****************************************************************!*\
6+
!*** css data:text/css;base64,LnN0YWxlIHt9 (exportType: text) ***!
7+
\****************************************************************/
8+
.a { color: red; }
9+
</style>
10+
</head>
11+
<body>
12+
<p>hi</p>
13+
</body>
14+
</html>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import "./page.html";
2+
3+
const readStyle = require("./helper");
4+
5+
const SHEET = ".a { color: green; }\n";
6+
7+
it("should write a header for the sheet the rebuild read", () => {
8+
const style = readStyle(STATS_JSON);
9+
10+
// The sheet changed, so the module it is read as changed with it: a header
11+
// held over from the build before would name the sheet that is no longer
12+
// there, and one cached against that module would still read `red`.
13+
expect(style.headers).toHaveLength(1);
14+
expect(style.html).not.toContain(".stale");
15+
expect(style.names).toBe(SHEET);
16+
expect(style.heads).toBe(SHEET);
17+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>style module header</title>
5+
<style>/*!****************************************************************!*\
6+
!*** css data:text/css;base64,LnN0YWxlIHt9 (exportType: text) ***!
7+
\****************************************************************/
8+
.a { color: green; }
9+
</style>
10+
</head>
11+
<body>
12+
<p>hi</p>
13+
</body>
14+
</html>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"use strict";
2+
3+
// `output.pathinfo` is what development turns on, and what the harness sets —
4+
// the header it writes above an inline sheet is what this case watches.
5+
6+
/** @type {import("../../../../").Configuration} */
7+
module.exports = {
8+
mode: "development",
9+
output: {
10+
htmlFilename: "[name].html"
11+
},
12+
module: {
13+
generator: {
14+
html: {
15+
extract: true
16+
}
17+
}
18+
},
19+
experiments: {
20+
html: true,
21+
css: true
22+
},
23+
node: {
24+
__dirname: false
25+
}
26+
};

0 commit comments

Comments
 (0)