Skip to content

Commit 3ba1d8f

Browse files
committed
fix(css): join only onto a rule that can lend its selectors
A selector an engine cannot parse invalidates the list it stands in, so growing one onto a rule that is not listable would take the joining selector down with it. Both sides are checked now, as an adjacent join already does.
1 parent d73d8a6 commit 3ba1d8f

3 files changed

Lines changed: 16 additions & 2 deletions

File tree

‎.changeset/030-css-merge-distant-rules.md‎

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

5-
Add `optimization.minimize.css.mergeDistantRules`, joining rules a third stands between.
5+
Add `optimization.minimize.css.mergeDistantRules` to join rules across a gap.

‎lib/css/syntax.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5414,7 +5414,11 @@ const _noteDistantRule = (taken, entry) => {
54145414
for (const property of _blockProperties(block.slice(1, -1))) {
54155415
_eachPropertyPrefix(property, note);
54165416
}
5417-
if (taken === null || _distantRules.has(block)) return;
5417+
// One selector an engine cannot parse invalidates the list it stands in, so a
5418+
// rule that cannot lend its selectors cannot borrow one either.
5419+
if (taken === null || !_entryListable(entry) || _distantRules.has(block)) {
5420+
return;
5421+
}
54185422
// The whole rule is the piece's first span, and only where the node printed
54195423
// one — a body a nested rule split does not describe itself.
54205424
const span = taken.spans[0];

‎test/CssSyntax.unittest.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7374,6 +7374,16 @@ describe("SourceProcessor — mergeDistantRules", () => {
73747374
expect(out.match(/margin:0/g)).toHaveLength(1);
73757375
});
73767376

7377+
it.each(["::-moz-placeholder", "::-webkit-scrollbar", ":local(.x)"])(
7378+
"declines %s as the rule a list would grow onto",
7379+
(selector) => {
7380+
// One selector an engine cannot parse invalidates the list it stands in,
7381+
// so joining onto it would take the other selector down with it.
7382+
const sheet = `${selector}{color:red}.b{margin:0}.c{color:red}`;
7383+
expect(minify(sheet, true)).toBe(sheet);
7384+
}
7385+
);
7386+
73777387
it("merges rules nested in an at-rule", () => {
73787388
expect(
73797389
minify("@media print{.a{color:red}.b{margin:0}.c{color:red}}", true)

0 commit comments

Comments
 (0)