Skip to content

Commit a93d53b

Browse files
feat(css): lower nesting, and add customMedia / rewriteDirSelector (#21950)
* feat(css): lower nesting, and add `customMedia` / `rewriteDirSelector` A target reading no CSS nesting was handed the nesting as written, so the rules in it were never read. They are now written on their own, `&` standing for the parent as `:is()` of it says — declined where the target reads no `:is()` to write it with, where a declaration follows a nested rule (hoisting it would let the rule win against a declaration read after it), and for a rule written in an at-rule, which is what the rules in it are read under. `color-mix()` was named by `_UNREADABLE_COLOR_CALL_RE` but not by `_FALLBACK_COLOR_FUNCTION_RE`, so a target reading none of it was left with a declaration it drops and no fallback before it. A shorthand lowered into longhands now carries the fallback against the longhand naming the color rather than against the shorthand the target never reads. `customMedia` writes the query a `@custom-media` names wherever a condition asks for it. `rewriteDirSelector` writes `:dir()` as the `[dir]` attribute selector; both are off until asked for, the first being a draft no browser ships unflagged and the second reading the attribute rather than the direction an element may inherit. * test(css): cover every branch the nesting scanners take Codecov read 11 lines of the new code uncovered: the escape, quote and bracket branches of the three selector scanners, and the guards around them. Each is now driven by a selector that reaches it — a quoted space, combinator or comma inside an attribute value, an escape written before the `&`, a pseudo or attribute holding a combinator of its own, and the part a dropped pseudo-element colon leaves empty. Two guards were dead rather than uncovered and are gone: the rule asking `_declarationsPrecedeRules` is one of the rules it reads, so that list is never empty, and `_withColorFallback` is handed text it built from a longhand and its value, so the name before the `:` is always there. * fix(css): keep a rewritten nested rule, and group a custom media list Two the review found, both confirmed against the code first. A nested rule the prefixes rewrote was dropped whole: the caller handed `_writeNested` no block for it, and the hoist then queued nothing and answered null, so the rule went nowhere. It is several rules with preludes of their own and no one rule to take out, so it stays where it was written; the rules written in it still come out against it. A `@custom-media` naming alternatives was written into a condition whatever stood beside it, leaving an `and` binding to the last alternative alone — `screen and (--m)` for `(a),(b)` says `(screen and a) or b`. CSS has no parentheses to group a list inside a condition, so one is now written only where the condition is the whole prelude; a query of one condition is written wherever it is asked for. * test(css): cover the paths that decline to lower through a real build The nesting, custom-media and color-mix declines were reached only by the unit tier, so a real build exercised one branch of each pair. * feat(css): resolve `@custom-selector` under one option with `@custom-media` The two are one draft and one build-time substitution, so the minimizer takes one switch for both: `customMedia` becomes `resolveCustomAtRules`, which now also writes what a `@custom-selector` name stands for. A name is written as `:is(…)` whatever its list holds, matching what `module.parser.css.customSelectors` already writes, so the same stylesheet reads the same either way. * chore: keep the changeset sentence under the length limit * feat(css): put case folding and escape rewriting under options Both rewrote a stylesheet with every option already off, so a caller asking the printer to change nothing still got a changed sheet. Over the repository's 619 stylesheets that closes 29 of the 42 that did not round trip, leaving `!important` (a keyword the printer writes rather than copies), a `data:` payload's percent escapes, and error recovery.
1 parent c04058e commit a93d53b

20 files changed

Lines changed: 1550 additions & 188 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+
Lower CSS nesting, `:dir()` and custom at-rules; gate case folding and escapes.

‎declarations/WebpackOptions.d.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2433,6 +2433,11 @@ export interface OptimizationMinimizeCss {
24332433
* @since 5.110.0
24342434
*/
24352435
convertLengthUnits?: boolean;
2436+
/**
2437+
* Write a name that matches ASCII case-insensitively in lowercase: an at-rule name, a property name, a pseudo-class or pseudo-element name, a function name, a unit, and a keyword standing in a value whose grammar takes keywords alone. `@MEDIA`, `COLOR`, `:NTH-CHILD`, `URL(`, `1PX` and `currentColor` become `@media`, `color`, `:nth-child`, `url(`, `1px` and `currentcolor`. On by default, and exact: CSS matches every one of these ASCII case-insensitively, so the fold names the same thing. What it never touches is a name the author chose — a custom property, a custom ident such as an animation or grid-area name, an id, a class, a type selector, an attribute's value, or anything inside a substituted value. `@charset` is left as written, being read as bytes rather than matched, and `!important` is always written in lowercase because the printer writes the keyword rather than copying it.
2438+
* @since 5.111.0
2439+
*/
2440+
foldCase?: boolean;
24362441
/**
24372442
* Write a spelling the `browserslist` target cannot read as one it can — the same value said another way, rather than left for an engine that will drop it: a 4-/8-digit hex as `rgba()`, a double-position gradient stop as the two stops it names, `inset` / `overflow` / `place-*` as the longhands they set, `text-decoration` as its own longhands where a slot is newer than the shorthand, `system-ui` as the stack of platform font names it stands for, a `:lang()` or `:not()` holding a list as the `:is()` that means it, a media feature range as the `min-`/`max-` pair, and `light-dark()` as the custom-property pair a color scheme switches. On by default, and only in effect for a `browserslist` target — any other target names no browsers to answer for. Off leaves every such spelling as written, which is how a stylesheet one of these rewrites gets wrong is minified; it never makes the minifier write a spelling the target cannot read.
24382443
* @since 5.111.0
@@ -2473,11 +2478,26 @@ export interface OptimizationMinimizeCss {
24732478
* @since 5.110.0
24742479
*/
24752480
removeDeadRules?: boolean;
2481+
/**
2482+
* Resolve the `@custom-media` and `@custom-selector` at-rules: write the query or the selector list a name stands for wherever one asks for it, and drop the rule that named it. `@custom-media --wide (width>400px)` with `@media (--wide)` becomes `@media (width>400px)`, and `@custom-selector :--heading h1, h2` with `:--heading` becomes `:is(h1, h2)`. Off until asked for: `module.parser.css.customMedia` and `module.parser.css.customSelectors` already resolve both in every stylesheet webpack parses, so this is for a `.css` asset that reached the minimizer without being parsed — one emitted by `asset/resource` or copied in. Only a name stated before the rule asking for it is written out — the rules are read in the order they are written — and a rule naming one this has not come to is left as the author had it. A selector list is written as `:is(…)`, so a name is substituted only where the target reads `:is()`.
2483+
* @since 5.111.0
2484+
*/
2485+
resolveCustomAtRules?: boolean;
24762486
/**
24772487
* Shorten the values of custom properties (`--x: #ffffff` -> `#fff`, `--y: 0.5rem` -> `.5rem`), which are otherwise written back exactly as authored. Off by default: `getComputedStyle().getPropertyValue()` hands this text back, so a rewritten value is a different CSSOM — the one place a declaration's authored text survives. What it may rewrite is exactly what any other value's tokens may be, a color in a substitution's fallback included — that fallback being the property's value rather than the function's own argument.
24782488
* @since 5.110.0
24792489
*/
24802490
rewriteCustomProperties?: boolean;
2491+
/**
2492+
* Write a `:dir()` the `browserslist` target cannot read as the `[dir]` attribute selector it approximates: `a:dir(rtl)` becomes `a[dir=rtl]`. Off until asked for, because the two are not the same question — `:dir()` reads the directionality an element resolves to, which it may inherit from an ancestor, while `[dir=rtl]` reads the attribute on the element itself, so an element inside a `dir="rtl"` ancestor matches the first and not the second. Only in effect for a `browserslist` target that reads no `:dir()`; any other target names no browsers to answer for.
2493+
* @since 5.111.0
2494+
*/
2495+
rewriteDirSelector?: boolean;
2496+
/**
2497+
* Write an escaped identifier the shortest way that names the same thing, in a value or an id: `grid-area:\66oot` becomes `grid-area:foot` and `#\41 x` becomes `#Ax`. On by default, and exact: the escape and what replaces it are the same identifier. Off leaves every escape as the author wrote it, which is what a consumer comparing the text rather than reading the identifier needs.
2498+
* @since 5.111.0
2499+
*/
2500+
rewriteEscapes?: boolean;
24812501
/**
24822502
* Write each color in the shortest spelling of the same value: `#ffffff` -> `#fff`, `rgb(1 2 3)` -> `#010203`, a named color where the property takes no identifier of the author's own, and every polar and Lab function the target agrees with hex on. On by default.
24832503
* @since 5.110.0

0 commit comments

Comments
 (0)