Skip to content

Commit c85f246

Browse files
feat(css): join rules a third stands between, behind an option (#21969)
* feat(css): join rules a third stands between, behind an option `mergeDistantRules` gives a rule the selectors of a later one printing the same block, past the rules standing between them — sound only where nothing between declares a property that block does, since such a rule won before the merge and would lose after it. Which properties those are is read from a table the generator derives from `mdn-data`, not from the property name: not every shorthand prefixes its longhands, so `inset` sets `top` and `font` sets `line-height` under no prefix at all. Off by default. Over the framework stylesheets it cuts 2457 raw bytes and costs 27 gzip ones. Co-authored-by: aryanraj45 <[email protected]> * test(css): add the new option to the CLI flags snapshot The flags are derived from the schema, so a property under `optimization.minimize.css` adds one entry there and one under `optimization.minimizeOptions.css`. * fix(css): record a rule flushed by an at-rule it cannot join A top-level node that is not a rule the printer can join — `@keyframes`, `@font-face` — is written out on its own path, and that path also writes out the rule waiting behind it. Neither was noted, so a later rule could take its selectors past declarations that were never recorded: with `.a{color:red} .b{color:blue} @Keyframes k{…} .c{color:red}`, `.c` joined `.a` and an element matching `.b` and `.c` read blue where it had read red. Over five framework stylesheets that path writes out a held rule 58 times. The note now happens inside `_emitHeld`, so every path that flushes one records it, and a node this cannot see into is noted as declaring everything. Reported by CodeRabbit on #21969. * perf(css): release what the distant-rule tables hold after a print Each holds a piece of the stylesheet just printed, so the last one printed stayed reachable until the next parse replaced the maps. They are emptied with the other per-parse buffers now, and cleared rather than replaced at setup, which allocates nothing per parse. Also covers the merge inside a block, which the unit tests reached only through the stylesheet's own stream: the same gates asked a second way. Reported by CodeRabbit on #21969. --------- Co-authored-by: aryanraj45 <[email protected]>
1 parent f01f902 commit c85f246

17 files changed

Lines changed: 1282 additions & 11 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.mergeDistantRules` to join rules across a gap.

‎declarations/WebpackOptions.d.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2459,6 +2459,11 @@ export interface OptimizationMinimizeCss {
24592459
* @since 5.111.0
24602460
*/
24612461
lowerUnsupported?: boolean;
2462+
/**
2463+
* Give a rule the selectors of a later one printing the same block, past the rules standing between them. Off by default on two counts: it reorders the cascade, so it holds only where nothing between the two declares a property the shared block does; and over the framework stylesheets it cuts raw bytes while costing gzip ones, a block repeated at a distance already compressing on its own. It is taken only where the copy of the block it drops outweighs the selector it writes instead. `mergeRules` is the safe half of this, joining only what nothing stands between.
2464+
* @since 5.111.0
2465+
*/
2466+
mergeDistantRules?: boolean;
24622467
/**
24632468
* Write a family of longhands as the one shorthand that sets them — four sides or corners, the two a pair shorthand sets, or the slots of an order-free one — even where unrelated declarations stand between them. On by default.
24642469
* @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[], 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, 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 = {}) => {
@@ -90,6 +90,7 @@ const cssMinify = async (input, sourceMap, minimizerOptions = {}) => {
9090
as,
9191
environment,
9292
convertLengthUnits,
93+
mergeDistantRules,
9394
rewriteCustomProperties,
9495
unusedSymbols,
9596
pseudoClasses,
@@ -105,6 +106,7 @@ const cssMinify = async (input, sourceMap, minimizerOptions = {}) => {
105106
content: code,
106107
environment,
107108
convertLengthUnits,
109+
mergeDistantRules,
108110
rewriteCustomProperties,
109111
unusedSymbols,
110112
pseudoClasses,

0 commit comments

Comments
 (0)