Skip to content

Commit 8950dd5

Browse files
committed
fix(css): write a nested combinator the way the printer writes one
Lowering nesting parts the parent from the nested selector with a space, and a selector opening on a combinator is parted by that combinator instead — so `a{>b{top:0}}` printed `a >b` where a second pass writes `a>b`. Read off `_COMBINATOR_CODES`, the set the printer writes tight, so `||` keeps its space here as it keeps it there. Swiper 14 loses 25 bytes of it.
1 parent 5ee3f70 commit 8950dd5

9 files changed

Lines changed: 125 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+
Write a nested selector opening on a combinator against its parent the way the printer writes one.

‎lib/css/syntax-printer.js‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1926,9 +1926,15 @@ const _resolveNestedPrelude = (prelude, parent) => {
19261926
const written = _splitSelectorList(prelude);
19271927
for (let i = 0; i < written.length; i++) {
19281928
const one = written[i].trim();
1929-
written[i] = _namesParent(one)
1930-
? _substituteAmpersand(one, reference)
1931-
: `${reference} ${one}`;
1929+
if (_namesParent(one)) {
1930+
written[i] = _substituteAmpersand(one, reference);
1931+
continue;
1932+
}
1933+
// A selector opening on a combinator is parted from the parent by that
1934+
// combinator, and the printer writes those tight — so a space in front of
1935+
// one is a space a second pass takes off. `||` is not one of them.
1936+
const combinator = _COMBINATOR_CODES.has(one.charCodeAt(0));
1937+
written[i] = combinator ? reference + one : `${reference} ${one}`;
19321938
}
19331939
return written.join(",");
19341940
};
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-nested-combinator minify-nested-combinator should compile 1`] = `".a>.child{top:0}.b+.next{top:1px}.c~.later{top:2px}.d>.child{top:3px}.e || .col{top:4px}.f .inside{top:5px}:is(.g,.h)>.child{top:6px}.i>.child{top:7px}"`;
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-nested-combinator minify-nested-combinator should compile 1`] = `".a>.child{top:0}.b+.next{top:1px}.c~.later{top:2px}.d>.child{top:3px}.e || .col{top:4px}.f .inside{top:5px}:is(.g,.h)>.child{top:6px}.i>.child{top:7px}"`;
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 write a nested combinator the way the printer does", () => {
4+
expect(true).toBe(true);
5+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* A nested selector opening on a combinator is parted from its parent by that
2+
combinator, and the printer writes those tight — so writing a space in front
3+
of one leaves a space a second pass takes off. */
4+
.a { > .child { top: 0 } }
5+
.b { + .next { top: 1px } }
6+
.c { ~ .later { top: 2px } }
7+
8+
/* However the source spaced it. */
9+
.d { > .child { top: 3px } }
10+
11+
/* `||` is not one the printer writes tight, so neither is this. */
12+
.e { || .col { top: 4px } }
13+
14+
/* A descendant is written with the space it means. */
15+
.f { .inside { top: 5px } }
16+
17+
/* A parent written as a list, and a selector naming it itself. */
18+
.g, .h { > .child { top: 6px } }
19+
.i { &> .child { top: 7px } }
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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>.child{top:0}");
16+
expect(css).toContain(".b+.next{top:1px}");
17+
expect(css).toContain(".c~.later{top:2px}");
18+
expect(css).toContain(".d>.child{top:3px}");
19+
expect(css).toContain(".e || .col{top:4px}");
20+
expect(css).toContain(".f .inside{top:5px}");
21+
expect(css).toContain(":is(.g,.h)>.child{top:6px}");
22+
expect(css).toContain(".i>.child{top:7px}");
23+
24+
// What the printer wrote is what a second pass writes.
25+
const again = await cssMinify({ "bundle0.css": css }, undefined, {
26+
environment: { browsers: ["chrome 100"] }
27+
});
28+
expect(again.code).toBe(css);
29+
}
30+
};
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, so the nested selectors are written 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/unitCases/CssSyntax.unittest.js‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10939,6 +10939,39 @@ describe("CssSyntax minify — the declaration a lowered shorthand kills", () =>
1093910939
});
1094010940
});
1094110941

10942+
describe("CssSyntax minify — a nested selector opening on a combinator", () => {
10943+
const T = ["chrome 100"];
10944+
10945+
it.each([
10946+
["child", "a{>b{top:0}}", "a>b{top:0}"],
10947+
["next sibling", "a{+b{top:0}}", "a+b{top:0}"],
10948+
["subsequent sibling", "a{~b{top:0}}", "a~b{top:0}"]
10949+
])("writes the %s combinator tight, as the printer does", (_n, css, out) => {
10950+
expect(minifyFor(css, T)).toBe(out);
10951+
});
10952+
10953+
it("writes it tight however the source spaced it", () => {
10954+
expect(minifyFor("a{ > b{top:0}}", T)).toBe("a>b{top:0}");
10955+
});
10956+
10957+
it("keeps the space in front of a column combinator", () => {
10958+
// `||` is not one the printer writes tight, so neither is this.
10959+
expect(minifyFor("a{||b{top:0}}", T)).toBe("a ||b{top:0}");
10960+
});
10961+
10962+
it("keeps the space in front of a descendant", () => {
10963+
expect(minifyFor("a{b{top:0}}", T)).toBe("a b{top:0}");
10964+
});
10965+
10966+
it("writes a parent that is a list the same way", () => {
10967+
expect(minifyFor("a,e{>b{top:0}}", T)).toBe(":is(a,e)>b{top:0}");
10968+
});
10969+
10970+
it("leaves a selector naming the parent to the `&` it spells", () => {
10971+
expect(minifyFor("a{&>b{top:0}}", T)).toBe("a>b{top:0}");
10972+
});
10973+
});
10974+
1094210975
describe("CssSyntax minify — nesting the target cannot read", () => {
1094310976
it("writes a rule nested in another on its own", () => {
1094410977
expect(minifyFor("a{color:red;& b{color:blue}}", ["chrome 100"])).toBe(

0 commit comments

Comments
 (0)