Skip to content

Commit 6e14d17

Browse files
feat(css): add customMedia and customSelectors minimizer options
Split resolveCustomAtRules into one option per at-rule, matching module.parser.css.customMedia/customSelectors; the old option is deprecated and turns on each one that is not set.
1 parent 8d6abf1 commit 6e14d17

11 files changed

Lines changed: 179 additions & 36 deletions

File tree

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
"webpack": patch
2+
"webpack": minor
33
---
44

5-
Resolve CSS custom media and selectors that read each other, in any order.
5+
Add CSS `customMedia`/`customSelectors` minimize options; resolve chained names.

‎declarations/WebpackOptions.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3127,6 +3127,16 @@ export interface OptimizationMinimizeCss {
31273127
* @since 5.110.0
31283128
*/
31293129
convertLengthUnits?: boolean;
3130+
/**
3131+
* Resolve the `@custom-media` at-rule: write the query a name stands for wherever a condition asks for it, and drop the rule that named it. `@custom-media --wide (width>400px)` with `@media (--wide)` becomes `@media (width>400px)`. Off by default: `module.parser.css.customMedia` already resolves it 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. Every rule is read first, so a name resolves wherever its rule stands, the last rule stating it holds, and one name may be read through another; names that read each other in a cycle stand for nothing and are left as written.
3132+
* @since 5.112.0
3133+
*/
3134+
customMedia?: boolean;
3135+
/**
3136+
* Resolve the `@custom-selector` at-rule: write the selector list a name stands for as `:is(…)` wherever a selector asks for it, and drop the rule that named it. `@custom-selector :--heading h1, h2` with `:--heading` becomes `:is(h1, h2)`. Off by default, for the reason `customMedia` is: `module.parser.css.customSelectors` already resolves it in every stylesheet webpack parses. Rules are read as `customMedia` reads them, and a name is written out only where the target reads `:is()`.
3137+
* @since 5.112.0
3138+
*/
3139+
customSelectors?: boolean;
31303140
/**
31313141
* 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.
31323142
* @since 5.112.0
@@ -3183,7 +3193,8 @@ export interface OptimizationMinimizeCss {
31833193
*/
31843194
removeDeadRules?: boolean;
31853195
/**
3186-
* 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()`.
3196+
* Deprecated in favor of `customMedia` and `customSelectors`: it turns on each of the two that is not set. Resolve the `@custom-media` and `@custom-selector` at-rules.
3197+
* @deprecated
31873198
* @since 5.111.0
31883199
*/
31893200
resolveCustomAtRules?: boolean;

‎lib/css/syntax-parser.js‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4196,7 +4196,9 @@ const TOP_LEVEL_CONSUMERS = {
41964196
* @property {boolean=} foldCase write a name that matches ASCII case-insensitively — an at-rule, property, pseudo, function, unit or keyword — in lowercase
41974197
* @property {boolean=} rewriteEscapes write an escaped identifier the shortest way that names it, wherever it stands
41984198
* @property {(boolean | "all" | "some" | string | RegExp | ((comment: string) => boolean))=} comments which comments survive: `"some"` (the default) the ones that carry something, `true` / `"all"` every one, `false` none, or the ones a pattern matches / a predicate accepts, over the comment's own text
4199-
* @property {boolean=} resolveCustomAtRules write the query or selector list a `@custom-media` / `@custom-selector` names wherever one asks for it, and drop the rule that named it
4199+
* @property {boolean=} customMedia write the query a `@custom-media` names wherever a condition asks for it, and drop the rule that named it
4200+
* @property {boolean=} customSelectors write the selector list a `@custom-selector` names as `:is(…)` wherever a selector asks for it, and drop the rule that named it
4201+
* @property {boolean=} resolveCustomAtRules turn on `customMedia` and `customSelectors`, each where it is not set
42004202
* @property {boolean=} mergeLonghands write a family of longhands as the one shorthand that sets them
42014203
* @property {boolean=} mergeRules join rules that print the same block, at-rules that share a prelude, and a named `@layer` block a later sibling opens again
42024204
* @property {boolean=} normalizeQuotes normalize a string's, `url()`'s, font family's and attribute value's quoting
@@ -7076,6 +7078,8 @@ const _ALL_TRANSFORMS = Object.freeze({
70767078
foldCase: true,
70777079
rewriteEscapes: true,
70787080
comments: "some",
7081+
customMedia: false,
7082+
customSelectors: false,
70797083
resolveCustomAtRules: false,
70807084
mergeLonghands: true,
70817085
mergeRules: true,
@@ -7114,6 +7118,12 @@ const _transformsFrom = (options) => {
71147118
if (value !== undefined) out[name] = value;
71157119
}
71167120
}
7121+
// TODO in the next major release: remove, `resolveCustomAtRules` is the old
7122+
// spelling of `customMedia` and `customSelectors` together
7123+
if (options !== undefined && options.resolveCustomAtRules === true) {
7124+
if (options.customMedia === undefined) out.customMedia = true;
7125+
if (options.customSelectors === undefined) out.customSelectors = true;
7126+
}
71177127
return out;
71187128
};
71197129

@@ -7182,7 +7192,9 @@ const grammar = (input, visitors, writer, options) => {
71827192
!_collectingCustomAtRules &&
71837193
writer.options.mode === "minify" &&
71847194
writer.options.transforms !== undefined &&
7185-
writer.options.transforms.resolveCustomAtRules === true &&
7195+
(writer.options.transforms.customMedia === true ||
7196+
writer.options.transforms.customSelectors === true ||
7197+
writer.options.transforms.resolveCustomAtRules === true) &&
71867198
_namesAtRule(input, _CUSTOM_AT_RULE_NAMES)
71877199
) {
71887200
_readCustomAtRulesAhead(input, writer);
@@ -11078,7 +11090,7 @@ const _minifyMediaQueryParts = (parts, media) => {
1107811090
// and case-sensitive.
1107911091
if (media) {
1108011092
// Before the passes below, which read the query this writes in.
11081-
if (_S._transforms.resolveCustomAtRules && _customMedia.size !== 0) {
11093+
if (_S._transforms.customMedia && _customMedia.size !== 0) {
1108211094
_writeCustomMedia(parts);
1108311095
}
1108411096
if (_S._transforms.foldCase) _lowercaseConditionParts(parts);
@@ -14500,7 +14512,7 @@ const _rulePrelude = (path, writer, minify, outParts) => {
1450014512
}
1450114513
if (
1450214514
name === "custom-selector" &&
14503-
_S._transforms.resolveCustomAtRules &&
14515+
_S._transforms.customSelectors &&
1450414516
_customSelectors.size !== 0
1450514517
) {
1450614518
// The list, past the name the rule states.
@@ -14519,7 +14531,7 @@ const _rulePrelude = (path, writer, minify, outParts) => {
1451914531
}
1452014532
if (minify && path.type() === T_QUALIFIED_RULE) {
1452114533
// Before the fold, so what a name stands for is what every pass below reads.
14522-
if (_S._transforms.resolveCustomAtRules && _customSelectors.size !== 0) {
14534+
if (_S._transforms.customSelectors && _customSelectors.size !== 0) {
1452314535
_writeCustomSelector(parts);
1452414536
}
1452514537
_foldPseudoNames(parts);

‎lib/css/syntax-printer.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4639,7 +4639,7 @@ const printer = (path, writer) => {
46394639
// not come to yet still needs it, and so does an engine that reads it.
46404640
if (
46414641
minify &&
4642-
_S._transforms.resolveCustomAtRules &&
4642+
_S._transforms.customMedia &&
46434643
path.atKeyword() === "custom-media" &&
46444644
_takeCustomMedia(prelude) &&
46454645
!_S._customMediaAllowed
@@ -4650,7 +4650,7 @@ const printer = (path, writer) => {
46504650
// name is down — but only where `:is()` can carry what it stands for.
46514651
if (
46524652
minify &&
4653-
_S._transforms.resolveCustomAtRules &&
4653+
_S._transforms.customSelectors &&
46544654
_S._isSelectorAllowed &&
46554655
path.atKeyword() === "custom-selector" &&
46564656
_takeCustomSelector(prelude)

‎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: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4053,6 +4053,16 @@
40534053
"type": "boolean",
40544054
"added": "5.110.0"
40554055
},
4056+
"customMedia": {
4057+
"description": "Resolve the `@custom-media` at-rule: write the query a name stands for wherever a condition asks for it, and drop the rule that named it. `@custom-media --wide (width>400px)` with `@media (--wide)` becomes `@media (width>400px)`. Off by default: `module.parser.css.customMedia` already resolves it 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. Every rule is read first, so a name resolves wherever its rule stands, the last rule stating it holds, and one name may be read through another; names that read each other in a cycle stand for nothing and are left as written.",
4058+
"type": "boolean",
4059+
"added": "5.112.0"
4060+
},
4061+
"customSelectors": {
4062+
"description": "Resolve the `@custom-selector` at-rule: write the selector list a name stands for as `:is(…)` wherever a selector asks for it, and drop the rule that named it. `@custom-selector :--heading h1, h2` with `:--heading` becomes `:is(h1, h2)`. Off by default, for the reason `customMedia` is: `module.parser.css.customSelectors` already resolves it in every stylesheet webpack parses. Rules are read as `customMedia` reads them, and a name is written out only where the target reads `:is()`.",
4063+
"type": "boolean",
4064+
"added": "5.112.0"
4065+
},
40564066
"dropOverriddenDeclarations": {
40574067
"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.",
40584068
"type": "boolean",
@@ -4109,8 +4119,9 @@
41094119
"added": "5.110.0"
41104120
},
41114121
"resolveCustomAtRules": {
4112-
"description": "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()`.",
4122+
"description": "Deprecated in favor of `customMedia` and `customSelectors`: it turns on each of the two that is not set. Resolve the `@custom-media` and `@custom-selector` at-rules.",
41134123
"type": "boolean",
4124+
"deprecated": true,
41144125
"added": "5.111.0"
41154126
},
41164127
"rewriteCustomProperties": {

0 commit comments

Comments
 (0)