Skip to content

Commit cb48da8

Browse files
feat(css): reduce a transform until it stops getting shorter
One reduction uncovers the next: `translate3d(x,0,0)` left a `translate(x,0)` nothing then folded, and a z factor of 1 leaves a 2D scale. Corpus: 4,505,803 -> 4,503,356 raw / 539,417 -> 539,246 gzip.
1 parent 20c3e9b commit cb48da8

6 files changed

Lines changed: 45 additions & 4 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, rounding what it prints the way an authored number is rounded, 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 each layer of 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 — including the properties spelling both axes out — and a `font-stretch` keyword into the percentages they resolve to, drops a `<position>`'s second value where it names the centre an omitted one already means, drops a shorthand slot holding the value it already defaults to — including one every value of which its grammar spells out, such as `background`'s image — a `flex` shrink factor no basis can be read as, a shadow's trailing zero lengths and a filter function's argument that is the amount an omitted one means, writes the percent-escapes a data URI's payload does not need as the bytes they name, drops a gradient's last color stop position where the spec's own fix-up already puts it there and — where the target reads CSS Images 4's two-position stop — writes two stops of one color as the one stop naming both, 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 calls in a value need, writes a rotation about one axis as the call naming it, drops a background size's second value where `auto` is what an omitted one already means, 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, rounding what it prints the way an authored number is rounded, 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 each layer of 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 — including the properties spelling both axes out — and a `font-stretch` keyword into the percentages they resolve to, drops a `<position>`'s second value where it names the centre an omitted one already means, drops a shorthand slot holding the value it already defaults to — including one every value of which its grammar spells out, such as `background`'s image — a `flex` shrink factor no basis can be read as, a shadow's trailing zero lengths and a filter function's argument that is the amount an omitted one means, writes the percent-escapes a data URI's payload does not need as the bytes they name, drops a gradient's last color stop position where the spec's own fix-up already puts it there and — where the target reads CSS Images 4's two-position stop — writes two stops of one color as the one stop naming both, 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 calls in a value need, writes a rotation about one axis as the call naming it, reduces a transform until it stops getting shorter, drops a background size's second value where `auto` is what an omitted one already means, and keeps a `@supports` condition as written; HTML normalizes each attribute against the element it is on.

‎lib/css/syntax.js‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6599,6 +6599,28 @@ const _dropDefaultSecondValue = (property, value) => {
65996599
return changed ? layers.join(",") : value;
66006600
};
66016601

6602+
const _TRANSFORM_CALL_RE = /^([a-z0-9]+)\((.*)\)$/i;
6603+
6604+
/**
6605+
* Reduce a transform function until it stops getting shorter: one reduction
6606+
* uncovers the next, `translate3d(x,0,0)` leaving the `translate(x,0)` that is
6607+
* `translate(x)`.
6608+
* @param {string} fn the lowercased function name
6609+
* @param {string} inner the already-joined argument text
6610+
* @returns {string | null} the shortest call, or null to keep the function
6611+
*/
6612+
const _reduceTransformFunctionDeep = (fn, inner) => {
6613+
let out = _reduceTransformFunction(fn, inner);
6614+
if (out === null) return null;
6615+
for (;;) {
6616+
const call = _TRANSFORM_CALL_RE.exec(out);
6617+
if (call === null) return out;
6618+
const next = _reduceTransformFunction(call[1].toLowerCase(), call[2]);
6619+
if (next === null) return out;
6620+
out = next;
6621+
}
6622+
};
6623+
66026624
/**
66036625
* Reduce a transform function to the shorter one naming the same matrix: a
66046626
* translation whose other axes are zero is that axis's own function, and a
@@ -6627,6 +6649,10 @@ const _reduceTransformFunction = (fn, inner) => {
66276649
if (fn === "scale" && args.length === 2 && args[0] === args[1]) {
66286650
return `scale(${args[0]})`;
66296651
}
6652+
// A z factor of 1 scales nothing along it, leaving the 2D scale.
6653+
if (fn === "scale3d" && args.length === 3 && args[2] === "1") {
6654+
return `scale(${args[0]},${args[1]})`;
6655+
}
66306656
// CSS Transforms 2 §13.1: `rotateZ(a)` is the `rotate(a)` naming the same
66316657
// rotation about the z axis.
66326658
if (fn === "rotatez" && args.length === 1) return `rotate(${args[0]})`;
@@ -7720,7 +7746,7 @@ const printer = (path, writer) => {
77207746
return `${name}()`;
77217747
}
77227748
}
7723-
const reduced = _reduceTransformFunction(fn, inner);
7749+
const reduced = _reduceTransformFunctionDeep(fn, inner);
77247750
if (reduced !== null) return reduced;
77257751
// The stops are folded over what dropping the direction leaves, a
77267752
// gradient carrying both otherwise keeping whichever ran second.

‎test/CssSyntax.unittest.js‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2746,7 +2746,12 @@ describe("CssSyntax minify — the value transforms' rejection paths", () => {
27462746
["a{transform:rotateZ(37deg)}", "a{transform:rotate(37deg)}"],
27472747
["a{transform:rotate3d(0,0,1,37deg)}", "a{transform:rotate(37deg)}"],
27482748
["a{transform:rotate3d(1,0,0,37deg)}", "a{transform:rotateX(37deg)}"],
2749-
["a{transform:rotate3d(0,1,0,37deg)}", "a{transform:rotateY(37deg)}"]
2749+
["a{transform:rotate3d(0,1,0,37deg)}", "a{transform:rotateY(37deg)}"],
2750+
// A z factor of 1 scales nothing along it, leaving the 2D scale.
2751+
["a{transform:scale3d(2,3,1)}", "a{transform:scale(2,3)}"],
2752+
// One reduction uncovers the next: the 2D call it leaves reduces too.
2753+
["a{transform:scale3d(1,1,1)}", "a{transform:scale(1)}"],
2754+
["a{transform:translate3d(-50%,0,0)}", "a{transform:translate(-50%)}"]
27502755
])("%s", (css, expected) => {
27512756
expect(minify(css)).toBe(expected);
27522757
});
@@ -2760,7 +2765,8 @@ describe("CssSyntax minify — the value transforms' rejection paths", () => {
27602765
// names the axis, but a negative one turns the rotation the other way.
27612766
["the axis is scaled", "a{transform:rotate3d(0,0,2,37deg)}"],
27622767
["the axis is negative", "a{transform:rotate3d(0,0,-1,37deg)}"],
2763-
["the vector names no axis", "a{transform:rotate3d(1,1,0,37deg)}"]
2768+
["the vector names no axis", "a{transform:rotate3d(1,1,0,37deg)}"],
2769+
["the z factor scales", "a{transform:scale3d(2,3,4)}"]
27642770
])("keeps it where %s", (_name, css) => {
27652771
expect(minify(css)).toBe(css);
27662772
});

‎test/configCases/css/minimize-values/__snapshots__/ConfigCacheTest.snap‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,5 +160,7 @@ Array [
160160
"/*! a size's omitted second value is \`auto\`, so writing it says nothing */.t74{background-size:1rem}.t74a{background-size:50%,2px}",
161161
"/*! declined: the omitted value is \`auto\`, not the first repeated */.t74b{background-size:1rem 1rem}",
162162
"/*! declined: a substitution could make the \`auto\` a third value */.t74c{background-size:var(--w) auto}",
163+
"/*! one reduction uncovers the next, down to the shortest call */.t75{transform:translate(-50%)}.t75a{transform:scale(1)}",
164+
"/*! declined: a z factor that scales keeps the 3D call */.t75b{transform:scale3d(2,3,4)}",
163165
]
164166
`;

‎test/configCases/css/minimize-values/__snapshots__/ConfigTest.snap‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,5 +160,7 @@ Array [
160160
"/*! a size's omitted second value is \`auto\`, so writing it says nothing */.t74{background-size:1rem}.t74a{background-size:50%,2px}",
161161
"/*! declined: the omitted value is \`auto\`, not the first repeated */.t74b{background-size:1rem 1rem}",
162162
"/*! declined: a substitution could make the \`auto\` a third value */.t74c{background-size:var(--w) auto}",
163+
"/*! one reduction uncovers the next, down to the shortest call */.t75{transform:translate(-50%)}.t75a{transform:scale(1)}",
164+
"/*! declined: a z factor that scales keeps the 3D call */.t75b{transform:scale3d(2,3,4)}",
163165
]
164166
`;

‎test/configCases/css/minimize-values/style.css‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,3 +357,8 @@
357357
.t74b { background-size: 1rem 1rem }
358358
/*! declined: a substitution could make the `auto` a third value */
359359
.t74c { background-size: var(--w) auto }
360+
/*! one reduction uncovers the next, down to the shortest call */
361+
.t75 { transform: translate3d(-50%, 0, 0) }
362+
.t75a { transform: scale3d(1, 1, 1) }
363+
/*! declined: a z factor that scales keeps the 3D call */
364+
.t75b { transform: scale3d(2, 3, 4) }

0 commit comments

Comments
 (0)