Skip to content

Commit c6b4dd3

Browse files
committed
fix(css): join the rules a nesting hoist leaves side by side
Lowering nesting writes a nested rule out on its own, as text, so the sibling merge never reads it: `a{.x{top:0}.y{top:0}}` printed two rules where a second pass writes `a .x,a .y{top:0}`. The run is joined where it is built, concatenating like every other join, and the list is ordered once where the run ends. The rule the hoist emptied stands in front of that run and takes the join too where it prints the same block.
1 parent 7e6cb67 commit c6b4dd3

11 files changed

Lines changed: 200 additions & 3 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+
Join the rules a lowered nesting hoist leaves side by side, so the printer writes what a second pass would.

‎lib/css/syntax-parser.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15027,6 +15027,7 @@ module.exports._pendingNested = _pendingNested;
1502715027
module.exports._prefixRemovable = _prefixRemovable;
1502815028
module.exports._prefixScope = _prefixScope;
1502915029
module.exports._roundSignificant = _roundSignificant;
15030+
module.exports._canonicalSelectorList = _canonicalSelectorList;
1503015031
module.exports._ruleEntryOf = _ruleEntryOf;
1503115032
module.exports._rulePrelude = _rulePrelude;
1503215033
module.exports._setRuleEntry = _setRuleEntry;

‎lib/css/syntax-printer.js‎

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ const {
162162
_appendChildTexts,
163163
_atKeyword,
164164
_blockSpans,
165+
_canonicalSelectorList,
165166
_collapseBox,
166167
_composeBlockBody,
167168
_customMedia,
@@ -1932,6 +1933,10 @@ const _resolveNestedPrelude = (prelude, parent) => {
19321933
return written.join(",");
19331934
};
19341935

1936+
// Whether the run `_writeNested` built took in the rule the hoist emptied,
1937+
// which is the caller's to write and so the caller's to leave out.
1938+
let _nestedTookParent = false;
1939+
19351940
/**
19361941
* Whether a block's declarations all stand before its rules, so taking the
19371942
* rules out of it leaves the cascade as it was.
@@ -1992,13 +1997,45 @@ const _writeNested = (path, node, prelude, rest, hoistable) => {
19921997
}
19931998
return null;
19941999
}
2000+
// Hoisting writes these out as text, so the sibling merge never reads them —
2001+
// two nested rules printing one block would stay two until a second pass.
2002+
// They are neighbors here and nowhere else, so the run is joined here.
19952003
let out = "";
2004+
/** @type {{ prelude: string, rest: string, joined: boolean } | null} */
2005+
let held = null;
2006+
// The rule the hoist emptied stands right in front of the run and may print
2007+
// the same block, so it is offered the join first. Read out of band, the
2008+
// caller writing that rule rather than this.
2009+
_nestedTookParent =
2010+
rest !== null && taken.length !== 0 && taken[0].rest === rest;
2011+
if (rest !== null && _nestedTookParent) {
2012+
held = { prelude, rest, joined: false };
2013+
}
19962014
for (let i = 0; i < taken.length; i++) {
1997-
out += taken[i].prelude + taken[i].rest;
2015+
const one = taken[i];
2016+
if (held !== null && held.rest === one.rest) {
2017+
// Concatenated, like every other join: the list is ordered once, below,
2018+
// where the run that grew it ends.
2019+
held.prelude += `,${one.prelude}`;
2020+
held.joined = true;
2021+
continue;
2022+
}
2023+
if (held !== null) out += _settledPrelude(held) + held.rest;
2024+
held = { prelude: one.prelude, rest: one.rest, joined: false };
19982025
}
2026+
if (held !== null) out += _settledPrelude(held) + held.rest;
19992027
return out;
20002028
};
20012029

2030+
/**
2031+
* A joined run's selector list, canonical. A run that joined nothing is written
2032+
* as it stands, so a rule the hoist only moved pays no read of its own list.
2033+
* @param {{ prelude: string, rest: string, joined: boolean }} run what the join left
2034+
* @returns {string} the list to write
2035+
*/
2036+
const _settledPrelude = (run) =>
2037+
run.joined ? _canonicalSelectorList(run.prelude) : run.prelude;
2038+
20022039
/**
20032040
* Whether a rule's nested rules are taken out of it and written on their own.
20042041
* @param {CssPath} path the walk's accessors
@@ -4508,7 +4545,13 @@ const printer = (path, writer) => {
45084545
);
45094546
if (nested === null) return "";
45104547
if (out !== text) return out + nested;
4511-
if (nested.length !== 0) return text + nested;
4548+
if (nested.length !== 0) {
4549+
// The run wrote this rule's selectors into itself, and only where
4550+
// nothing but them and its block stood here.
4551+
return _nestedTookParent && text === prelude + rest
4552+
? nested
4553+
: text + nested;
4554+
}
45124555
// Where the block is what a sibling is compared against, remember the rule
45134556
// so the parent parts it without re-scanning for the `{`. An at-rule
45144557
// remembers its block's entries too, so a sibling's rules can join them.
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-hoisted-nested-join minify-hoisted-nested-join should compile 1`] = `".a .x,.a .y{top:0}.b .o,.b .p,.b .q{left:0}.c,.c .x,.c .y{right:0}.d{bottom:1px}.d .x,.d .y{bottom:0}.e .x{margin:0}.e .y{margin:1px}.e .z{margin:0}@media print{.f .x,.f .y{padding:0}}.g{top:0;.x{top:0}left:0}"`;
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-hoisted-nested-join minify-hoisted-nested-join should compile 1`] = `".a .x,.a .y{top:0}.b .o,.b .p,.b .q{left:0}.c,.c .x,.c .y{right:0}.d{bottom:1px}.d .x,.d .y{bottom:0}.e .x{margin:0}.e .y{margin:1px}.e .z{margin:0}@media print{.f .x,.f .y{padding:0}}.g{top:0;.x{top:0}left:0}"`;
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 join the rules a hoist leaves side by side", () => {
4+
expect(true).toBe(true);
5+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* Hoisting writes a nested rule out on its own, and two of them printing one
2+
block are neighbors nowhere else — so the join is offered there or left to
3+
a second pass. */
4+
.a { .x { top: 0 } .y { top: 0 } }
5+
6+
/* A run of them ends as one list, in the printer's own order. */
7+
.b { .q { left: 0 } .p { left: 0 } .o { left: 0 } }
8+
9+
/* The rule the hoist emptied stands in front of the run and prints the same
10+
block, so it joins too. */
11+
.c { right: 0; .x { right: 0 } .y { right: 0 } }
12+
13+
/* One that prints another block is left where it was written. */
14+
.d { bottom: 1px; .x { bottom: 0 } .y { bottom: 0 } }
15+
16+
/* A rule printing another block parts the run. */
17+
.e { .x { margin: 0 } .y { margin: 1px } .z { margin: 0 } }
18+
19+
/* Under the at-rule they were written in. */
20+
@media print { .f { .x { padding: 0 } .y { padding: 0 } } }
21+
22+
/* A declaration after a nested rule is read after it, so this block keeps its
23+
nesting and there is no run to join. */
24+
.g { top: 0; .x { top: 0 } left: 0 }
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
expect(css).toContain(".a .x,.a .y{top:0}");
16+
expect(css).toContain(".b .o,.b .p,.b .q{left:0}");
17+
expect(css).toContain(".c,.c .x,.c .y{right:0}");
18+
expect(css).toContain(".d{bottom:1px}.d .x,.d .y{bottom:0}");
19+
expect(css).toContain(".e .x{margin:0}.e .y{margin:1px}.e .z{margin:0}");
20+
expect(css).toContain("@media print{.f .x,.f .y{padding:0}}");
21+
expect(css).toContain(".g{top:0;.x{top:0}left:0}");
22+
23+
// What the printer wrote is what a second pass writes.
24+
const again = await cssMinify({ "bundle0.css": css }, undefined, {
25+
environment: { browsers: ["chrome 100"] }
26+
});
27+
expect(again.code).toBe(css);
28+
}
29+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
// A target that reads no nesting is what hoists the nested rules out.
6+
target: "browserslist: chrome 100",
7+
mode: "production",
8+
output: {
9+
pathinfo: false
10+
},
11+
optimization: {
12+
minimize: true,
13+
minimizer: ["..."]
14+
},
15+
experiments: {
16+
css: true
17+
}
18+
};

‎test/external/wpt‎

Submodule wpt updated 1651 files

0 commit comments

Comments
 (0)