Skip to content

Commit 21bf6c8

Browse files
feat(css): offer dropping an overridden declaration with no target named (#22175)
Only the last of a set of declarations of one property can be read, so the earlier carry nothing — but an earlier one differently spelled is usually a fallback, and dropping it is how a `color(display-p3 …)` pair is lost. With a `browserslist` selection that is answerable and already answered: the drop is made wherever every browser named is known to read the later value. With no selection there are no engines to name, so nothing was dropped at all, where csso and cssnano drop unconditionally. `optimization.minimize.css.dropOverriddenDeclarations`, off by default, is how a build says to assume it. It reaches one line — `_valueReadByTarget` answers with the option rather than `false` where no selection was made — so everything the drop already declines it declines still. What it does not do is override a target that was named. A selection saying the later value is unreadable keeps the fallback whatever the option says, and one naming a browser no compat dataset covers still answers for less than the whole of it and keeps it too: naming a browser states a target, and the option assumes one only where none was stated. Forwarded to the CSS minifier the HTML minifier nests, so an inline `<style>` and a `style=""` are held to the same rule as a `.css` asset.
1 parent 3733511 commit 21bf6c8

19 files changed

Lines changed: 205 additions & 14 deletions

File tree

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+
Add `optimization.minimize.css.dropOverriddenDeclarations`, dropping a declaration a later one overrides where no target is named.

‎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 | Buffer }} input a single `{ filename: code }` entry; a `Buffer` is read as UTF-8
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, convertApproximateColors?: boolean, mergeDistantRules?: 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
36+
* @param {{ as?: "stylesheet" | "block-contents", environment?: CssEnvironment, convertLengthUnits?: boolean, convertApproximateColors?: boolean, dropOverriddenDeclarations?: boolean, mergeDistantRules?: 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 = {}) => {
@@ -96,6 +96,7 @@ const cssMinify = async (input, sourceMap, minimizerOptions = {}) => {
9696
environment,
9797
convertLengthUnits,
9898
convertApproximateColors,
99+
dropOverriddenDeclarations,
99100
mergeDistantRules,
100101
rewriteCustomProperties,
101102
unusedSymbols,
@@ -113,6 +114,7 @@ const cssMinify = async (input, sourceMap, minimizerOptions = {}) => {
113114
environment,
114115
convertLengthUnits,
115116
convertApproximateColors,
117+
dropOverriddenDeclarations,
116118
mergeDistantRules,
117119
rewriteCustomProperties,
118120
unusedSymbols,

‎lib/css/syntax.js‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2049,6 +2049,7 @@ const _setupParse = (input, lc) => {
20492049
_transforms = _DEFAULT_TRANSFORMS;
20502050
_commentsKept = "some";
20512051
_rewriteCustomProperties = false;
2052+
_dropOverriddenDeclarations = false;
20522053
_unitScale = ABSOLUTE_UNIT_SCALE;
20532054
// Back to what the module loads with, so no parse inherits a target of its own.
20542055
_hexAlphaAllowed = true;
@@ -4119,6 +4120,7 @@ const TOP_LEVEL_CONSUMERS = {
41194120
* @property {boolean=} convertLengthUnits rewrite a length into a shorter unit it is exactly equal in (`16px` -> `1pc`); off by default because it earns nothing once the asset is compressed, and only read while printing. A time is always rewritten
41204121
* @property {boolean=} convertApproximateColors write a polar or Lab color as the nearest hex even where that hex only approximates it: a channel too near a `.5` boundary for two engines to round it alike, and a color outside the sRGB gamut, which hex can only clip. Off by default, and only read while printing; a space engines read through transfers of their own (`a98-rgb`, `prophoto-rgb`) is left alone either way
41214122
* @property {boolean=} mergeDistantRules give a rule the selectors of a later one printing the same block, past the rules between them; off by default, since it reorders the cascade and is sound only where nothing between declares what the block does
4123+
* @property {boolean=} dropOverriddenDeclarations drop a declaration a later one in the same block overrides even where nothing states that the target can read the later value; off by default, and only read while printing. With a `browserslist` selection the drop is already made wherever every browser it names is known to read the later value, so this widens the case where no target is selected — a selection naming a browser the tables do not cover still answers for the whole of it
41224124
* @property {boolean=} rewriteCustomProperties shorten a custom property's value the way any other value is shortened (`--x:#ffffff` -> `#fff`); off by default because `getPropertyValue()` hands that text back, and only read while printing. What it may rewrite is what any other value's tokens may be, a color in a substitution's fallback included — that being the property's value rather than the function's own argument
41234125
* @property {EmbeddedSourceRenderer=} renderEmbeddedSource renders source this stylesheet embeds: the payload of a `url()` `data:` URL whose media type names a language webpack knows (SVG, CSS, HTML, JSON, JavaScript). Absent, a data URL is emitted exactly as written
41244126
* @property {{ [name: string]: string }=} pseudoClasses each pseudo-class to write as a class instead (`{ "focus-visible": "focus-visible" }`), so a script can apply it where the engine does not. Only read while printing
@@ -6728,6 +6730,8 @@ const grammar = (input, visitors, writer, options) => {
67286730
writer !== undefined && writer.options.mergeDistantRules === true;
67296731
_rewriteCustomProperties =
67306732
writer !== undefined && writer.options.rewriteCustomProperties === true;
6733+
_dropOverriddenDeclarations =
6734+
writer !== undefined && writer.options.dropOverriddenDeclarations === true;
67316735
// `_setupParse` left the default in place: only a caller's switches resolve.
67326736
if (writer !== undefined && writer.options.transforms !== undefined) {
67336737
_transforms = _transformsFrom(writer.options.transforms);
@@ -7008,7 +7012,7 @@ const _dropSeparatorAfter = (cc) =>
70087012
* What the CSS printer may be told, on top of the `mode` every language has —
70097013
* the printing slice of `CssProcessOptions`, named once so nothing outside CSS
70107014
* has to enumerate it.
7011-
* @typedef {Pick<CssProcessOptions, "environment" | "convertLengthUnits" | "convertApproximateColors" | "mergeDistantRules" | "rewriteCustomProperties" | "renderEmbeddedSource" | "deferEmbeddedSource" | "transforms" | "unusedSymbols" | "pseudoClasses">} CssPrintOptions
7015+
* @typedef {Pick<CssProcessOptions, "environment" | "convertLengthUnits" | "convertApproximateColors" | "dropOverriddenDeclarations" | "mergeDistantRules" | "rewriteCustomProperties" | "renderEmbeddedSource" | "deferEmbeddedSource" | "transforms" | "unusedSymbols" | "pseudoClasses">} CssPrintOptions
70127016
*/
70137017

70147018
/** @typedef {import("../util/SourceProcessor").PrintContext<CssPath, Node, CssPrintOptions>} PrintContext */
@@ -14423,10 +14427,13 @@ const _familyValue = (property, longhands, values) => {
1442314427
* @returns {boolean} true when no target is left reading the earlier one
1442414428
*/
1442514429
const _valueReadByTarget = (property, value) => {
14426-
// No selection names no browser to answer for, and one naming a browser the
14427-
// tables do not cover answers for less than the whole of it; either way
14428-
// nothing is known to read the later value and the earlier one stands.
14429-
if (_prefixBrowsers === null || _someBrowserUnanswered) return false;
14430+
// No selection names no browser to answer for, so nothing is known to read the
14431+
// later value: the earlier one stands unless asked to assume otherwise.
14432+
if (_prefixBrowsers === null) return _dropOverriddenDeclarations;
14433+
// A selection naming a browser the tables do not cover answers for less than
14434+
// the whole of it. Naming a browser states a target, which the option that
14435+
// assumes one does not override, so this stands whatever it says.
14436+
if (_someBrowserUnanswered) return false;
1443014437
const open = value.indexOf("(");
1443114438
if (open === -1) {
1443214439
return _readsAll(
@@ -16759,9 +16766,9 @@ const _composeBlockBody = (
1675916766
// (`_valueReadByTarget`), since otherwise the earlier is its fallback —
1676016767
// dropping those is how esbuild and lightningcss lose a `color(display-p3 …)`
1676116768
// pair.
16762-
// TODO answer the same question with no browserslist selection, as csso and
16763-
// cssnano do: theirs is unconditional, and naming the engines is what makes
16764-
// this one sound — with no selection there are none to name.
16769+
// With no selection there are no engines to name, so the same question is
16770+
// answered only where `dropOverriddenDeclarations` asks for it — csso and
16771+
// cssnano answer it unconditionally, and give up the same fallback pair.
1676516772
/** @type {Set<number> | null} */
1676616773
let superseded = null;
1676716774
// CSS Animations 1 §3: a keyframe ignores an `!important` declaration, so one
@@ -18115,6 +18122,10 @@ let _inCustomProperty = false;
1811518122
// Off, that verbatim rule stands; on, a custom property's tokens print like any
1811618123
// other value's and `getPropertyValue()` hands back the rewritten text.
1811718124
let _rewriteCustomProperties = false;
18125+
// Whether a declaration a later one overrides is dropped with no `browserslist`
18126+
// selection to say the later value is readable. Off by default: it gives up the
18127+
// fallback pair an engine that cannot read the later spelling depends on.
18128+
let _dropOverriddenDeclarations = false;
1811818129
let _inSubstitutedValue = false;
1811918130
// Inside a color the engine computes with rather than paints.
1812018131

‎lib/html/builtinEmbeddedRenderer.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const { CSS_TYPE } = require("../ModuleSourceTypeConstants");
1313
/**
1414
* What this minifies inline CSS with: the CSS minifier's own options, so an
1515
* inline declaration is held to the rules a `.css` asset is.
16-
* @typedef {Pick<CssProcessOptions, "environment" | "convertLengthUnits" | "convertApproximateColors" | "rewriteCustomProperties" | "transforms" | "unusedSymbols" | "pseudoClasses">} BuiltinEmbeddedRendererOptions
16+
* @typedef {Pick<CssProcessOptions, "environment" | "convertLengthUnits" | "convertApproximateColors" | "dropOverriddenDeclarations" | "rewriteCustomProperties" | "transforms" | "unusedSymbols" | "pseudoClasses">} BuiltinEmbeddedRendererOptions
1717
*/
1818

1919
// Both are loaded on first render rather than at module load: a document that

‎lib/html/htmlMinify.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
* re-resolves in the worker — via the installed package, or the dev self-link.
3636
* @param {{ [file: string]: string | Buffer }} input a single `{ filename: code }` entry; a `Buffer` is read as UTF-8
3737
* @param {(RawSourceMap | undefined)=} sourceMap input source map (unused: safe serialize keeps positions token-coarse, so no map is produced yet)
38-
* @param {Omit<HtmlPrintOptions, "renderEmbeddedSource" | "deferEmbeddedSource"> & { environment?: CssEnvironment, css?: { convertLengthUnits?: boolean, convertApproximateColors?: boolean, rewriteCustomProperties?: boolean, unusedSymbols?: string[], pseudoClasses?: { [name: string]: string } }, minifyConditionalComments?: boolean, renderEmbeddedSource?: AsyncEmbeddedSourceRenderer }=} minimizerOptions minimizer options — `environment` and `css` (`optimization.minimize.css`, whole) reach the CSS minifier this runs over inline CSS; the rest is `optimization.minimize.html`. The two stand apart rather than merged: both languages name a `quotes` and a `comments`, and one flat object would hand each the other's answer. `renderEmbeddedSource` minifies source this document embeds and may be asynchronous — one parse serves both it and the output. What it declines this minifies itself, for the languages webpack ships a minifier for
38+
* @param {Omit<HtmlPrintOptions, "renderEmbeddedSource" | "deferEmbeddedSource"> & { environment?: CssEnvironment, css?: { convertLengthUnits?: boolean, convertApproximateColors?: boolean, dropOverriddenDeclarations?: boolean, rewriteCustomProperties?: boolean, unusedSymbols?: string[], pseudoClasses?: { [name: string]: string } }, minifyConditionalComments?: boolean, renderEmbeddedSource?: AsyncEmbeddedSourceRenderer }=} minimizerOptions minimizer options — `environment` and `css` (`optimization.minimize.css`, whole) reach the CSS minifier this runs over inline CSS; the rest is `optimization.minimize.html`. The two stand apart rather than merged: both languages name a `quotes` and a `comments`, and one flat object would hand each the other's answer. `renderEmbeddedSource` minifies source this document embeds and may be asynchronous — one parse serves both it and the output. What it declines this minifies itself, for the languages webpack ships a minifier for
3939
* @returns {Promise<{ code: string, warnings?: (Error | string)[], errors?: (Error | string)[] }>} the minified HTML, and what a renderer reported over what it embeds
4040
*/
4141
const htmlMinify = async (input, sourceMap, minimizerOptions = {}) => {
@@ -84,6 +84,7 @@ const htmlMinify = async (input, sourceMap, minimizerOptions = {}) => {
8484
environment,
8585
convertLengthUnits: css.convertLengthUnits,
8686
convertApproximateColors: css.convertApproximateColors,
87+
dropOverriddenDeclarations: css.dropOverriddenDeclarations,
8788
rewriteCustomProperties: css.rewriteCustomProperties,
8889
unusedSymbols: css.unusedSymbols,
8990
pseudoClasses: css.pseudoClasses,

‎schemas/WebpackOptions.check.js‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎schemas/WebpackOptions.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4024,6 +4024,11 @@
40244024
"type": "boolean",
40254025
"added": "5.110.0"
40264026
},
4027+
"dropOverriddenDeclarations": {
4028+
"description": "Drop a declaration a later one in the same block overrides even where nothing states that the target can read the later value. Off by default. With a `browserslist` target this is already done wherever every browser it names is known to read the later value, so the option only widens the case where no target is selected and there are no engines to name \u2014 which is what csso and cssnano do unconditionally. What it gives up is the fallback pair: `color:#c65d06;color:lab(50% 100 -100)` loses the hex, so an engine that cannot read `lab()` is left with nothing. A selection naming a browser the compat tables do not cover is still answered for the whole of it rather than by this option, since naming a browser states a target the option does not override.",
4029+
"type": "boolean",
4030+
"added": "5.112.0"
4031+
},
40274032
"foldCase": {
40284033
"description": "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.",
40294034
"type": "boolean",

‎test/__snapshots__/Cli.basictest.js.snap‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11202,6 +11202,19 @@ Object {
1120211202
"multiple": false,
1120311203
"simpleType": "boolean",
1120411204
},
11205+
"optimization-minimize-css-drop-overridden-declarations": Object {
11206+
"configs": Array [
11207+
Object {
11208+
"description": "Drop a declaration a later one in the same block overrides even where nothing states that the target can read the later value. Off by default. With a \`browserslist\` target this is already done wherever every browser it names is known to read the later value, so the option only widens the case where no target is selected and there are no engines to name — which is what csso and cssnano do unconditionally. What it gives up is the fallback pair: \`color:#c65d06;color:lab(50% 100 -100)\` loses the hex, so an engine that cannot read \`lab()\` is left with nothing. A selection naming a browser the compat tables do not cover is still answered for the whole of it rather than by this option, since naming a browser states a target the option does not override.",
11209+
"multiple": false,
11210+
"path": "optimization.minimize.css.dropOverriddenDeclarations",
11211+
"type": "boolean",
11212+
},
11213+
],
11214+
"description": "Drop a declaration a later one in the same block overrides even where nothing states that the target can read the later value. Off by default. With a \`browserslist\` target this is already done wherever every browser it names is known to read the later value, so the option only widens the case where no target is selected and there are no engines to name — which is what csso and cssnano do unconditionally. What it gives up is the fallback pair: \`color:#c65d06;color:lab(50% 100 -100)\` loses the hex, so an engine that cannot read \`lab()\` is left with nothing. A selection naming a browser the compat tables do not cover is still answered for the whole of it rather than by this option, since naming a browser states a target the option does not override.",
11215+
"multiple": false,
11216+
"simpleType": "boolean",
11217+
},
1120511218
"optimization-minimize-css-fold-case": Object {
1120611219
"configs": Array [
1120711220
Object {
@@ -11880,6 +11893,19 @@ Object {
1188011893
"multiple": false,
1188111894
"simpleType": "boolean",
1188211895
},
11896+
"optimization-minimize-options-css-drop-overridden-declarations": Object {
11897+
"configs": Array [
11898+
Object {
11899+
"description": "Drop a declaration a later one in the same block overrides even where nothing states that the target can read the later value. Off by default. With a \`browserslist\` target this is already done wherever every browser it names is known to read the later value, so the option only widens the case where no target is selected and there are no engines to name — which is what csso and cssnano do unconditionally. What it gives up is the fallback pair: \`color:#c65d06;color:lab(50% 100 -100)\` loses the hex, so an engine that cannot read \`lab()\` is left with nothing. A selection naming a browser the compat tables do not cover is still answered for the whole of it rather than by this option, since naming a browser states a target the option does not override.",
11900+
"multiple": false,
11901+
"path": "optimization.minimizeOptions.css.dropOverriddenDeclarations",
11902+
"type": "boolean",
11903+
},
11904+
],
11905+
"description": "Drop a declaration a later one in the same block overrides even where nothing states that the target can read the later value. Off by default. With a \`browserslist\` target this is already done wherever every browser it names is known to read the later value, so the option only widens the case where no target is selected and there are no engines to name — which is what csso and cssnano do unconditionally. What it gives up is the fallback pair: \`color:#c65d06;color:lab(50% 100 -100)\` loses the hex, so an engine that cannot read \`lab()\` is left with nothing. A selection naming a browser the compat tables do not cover is still answered for the whole of it rather than by this option, since naming a browser states a target the option does not override.",
11906+
"multiple": false,
11907+
"simpleType": "boolean",
11908+
},
1188311909
"optimization-minimize-options-css-fold-case": Object {
1188411910
"configs": Array [
1188511911
Object {

0 commit comments

Comments
 (0)