Skip to content

Commit 09aa8a2

Browse files
feat(css): join adjacent rules and fold more value spellings
Join adjacent qualified rules that share a selector, the blocks of two adjacent at-rules of one condition, and a border side's three longhands into its shorthand, each declining wherever the cascade could read the result differently. Fold a one-axis skew and rotation, an alpha percentage, a ratio's implied denominator, a gradient's defaulted stop positions and a data URI's needless escapes, and reduce a transform until it stops getting shorter.
1 parent 5c5d009 commit 09aa8a2

55 files changed

Lines changed: 2974 additions & 123 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
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+
Minify CSS values, shorthands and adjacent rules further.

‎declarations/WebpackOptions.d.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2876,6 +2876,11 @@ export interface Environment {
28762876
* @since 5.110.0
28772877
*/
28782878
cssColorHexAlpha?: boolean;
2879+
/**
2880+
* The environment supports two positions on one gradient color stop ('red 0% 50%').
2881+
* @since 5.110.0
2882+
*/
2883+
cssGradientDoublePosition?: boolean;
28792884
/**
28802885
* The environment supports the 'inset' shorthand property.
28812886
* @since 5.110.0

‎lib/config/browserslistTargetHandler.js‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,23 @@ const resolve = (browsers) => {
524524
android: 104
525525
// baidu / and_qq / and_uc / kaios: Not tracked
526526
}),
527+
// CSS Images 4's two-position color stop (`red 0% 50%`), which stands for
528+
// the two stops it would otherwise take.
529+
cssGradientDoublePosition: cssChecker({
530+
chrome: 72,
531+
and_chr: 72,
532+
edge: 79,
533+
firefox: 83,
534+
and_ff: 83,
535+
// ie: Not supported,
536+
opera: 60,
537+
op_mob: 51,
538+
safari: [12, 1],
539+
ios_saf: [12, 2],
540+
samsung: [11, 1],
541+
android: 72
542+
// baidu / and_qq / and_uc / kaios: Not tracked
543+
}),
527544
// CSS Position 3's `inset` shorthand. Newer than the `top` / `right` /
528545
// `bottom` / `left` longhands it merges, which are CSS 1.
529546
cssInsetShorthand: cssChecker({

‎lib/config/defaults.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1698,6 +1698,15 @@ const applyOutputDefaults = (
16981698
() =>
16991699
tp && optimistic(/** @type {boolean | undefined} */ (tp.cssColorHexAlpha))
17001700
);
1701+
F(
1702+
environment,
1703+
"cssGradientDoublePosition",
1704+
() =>
1705+
tp &&
1706+
optimistic(
1707+
/** @type {boolean | undefined} */ (tp.cssGradientDoublePosition)
1708+
)
1709+
);
17011710
F(
17021711
environment,
17031712
"cssInsetShorthand",
@@ -2465,6 +2474,8 @@ const applyOptimizationDefaults = (
24652474
const outputEnvironment = compiler.options.output.environment || {};
24662475
const cssEnvironment = {
24672476
cssColorHexAlpha: outputEnvironment.cssColorHexAlpha !== false,
2477+
cssGradientDoublePosition:
2478+
outputEnvironment.cssGradientDoublePosition !== false,
24682479
cssInsetShorthand: outputEnvironment.cssInsetShorthand !== false,
24692480
cssMediaQueryRange: outputEnvironment.cssMediaQueryRange !== false
24702481
};

‎lib/config/target.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ const getDefaultTarget = (context) => {
8989
* `defaults` reads as the modern spelling being available.
9090
* @typedef {object} CssTargetProperties
9191
* @property {boolean | null} cssColorHexAlpha 4- and 8-digit hex colors (`#rgba`, `#rrggbbaa`) are available
92+
* @property {boolean | null} cssGradientDoublePosition two positions on one gradient color stop (`red 0% 50%`) are available
9293
* @property {boolean | null} cssInsetShorthand the `inset` shorthand property is available
9394
* @property {boolean | null} cssMediaQueryRange media query range syntax (`(width >= 600px)`) is available
9495
*/

‎lib/css/cssMinify.js‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,11 @@ const cssMinify = (input, sourceMap, minimizerOptions = {}) => {
3939
// authored text survives;
4040
// - dropping a declaration a later one overrides, which loses a fallback pair
4141
// an engine that cannot read the newer spelling depends on;
42-
// - merging rules that share a block, and merging non-adjacent `@media`, both
43-
// of which reorder the cascade (csso loses ~12,900 Tailwind classes to it);
42+
// - merging rules a third stands between, and merging non-adjacent `@media`,
43+
// both of which reorder the cascade (csso loses ~12,900 Tailwind classes to
44+
// it). Adjacent rules sharing a block do join, nothing being between them to
45+
// step over — only where every selector is a shape each engine parses, since
46+
// one it cannot invalidates the whole list and loses the rest with it;
4447
// - rewriting a length into a shorter unit it is exactly equal in
4548
// (`convertLengthUnits`). It is sound — CSS Values 4 fixes the absolute
4649
// units against each other, so `1pc` is `16px` on every medium — but it

‎lib/css/data.js‎

Lines changed: 186 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -497,17 +497,17 @@ const ONE_VALUE_PAIR_SHORTHANDS = new Set(["overflow"]);
497497
// appearing once, in grammar order. A merge emits every value, so the only
498498
// question is whether each parses back into the longhand it was authored on.
499499
// prettier-ignore
500-
const FAMILY_LONGHANDS = new Map([["column-rule", ["column-rule-width","column-rule-style","column-rule-color"]], ["flex-flow", ["flex-direction","flex-wrap"]], ["list-style", ["list-style-type","list-style-position","list-style-image"]], ["outline", ["outline-width","outline-style","outline-color"]], ["text-decoration", ["text-decoration-line","text-decoration-style","text-decoration-color","text-decoration-thickness"]], ["text-emphasis", ["text-emphasis-style","text-emphasis-color"]], ["text-wrap", ["text-wrap-mode","text-wrap-style"]]]);
500+
const FAMILY_LONGHANDS = new Map([["border-bottom", ["border-bottom-width","border-bottom-style","border-bottom-color"]], ["border-left", ["border-left-width","border-left-style","border-left-color"]], ["border-right", ["border-right-width","border-right-style","border-right-color"]], ["border-top", ["border-top-width","border-top-style","border-top-color"]], ["column-rule", ["column-rule-width","column-rule-style","column-rule-color"]], ["flex-flow", ["flex-direction","flex-wrap"]], ["list-style", ["list-style-type","list-style-position","list-style-image"]], ["outline", ["outline-width","outline-style","outline-color"]], ["text-decoration", ["text-decoration-line","text-decoration-style","text-decoration-color","text-decoration-thickness"]], ["text-emphasis", ["text-emphasis-style","text-emphasis-color"]], ["text-wrap", ["text-wrap-mode","text-wrap-style"]]]);
501501

502502
// What each of those longhands accepts as a whole value: the keywords it names,
503503
// and the value classes it reaches. A value acceptable to a second slot is what
504504
// makes the merge ambiguous, and `FAMILY_SLOT_CLASSES` names a type the printer
505505
// cannot classify as readily as one it can, so an unknown one declines.
506506
// prettier-ignore
507-
const FAMILY_SLOT_KEYWORDS = new Map([["column-rule-width", ["medium","thick","thin"]], ["column-rule-style", ["dashed","dotted","double","groove","hidden","inset","none","outset","ridge","solid"]], ["column-rule-color", []], ["flex-direction", ["column","column-reverse","row","row-reverse"]], ["flex-wrap", ["nowrap","wrap","wrap-reverse"]], ["list-style-type", ["none"]], ["list-style-position", ["inside","outside"]], ["list-style-image", ["none"]], ["outline-width", ["medium","thick","thin"]], ["outline-style", ["auto","dashed","dotted","double","groove","inset","none","outset","ridge","solid"]], ["outline-color", ["auto"]], ["text-decoration-line", ["blink","grammar-error","line-through","none","overline","spelling-error","underline"]], ["text-decoration-style", ["dashed","dotted","double","solid","wavy"]], ["text-decoration-color", []], ["text-decoration-thickness", ["auto","from-font"]], ["text-emphasis-style", ["circle","dot","double-circle","filled","none","open","sesame","triangle"]], ["text-emphasis-color", []], ["text-wrap-mode", ["nowrap","wrap"]], ["text-wrap-style", ["auto","balance","pretty","stable"]]]);
507+
const FAMILY_SLOT_KEYWORDS = new Map([["border-bottom-width", ["medium","thick","thin"]], ["border-bottom-style", ["dashed","dotted","double","groove","hidden","inset","none","outset","ridge","solid"]], ["border-bottom-color", []], ["border-left-width", ["medium","thick","thin"]], ["border-left-style", ["dashed","dotted","double","groove","hidden","inset","none","outset","ridge","solid"]], ["border-left-color", []], ["border-right-width", ["medium","thick","thin"]], ["border-right-style", ["dashed","dotted","double","groove","hidden","inset","none","outset","ridge","solid"]], ["border-right-color", []], ["border-top-width", ["medium","thick","thin"]], ["border-top-style", ["dashed","dotted","double","groove","hidden","inset","none","outset","ridge","solid"]], ["border-top-color", []], ["column-rule-width", ["medium","thick","thin"]], ["column-rule-style", ["dashed","dotted","double","groove","hidden","inset","none","outset","ridge","solid"]], ["column-rule-color", []], ["flex-direction", ["column","column-reverse","row","row-reverse"]], ["flex-wrap", ["nowrap","wrap","wrap-reverse"]], ["list-style-type", ["none"]], ["list-style-position", ["inside","outside"]], ["list-style-image", ["none"]], ["outline-width", ["medium","thick","thin"]], ["outline-style", ["auto","dashed","dotted","double","groove","inset","none","outset","ridge","solid"]], ["outline-color", ["auto"]], ["text-decoration-line", ["blink","grammar-error","line-through","none","overline","spelling-error","underline"]], ["text-decoration-style", ["dashed","dotted","double","solid","wavy"]], ["text-decoration-color", []], ["text-decoration-thickness", ["auto","from-font"]], ["text-emphasis-style", ["circle","dot","double-circle","filled","none","open","sesame","triangle"]], ["text-emphasis-color", []], ["text-wrap-mode", ["nowrap","wrap"]], ["text-wrap-style", ["auto","balance","pretty","stable"]]]);
508508

509509
// prettier-ignore
510-
const FAMILY_SLOT_CLASSES = new Map([["column-rule-width", ["length"]], ["column-rule-style", []], ["column-rule-color", ["color"]], ["flex-direction", []], ["flex-wrap", []], ["list-style-type", ["custom-ident","string"]], ["list-style-position", []], ["list-style-image", ["image"]], ["outline-width", ["length"]], ["outline-style", []], ["outline-color", ["color"]], ["text-decoration-line", []], ["text-decoration-style", []], ["text-decoration-color", ["color"]], ["text-decoration-thickness", ["length","percentage"]], ["text-emphasis-style", ["string"]], ["text-emphasis-color", ["color"]], ["text-wrap-mode", []], ["text-wrap-style", []]]);
510+
const FAMILY_SLOT_CLASSES = new Map([["border-bottom-width", ["length"]], ["border-bottom-style", []], ["border-bottom-color", ["color"]], ["border-left-width", ["length"]], ["border-left-style", []], ["border-left-color", ["color"]], ["border-right-width", ["length"]], ["border-right-style", []], ["border-right-color", ["color"]], ["border-top-width", ["length"]], ["border-top-style", []], ["border-top-color", ["color"]], ["column-rule-width", ["length"]], ["column-rule-style", []], ["column-rule-color", ["color"]], ["flex-direction", []], ["flex-wrap", []], ["list-style-type", ["custom-ident","string"]], ["list-style-position", []], ["list-style-image", ["image"]], ["outline-width", ["length"]], ["outline-style", []], ["outline-color", ["color"]], ["text-decoration-line", []], ["text-decoration-style", []], ["text-decoration-color", ["color"]], ["text-decoration-thickness", ["length","percentage"]], ["text-emphasis-style", ["string"]], ["text-emphasis-color", ["color"]], ["text-wrap-mode", []], ["text-wrap-style", []]]);
511511

512512
// The identifiers that are a `<color>` on their own — named, system and the two
513513
// context-dependent ones. Read off the `<color>` grammar outside any function,
@@ -892,6 +892,55 @@ const SHORTHAND_INITIAL_KEYWORDS = new Map([
892892
["running", new Set(["paused", "running"])]
893893
])
894894
],
895+
[
896+
"background",
897+
new Map([
898+
[
899+
"none",
900+
new Set([
901+
"color()",
902+
"color-mix()",
903+
"conic-gradient()",
904+
"cross-fade()",
905+
"element()",
906+
"hsl()",
907+
"hsla()",
908+
"hwb()",
909+
"image()",
910+
"image-set()",
911+
"lab()",
912+
"lch()",
913+
"light-dark()",
914+
"linear-gradient()",
915+
"none",
916+
"oklab()",
917+
"oklch()",
918+
"paint()",
919+
"radial-gradient()",
920+
"repeating-conic-gradient()",
921+
"repeating-linear-gradient()",
922+
"repeating-radial-gradient()",
923+
"rgb()",
924+
"rgba()",
925+
"src()",
926+
"type()",
927+
"url()"
928+
])
929+
],
930+
[
931+
"repeat",
932+
new Set([
933+
"no-repeat",
934+
"repeat",
935+
"repeat-x",
936+
"repeat-y",
937+
"round",
938+
"space"
939+
])
940+
],
941+
["scroll", new Set(["fixed", "local", "scroll"])]
942+
])
943+
],
895944
[
896945
"border",
897946
new Map([
@@ -934,7 +983,41 @@ const SHORTHAND_INITIAL_KEYWORDS = new Map([
934983
],
935984
[
936985
"border-image",
937-
new Map([["stretch", new Set(["repeat", "round", "space", "stretch"])]])
986+
new Map([
987+
[
988+
"none",
989+
new Set([
990+
"color()",
991+
"color-mix()",
992+
"conic-gradient()",
993+
"cross-fade()",
994+
"element()",
995+
"hsl()",
996+
"hsla()",
997+
"hwb()",
998+
"image()",
999+
"image-set()",
1000+
"lab()",
1001+
"lch()",
1002+
"light-dark()",
1003+
"linear-gradient()",
1004+
"none",
1005+
"oklab()",
1006+
"oklch()",
1007+
"paint()",
1008+
"radial-gradient()",
1009+
"repeating-conic-gradient()",
1010+
"repeating-linear-gradient()",
1011+
"repeating-radial-gradient()",
1012+
"rgb()",
1013+
"rgba()",
1014+
"src()",
1015+
"type()",
1016+
"url()"
1017+
])
1018+
],
1019+
["stretch", new Set(["repeat", "round", "space", "stretch"])]
1020+
])
9381021
],
9391022
[
9401023
"border-left",
@@ -1029,6 +1112,38 @@ const SHORTHAND_INITIAL_KEYWORDS = new Map([
10291112
new Map([
10301113
["add", new Set(["add", "exclude", "intersect", "subtract"])],
10311114
["match-source", new Set(["alpha", "luminance", "match-source"])],
1115+
[
1116+
"none",
1117+
new Set([
1118+
"color()",
1119+
"color-mix()",
1120+
"conic-gradient()",
1121+
"cross-fade()",
1122+
"element()",
1123+
"hsl()",
1124+
"hsla()",
1125+
"hwb()",
1126+
"image()",
1127+
"image-set()",
1128+
"lab()",
1129+
"lch()",
1130+
"light-dark()",
1131+
"linear-gradient()",
1132+
"none",
1133+
"oklab()",
1134+
"oklch()",
1135+
"paint()",
1136+
"radial-gradient()",
1137+
"repeating-conic-gradient()",
1138+
"repeating-linear-gradient()",
1139+
"repeating-radial-gradient()",
1140+
"rgb()",
1141+
"rgba()",
1142+
"src()",
1143+
"type()",
1144+
"url()"
1145+
])
1146+
],
10321147
[
10331148
"repeat",
10341149
new Set([
@@ -1046,6 +1161,38 @@ const SHORTHAND_INITIAL_KEYWORDS = new Map([
10461161
"mask-border",
10471162
new Map([
10481163
["alpha", new Set(["alpha", "luminance"])],
1164+
[
1165+
"none",
1166+
new Set([
1167+
"color()",
1168+
"color-mix()",
1169+
"conic-gradient()",
1170+
"cross-fade()",
1171+
"element()",
1172+
"hsl()",
1173+
"hsla()",
1174+
"hwb()",
1175+
"image()",
1176+
"image-set()",
1177+
"lab()",
1178+
"lch()",
1179+
"light-dark()",
1180+
"linear-gradient()",
1181+
"none",
1182+
"oklab()",
1183+
"oklch()",
1184+
"paint()",
1185+
"radial-gradient()",
1186+
"repeating-conic-gradient()",
1187+
"repeating-linear-gradient()",
1188+
"repeating-radial-gradient()",
1189+
"rgb()",
1190+
"rgba()",
1191+
"src()",
1192+
"type()",
1193+
"url()"
1194+
])
1195+
],
10491196
["stretch", new Set(["repeat", "round", "space", "stretch"])]
10501197
])
10511198
],
@@ -1156,6 +1303,17 @@ const GENERIC_FONT_FAMILIES = new Set([
11561303
"ui-serif"
11571304
]);
11581305

1306+
// Each gradient function -> the positions its last color stop already means, so
1307+
// writing one of them there says nothing (CSS Images 3 §3.4.3).
1308+
const GRADIENT_LAST_POSITIONS = new Map([
1309+
["conic-gradient", new Set(["100%", "360deg", "1turn"])],
1310+
["linear-gradient", new Set(["100%"])],
1311+
["radial-gradient", new Set(["100%"])],
1312+
["repeating-conic-gradient", new Set(["100%", "360deg", "1turn"])],
1313+
["repeating-linear-gradient", new Set(["100%"])],
1314+
["repeating-radial-gradient", new Set(["100%"])]
1315+
]);
1316+
11591317
// The properties whose value is a position, where each edge keyword names the
11601318
// percentage that axis resolves to.
11611319
const POSITION_PROPERTIES = new Set([
@@ -1839,6 +1997,18 @@ const DROPPABLE_WHEN_EMPTY_AT_RULES = new Set([
18391997
"container"
18401998
]);
18411999

2000+
// At-rules whose block holds rules and whose prelude states a condition, so two
2001+
// adjacent blocks with the same prelude are the one block they resolve to.
2002+
const MERGEABLE_AT_RULES = new Set([
2003+
"container",
2004+
"document",
2005+
"layer",
2006+
"media",
2007+
"scope",
2008+
"starting-style",
2009+
"supports"
2010+
]);
2011+
18422012
// The math functions whose result steps with their arguments, so a value inside
18432013
// one keeps the unit and the digits it was written with.
18442014
const STEPPED_FUNCTIONS = new Set(["mod", "rem", "round"]);
@@ -2075,6 +2245,14 @@ const INTEGER_PROPERTIES = new Set([
20752245
"z-index"
20762246
]);
20772247

2248+
// The properties whose value is one `<number> | <percentage>`, where the
2249+
// percentage is the number hundredfold and the two compute to the same thing.
2250+
const ALPHA_VALUE_PROPERTIES = new Set(["opacity", "shape-image-threshold"]);
2251+
2252+
// The properties taking a `<ratio>`, whose second number of `1` is the one an
2253+
// omitted denominator means.
2254+
const RATIO_PROPERTIES = new Set(["aspect-ratio"]);
2255+
20782256
// The keywords of every property a `css/module` reads a scoped name out of,
20792257
// each mapped to how many times it may be spelled before the next one is the
20802258
// name (`Infinity` — never the name). Derived from each property's grammar.
@@ -2862,6 +3040,7 @@ const RGB_TO_NAME = new Map([
28623040
]);
28633041

28643042
module.exports.ABSOLUTE_UNIT_SCALE = ABSOLUTE_UNIT_SCALE;
3043+
module.exports.ALPHA_VALUE_PROPERTIES = ALPHA_VALUE_PROPERTIES;
28653044
module.exports.ANGLE_UNITS = ANGLE_UNITS;
28663045
module.exports.ARC_COSINE_DEGREES = ARC_COSINE_DEGREES;
28673046
module.exports.ARC_SINE_DEGREES = ARC_SINE_DEGREES;
@@ -2891,6 +3070,7 @@ module.exports.FLEX_KEYWORDS = FLEX_KEYWORDS;
28913070
module.exports.FONT_STRETCH_PERCENTAGES = FONT_STRETCH_PERCENTAGES;
28923071
module.exports.FONT_WEIGHT_NUMBERS = FONT_WEIGHT_NUMBERS;
28933072
module.exports.GENERIC_FONT_FAMILIES = GENERIC_FONT_FAMILIES;
3073+
module.exports.GRADIENT_LAST_POSITIONS = GRADIENT_LAST_POSITIONS;
28943074
module.exports.INITIAL_VALUE_KEYWORDS = INITIAL_VALUE_KEYWORDS;
28953075
module.exports.INTEGER_PROPERTIES = INTEGER_PROPERTIES;
28963076
module.exports.LEGACY_PSEUDO_ELEMENTS = LEGACY_PSEUDO_ELEMENTS;
@@ -2900,6 +3080,7 @@ module.exports.MATH_FUNCTION_ARITY = MATH_FUNCTION_ARITY;
29003080
module.exports.MATH_FUNCTION_FOLD = MATH_FUNCTION_FOLD;
29013081
module.exports.MATH_FUNCTION_KEYWORDS = MATH_FUNCTION_KEYWORDS;
29023082
module.exports.MATH_FUNCTION_SUM_ARGUMENTS = MATH_FUNCTION_SUM_ARGUMENTS;
3083+
module.exports.MERGEABLE_AT_RULES = MERGEABLE_AT_RULES;
29033084
module.exports.NEGATIVE_ACCEPTING_PROPERTIES = NEGATIVE_ACCEPTING_PROPERTIES;
29043085
module.exports.NTH_PSEUDO_FUNCTIONS = NTH_PSEUDO_FUNCTIONS;
29053086
module.exports.ONE_VALUE_PAIR_SHORTHANDS = ONE_VALUE_PAIR_SHORTHANDS;
@@ -2908,6 +3089,7 @@ module.exports.POSITION_PROPERTIES = POSITION_PROPERTIES;
29083089
module.exports.POSITION_X_KEYWORDS = POSITION_X_KEYWORDS;
29093090
module.exports.POSITION_Y_KEYWORDS = POSITION_Y_KEYWORDS;
29103091
module.exports.QUARTER_TURN_ANGLE = QUARTER_TURN_ANGLE;
3092+
module.exports.RATIO_PROPERTIES = RATIO_PROPERTIES;
29113093
module.exports.REPEAT_STYLE_KEYWORDS = REPEAT_STYLE_KEYWORDS;
29123094
module.exports.REPEAT_STYLE_PROPERTIES = REPEAT_STYLE_PROPERTIES;
29133095
module.exports.RGB_TO_NAME = RGB_TO_NAME;

0 commit comments

Comments
 (0)