Skip to content

Commit dfc4bc4

Browse files
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]>
1 parent f01f902 commit dfc4bc4

16 files changed

Lines changed: 1159 additions & 12 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)