Skip to content

Commit d174cdf

Browse files
perf(css): drop a fallback every browserslist target reads past (#22158)
* perf(css): drop a fallback every browserslist target reads past A declaration is a fallback only for an engine that cannot read the one after it. `removeDeadRules` had no way to ask which engines those are, so it dropped an earlier declaration only where the later one restated it — identical text, a number in the same unit, a hex for a hex, an old color name. Everything else was kept, including a hex standing before an `oklch()` on a target that reads every color function there is. `collectValueSupport` derives the missing answer from the compat data already in the tree: a color function by its own name, and a property's keyword as `property keyword`, taken only where BCD gives the construct a row of its own — a parent's row answers for what was filed under it, not for a name it never mentions, which is why `canvastext` is absent rather than dated by its ancient `<system-color>` parent. The table is packed as one string and unpacked on the first lookup a build makes, which costs 3.7x less than the `Map` literal and nothing at all to a build that never asks. Reading it is `_valueReadByTarget`, and it answers only for a value whose every construct it can name: a bare keyword, or one color function over numbers and named colors. Anything else — a relative color, a mix naming its space, a `color()` naming a predefined one — stays unknown, so the declaration before it stands. With no browserslist selection there are no engines to name and nothing changes. * test(css): cover the value-support reader where the minimizer's worker hides it `minimize-dead-fallbacks` drives `_valueReadByTarget`'s whole call branch, but the minimizer runs it inside the worker pool, where coverage cannot follow — the integration flag reports 16.27% of the diff against the unit flag's 80.76%, so the branch reads as untested. Cover it from `CssSyntax.unittest.js`, which runs the printer in process: one case per argument kind it can name and per kind it cannot, both sides of the target gate, and the empty call whose guard is load-bearing — without it an invalid `light-dark()` would drop the declaration before it. `collectValueSupport` takes the BCD property tree as a defaulted argument, as `collectLaterColorNames` and `collectColorFunctions` already do, so a stub can drive the grammar filter (`is_transitionable` is no value of `display`) and the row-less tree its throw names. * fix(css): keep a fallback where the selection names an uncovered browser `_useBrowsers` skips a browserslist name no compat dataset answers for — `op_mini`, `and_uc`, `and_qq`, `baidu`, `kaios`, `bb` — so a selection naming one alongside a covered browser was answered for in part. With `chrome 130, op_mini all`, `_readsAll` consulted Chrome alone and dropped the hex before a `lab()`, leaving Opera Mini nothing it can read. Reported by CodeRabbit on #22158 and reproduced before fixing. The skip itself is right for prefixes and wrong here, and the two want opposite answers: reaching for a spelling an uncovered browser may not read costs it nothing, while taking away the declaration before one takes away the only spelling it had. So the skip is noted rather than only taken, and `_valueReadByTarget` declines for the whole selection when any of it went unanswered; prefixing is left exactly as it was.
1 parent 39f14f2 commit d174cdf

22 files changed

Lines changed: 1127 additions & 17 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+
Drop a fallback declaration every `browserslist` target reads past.

‎cspell.json‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
"contenturl",
8686
"contextifies",
8787
"contextify",
88+
"crispedges",
8889
"crossorigin",
8990
"cspellcache",
9091
"cssmodules",
@@ -163,6 +164,7 @@
163164
"funcindex",
164165
"functype",
165166
"gcov",
167+
"geometricprecision",
166168
"gibibytes",
167169
"gitattributes",
168170
"gitsubmodule",
@@ -229,6 +231,7 @@
229231
"libuv",
230232
"lightgoldenrodyellow",
231233
"lightningcss",
234+
"linearrgb",
232235
"linkedom",
233236
"lintunit",
234237
"loadername",
@@ -283,6 +286,7 @@
283286
"mynamespace",
284287
"Natsu",
285288
"navigations",
289+
"nesw",
286290
"Neue",
287291
"Nitin",
288292
"nmodule",
@@ -304,12 +308,15 @@
304308
"nwjs",
305309
"oklab",
306310
"oklch",
311+
"oldstyle",
307312
"omittable",
308313
"onbeforeload",
309314
"onconnect",
310315
"oneline",
311316
"opencollective",
312317
"opensource",
318+
"optimizelegibility",
319+
"optimizespeed",
313320
"opuuus",
314321
"ource",
315322
"overridable",
@@ -483,6 +490,7 @@
483490
"toplevel",
484491
"totalmem",
485492
"traceur",
493+
"transitionable",
486494
"translatex",
487495
"translatey",
488496
"translatez",
@@ -502,6 +510,7 @@
502510
"undefine",
503511
"undelayed",
504512
"unexception",
513+
"unicase",
505514
"Uninstantiated",
506515
"uniquename",
507516
"unitless",
@@ -523,6 +532,9 @@
523532
"valtype",
524533
"valuetype",
525534
"viewports",
535+
"visiblefill",
536+
"visiblepainted",
537+
"visiblestroke",
526538
"vmax",
527539
"vmin",
528540
"walltime",

‎lib/css/data.js‎

Lines changed: 520 additions & 1 deletion
Large diffs are not rendered by default.

‎lib/css/syntax.js‎

Lines changed: 128 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ const {
138138
UNIT_CONVERSION_TARGETS,
139139
UNIT_GROUP_BASE,
140140
UNSHARED_LONGHAND_KEYWORDS,
141+
VALUE_SUPPORT_PACKED,
141142
X_AXIS_TRANSFORMS,
142143
ZERO_ANGLE_FUNCTIONS,
143144
ZERO_UNIT_KEEPING_PROPERTIES,
@@ -6756,6 +6757,7 @@ const grammar = (input, visitors, writer, options) => {
67566757
if (browsers === undefined) {
67576758
_seenPrefixRules = null;
67586759
_prefixBrowsers = null;
6760+
_someBrowserUnanswered = false;
67596761
_prefixingOn = false;
67606762
} else {
67616763
_useBrowsers(browsers);
@@ -6915,6 +6917,7 @@ const grammar = (input, visitors, writer, options) => {
69156917
// The parsed selection itself stays in `_parsedBrowsersMemo`, which the next
69166918
// asset of the same build reuses; only this parse's pointer is dropped.
69176919
_prefixBrowsers = null;
6920+
_someBrowserUnanswered = false;
69186921
_prefixDropCandidate = null;
69196922
// Drop the module-level column references so the last parsed source (and
69206923
// its LocConverter / child lists / visitors) don't stay alive between
@@ -8341,6 +8344,10 @@ let _browsersArray = null;
83418344
let _browsersKey = null;
83428345
/** @type {(number[] | undefined)[] | null} */
83438346
let _parsedBrowsersMemo = null;
8347+
// Whether the selection named a browser no compat dataset answers for, which
8348+
// is what a rewrite taking a declaration away has to decline on.
8349+
let _someBrowserUnanswered = false;
8350+
let _someBrowserUnansweredMemo = false;
83448351
// Each axis' table -> its per-construct decision for the selection in hand. Kept
83458352
// per table rather than under one tagged key, so a lookup allocates no string.
83468353
/** @type {Map<Map<string, [string, number][]>, Map<string, Set<string> | null>>} */
@@ -8368,6 +8375,7 @@ const _useBrowsers = (browsers) => {
83688375
// it does not, which is a worker handed each asset's own copy.
83698376
if (browsers === _browsersArray) {
83708377
_prefixBrowsers = _parsedBrowsersMemo;
8378+
_someBrowserUnanswered = _someBrowserUnansweredMemo;
83718379
return;
83728380
}
83738381
_browsersArray = browsers;
@@ -8380,17 +8388,28 @@ const _useBrowsers = (browsers) => {
83808388
/** @type {(number[] | undefined)[]} */
83818389
const parsed = Array.from({ length: SUPPORT_BROWSERS.length });
83828390
let named = 0;
8391+
let unanswered = false;
83838392
for (const entry of browsers) {
83848393
const space = entry.indexOf(" ");
83858394
const name = space === -1 ? entry : entry.slice(0, space);
83868395
const version =
83878396
space === -1 ? null : _encodeBrowserVersion(entry.slice(space + 1));
8388-
// A browserslist name no compat dataset covers, or a version that did not
8389-
// parse, is skipped — the same browsers lightningcss's target mapping
8390-
// drops, so both minifiers prefix for the same selection.
8397+
// A browserslist name no compat dataset covers, or a version that did
8398+
// not parse, is skipped — the same browsers lightningcss's target
8399+
// mapping drops, so both minifiers prefix for the same selection.
8400+
8401+
// WHY: noted rather than only skipped, because the two readers of this
8402+
// want opposite answers for a browser nothing states. Reaching for a
8403+
// spelling it may not read costs it nothing, so prefixing goes on
8404+
// without it; dropping the declaration before one takes away the only
8405+
// spelling it could have read, so `_valueReadByTarget` declines for the
8406+
// whole selection instead.
83918407
const slot =
83928408
version === null ? undefined : SUPPORT_BROWSER_SLOT.get(name);
8393-
if (slot === undefined) continue;
8409+
if (slot === undefined) {
8410+
unanswered = true;
8411+
continue;
8412+
}
83948413
// Every selected version is kept: a prefix window is an interval, so an
83958414
// older selection outside it does not answer for a newer one inside.
83968415
const versions = parsed[slot];
@@ -8402,8 +8421,10 @@ const _useBrowsers = (browsers) => {
84028421
}
84038422
}
84048423
_parsedBrowsersMemo = named === 0 ? null : parsed;
8424+
_someBrowserUnansweredMemo = unanswered;
84058425
}
84068426
_prefixBrowsers = _parsedBrowsersMemo;
8427+
_someBrowserUnanswered = _someBrowserUnansweredMemo;
84078428
};
84088429

84098430
/**
@@ -8427,6 +8448,36 @@ const _encodeBrowserVersion = (version) => {
84278448
return major * 100000 + minor;
84288449
};
84298450

8451+
// `VALUE_SUPPORT_PACKED` unpacked, on the first lookup a build makes: only a
8452+
// build with a target and two declarations of one property asks.
8453+
/** @type {Map<string, number> | null} */
8454+
let _valueSupport = null;
8455+
8456+
/**
8457+
* When every target browser first read one value, by the spelling
8458+
* `VALUE_SUPPORT_PACKED` names it: `"<property> <keyword>"` for a keyword a
8459+
* property's own grammar states, the bare name for a color function.
8460+
* @param {string} key the spelling
8461+
* @returns {number | undefined} its `SUPPORT_PROFILES` row, or undefined
8462+
*/
8463+
const _valueSupportedFrom = (key) => {
8464+
let table = _valueSupport;
8465+
if (table === null) {
8466+
table = new Map();
8467+
for (const group of VALUE_SUPPORT_PACKED.split("|")) {
8468+
const colon = group.indexOf(":");
8469+
const property = group.slice(0, colon);
8470+
const prefix = property.length === 0 ? "" : `${property} `;
8471+
const entries = group.slice(colon + 1).split(" ");
8472+
for (let at = 0; at < entries.length; at += 2) {
8473+
table.set(prefix + entries[at], Number(entries[at + 1]));
8474+
}
8475+
}
8476+
_valueSupport = table;
8477+
}
8478+
return table.get(key);
8479+
};
8480+
84308481
/**
84318482
* Whether every target browser reads a CSS ability, so a spelling that needs it
84328483
* may be reached for. No selection names no browser to answer for, so the
@@ -14294,6 +14345,62 @@ const _familyValue = (property, longhands, values) => {
1429414345
return _joinSlots(dropped === null ? values : dropped);
1429514346
};
1429614347

14348+
/**
14349+
* Whether every target browser reads a later declaration's whole value, which
14350+
* leaves an earlier one of the same property carrying nothing rather than
14351+
* standing as its fallback. Only a value whose every construct is named in
14352+
* `VALUE_SUPPORT_PACKED` answers yes: a keyword the property's own grammar
14353+
* states, or one color function over numbers and named colors.
14354+
* @param {string} property the printed property name
14355+
* @param {string} value the later declaration's printed value
14356+
* @returns {boolean} true when no target is left reading the earlier one
14357+
*/
14358+
const _valueReadByTarget = (property, value) => {
14359+
// No selection names no browser to answer for, and one naming a browser the
14360+
// tables do not cover answers for less than the whole of it; either way
14361+
// nothing is known to read the later value and the earlier one stands.
14362+
if (_prefixBrowsers === null || _someBrowserUnanswered) return false;
14363+
const open = value.indexOf("(");
14364+
if (open === -1) {
14365+
return _readsAll(
14366+
_valueSupportedFrom(`${property} ${toLowerCaseIfNeeded(value)}`)
14367+
);
14368+
}
14369+
if (
14370+
open === 0 ||
14371+
value.includes("(", open + 1) ||
14372+
value.charCodeAt(value.length - 1) !== CC_RIGHT_PARENTHESIS
14373+
) {
14374+
return false;
14375+
}
14376+
if (
14377+
!_readsAll(_valueSupportedFrom(toLowerCaseIfNeeded(value.slice(0, open))))
14378+
) {
14379+
return false;
14380+
}
14381+
const args = value.slice(open + 1, -1).match(_COLOR_ARGUMENT_RE);
14382+
if (args === null) return false;
14383+
for (const argument of args) {
14384+
if (_COLOR_NUMBER_RE.test(argument) || _COLOR_HUE_RE.test(argument)) {
14385+
continue;
14386+
}
14387+
// A hex of three or six digits is read wherever a color is; four and eight
14388+
// state an alpha, which is the target's own question and not asked here.
14389+
if (_LONE_HEX_RE.test(argument)) {
14390+
if (argument.length === 4 || argument.length === 7) continue;
14391+
return false;
14392+
}
14393+
// A named color reaches as far as naming one does, bar the two that
14394+
// arrived after the list and carry a row of their own.
14395+
const lowered = toLowerCaseIfNeeded(argument);
14396+
if (COLOR_NAME_TO_RGB.has(lowered) && !LATER_COLOR_NAMES.has(lowered)) {
14397+
continue;
14398+
}
14399+
return false;
14400+
}
14401+
return true;
14402+
};
14403+
1429714404
/**
1429814405
* Whether a later declaration of one property leaves an earlier one carrying
1429914406
* nothing: the cascade reads the later, and it writes every name the earlier
@@ -14303,6 +14410,7 @@ const _familyValue = (property, longhands, values) => {
1430314410
* @param {string} laterValue its printed value
1430414411
* @param {Node} earlier the earlier declaration of the same property
1430514412
* @param {string} earlierValue its printed value
14413+
* @param {string} property the printed property both write
1430614414
* @param {boolean} names whether the property takes a `<custom-ident>` list
1430714415
* @param {boolean} keyframe whether the block is a keyframe
1430814416
* @returns {boolean} true when the earlier one is read for nothing
@@ -14313,6 +14421,7 @@ const _laterWriteWins = (
1431314421
laterValue,
1431414422
earlier,
1431514423
earlierValue,
14424+
property,
1431614425
names,
1431714426
keyframe
1431814427
) => {
@@ -14322,6 +14431,9 @@ const _laterWriteWins = (
1432214431
// An `!important` earlier one still wins over a later plain declaration,
1432314432
// which leaves the later dead rather than it.
1432414433
if (!important && path.important(earlier)) return false;
14434+
// A target that reads the later value everywhere is one nothing falls back
14435+
// from, whatever the two values have in common.
14436+
if (_valueReadByTarget(property, laterValue)) return true;
1432514437
return names
1432614438
? _coveredByLater(laterValue, earlierValue)
1432714439
: _sameVocabulary(laterValue, earlierValue);
@@ -14351,13 +14463,15 @@ const _writeIsDead = (path, writer, earlier, later, keyframe) => {
1435114463
) {
1435214464
return false;
1435314465
}
14466+
const property = _printedProperty(laterText, laterColon);
1435414467
return _laterWriteWins(
1435514468
path,
1435614469
later,
1435714470
_printedValue(laterText, laterColon),
1435814471
earlier,
1435914472
_printedValue(earlierText, earlierColon),
14360-
CUSTOM_IDENT_LIST_PROPERTIES.has(_printedProperty(laterText, laterColon)),
14473+
property,
14474+
CUSTOM_IDENT_LIST_PROPERTIES.has(property),
1436114475
keyframe
1436214476
);
1436314477
};
@@ -16570,14 +16684,14 @@ const _composeBlockBody = (
1657016684
}
1657116685
// WHY: only the last of a set of declarations of one property can be read, so
1657216686
// the earlier carry nothing, whatever stands between them. Identical text
16573-
// always resolves that way; a differently spelled value only where the later
16574-
// writes every name the earlier does, since otherwise the earlier is its
16575-
// fallback — dropping those is how esbuild and lightningcss lose a
16576-
// `color(display-p3 …)` pair.
16577-
// TODO offer the wider "drop whatever a later declaration overrides" behind an
16578-
// option, as csso and cssnano expose theirs. It needs support data per
16579-
// keyword: a bare ident may be invalid, or newer than the value it stands
16580-
// after, and neither supersedes anything.
16687+
// always resolves that way; a differently spelled value where the later writes
16688+
// every name the earlier does, or where every target reads it
16689+
// (`_valueReadByTarget`), since otherwise the earlier is its fallback —
16690+
// dropping those is how esbuild and lightningcss lose a `color(display-p3 …)`
16691+
// pair.
16692+
// TODO answer the same question with no browserslist selection, as csso and
16693+
// cssnano do: theirs is unconditional, and naming the engines is what makes
16694+
// this one sound — with no selection there are none to name.
1658116695
/** @type {Set<number> | null} */
1658216696
let superseded = null;
1658316697
// CSS Animations 1 §3: a keyframe ignores an `!important` declaration, so one
@@ -16647,6 +16761,7 @@ const _composeBlockBody = (
1664716761
value,
1664816762
items[before],
1664916763
written,
16764+
property,
1665016765
names,
1665116766
keyframe
1665216767
)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`ConfigCacheTestCases css minimize-dead-fallbacks-legacy minimize-dead-fallbacks-legacy should compile 1`] = `
4+
Array [
5+
"/*! kept: chrome 60 reads no \`lab()\`, so the hex is the only color it gets */.t01{color:#c65d06;color:lab(50% 100 -100)}",
6+
"/*! ...nor \`oklch()\` */.t02{color:#7d2181;color:oklch(90% .3 140)}",
7+
"/*! ...nor \`lch()\` */.t03{color:red;color:lch(50% 130 20)}",
8+
"/*! ...and an \`!important\` later one it cannot read is still unreadable */.t04{color:red;color:oklch(90% .3 140)!important}",
9+
"/*! ...nor \`light-dark()\`, whose arguments this can name either way */.t04a{color:red;color:light-dark(#fff,#000)}",
10+
"/*! a keyword every selected engine does read is dropped here as well, the
11+
question being the target rather than the age of the spelling */.t05{display:flex}",
12+
"/*! ...where one of them does not, the fallback stands (and the spelling it
13+
reads is written beside it) */.t06{overflow:hidden;overflow:-moz-hidden-unscrollable;overflow:clip}",
14+
"/*! ...same */.t07{width:auto;width:-moz-fit-content;width:fit-content}",
15+
]
16+
`;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`ConfigTestCases css minimize-dead-fallbacks-legacy minimize-dead-fallbacks-legacy should compile 1`] = `
4+
Array [
5+
"/*! kept: chrome 60 reads no \`lab()\`, so the hex is the only color it gets */.t01{color:#c65d06;color:lab(50% 100 -100)}",
6+
"/*! ...nor \`oklch()\` */.t02{color:#7d2181;color:oklch(90% .3 140)}",
7+
"/*! ...nor \`lch()\` */.t03{color:red;color:lch(50% 130 20)}",
8+
"/*! ...and an \`!important\` later one it cannot read is still unreadable */.t04{color:red;color:oklch(90% .3 140)!important}",
9+
"/*! ...nor \`light-dark()\`, whose arguments this can name either way */.t04a{color:red;color:light-dark(#fff,#000)}",
10+
"/*! a keyword every selected engine does read is dropped here as well, the
11+
question being the target rather than the age of the spelling */.t05{display:flex}",
12+
"/*! ...where one of them does not, the fallback stands (and the spelling it
13+
reads is written beside it) */.t06{overflow:hidden;overflow:-moz-hidden-unscrollable;overflow:clip}",
14+
"/*! ...same */.t07{width:auto;width:-moz-fit-content;width:fit-content}",
15+
]
16+
`;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import "./style.css";
2+
3+
it("should drop a fallback every target reads past", () => {
4+
// The emitted stylesheet is snapshotted in test.config.js (afterExecute);
5+
// this keeps a runnable entry so the chunk and its `.css` are produced.
6+
expect(true).toBe(true);
7+
});

0 commit comments

Comments
 (0)