Skip to content

Commit 21172f7

Browse files
fix(html): minify a merged <style> run as the one sheet it prints (#22302)
* fix(html): minify a merged <style> run as the one sheet it prints mergeStyles joined each sheet as minified on its own, so a second pass over the joined element folded rules across them (`a{color:red}` and `b{color:red}` became `a,b{color:red}` only then), and a blank sheet joined to a rule kept its whitespace. The joined run is now rendered again as one sheet; on the async path processAsync asks for what a reprint offers that no earlier print did, a bounded number of times. Also covers the branches #22290 left untested and drops a whitespace skip in the CSS end-of-input closer that no input can reach. * perf(html): join a <style> run's sources and minify it once A sheet whose source ends at a rule (a `}` or an at-rule's `;`, outside every string, comment and block) is appended to as written, so a run is offered to the CSS minifier once: no per-sheet renders, and no second print on the async path. Only a sheet the input left open is still joined on its answer, which is the one case processAsync has to print again for. * fix(html): stop a <style> run at a sheet the input left open Nothing is appended to a sheet whose source does not end at a rule, so a run is decided on its sources alone and processAsync goes back to one reprint with no reprint loop. A second pass reads such a sheet closed and joins it; the two fixtures carrying one record that as expected in the HTML sweep. * fix(html): close a sheet the input left open before joining the next A <style> the input left inside a string, comment or block, or with a statement unfinished, is closed as its own end of input closed it (the quote, comment end and closers, then `{}` or `;`) and the next sheet is appended to that, so every run joins again and is minified once, with no reprint. Only a sheet ending on a lone backslash is not joined, and none is joined printed as written, where the closing would stay in a custom property's value. A syntax-equivalence test holds each such run to the separate sheets' computed styles in the real browser the job runs. * fix(html): read a lone @ as a delimiter when closing a sheet to join A lone @ opens an ordinary rule's prelude, which only {} ends, so closing it with ; let the next sheet's first rule be swallowed. Found by cutting every test and corpus stylesheet and comparing the joined sheet with the two apart in Chromium. * fix(html): read an unquoted url as one token when closing a sheet to join An unquoted url( is a single token, and a bad one runs to the first ) its remnants reach, so a ( inside it opens nothing. Counting it as a block added a ) that broke the declaration after it. Found by the same Chromium comparison over every cut of every test and corpus stylesheet. * fix(css,html): keep a custom property's value the input ran out in open A browser stores such a value as written, open, so the minifier prints it that way instead of closing it, and a <style> run is not joined after one. Cover the url scanner's remaining branches. * fix(html): stop a <style> run at a sheet declaring a namespace A namespace applies to the whole sheet declaring it, so a sheet joined after one read its selectors in it. An escaped at-rule name now stops a run too, and a linear scan replaces the backtracking custom-property regexp. * test(css): retire two WebKit value defects the open custom property fixed A custom property's value the input ran out in is now printed open as written, so WebKit reads the paint() probe the same through var() and both entries stopped matching.
1 parent 4873285 commit 21172f7

17 files changed

Lines changed: 863 additions & 160 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Minify a merged `<style>` run as one sheet, closing each where its input ended.

‎lib/css/syntax-parser.js‎

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,6 +1978,9 @@ const _setupParse = (input, lc) => {
19781978
_S._transforms = _DEFAULT_TRANSFORMS;
19791979
_commentsKept = "some";
19801980
_S._rewriteCustomProperties = false;
1981+
_S._openCustomValue = null;
1982+
_S._eofPrinted = "";
1983+
_S._eofOpen = "";
19811984
_dropOverriddenDeclarations = false;
19821985
_S._unitScale = ABSOLUTE_UNIT_SCALE;
19831986
// Back to what the module loads with, so no parse inherits a target of its own.
@@ -7263,6 +7266,12 @@ const grammar = (input, visitors, writer, options) => {
72637266
if (_S._lightDarkUsed && writer !== undefined) {
72647267
writer.emitStreamed(LIGHT_DARK_DEFAULTS);
72657268
}
7269+
// A value the input ran out in ends the output as it ended the input: the
7270+
// engine keeps a custom property's value as written, left open.
7271+
const open = _S._openCustomValue;
7272+
if (open !== null && writer !== undefined) {
7273+
writer.replaceEnd(open.printed, open.source, CC_RIGHT_CURLY);
7274+
}
72667275
} else {
72677276
_emitBlockContents(
72687277
_blockContentsNodes,
@@ -7275,6 +7284,7 @@ const grammar = (input, visitors, writer, options) => {
72757284
// be a whole document — and a closure over it, so both go with the parse.
72767285
_S._renderEmbeddedSource = undefined;
72777286
_S._deferEmbeddedSource = undefined;
7287+
_S._openCustomValue = null;
72787288
_heldTopLevel = null;
72797289
_priorTopLevel = null;
72807290
_topLevelRules = null;
@@ -11081,11 +11091,26 @@ const _customPropertyValue = (path, node, writer, from, to) => {
1108111091
if (kept !== "") {
1108211092
if (spaced && out !== "") out += " ";
1108311093
out += kept;
11094+
// A kept comment the input ran out in is closed as its end of input
11095+
// closed it, or the `)` and `}` printed after it would be inside it.
11096+
const opened = kept.lastIndexOf("/*");
11097+
if (
11098+
to === _S._input.length &&
11099+
_blockContentsNodes === null &&
11100+
!kept.includes("*/", opened + 2)
11101+
) {
11102+
out += "*/";
11103+
_S._eofPrinted = "*/";
11104+
_S._eofOpen = "";
11105+
}
1108411106
}
1108511107
}
1108611108
return out;
1108711109
};
1108811110

11111+
// An input ending on an escape that escapes nothing: an odd run of backslashes.
11112+
const _LONE_BACKSLASH_AT_END_REGEXP = /(?:^|[^\\])(?:\\\\)*\\$/;
11113+
1108911114
/** What makes a token worth reading through rather than writing back whole. */
1109011115
const CUSTOM_PROPERTY_REWRITABLE_RE = /[\t\n\f\r ,]|\/\*/;
1109111116

@@ -11121,7 +11146,14 @@ const _customPropertyToken = (path, child, writer) => {
1112111146
(type !== T_FUNCTION && type !== T_SIMPLE_BLOCK) ||
1112211147
!CUSTOM_PROPERTY_REWRITABLE_RE.test(source)
1112311148
) {
11124-
return closes ? _closeAtEof(source) : source;
11149+
if (!closes) return source;
11150+
_S._eofPrinted = _closeAtEof(source);
11151+
// An escape the input ran out in is a token the engine rewrites, and writes
11152+
// back closed; anything else it keeps as written, open.
11153+
_S._eofOpen = _LONE_BACKSLASH_AT_END_REGEXP.test(_S._input)
11154+
? _S._eofPrinted
11155+
: source;
11156+
return _S._eofPrinted;
1112511157
}
1112611158
const start = path.start(child);
1112711159
const end = path.end(child);
@@ -11137,24 +11169,45 @@ const _customPropertyToken = (path, child, writer) => {
1113711169
// can end on the closer's character (`f(")`) without closing anything.
1113811170
const i0 = _nodeIndex(child);
1113911171
const count = _listLens[i0];
11140-
const closed =
11141-
(count === 0
11172+
const lastEnd =
11173+
count === 0
1114211174
? opened
1114311175
: path.end(
1114411176
/** @type {ComponentValue} */ (
1114511177
_nodeRef(_flat[_listStarts[i0] + count - 1])
1114611178
)
11147-
)) < end;
11179+
);
11180+
// Only what follows the last child, less whitespace and closed comments: a
11181+
// comment the input ran out in can end on the closer's character too.
11182+
const closed =
11183+
lastEnd < end &&
11184+
_S._input
11185+
.slice(lastEnd, end)
11186+
.replace(/\/\*[\s\S]*?\*\//g, "")
11187+
.trim() === closer;
1114811188
const inner = _customPropertyValue(
1114911189
path,
1115011190
child,
1115111191
writer,
1115211192
opened,
1115311193
closed ? end - 1 : end
1115411194
);
11155-
return `${_S._input.slice(start, opened)}${inner}${
11195+
const text = `${_S._input.slice(start, opened)}${inner}${
1115611196
closed || closes ? closer : ""
1115711197
}`;
11198+
if (closes) {
11199+
// What it prints left open: its own closer, and what its last token closed.
11200+
const last = _S._eofPrinted;
11201+
const innerOpen =
11202+
last !== "" && inner.endsWith(last)
11203+
? inner.slice(0, inner.length - last.length) + _S._eofOpen
11204+
: inner;
11205+
_S._eofPrinted = text;
11206+
_S._eofOpen = `${_S._input.slice(start, opened)}${innerOpen}${
11207+
closed ? closer : ""
11208+
}`;
11209+
}
11210+
return text;
1115811211
};
1115911212

1116011213
/**
@@ -13489,10 +13542,10 @@ const _closeAtEof = (text) => {
1348913542
}
1349013543
}
1349113544
let out = text;
13492-
// Comments are consumed between tokens, so one left open is what follows the last.
13545+
// Comments are consumed between tokens, so one left open is what follows the
13546+
// last; whitespace between them would be a token itself.
1349313547
let at = lastEnd;
1349413548
for (;;) {
13495-
while (at < out.length && isWhitespace(out.charCodeAt(at))) at++;
1349613549
if (!out.startsWith("/*", at)) break;
1349713550
const end = out.indexOf("*/", at + 2);
1349813551
if (end === -1) {
@@ -14910,6 +14963,12 @@ const _S = {
1491014963
// Off, that verbatim rule stands; on, a custom property's tokens print like any
1491114964
// other value's and `getPropertyValue()` hands back the rewritten text.
1491214965
_rewriteCustomProperties: false,
14966+
// A custom property's value the input ran out in, as printed and left open.
14967+
/** @type {{ printed: string, source: string } | null} */
14968+
_openCustomValue: null,
14969+
// The token the input ran out in, as printed closed and as it prints open.
14970+
_eofPrinted: "",
14971+
_eofOpen: "",
1491314972
_input: "",
1491414973
// Set by the walk while inside a `@media` / `@container` prelude, where a `(…)`
1491514974
// is a media feature rather than a declaration or a selector list.

‎lib/css/syntax-printer.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4437,6 +4437,20 @@ const printer = (path, writer) => {
44374437
value = _S._input.slice(from, to);
44384438
if (to === _S._input.length) value = _closeAtEof(value);
44394439
}
4440+
// Closed as printed, since a `}` may still follow; the grammar leaves it
4441+
// open where nothing does, as the engine keeps it.
4442+
const last = _S._eofPrinted;
4443+
if (
4444+
to === _S._input.length &&
4445+
last !== "" &&
4446+
value.endsWith(last) &&
4447+
last !== _S._eofOpen
4448+
) {
4449+
_S._openCustomValue = {
4450+
printed: value,
4451+
source: value.slice(0, value.length - last.length) + _S._eofOpen
4452+
};
4453+
}
44404454
} else if (property === "unicode-range") {
44414455
// `U+…` tokenizes as numbers, so the generic numeric normalization
44424456
// would corrupt it; each range is shortened as the urange it is.

0 commit comments

Comments
 (0)