Skip to content

Commit ff892d9

Browse files
test(css): pin the layer cases the at-rule gather declines, and trim the changeset
Reported by CodeRabbit on #22174. The changeset line ran to 86 characters where the guide caps it at 80. `@keyframes phi` was asserted by a substring neither the merged nor the separated form contains, so it could not fail; it now states the whole two-block sequence the case is about. An anonymous `@layer` was not covered at all — `style.css` had only the named one — and it is the case worth pinning, since two anonymous layers are two layers rather than one condition written twice. `_gatherDistantAtRule` now names the layer rule itself rather than leaving it to `_joinRuleEntries`, which already refuses both spellings (`ANONYMOUS_LAYER_RE`, and its `mergeRules` bail). No output moves — the new case passes on the parent commit too — but the streaming path now reads like the nested one, and a layer no longer occupies a held piece that could never be folded.
1 parent c1eb468 commit ff892d9

6 files changed

Lines changed: 17 additions & 4 deletions

File tree

‎.changeset/086-css-merge-distant-at-rules.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"webpack": patch
33
---
44

5-
Join an at-rule with a later one stating the same condition under `mergeDistantRules`.
5+
Merge a distant at-rule stating the same condition under `mergeDistantRules`.

‎lib/css/syntax.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6163,6 +6163,10 @@ const _gatherDistantAtRule = (
61636163
// it says once, and joining two of those is not this rewrite.
61646164
if (entry.children === null) return false;
61656165
const prelude = text.slice(0, entry.prelude);
6166+
// A layer is not a condition stated twice: a named one sits where its name
6167+
// was first written, and an anonymous one opens a layer of its own. The join
6168+
// refuses both, so this only keeps the piece from being held for one.
6169+
if (_atRuleName(prelude) === "layer") return false;
61666170
const seen = _distantAtRules.get(prelude);
61676171
if (seen === undefined) {
61686172
// `_noteDistantRule` numbers this node next, so that is where it stands.

‎test/configCases/css/minimize-merge-distant-rules/__snapshots__/ConfigCacheTest.snap‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
exports[`ConfigCacheTestCases css minimize-merge-distant-rules minimize-merge-distant-rules should compile 1`] = `
44
Array [
5-
".alpha,.gamma{color:red;background:blue}.spacer{margin:0}.delta{color:blue}.between{color:green}.epsilon{color:blue}.zeta{margin-top:0}.shorthand{margin:1px}.eta{margin-top:0}.theta{padding:0}@media print{.printed{width:0}}.iota{padding:0}.kappa{outline:0}.spacing{margin:2px}.lambda-is-a-long-name{outline:0}@media (width>=100px){.mu{color:red}.nu{color:blue}.xi{color:lime}}.apart{margin:0}.apart-again{padding:0}@media (width>=200px){.omicron{color:red}}.shadows{color:green}@media (width>=200px){.pi{color:blue}}@media (width>=300px){.rho{width:0}}@media (width>=400px){.opaque{height:0}}@media (width>=300px){.sigma{width:1px}}@layer named{.tau{color:red}}.between-layers{margin:1px}@layer named{.upsilon{color:blue}}@keyframes phi{0%{opacity:0}}.between-frames{margin:2px}@keyframes phi{to{opacity:1}}",
5+
".alpha,.gamma{color:red;background:blue}.spacer{margin:0}.delta{color:blue}.between{color:green}.epsilon{color:blue}.zeta{margin-top:0}.shorthand{margin:1px}.eta{margin-top:0}.theta{padding:0}@media print{.printed{width:0}}.iota{padding:0}.kappa{outline:0}.spacing{margin:2px}.lambda-is-a-long-name{outline:0}@media (width>=100px){.mu{color:red}.nu{color:blue}.xi{color:lime}}.apart{margin:0}.apart-again{padding:0}@media (width>=200px){.omicron{color:red}}.shadows{color:green}@media (width>=200px){.pi{color:blue}}@media (width>=300px){.rho{width:0}}@media (width>=400px){.opaque{height:0}}@media (width>=300px){.sigma{width:1px}}@layer named{.tau{color:red}}.between-layers{margin:1px}@layer named{.upsilon{color:blue}}@layer{.chi{color:red}}.between-anonymous{margin:3px}@layer{.psi{color:blue}}@keyframes phi{0%{opacity:0}}.between-frames{margin:2px}@keyframes phi{to{opacity:1}}",
66
]
77
`;

‎test/configCases/css/minimize-merge-distant-rules/__snapshots__/ConfigTest.snap‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
exports[`ConfigTestCases css minimize-merge-distant-rules minimize-merge-distant-rules should compile 1`] = `
44
Array [
5-
".alpha,.gamma{color:red;background:blue}.spacer{margin:0}.delta{color:blue}.between{color:green}.epsilon{color:blue}.zeta{margin-top:0}.shorthand{margin:1px}.eta{margin-top:0}.theta{padding:0}@media print{.printed{width:0}}.iota{padding:0}.kappa{outline:0}.spacing{margin:2px}.lambda-is-a-long-name{outline:0}@media (width>=100px){.mu{color:red}.nu{color:blue}.xi{color:lime}}.apart{margin:0}.apart-again{padding:0}@media (width>=200px){.omicron{color:red}}.shadows{color:green}@media (width>=200px){.pi{color:blue}}@media (width>=300px){.rho{width:0}}@media (width>=400px){.opaque{height:0}}@media (width>=300px){.sigma{width:1px}}@layer named{.tau{color:red}}.between-layers{margin:1px}@layer named{.upsilon{color:blue}}@keyframes phi{0%{opacity:0}}.between-frames{margin:2px}@keyframes phi{to{opacity:1}}",
5+
".alpha,.gamma{color:red;background:blue}.spacer{margin:0}.delta{color:blue}.between{color:green}.epsilon{color:blue}.zeta{margin-top:0}.shorthand{margin:1px}.eta{margin-top:0}.theta{padding:0}@media print{.printed{width:0}}.iota{padding:0}.kappa{outline:0}.spacing{margin:2px}.lambda-is-a-long-name{outline:0}@media (width>=100px){.mu{color:red}.nu{color:blue}.xi{color:lime}}.apart{margin:0}.apart-again{padding:0}@media (width>=200px){.omicron{color:red}}.shadows{color:green}@media (width>=200px){.pi{color:blue}}@media (width>=300px){.rho{width:0}}@media (width>=400px){.opaque{height:0}}@media (width>=300px){.sigma{width:1px}}@layer named{.tau{color:red}}.between-layers{margin:1px}@layer named{.upsilon{color:blue}}@layer{.chi{color:red}}.between-anonymous{margin:3px}@layer{.psi{color:blue}}@keyframes phi{0%{opacity:0}}.between-frames{margin:2px}@keyframes phi{to{opacity:1}}",
66
]
77
`;

‎test/configCases/css/minimize-merge-distant-rules/style.css‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@
4747
.between-layers { margin: 1px }
4848
@layer named { .upsilon { color: blue } }
4949

50+
/* ...and an anonymous one opens a layer of its own every time it is written,
51+
so two of them are two layers rather than one condition stated twice. */
52+
@layer { .chi { color: red } }
53+
.between-anonymous { margin: 3px }
54+
@layer { .psi { color: blue } }
55+
5056
/* `@keyframes` is redeclared by a later copy rather than added to. */
5157
@keyframes phi { from { opacity: 0 } }
5258
.between-frames { margin: 2px }

‎test/configCases/css/minimize-merge-distant-rules/test.config.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ module.exports = {
2323
expect(css).not.toContain(".omicron{color:red}.pi");
2424
expect(css).not.toContain(".rho{width:0}.sigma");
2525
expect(css).not.toContain(".tau{color:red}.upsilon");
26-
expect(css).not.toContain("opacity:0}.5");
26+
expect(css).not.toContain(".chi{color:red}.psi");
27+
expect(css).toContain(
28+
"@keyframes phi{0%{opacity:0}}.between-frames{margin:2px}@keyframes phi{to{opacity:1}}"
29+
);
2730
}
2831
};

0 commit comments

Comments
 (0)