Skip to content

Commit ab902ff

Browse files
fix(css): order a joined seam's list, and gather a layer block too big to assemble (#22224)
* fix(css): order the list a joined at-rule's seam leaves Joining two at-rule blocks with one prelude joins the pair meeting at the seam, and `_joinRuleEntries` concatenates the two preludes rather than ordering them — every other join settles its list afterwards, this one never did. So `@media print{.b{x:1}}@media print{.a{x:1}}` printed `.b,.a` where a second pass writes `.a,.b`. Ordered once per join, so a run stays linear. Every at-rule whose block holds rules reaches it. * fix(css): gather a named layer block too big to assemble A named `@layer` block past `_STREAM_MIN_NODES` streams its body, so its text is out before it closes and the parent's gather, which reads a body whole, never sees it: `@layer u{@layer a{}@layer a{<4096 rules>}}` wrote two blocks of one layer where a second pass wrote one. Hold a block whose layer a sibling already opened, read off the source, so it stays assembled and `_mergeNamedLayerBlocks` takes it. Only that block is materialized. * test(css): cover both fixes end to end in config cases The unit tests call the printer; these run the two through a build, so the seam a joined at-rule leaves and the layer a held block gathers into are read off the emitted stylesheet. Each asserts the output is what a second pass writes. The gather is only reached by a block big enough to stream, so its filler is generated by a loader rather than checked in. * fix(css): read a layer name past a comment, and take out a repeated selector The name a held block is matched on is read off the source, where a comment stands wherever a space may — `@layer /*c*/ a{` read as a name of its own, so the block streamed and the gather lost it. One is taken out rather than spelled, the name carrying no whitespace itself, and only where the prelude holds one. Ordering the seam's list also reads out a selector both sides carry, which the join wrote twice. * perf(css): settle a joined block's seams where its run ends The seam ordering runs inside `_joinRuleEntries`, so a run of joins orders the list it is still growing once per join — quadratic in the length of the run. The deep-nesting printer benchmark pays 335 ms -> 479 ms for it on CodSpeed, and 19 ms -> 34 ms here on the same fixture. A seam now concatenates like every other join and says so (`unordered`), and the run that grew it settles it once where it ends, which is what every other join already does — `_mergeAdjacentRules` through `finish()`, the distant merges through `_orderDistantLists`. `_settleJoinedSeams` reads only the children carrying a seam and writes the block's text again only where one actually moved, so a block that grew none costs a walk of its children. The gathers and distant merges end a run of their own, so each settles there: the streamed layer fold, `_gatherDistantAtRule`, `_mergeNamedLayerBlocks`, `_mergeDistantAtRule`, and the fold a finished run offers the rule before it. Output is unchanged. Byte-identical over the 1184 stylesheets the test tree carries and 4000 generated sheets, and the deep-nesting fixture is back to 19.3 ms against a 19.2 ms baseline. Tests: the seam a gather leaves, one a run of gathers grows, the distant at-rule seams, and a gathered block big enough to stream — the paths that reach a seam without a join run behind them. Co-authored-by: aryanraj45 <[email protected]> * test(css): cover the seam a top-level layer gather leaves The one settle site the suite did not reach: a layer the sheet itself holds, where the block a later one folds into is a rule already written out. Co-authored-by: aryanraj45 <[email protected]> * chore(changeset): fold the two entries into one One changeset per pull request: the seam order and the layer gather are the same bug family, so they read as one line in the changelog. Co-authored-by: aryanraj45 <[email protected]> * chore(changeset): keep the entry within the length the guide asks for Co-authored-by: aryanraj45 <[email protected]> --------- Co-authored-by: aryanraj45 <[email protected]>
1 parent e7911a5 commit ab902ff

14 files changed

Lines changed: 590 additions & 41 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+
Order joined at-rule selector lists and gather a repeated large `@layer` block.

‎lib/css/syntax.js‎

Lines changed: 216 additions & 35 deletions
Large diffs are not rendered by default.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`ConfigCacheTestCases css minify-at-rule-seam-order minify-at-rule-seam-order should compile 1`] = `"@media print{.a,.b{color:red}}@supports (color:red){.c,.d{top:0}}@layer named{.e,.f{left:0}}@starting-style{.g,.h{opacity:0}}@container (width>0px){.s1,.s2,.s3,.s4{right:0}}@media screen{.keep-first{color:red}.keep-second{color:blue}}"`;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`ConfigTestCases css minify-at-rule-seam-order minify-at-rule-seam-order should compile 1`] = `"@media print{.a,.b{color:red}}@supports (color:red){.c,.d{top:0}}@layer named{.e,.f{left:0}}@starting-style{.g,.h{opacity:0}}@container (width>0px){.s1,.s2,.s3,.s4{right:0}}@media screen{.keep-first{color:red}.keep-second{color:blue}}"`;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import "./style.css";
2+
3+
it("should order the list two joined at-rule blocks meet at", () => {
4+
expect(true).toBe(true);
5+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* Two blocks of one condition join, and the pair meeting at the seam becomes
2+
one rule — whose selector list has to be written in the printer's own order,
3+
not the order the two were written in. */
4+
@media print { .b { color: red } }
5+
@media print { .a { color: red } }
6+
7+
@supports (color: red) { .d { top: 0 } }
8+
@supports (color: red) { .c { top: 0 } }
9+
10+
@layer named { .f { left: 0 } }
11+
@layer named { .e { left: 0 } }
12+
13+
@starting-style { .h { opacity: 0 } }
14+
@starting-style { .g { opacity: 0 } }
15+
16+
/* A run of them: each join settles the list it grew, so the run stays linear
17+
and ends in one ordered list. */
18+
@container (width > 0px) { .s4 { right: 0 } }
19+
@container (width > 0px) { .s3 { right: 0 } }
20+
@container (width > 0px) { .s2 { right: 0 } }
21+
@container (width > 0px) { .s1 { right: 0 } }
22+
23+
/* A seam whose two rules print different blocks joins nothing, and the two
24+
stay as they were written. */
25+
@media screen { .keep-first { color: red } }
26+
@media screen { .keep-second { color: blue } }
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"use strict";
2+
3+
const fs = require("fs");
4+
const path = require("path");
5+
const cssMinify = require("../../../../lib/css/cssMinify");
6+
7+
module.exports = {
8+
async afterExecute(options) {
9+
const css = fs.readFileSync(
10+
path.join(options.output.path, "bundle0.css"),
11+
"utf8"
12+
);
13+
expect(css).toMatchSnapshot();
14+
15+
// Every at-rule whose block holds rules reaches the same seam, so each is
16+
// held to the printer's own order rather than the order the two were in.
17+
expect(css).toContain("@media print{.a,.b{color:red}}");
18+
expect(css).toContain("@supports (color:red){.c,.d{top:0}}");
19+
expect(css).toContain("@layer named{.e,.f{left:0}}");
20+
expect(css).toContain("@starting-style{.g,.h{opacity:0}}");
21+
// A run of joins ends in one list, ordered once the sheet leaves nothing
22+
// further to append.
23+
expect(css).toContain("@container (width>0px){.s1,.s2,.s3,.s4{right:0}}");
24+
// A seam that joins nothing leaves both rules where they were written.
25+
expect(css).toContain(
26+
"@media screen{.keep-first{color:red}.keep-second{color:blue}}"
27+
);
28+
29+
// What the printer wrote is what a second pass writes.
30+
const again = await cssMinify({ "bundle0.css": css });
31+
expect(again.code).toBe(css);
32+
}
33+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
target: "browserslist: chrome 120",
6+
mode: "production",
7+
output: {
8+
pathinfo: false
9+
},
10+
optimization: {
11+
minimize: true,
12+
minimizer: ["..."]
13+
},
14+
experiments: {
15+
css: true
16+
}
17+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"use strict";
2+
3+
// The gather this case is about is only reached by a block big enough to stream
4+
// its own body, which is a few thousand rules. Generated here rather than
5+
// checked in, so the fixture states the shape and not the bulk.
6+
const RULES = 3400;
7+
8+
/**
9+
* @param {string} source the stylesheet
10+
* @returns {string} it, with the filler marker expanded
11+
*/
12+
module.exports = function fillerLoader(source) {
13+
let filler = "";
14+
for (let i = 0; i < RULES; i++) filler += `.f${i}{top:${i + 1}px}`;
15+
return source.replace("/* FILLER */", filler);
16+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import "./style.css";
2+
3+
it("should gather a named layer block too big to assemble", () => {
4+
expect(true).toBe(true);
5+
});

0 commit comments

Comments
 (0)