Skip to content

Commit b8cd632

Browse files
feat(css): evaluate color(), color-mix() and relative colors (#21928)
* feat(css): evaluate color(), color-mix() and relative colors, and lower four more spellings The color work rests on one table: every predefined color space CSS Color 4 §10 names, derived in the generator from the primaries, white point and transfer function the spec states, as the pair of functions taking linear-light sRGB to its components and back. `lib/css/syntax.js` implements no color space of its own — it reads a color into a space, works one component at a time, and reads it back — and a space added to the generator needs no arithmetic beside it. On that: - `color(<space> …)` folds to the sRGB it names, and gets the same fallback the Lab family does where the target reads no `color()`. - A color sRGB cannot show now takes a `color(display-p3 …)` rung between the clipped hex and the author's own, wherever the target reads one — so an engine reading no Lab family still gets the wider gamut. Skipped where P3 cannot show it either, which is a color no fallback preserves. - `color-mix()` is evaluated in any of the fifteen interpolation spaces, with the four hue methods, percentages either side of a color, and the alphas premultiplied as CSS Color 5 §3 states. - A relative color (`rgb(from rebeccapurple r calc(g * 2) b)`) is resolved against its origin: the channels are bound to the function's own keywords and each argument read against them, an expression included. And three more spellings the target decides: - `system-ui` is written as the stack each platform's own engine reads. - `text-decoration` naming more than its line becomes the line in the shorthand — which is what resets the family — and a longhand per other slot. - `optimization.minimize.css.pseudoClasses` writes a pseudo-class as an ordinary class, at any depth, so a script can apply it where the engine reads none. Every fold declines rather than guesses: a component that cannot be pinned down, a channel on a `.5` boundary, a result outside the gamut it would be written in. A mix in a space reached from sRGB by arithmetic alone is held to the tight boundary margin rather than the Lab family's wide one. * feat(css): write light-dark() as the pair the color scheme switches, and hold every lowering to a real engine `light-dark()` names a color per scheme. For a target that does not read it, the call becomes `var(--webpack-light, A) var(--webpack-dark, B)` — `initial` is the guaranteed-invalid value, so the property set to it takes the fallback and the one set to nothing substitutes nothing — and every rule that says which scheme it is in says it again as that pair, with a `(prefers-color-scheme:dark)` copy where it hands the choice to the user. Two things it does that the ecosystem's version does not. A stylesheet that names no `color-scheme` at all still reads the light half, from a `:where(:root)` rule of no specificity — elsewhere that case leaves a declaration the engine drops. And the call is left alone where a half names a color the target cannot read: substituted, that is invalid at computed-value time, which is `unset` rather than the declaration before it. The whole rewrite needs `:where()`, so a target reading neither is one it leaves alone. That last point is a defect this turn's new check found in what shipped before it: a color fallback pair standing before a value a substitution reaches is bytes nothing ever reads, and is no longer written. The check is the other half of this commit. `syntaxEquivalence.spectest.js` now holds every lowering to a real engine: one that reads both spellings has to compute the same style from either, across color schemes and writing directions, with colors compared as painted rather than as text — the engine keeps a mix at the precision it computed it, while the printer writes the byte it lands on. Beside it, a unit test pins the browser version each rewrite turns on at, either side of the one the compat data gives. * feat(css): write a selector list a pseudo does not take as the :is() it means `:not(a, b)` matches what neither of them matches, which is what `:not(:is(a, b))` says — and `:is()` carries the specificity of its most specific argument, which is the specificity the list inside `:not()` has. `:lang(a, b)` matches either, which is `:is(:lang(a), :lang(b))`. Both need `:is()` itself, so a target reading neither keeps what the author wrote. Each has its own window. Gecko took `:is()` six releases before it took a list inside `:not()`, so that rewrite is written for Firefox 78 through 83 and no other target. No Chromium has ever taken a list inside `:lang()`, so every Chrome target is written the second way. The `:lang()` one is held to a reference rather than to the engine's reading of the source: Chromium reads no spelling of `:lang(en, fr)` at all, so the equivalence check compares the rewrite against the pair of rules that state the same thing.
1 parent 2ee1eb5 commit b8cd632

38 files changed

Lines changed: 3825 additions & 265 deletions
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": minor
3+
---
4+
5+
Evaluate `color()`, `color-mix()` and a relative color, write `light-dark()` as the pair a color scheme switches, and add `optimization.minimize.css.pseudoClasses`.

‎cspell.json‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,13 @@
5050
"callees",
5151
"callme",
5252
"camelcase",
53+
"Cantarell",
5354
"canvastext",
5455
"carryforward",
5556
"chainable",
5657
"checkmark",
58+
"chromaticities",
59+
"chromaticity",
5760
"chunkfilename",
5861
"chunkhash",
5962
"chunkname",
@@ -282,6 +285,7 @@
282285
"noshade",
283286
"nosource",
284287
"nosources",
288+
"Noto",
285289
"novalidate",
286290
"nowrap",
287291
"nwjs",
@@ -334,6 +338,7 @@
334338
"prewalking",
335339
"prioritise",
336340
"promisify",
341+
"prophoto",
337342
"proxied",
338343
"publicpath",
339344
"punct",
@@ -373,20 +378,21 @@
373378
"rotatey",
374379
"rotatez",
375380
"rowspan",
376-
"rspack",
377381
"rrggbb",
378382
"rrggbbaa",
379383
"rrrlll",
384+
"rspack",
380385
"runtime",
381386
"runtimes",
382387
"rvalue",
383388
"samsunginternet",
384389
"sandboxed",
390+
"SAPUI",
385391
"scalex",
386392
"scaley",
387393
"scalez",
388-
"SAPUI",
389394
"sccs",
395+
"Segoe",
390396
"selectedcontent",
391397
"serializables",
392398
"serializer",
@@ -399,6 +405,7 @@
399405
"shama",
400406
"shellcheck",
401407
"skypack",
408+
"slateblue",
402409
"slsh",
403410
"smol",
404411
"snapshotting",

‎declarations/WebpackOptions.d.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2448,6 +2448,16 @@ export interface OptimizationMinimizeCss {
24482448
* @since 5.110.0
24492449
*/
24502450
normalizeQuotes?: boolean;
2451+
/**
2452+
* Each pseudo-class to write as an ordinary class instead, as `{ "focus-visible": "focus-visible" }` — so a script can apply the class where the engine reads no such pseudo. Only a plain pseudo-class is rewritten, wherever it stands in a selector: a pseudo-element (`::hover`) and a functional pseudo of the same name (`:hover(…)`) are not what a class stands in for, and a `:name` inside a quoted attribute value is nobody's pseudo. Nothing applies the class — that is the script's part.
2453+
* @since 5.111.0
2454+
*/
2455+
pseudoClasses?: {
2456+
/**
2457+
* The class to write instead, without its `.`.
2458+
*/
2459+
[k: string]: string;
2460+
};
24512461
/**
24522462
* Compute a call into the shorter call naming the same value: `calc()` and every math function over constants, a transform naming one axis or an identity, a gradient's default direction and its implied stops, an easing function that has a keyword, and a filter function given the amount an omitted argument already means. On by default.
24532463
* @since 5.110.0

‎lib/css/cssMinify.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* re-resolves in the worker — via the installed package, or the dev self-link.
3434
* @param {{ [file: string]: string }} input a single `{ filename: code }` entry
3535
* @param {object=} sourceMap the asset's input source map — the plugin chains it onto the map returned here, so it isn't read directly
36-
* @param {{ as?: "stylesheet" | "block-contents", environment?: CssEnvironment, convertLengthUnits?: boolean, rewriteCustomProperties?: boolean, unusedSymbols?: string[], renderEmbeddedSource?: AsyncEmbeddedSourceRenderer } & CssTransformOptions=} minimizerOptions minimizer options — `environment` carries the target's CSS abilities (see `output.environment`), so a spelling the target cannot read is not reached for; the rest is `optimization.minimize.css` (e.g. `convertLengthUnits`, and the per-transform switches). `renderEmbeddedSource` minifies source this stylesheet embeds, and may be asynchronous: one parse serves both it and the output
36+
* @param {{ as?: "stylesheet" | "block-contents", environment?: CssEnvironment, convertLengthUnits?: boolean, rewriteCustomProperties?: boolean, unusedSymbols?: string[], pseudoClasses?: { [name: string]: string }, renderEmbeddedSource?: AsyncEmbeddedSourceRenderer } & CssTransformOptions=} minimizerOptions minimizer options — `environment` carries the target's CSS abilities (see `output.environment`), so a spelling the target cannot read is not reached for; the rest is `optimization.minimize.css` (e.g. `convertLengthUnits`, and the per-transform switches). `renderEmbeddedSource` minifies source this stylesheet embeds, and may be asynchronous: one parse serves both it and the output
3737
* @returns {Promise<{ code: string, map?: SourceMap, warnings?: (Error | string)[], errors?: (Error | string)[] }>} the minified CSS, its input->output source map (one anchor for the whole of a `block-contents` print, which is emitted as one piece), and what a renderer reported over what this embeds
3838
*/
3939
const cssMinify = async (input, sourceMap, minimizerOptions = {}) => {
@@ -92,6 +92,7 @@ const cssMinify = async (input, sourceMap, minimizerOptions = {}) => {
9292
convertLengthUnits,
9393
rewriteCustomProperties,
9494
unusedSymbols,
95+
pseudoClasses,
9596
renderEmbeddedSource
9697
} = minimizerOptions;
9798
// One set of options, handed to the print. `as` names the production: a
@@ -106,6 +107,7 @@ const cssMinify = async (input, sourceMap, minimizerOptions = {}) => {
106107
convertLengthUnits,
107108
rewriteCustomProperties,
108109
unusedSymbols,
110+
pseudoClasses,
109111
// The per-transform switches stand beside those rather than nested in
110112
// them, so a config names one the way it names `convertLengthUnits`;
111113
// `pickTransforms` is what knows which names those are.

0 commit comments

Comments
 (0)