Skip to content

Commit 4700010

Browse files
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.
1 parent d7e0fd9 commit 4700010

21 files changed

Lines changed: 969 additions & 13 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: 11 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",
@@ -502,6 +509,7 @@
502509
"undefine",
503510
"undelayed",
504511
"unexception",
512+
"unicase",
505513
"Uninstantiated",
506514
"uniquename",
507515
"unitless",
@@ -523,6 +531,9 @@
523531
"valtype",
524532
"valuetype",
525533
"viewports",
534+
"visiblefill",
535+
"visiblepainted",
536+
"visiblestroke",
526537
"vmax",
527538
"vmin",
528539
"walltime",

‎lib/css/data.js‎

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

‎lib/css/syntax.js‎

Lines changed: 103 additions & 9 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,
@@ -8409,6 +8410,36 @@ const _encodeBrowserVersion = (version) => {
84098410
return major * 100000 + minor;
84108411
};
84118412

8413+
// `VALUE_SUPPORT_PACKED` unpacked, on the first lookup a build makes: only a
8414+
// build with a target and two declarations of one property asks.
8415+
/** @type {Map<string, number> | null} */
8416+
let _valueSupport = null;
8417+
8418+
/**
8419+
* When every target browser first read one value, by the spelling
8420+
* `VALUE_SUPPORT_PACKED` names it: `"<property> <keyword>"` for a keyword a
8421+
* property's own grammar states, the bare name for a color function.
8422+
* @param {string} key the spelling
8423+
* @returns {number | undefined} its `SUPPORT_PROFILES` row, or undefined
8424+
*/
8425+
const _valueSupportedFrom = (key) => {
8426+
let table = _valueSupport;
8427+
if (table === null) {
8428+
table = new Map();
8429+
for (const group of VALUE_SUPPORT_PACKED.split("|")) {
8430+
const colon = group.indexOf(":");
8431+
const property = group.slice(0, colon);
8432+
const prefix = property.length === 0 ? "" : `${property} `;
8433+
const entries = group.slice(colon + 1).split(" ");
8434+
for (let at = 0; at < entries.length; at += 2) {
8435+
table.set(prefix + entries[at], Number(entries[at + 1]));
8436+
}
8437+
}
8438+
_valueSupport = table;
8439+
}
8440+
return table.get(key);
8441+
};
8442+
84128443
/**
84138444
* Whether every target browser reads a CSS ability, so a spelling that needs it
84148445
* may be reached for. No selection names no browser to answer for, so the
@@ -14276,6 +14307,61 @@ const _familyValue = (property, longhands, values) => {
1427614307
return _joinSlots(dropped === null ? values : dropped);
1427714308
};
1427814309

14310+
/**
14311+
* Whether every target browser reads a later declaration's whole value, which
14312+
* leaves an earlier one of the same property carrying nothing rather than
14313+
* standing as its fallback. Only a value whose every construct is named in
14314+
* `VALUE_SUPPORT_PACKED` answers yes: a keyword the property's own grammar
14315+
* states, or one color function over numbers and named colors.
14316+
* @param {string} property the printed property name
14317+
* @param {string} value the later declaration's printed value
14318+
* @returns {boolean} true when no target is left reading the earlier one
14319+
*/
14320+
const _valueReadByTarget = (property, value) => {
14321+
// No selection names no browser to answer for, so nothing is known to read
14322+
// the later value and the earlier one stands.
14323+
if (_prefixBrowsers === null) return false;
14324+
const open = value.indexOf("(");
14325+
if (open === -1) {
14326+
return _readsAll(
14327+
_valueSupportedFrom(`${property} ${toLowerCaseIfNeeded(value)}`)
14328+
);
14329+
}
14330+
if (
14331+
open === 0 ||
14332+
value.includes("(", open + 1) ||
14333+
value.charCodeAt(value.length - 1) !== CC_RIGHT_PARENTHESIS
14334+
) {
14335+
return false;
14336+
}
14337+
if (
14338+
!_readsAll(_valueSupportedFrom(toLowerCaseIfNeeded(value.slice(0, open))))
14339+
) {
14340+
return false;
14341+
}
14342+
const args = value.slice(open + 1, -1).match(_COLOR_ARGUMENT_RE);
14343+
if (args === null) return false;
14344+
for (const argument of args) {
14345+
if (_COLOR_NUMBER_RE.test(argument) || _COLOR_HUE_RE.test(argument)) {
14346+
continue;
14347+
}
14348+
// A hex of three or six digits is read wherever a color is; four and eight
14349+
// state an alpha, which is the target's own question and not asked here.
14350+
if (_LONE_HEX_RE.test(argument)) {
14351+
if (argument.length === 4 || argument.length === 7) continue;
14352+
return false;
14353+
}
14354+
// A named color reaches as far as naming one does, bar the two that
14355+
// arrived after the list and carry a row of their own.
14356+
const lowered = toLowerCaseIfNeeded(argument);
14357+
if (COLOR_NAME_TO_RGB.has(lowered) && !LATER_COLOR_NAMES.has(lowered)) {
14358+
continue;
14359+
}
14360+
return false;
14361+
}
14362+
return true;
14363+
};
14364+
1427914365
/**
1428014366
* Whether a later declaration of one property leaves an earlier one carrying
1428114367
* nothing: the cascade reads the later, and it writes every name the earlier
@@ -14285,6 +14371,7 @@ const _familyValue = (property, longhands, values) => {
1428514371
* @param {string} laterValue its printed value
1428614372
* @param {Node} earlier the earlier declaration of the same property
1428714373
* @param {string} earlierValue its printed value
14374+
* @param {string} property the printed property both write
1428814375
* @param {boolean} names whether the property takes a `<custom-ident>` list
1428914376
* @param {boolean} keyframe whether the block is a keyframe
1429014377
* @returns {boolean} true when the earlier one is read for nothing
@@ -14295,6 +14382,7 @@ const _laterWriteWins = (
1429514382
laterValue,
1429614383
earlier,
1429714384
earlierValue,
14385+
property,
1429814386
names,
1429914387
keyframe
1430014388
) => {
@@ -14304,6 +14392,9 @@ const _laterWriteWins = (
1430414392
// An `!important` earlier one still wins over a later plain declaration,
1430514393
// which leaves the later dead rather than it.
1430614394
if (!important && path.important(earlier)) return false;
14395+
// A target that reads the later value everywhere is one nothing falls back
14396+
// from, whatever the two values have in common.
14397+
if (_valueReadByTarget(property, laterValue)) return true;
1430714398
return names
1430814399
? _coveredByLater(laterValue, earlierValue)
1430914400
: _sameVocabulary(laterValue, earlierValue);
@@ -14333,13 +14424,15 @@ const _writeIsDead = (path, writer, earlier, later, keyframe) => {
1433314424
) {
1433414425
return false;
1433514426
}
14427+
const property = _printedProperty(laterText, laterColon);
1433614428
return _laterWriteWins(
1433714429
path,
1433814430
later,
1433914431
_printedValue(laterText, laterColon),
1434014432
earlier,
1434114433
_printedValue(earlierText, earlierColon),
14342-
CUSTOM_IDENT_LIST_PROPERTIES.has(_printedProperty(laterText, laterColon)),
14434+
property,
14435+
CUSTOM_IDENT_LIST_PROPERTIES.has(property),
1434314436
keyframe
1434414437
);
1434514438
};
@@ -16499,14 +16592,14 @@ const _composeBlockBody = (
1649916592
}
1650016593
// WHY: only the last of a set of declarations of one property can be read, so
1650116594
// the earlier carry nothing, whatever stands between them. Identical text
16502-
// always resolves that way; a differently spelled value only where the later
16503-
// writes every name the earlier does, since otherwise the earlier is its
16504-
// fallback — dropping those is how esbuild and lightningcss lose a
16505-
// `color(display-p3 …)` pair.
16506-
// TODO offer the wider "drop whatever a later declaration overrides" behind an
16507-
// option, as csso and cssnano expose theirs. It needs support data per
16508-
// keyword: a bare ident may be invalid, or newer than the value it stands
16509-
// after, and neither supersedes anything.
16595+
// always resolves that way; a differently spelled value where the later writes
16596+
// every name the earlier does, or where every target reads it
16597+
// (`_valueReadByTarget`), since otherwise the earlier is its fallback —
16598+
// dropping those is how esbuild and lightningcss lose a `color(display-p3 …)`
16599+
// pair.
16600+
// TODO answer the same question with no browserslist selection, as csso and
16601+
// cssnano do: theirs is unconditional, and naming the engines is what makes
16602+
// this one sound — with no selection there are none to name.
1651016603
/** @type {Set<number> | null} */
1651116604
let superseded = null;
1651216605
// CSS Animations 1 §3: a keyframe ignores an `!important` declaration, so one
@@ -16576,6 +16669,7 @@ const _composeBlockBody = (
1657616669
value,
1657716670
items[before],
1657816671
written,
16672+
property,
1657916673
names,
1658016674
keyframe
1658116675
)
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+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* The `minimize-dead-fallbacks` corpus under a selection that needs the
2+
fallbacks: the same declarations, none of them dropped. A pair that survives
3+
the gate here would be one dropped for engines that read only the first. */
4+
5+
/*! kept: chrome 60 reads no `lab()`, so the hex is the only color it gets */
6+
.t01 { color: #c65d06; color: lab(50% 100 -100) }
7+
/*! ...nor `oklch()` */
8+
.t02 { color: #7d2181; color: oklch(90% .3 140) }
9+
/*! ...nor `lch()` */
10+
.t03 { color: red; color: lch(50% 130 20) }
11+
/*! ...and an `!important` later one it cannot read is still unreadable */
12+
.t04 { color: red; color: oklch(90% .3 140) !important }
13+
/*! ...nor `light-dark()`, whose arguments this can name either way */
14+
.t04a { color: red; color: light-dark(#fff, #000) }
15+
/*! a keyword every selected engine does read is dropped here as well, the
16+
question being the target rather than the age of the spelling */
17+
.t05 { display: block; display: flex }
18+
/*! ...where one of them does not, the fallback stands (and the spelling it
19+
reads is written beside it) */
20+
.t06 { overflow: hidden; overflow: clip }
21+
/*! ...same */
22+
.t07 { width: auto; width: fit-content }
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"use strict";
2+
3+
const readMinifiedSections = require("../../../helpers/readMinifiedSections");
4+
5+
module.exports = {
6+
findBundle() {
7+
return ["bundle0.js"];
8+
},
9+
afterExecute(options) {
10+
expect(
11+
readMinifiedSections(options.output.path, "bundle0.css")
12+
).toMatchSnapshot();
13+
}
14+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
// A selection that reads none of the color functions below, so each fallback
6+
// before one is still the only color those engines get.
7+
target: "browserslist: chrome 60, firefox 60, safari 11",
8+
mode: "production",
9+
output: {
10+
pathinfo: false
11+
},
12+
optimization: {
13+
minimize: true,
14+
// `"..."` keeps the default minimizer, which resolves the target.
15+
minimizer: ["..."]
16+
},
17+
experiments: {
18+
css: true
19+
}
20+
};

0 commit comments

Comments
 (0)