Skip to content

Commit 916cf37

Browse files
fix(css): keep a rule nested under a selector opening on a combinator when lowering (#22707)
* fix(css): keep a rule nested under a selector opening on a combinator when lowering Lowering resolves nested rules innermost first, so a rule inside one whose list opens on a combinator was written as :is(~b) c, which no engine parses, and a list mixing selectors that name & with ones that do not lost the parent the others imply. Such a parent is now written with & in every selector, which the next level out fills in. Found in Swiper 14 on the default browserslist. * fix(css): trim only CSS whitespace off a nested selector when lowering String#trim also takes U+1680 and the other Unicode spaces, which an identifier carries as ordinary characters, so .b<U+1680> became .b. * test: make two watch-timing cases robust to slow and lossy runners hotspots-per-build bounded a 130 ms busy loop by wall clock, so a pause inside the loop under Bun --smol failed it; it now compares the report with the loop's own measured time, which still catches carried-over time. The watch harness could not re-send a deletion macOS dropped, since the retouch skipped files that no longer exist: it now deletes them again, and counts a deleted file a build resolved as missing as seen.
1 parent 2ebe482 commit 916cf37

10 files changed

Lines changed: 112 additions & 18 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+
Keep a CSS rule nested under a selector opening on a combinator when lowering.

‎lib/css/syntax-parser.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19733,6 +19733,7 @@ module.exports._NUMERIC_RE = _NUMERIC_RE;
1973319733
module.exports._atKeywordOf = _atKeywordOf;
1973419734
module.exports._printedSource = _printedSource;
1973519735
module.exports._rawName = _rawName;
19736+
module.exports._trimCssWhiteSpace = _trimCssWhiteSpace;
1973619737
module.exports._declarationsOf = _declarationsOf;
1973719738
module.exports._rulesOf = _rulesOf;
1973819739
module.exports._isImportant = _isImportant;

‎lib/css/syntax-printer.js‎

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ const {
212212
_takeUnusedRule,
213213
_terminate,
214214
_tokenizeCalc,
215+
_trimCssWhiteSpace,
215216
_unreadableColorFunction,
216217
_unwrapLoneIs,
217218
_valueClasses,
@@ -2240,7 +2241,7 @@ const _resolveNestedPrelude = (prelude, parent) => {
22402241
const reference = _isCompoundSelector(parent) ? parent : `:is(${parent})`;
22412242
const written = _splitSelectorList(prelude);
22422243
for (let i = 0; i < written.length; i++) {
2243-
const one = written[i].trim();
2244+
const one = _trimCssWhiteSpace(written[i]);
22442245
if (_namesParent(one)) {
22452246
written[i] = _substituteAmpersand(one, reference);
22462247
continue;
@@ -2261,6 +2262,33 @@ const _resolveNestedPrelude = (prelude, parent) => {
22612262
return written.length === 1 ? list : _canonicalSelectorList(list);
22622263
};
22632264

2265+
/**
2266+
* A nested rule's prelude with its parent written as `&` in every selector, where
2267+
* one opens on a combinator or only some name `&`: either way `:is()` of the list
2268+
* would lose the parent the others imply.
2269+
* @param {string} prelude the nested rule's printed prelude
2270+
* @returns {string} the prelude, every selector naming `&`, or as it was
2271+
*/
2272+
const _parentNamed = (prelude) => {
2273+
const written = _splitSelectorList(prelude);
2274+
let relative = false;
2275+
let naming = 0;
2276+
for (let i = 0; i < written.length; i++) {
2277+
written[i] = _trimCssWhiteSpace(written[i]);
2278+
if (_COMBINATOR_CODES.has(written[i].charCodeAt(0))) relative = true;
2279+
if (_namesParent(written[i])) naming++;
2280+
}
2281+
if (!relative && (naming === 0 || naming === written.length)) return prelude;
2282+
for (let i = 0; i < written.length; i++) {
2283+
const one = written[i];
2284+
if (_namesParent(one)) continue;
2285+
written[i] = _COMBINATOR_CODES.has(one.charCodeAt(0))
2286+
? `&${one}`
2287+
: `& ${one}`;
2288+
}
2289+
return written.join(",");
2290+
};
2291+
22642292
// Whether the run `_writeNested` built took in the rule the hoist emptied,
22652293
// which is the caller's to write and so the caller's to leave out.
22662294
let _nestedTookParent = false;
@@ -2356,11 +2384,17 @@ const _writeNested = (
23562384
// sibling's rules are on the list too, waiting for the parent they share.
23572385
/** @type {{ prelude: string, rest: string, owner: Node | null, apart?: boolean }[]} */
23582386
const taken = [];
2387+
// A rule written in another names its own parent where its list opens on a
2388+
// combinator, so that parent stays an `&` the next level out writes in.
2389+
const against =
2390+
path.parent !== null && path.type(path.parent) !== NodeType.Stylesheet
2391+
? _parentNamed(prelude)
2392+
: prelude;
23592393
for (let i = _pendingNested.length - 1; i >= 0; i--) {
23602394
if (_pendingNested[i].owner !== node) continue;
23612395
const one = _pendingNested[i];
23622396
_pendingNested.splice(i, 1);
2363-
one.prelude = _resolveNestedPrelude(one.prelude, prelude);
2397+
one.prelude = _resolveNestedPrelude(one.prelude, against);
23642398
taken.unshift(one);
23652399
}
23662400
const hoisted = hoistable && _hoistedOutOf(path, path.parent);

‎test/specCases/syntaxEquivalence.spectest.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,6 +1522,19 @@ const LOWERING_FIXTURES = [
15221522
["#z", "color"]
15231523
]
15241524
},
1525+
{
1526+
name: "nesting under a rule opening on a combinator",
1527+
// Swiper writes `.swiper-vertical{~.swiper-button-prev{.icon{…}}}`; the
1528+
// parent goes into `:is()` written out, which takes no relative argument.
1529+
produces: [":is(a b,a~b) c{"],
1530+
css: "a{b,~b{c{color:red}}}",
1531+
browsers: ["chrome 100"],
1532+
html: "<a><b><c id=t>t</c></b></a><b><c id=u>u</c></b>",
1533+
probes: [
1534+
["#t", "color"],
1535+
["#u", "color"]
1536+
]
1537+
},
15251538
{
15261539
name: "light-dark()",
15271540
// The pair the lowering writes, which a script enumerating the computed

‎test/templates/WatchTestCases.js‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,14 @@ const describeCases = (config) => {
384384
// macOS can drop a change event entirely
385385
const now = new Date();
386386
for (const file of unseenChanges) {
387-
if (fs.existsSync(file)) fs.utimesSync(file, now, now);
387+
if (fs.existsSync(file)) {
388+
fs.utimesSync(file, now, now);
389+
} else if (fs.existsSync(path.dirname(file))) {
390+
// a dropped deletion is re-sent by deleting again; no build
391+
// runs between the two calls, so none sees the file
392+
fs.writeFileSync(file, "");
393+
fs.unlinkSync(file);
394+
}
388395
}
389396
retouchUnseenChanges(compilers, retries - 1);
390397
}, RETOUCH_DELAY);
@@ -447,7 +454,8 @@ const describeCases = (config) => {
447454
}
448455
}
449456
// a change this build no longer depends on cannot make it stale, and
450-
// nothing watches it any more to report it (e.g. a deleted import)
457+
// nothing watches it any more to report it (e.g. a deleted import);
458+
// a deleted file it resolved as missing is one it saw go
451459
const builds = /** @type {import("../../").Stats[]} */ (
452460
"stats" in stats ? stats.stats : [stats]
453461
);
@@ -456,7 +464,8 @@ const describeCases = (config) => {
456464
!builds.some(
457465
({ compilation }) =>
458466
compilation.fileDependencies.has(file) ||
459-
compilation.missingDependencies.has(file) ||
467+
(compilation.missingDependencies.has(file) &&
468+
fs.existsSync(file)) ||
460469
isWithin(file, compilation.contextDependencies)
461470
)
462471
) {

‎test/unitCases/CssSyntax.unittest.js‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12810,6 +12810,23 @@ describe("CssSyntax minify — a nested selector opening on a combinator", () =>
1281012810
it("leaves a selector naming the parent to the `&` it spells", () => {
1281112811
expect(minifyFor("a{&>b{top:0}}", T)).toBe("a>b{top:0}");
1281212812
});
12813+
12814+
it.each([
12815+
["a{~b{c{top:0}}}", ":is(a~b) c{top:0}"],
12816+
["a{b,~b{c{top:0}}}", ":is(a b,a~b) c{top:0}"],
12817+
["a,e{>b{c{top:0}}}", ":is(:is(a,e)>b) c{top:0}"],
12818+
["a{d{~b{c{top:0}}}}", "a :is(d~b) c{top:0}"],
12819+
// A list where only some name `&`: the others keep the parent they imply.
12820+
["a{b,&~d{c{top:0}}}", ":is(a~d,a b) c{top:0}"],
12821+
// U+1680 is an identifier code point, which `String#trim` would take off.
12822+
["a{.b ,~b{c{top:0}}}", ":is(a .b ,a~b) c{top:0}"]
12823+
])(
12824+
"writes %s, a rule nested in one opening on a combinator, with no relative argument",
12825+
(css, out) => {
12826+
// `:is()` takes no relative selector, so `:is(~b)` would drop the rule.
12827+
expect(minifyFor(css, T)).toBe(out);
12828+
}
12829+
);
1281312830
});
1281412831

1281512832
describe("CssSyntax minify — nesting the target cannot read", () => {

‎test/watchCases/performance/hotspots-per-build/0/index.js‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ it("should report only this build's time, step 0", () => {
88
expect(hint).toBeDefined();
99

1010
const ms = Number(/BurnPlugin \((\d+) ms/.exec(hint.message)[1]);
11+
const { burned } = COMPILER.options.plugins.find(
12+
(plugin) => "burned" in plugin
13+
);
1114

12-
// The plugin burns the same amount every build. Anything near a multiple of
13-
// it means the last build's time was carried into this one.
14-
expect(ms).toBeGreaterThanOrEqual(100);
15-
expect(ms).toBeLessThan(240);
15+
// Measured against this build's own burn, so a slow machine cancels out; a
16+
// carried-over build would add a whole earlier burn (130 ms or more).
17+
expect(ms).toBeGreaterThanOrEqual(Math.floor(burned));
18+
expect(ms - burned).toBeLessThan(100);
1619
});

‎test/watchCases/performance/hotspots-per-build/1/index.js‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ it("should report only this build's time, step 1", () => {
88
expect(hint).toBeDefined();
99

1010
const ms = Number(/BurnPlugin \((\d+) ms/.exec(hint.message)[1]);
11+
const { burned } = COMPILER.options.plugins.find(
12+
(plugin) => "burned" in plugin
13+
);
1114

12-
// The plugin burns the same amount every build. Anything near a multiple of
13-
// it means the last build's time was carried into this one.
14-
expect(ms).toBeGreaterThanOrEqual(100);
15-
expect(ms).toBeLessThan(240);
15+
// Measured against this build's own burn, so a slow machine cancels out; a
16+
// carried-over build would add a whole earlier burn (130 ms or more).
17+
expect(ms).toBeGreaterThanOrEqual(Math.floor(burned));
18+
expect(ms - burned).toBeLessThan(100);
1619
});

‎test/watchCases/performance/hotspots-per-build/2/index.js‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ it("should report only this build's time, step 2", () => {
88
expect(hint).toBeDefined();
99

1010
const ms = Number(/BurnPlugin \((\d+) ms/.exec(hint.message)[1]);
11+
const { burned } = COMPILER.options.plugins.find(
12+
(plugin) => "burned" in plugin
13+
);
1114

12-
// The plugin burns the same amount every build. Anything near a multiple of
13-
// it means the last build's time was carried into this one.
14-
expect(ms).toBeGreaterThanOrEqual(100);
15-
expect(ms).toBeLessThan(240);
15+
// Measured against this build's own burn, so a slow machine cancels out; a
16+
// carried-over build would add a whole earlier burn (130 ms or more).
17+
expect(ms).toBeGreaterThanOrEqual(Math.floor(burned));
18+
expect(ms - burned).toBeLessThan(100);
1619
});

‎test/watchCases/performance/hotspots-per-build/webpack.config.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
"use strict";
22

33
/**
4-
* Holds the main thread for the same stretch on every build.
4+
* Holds the main thread for the same stretch on every build, keeping how long it really took.
55
* @type {import("../../../../").WebpackPluginInstance}
66
*/
77
const burn = {
8+
burned: 0,
89
apply(compiler) {
910
compiler.hooks.compilation.tap("BurnPlugin", (compilation) => {
1011
compilation.hooks.afterSeal.tap("BurnPlugin", () => {
12+
const start = process.hrtime();
1113
const until = Date.now() + 130;
1214

1315
while (Date.now() < until) {
1416
// Spinning on purpose.
1517
}
18+
19+
const [seconds, nanoseconds] = process.hrtime(start);
20+
21+
burn.burned = seconds * 1000 + nanoseconds / 1e6;
1622
});
1723
});
1824
}

0 commit comments

Comments
 (0)