Skip to content

Commit 28df2fb

Browse files
perf(css): drop shorthand slots holding their initial, write font-stretch as a percentage
1 parent 6b16677 commit 28df2fb

7 files changed

Lines changed: 602 additions & 14 deletions

File tree

‎.changeset/070-css-html-minify-transforms.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"webpack": minor
33
---
44

5-
Minify CSS and HTML further where the document is unchanged: CSS rounds a number to 6 significant digits, rewrites a time into the shorter of `ms` / `s`, folds `calc()` and every math function the spec's grammars name over constants whose result is exact, reducing `calc-size()`'s size in place, down to the sum of unlike units it reduces to, takes the parentheses off a folded negative where the property accepts one, drops a zero's unit inside a call whose every number is a length, converts every polar and Lab color function to hex where the engine agrees, merges four box longhands with unrelated declarations between them — sides or corners — the two a pair shorthand sets, and the slots of an order-free one where each value parses back into its own, collapses an `and` of two one-sided media-feature ranges into the interval it describes, writes `2n+1` as the `odd` it names, a zero-step An+B as the child it selects, a `from` keyframe as `0%`, each `unicode-range` in its shortest equal spelling and each named color in the shortest spelling of its value where the property takes no identifier of the author's own, writes a two-keyword `display` as the one keyword naming the same box, a transform and a linear gradient in their shorter equal form, the `font` shorthand's weight as the number naming it and a `transition` in the order its grammar lists the slots, unquotes a font family the quotes carry nothing for, rewrites a `<position>` written as edge keywords into the percentages they resolve to, collapses the values a property's own grammar already implies — two equal `<repeat-style>` keywords, an `initial` whose initial value is a shorter word and a `grid-template-areas` row's padding — drops the unit a zero angle does not need and the whitespace neither a combinator inside a selector function nor two transform functions need, and keeps a `@supports` condition as written; HTML normalizes each attribute against the element it is on.
5+
Minify CSS and HTML further where the document is unchanged: CSS rounds a number to 6 significant digits, rewrites a time into the shorter of `ms` / `s`, folds `calc()` and every math function the spec's grammars name over constants whose result is exact, reducing `calc-size()`'s size in place, down to the sum of unlike units it reduces to, takes the parentheses off a folded negative where the property accepts one, drops a zero's unit inside a call whose every number is a length, converts every polar and Lab color function to hex where the engine agrees, merges four box longhands with unrelated declarations between them — sides or corners — the two a pair shorthand sets, and the slots of an order-free one where each value parses back into its own, collapses an `and` of two one-sided media-feature ranges into the interval it describes, writes `2n+1` as the `odd` it names, a zero-step An+B as the child it selects, a `from` keyframe as `0%`, each `unicode-range` in its shortest equal spelling and each named color in the shortest spelling of its value where the property takes no identifier of the author's own, writes a two-keyword `display` as the one keyword naming the same box, a transform and a linear gradient in their shorter equal form, the `font` shorthand's weight as the number naming it and a `transition` in the order its grammar lists the slots, unquotes a font family the quotes carry nothing for, rewrites a `<position>` written as edge keywords and a `font-stretch` keyword into the percentages they resolve to, drops a shorthand slot holding the value it already defaults to, collapses the values a property's own grammar already implies — two equal `<repeat-style>` keywords, an `initial` whose initial value is a shorter word and a `grid-template-areas` row's padding — drops the unit a zero angle does not need and the whitespace neither a combinator inside a selector function nor two transform functions need, and keeps a `@supports` condition as written; HTML normalizes each attribute against the element it is on.

‎lib/css/data.js‎

Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,261 @@ const DISPLAY_SHORT_FORMS = new Map([
855855
["run-in flow", "run-in"]
856856
]);
857857

858+
// Each shorthand -> the keywords one of its values may drop, each with the rest
859+
// of the keywords its own slot takes. A sibling out of that set means the value
860+
// fills the slot twice, which is a declaration the engine drops.
861+
const SHORTHAND_INITIAL_KEYWORDS = new Map([
862+
[
863+
"animation",
864+
new Map([
865+
[
866+
"ease",
867+
new Set([
868+
"ease",
869+
"ease-in",
870+
"ease-in-out",
871+
"ease-out",
872+
"linear",
873+
"step-end",
874+
"step-start"
875+
])
876+
],
877+
[
878+
"normal",
879+
new Set(["alternate", "alternate-reverse", "normal", "reverse"])
880+
],
881+
["running", new Set(["paused", "running"])]
882+
])
883+
],
884+
[
885+
"border",
886+
new Map([
887+
[
888+
"none",
889+
new Set([
890+
"dashed",
891+
"dotted",
892+
"double",
893+
"groove",
894+
"hidden",
895+
"inset",
896+
"none",
897+
"outset",
898+
"ridge",
899+
"solid"
900+
])
901+
]
902+
])
903+
],
904+
[
905+
"border-bottom",
906+
new Map([
907+
[
908+
"none",
909+
new Set([
910+
"dashed",
911+
"dotted",
912+
"double",
913+
"groove",
914+
"hidden",
915+
"inset",
916+
"none",
917+
"outset",
918+
"ridge",
919+
"solid"
920+
])
921+
]
922+
])
923+
],
924+
[
925+
"border-image",
926+
new Map([["stretch", new Set(["repeat", "round", "space", "stretch"])]])
927+
],
928+
[
929+
"border-left",
930+
new Map([
931+
[
932+
"none",
933+
new Set([
934+
"dashed",
935+
"dotted",
936+
"double",
937+
"groove",
938+
"hidden",
939+
"inset",
940+
"none",
941+
"outset",
942+
"ridge",
943+
"solid"
944+
])
945+
]
946+
])
947+
],
948+
[
949+
"border-right",
950+
new Map([
951+
[
952+
"none",
953+
new Set([
954+
"dashed",
955+
"dotted",
956+
"double",
957+
"groove",
958+
"hidden",
959+
"inset",
960+
"none",
961+
"outset",
962+
"ridge",
963+
"solid"
964+
])
965+
]
966+
])
967+
],
968+
[
969+
"border-top",
970+
new Map([
971+
[
972+
"none",
973+
new Set([
974+
"dashed",
975+
"dotted",
976+
"double",
977+
"groove",
978+
"hidden",
979+
"inset",
980+
"none",
981+
"outset",
982+
"ridge",
983+
"solid"
984+
])
985+
]
986+
])
987+
],
988+
[
989+
"column-rule",
990+
new Map([
991+
[
992+
"none",
993+
new Set([
994+
"dashed",
995+
"dotted",
996+
"double",
997+
"groove",
998+
"hidden",
999+
"inset",
1000+
"none",
1001+
"outset",
1002+
"ridge",
1003+
"solid"
1004+
])
1005+
]
1006+
])
1007+
],
1008+
[
1009+
"flex-flow",
1010+
new Map([
1011+
["nowrap", new Set(["nowrap", "wrap", "wrap-reverse"])],
1012+
["row", new Set(["column", "column-reverse", "row", "row-reverse"])]
1013+
])
1014+
],
1015+
["list-style", new Map([["outside", new Set(["inside", "outside"])]])],
1016+
[
1017+
"mask",
1018+
new Map([
1019+
["add", new Set(["add", "exclude", "intersect", "subtract"])],
1020+
["match-source", new Set(["alpha", "luminance", "match-source"])],
1021+
[
1022+
"repeat",
1023+
new Set([
1024+
"no-repeat",
1025+
"repeat",
1026+
"repeat-x",
1027+
"repeat-y",
1028+
"round",
1029+
"space"
1030+
])
1031+
]
1032+
])
1033+
],
1034+
[
1035+
"mask-border",
1036+
new Map([
1037+
["alpha", new Set(["alpha", "luminance"])],
1038+
["stretch", new Set(["repeat", "round", "space", "stretch"])]
1039+
])
1040+
],
1041+
[
1042+
"outline",
1043+
new Map([
1044+
[
1045+
"none",
1046+
new Set([
1047+
"auto",
1048+
"dashed",
1049+
"dotted",
1050+
"double",
1051+
"groove",
1052+
"inset",
1053+
"none",
1054+
"outset",
1055+
"ridge",
1056+
"solid"
1057+
])
1058+
]
1059+
])
1060+
],
1061+
[
1062+
"text-decoration",
1063+
new Map([
1064+
[
1065+
"none",
1066+
new Set([
1067+
"blink",
1068+
"grammar-error",
1069+
"line-through",
1070+
"none",
1071+
"overline",
1072+
"spelling-error",
1073+
"underline"
1074+
])
1075+
],
1076+
["solid", new Set(["dashed", "dotted", "double", "solid", "wavy"])]
1077+
])
1078+
],
1079+
[
1080+
"transition",
1081+
new Map([
1082+
[
1083+
"ease",
1084+
new Set([
1085+
"ease",
1086+
"ease-in",
1087+
"ease-in-out",
1088+
"ease-out",
1089+
"linear",
1090+
"step-end",
1091+
"step-start"
1092+
])
1093+
],
1094+
["normal", new Set(["allow-discrete", "normal"])]
1095+
])
1096+
]
1097+
]);
1098+
1099+
// Each `font-stretch` keyword -> the percentage it names, which is the same
1100+
// value in fewer bytes.
1101+
const FONT_STRETCH_PERCENTAGES = new Map([
1102+
["ultra-condensed", "50%"],
1103+
["extra-condensed", "62.5%"],
1104+
["condensed", "75%"],
1105+
["semi-condensed", "87.5%"],
1106+
["normal", "100%"],
1107+
["semi-expanded", "112.5%"],
1108+
["expanded", "125%"],
1109+
["extra-expanded", "150%"],
1110+
["ultra-expanded", "200%"]
1111+
]);
1112+
8581113
// The generic font families: an unquoted one of these names the generic rather
8591114
// than a family called that, so a quoted family spelled like one keeps its quotes.
8601115
const GENERIC_FONT_FAMILIES = new Set([
@@ -2607,6 +2862,7 @@ module.exports.FAMILY_LONGHANDS = FAMILY_LONGHANDS;
26072862
module.exports.FAMILY_SLOT_CLASSES = FAMILY_SLOT_CLASSES;
26082863
module.exports.FAMILY_SLOT_KEYWORDS = FAMILY_SLOT_KEYWORDS;
26092864
module.exports.FLEX_KEYWORDS = FLEX_KEYWORDS;
2865+
module.exports.FONT_STRETCH_PERCENTAGES = FONT_STRETCH_PERCENTAGES;
26102866
module.exports.FONT_WEIGHT_NUMBERS = FONT_WEIGHT_NUMBERS;
26112867
module.exports.GENERIC_FONT_FAMILIES = GENERIC_FONT_FAMILIES;
26122868
module.exports.INITIAL_VALUE_KEYWORDS = INITIAL_VALUE_KEYWORDS;
@@ -2630,6 +2886,7 @@ module.exports.REPEAT_STYLE_KEYWORDS = REPEAT_STYLE_KEYWORDS;
26302886
module.exports.REPEAT_STYLE_PROPERTIES = REPEAT_STYLE_PROPERTIES;
26312887
module.exports.RGB_TO_NAME = RGB_TO_NAME;
26322888
module.exports.SELECTOR_FUNCTIONS = SELECTOR_FUNCTIONS;
2889+
module.exports.SHORTHAND_INITIAL_KEYWORDS = SHORTHAND_INITIAL_KEYWORDS;
26332890
module.exports.SLASH_BOX_SHORTHANDS = SLASH_BOX_SHORTHANDS;
26342891
module.exports.STEPPED_FUNCTIONS = STEPPED_FUNCTIONS;
26352892
module.exports.SUBSTITUTION_FUNCTIONS = SUBSTITUTION_FUNCTIONS;

‎lib/css/syntax.js‎

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const {
2727
FAMILY_SLOT_CLASSES,
2828
FAMILY_SLOT_KEYWORDS,
2929
FLEX_KEYWORDS,
30+
FONT_STRETCH_PERCENTAGES,
3031
FONT_WEIGHT_NUMBERS,
3132
GENERIC_FONT_FAMILIES,
3233
INITIAL_VALUE_KEYWORDS,
@@ -49,6 +50,7 @@ const {
4950
REPEAT_STYLE_PROPERTIES,
5051
RGB_TO_NAME,
5152
SELECTOR_FUNCTIONS,
53+
SHORTHAND_INITIAL_KEYWORDS,
5254
SLASH_BOX_SHORTHANDS,
5355
STEPPED_FUNCTIONS,
5456
SUBSTITUTION_FUNCTIONS,
@@ -6316,6 +6318,44 @@ const _collapseRepeatedPair = (property, components) => {
63166318
return null;
63176319
};
63186320

6321+
/**
6322+
* Drop the shorthand slots holding what they already default to. A sibling out
6323+
* of the same slot's keywords means the value fills that slot twice, which is a
6324+
* declaration the engine drops — unwriting one of them would revive it.
6325+
* @param {string} property the declaration's lowercased property name
6326+
* @param {string[]} components the value's top-level components
6327+
* @returns {string[] | null} the fragments to join, or `null` to keep the value
6328+
*/
6329+
const _dropInitialKeywords = (property, components) => {
6330+
const table = SHORTHAND_INITIAL_KEYWORDS.get(property);
6331+
if (table === undefined || components.length < 2) return null;
6332+
// A comma parts two layers and a `/` reaches a slot through another's value;
6333+
// either way the slots are no longer this one flat list.
6334+
for (const component of components) {
6335+
if (component.includes(",") || component === "/") return null;
6336+
}
6337+
const lowered = components.map(toLowerCaseIfNeeded);
6338+
const kept = [];
6339+
for (let i = 0; i < components.length; i++) {
6340+
const siblings = table.get(lowered[i]);
6341+
if (
6342+
siblings !== undefined &&
6343+
!lowered.some((other, j) => j !== i && siblings.has(other))
6344+
) {
6345+
continue;
6346+
}
6347+
kept.push(components[i]);
6348+
}
6349+
if (kept.length === components.length) return null;
6350+
if (kept.length !== 0) return kept;
6351+
// Every slot held its own initial, so any one of them says all of them.
6352+
let shortest = components[0];
6353+
for (const component of components) {
6354+
if (component.length < shortest.length) shortest = component;
6355+
}
6356+
return [shortest];
6357+
};
6358+
63196359
/**
63206360
* Rewrite a `<position>` written as edge keywords into the percentages they
63216361
* resolve to. Keywords only: an offset beside one is the 3/4-value syntax,
@@ -6410,7 +6450,11 @@ const _collapseShorthand = (path, property, children, writer) => {
64106450
if (property === "grid-template-areas") {
64116451
return _collapseGridTemplateAreas(path, children, writer);
64126452
}
6413-
const components = _valueComponents(path, children, writer);
6453+
const written = _valueComponents(path, children, writer);
6454+
// A slot holding its own initial goes first, so what follows reads the
6455+
// components that are left rather than the ones the author wrote.
6456+
const dropped = _dropInitialKeywords(property, written);
6457+
const components = dropped === null ? written : dropped;
64146458
if (property === "display" && components.length === 2) {
64156459
const pair = `${toLowerCaseIfNeeded(components[0])} ${toLowerCaseIfNeeded(components[1])}`;
64166460
// `<display-outside> || <display-inside>` is order-free, so both readings.
@@ -6419,13 +6463,15 @@ const _collapseShorthand = (path, property, children, writer) => {
64196463
DISPLAY_SHORT_FORMS.get(pair.split(" ").reverse().join(" "));
64206464
if (short !== undefined) return [short];
64216465
}
6422-
if (property === "font") return _numberFontShorthandWeight(components);
6466+
if (property === "font") {
6467+
return _numberFontShorthandWeight(components) || dropped;
6468+
}
64236469
// One layer only: a comma parts two of them and each would need its own pass.
64246470
if (
64256471
property === "transition" &&
64266472
!components.some((one) => one.includes(","))
64276473
) {
6428-
return _orderTransitionSlots(components);
6474+
return _orderTransitionSlots(components) || dropped;
64296475
}
64306476
// `initial` computes to the property's initial value, so where that value is
64316477
// a shorter keyword the two are the same declaration.
@@ -6442,9 +6488,18 @@ const _collapseShorthand = (path, property, children, writer) => {
64426488
) {
64436489
return components;
64446490
}
6491+
// A `font-stretch` keyword is the percentage it names, in fewer bytes.
6492+
if (property === "font-stretch" && components.length === 1) {
6493+
const percentage = FONT_STRETCH_PERCENTAGES.get(
6494+
toLowerCaseIfNeeded(components[0])
6495+
);
6496+
if (percentage !== undefined && percentage.length < components[0].length) {
6497+
return [percentage];
6498+
}
6499+
}
64456500
const position = _collapsePositionKeywords(property, components);
64466501
if (position !== null) return position;
6447-
return _collapseRepeatedPair(property, components);
6502+
return _collapseRepeatedPair(property, components) || dropped;
64486503
};
64496504

64506505
// The length units, shared by the zero-unit drop and the value classifier. No

0 commit comments

Comments
 (0)