PostCSS plugin that reduces transform functions to their simplest equivalent forms.
npx @tessl/cli install tessl/npm-postcss-reduce-transforms@7.0.0PostCSS Reduce Transforms is a PostCSS plugin that optimizes CSS transform functions by reducing them to their simplest equivalent forms. It converts complex 3D transform functions to simpler 2D equivalents where possible, consolidates redundant parameters, and reduces matrix3d functions to matrix when appropriate.
npm install postcss-reduce-transformsconst reduceTransforms = require('postcss-reduce-transforms');For ES modules:
import reduceTransforms from 'postcss-reduce-transforms';const postcss = require('postcss');
const reduceTransforms = require('postcss-reduce-transforms');
// As part of a PostCSS processing chain
postcss([reduceTransforms()])
.process(css, { from: undefined })
.then(result => {
console.log(result.css);
});
// With other PostCSS plugins (typical usage)
const postcss = require('postcss');
const autoprefixer = require('autoprefixer');
const reduceTransforms = require('postcss-reduce-transforms');
postcss([
autoprefixer,
reduceTransforms(), // Usually run after other transform plugins
])
.process(css, { from: 'input.css', to: 'output.css' })
.then(result => {
// Optimized CSS with reduced transform functions
console.log(result.css);
});Example transformation:
Input CSS:
h1 {
transform: rotate3d(0, 0, 1, 20deg);
}Output CSS:
h1 {
transform: rotate(20deg);
}PostCSS Reduce Transforms is built around several key components:
postcssPlugin name and prepare() methodpostcss-value-parser to parse and manipulate CSS transform function syntax treeswalk() methodvar() and env() functions during optimizationThe plugin processes CSS declarations matching /transform$/i (including vendor prefixes) in the OnceExit phase, ensuring all transforms are optimized after the CSS has been fully processed.
Creates a PostCSS plugin instance that processes CSS transform declarations and optimizes them.
/**
* Creates a PostCSS plugin that reduces CSS transform functions
* @returns {import('postcss').Plugin} PostCSS plugin instance
*/
function reduceTransforms(): Plugin;The plugin automatically processes all CSS declarations matching the pattern /transform$/i (including transform, Transform, -webkit-transform, etc.) and applies the following optimizations:
Converts matrix3d functions to simpler matrix equivalents when possible.
Transformation Rule:
matrix3d(a, b, 0, 0, c, d, 0, 0, 0, 0, 1, 0, tx, ty, 0, 1) → matrix(a, b, c, d, tx, ty)Example:
/* Input */
transform: matrix3d(20, 20, 0, 0, 40, 40, 0, 0, 0, 0, 1, 0, 80, 80, 0, 1);
/* Output */
transform: matrix(20, 20, 40, 40, 80, 80);Converts rotate3d functions to their axis-specific equivalents.
Transformation Rules:
rotate3d(1, 0, 0, a) → rotateX(a)rotate3d(0, 1, 0, a) → rotateY(a)rotate3d(0, 0, 1, a) → rotate(a)Examples:
/* Input */
transform: rotate3d(1, 0, 0, 45deg);
transform: rotate3d(0, 1, 0, 90deg);
transform: rotate3d(0, 0, 1, 180deg);
/* Output */
transform: rotateX(45deg);
transform: rotateY(90deg);
transform: rotate(180deg);Converts rotateZ functions to simpler rotate equivalents.
Transformation Rule:
rotateZ(rz) → rotate(rz)Example:
/* Input */
transform: rotateZ(45deg);
/* Output */
transform: rotate(45deg);Optimizes scale functions by removing redundant parameters.
Transformation Rules:
scale(sx, sy) → scale(sx) when sx === syscale(sx, 1) → scaleX(sx)scale(1, sy) → scaleY(sy)Examples:
/* Input */
transform: scale(2, 2);
transform: scale(1.5, 1);
transform: scale(1, 0.8);
/* Output */
transform: scale(2);
transform: scaleX(1.5);
transform: scaleY(0.8);Converts scale3d functions to their axis-specific equivalents when possible.
Transformation Rules:
scale3d(sx, 1, 1) → scaleX(sx)scale3d(1, sy, 1) → scaleY(sy)scale3d(1, 1, sz) → scaleZ(sz)Examples:
/* Input */
transform: scale3d(2, 1, 1);
transform: scale3d(1, 1.5, 1);
transform: scale3d(1, 1, 0.5);
/* Output */
transform: scaleX(2);
transform: scaleY(1.5);
transform: scaleZ(0.5);Optimizes translate functions by removing zero values and converting to axis-specific functions.
Transformation Rules:
translate(tx, 0) → translate(tx)translate(0, ty) → translateY(ty)Examples:
/* Input */
transform: translate(100px, 0);
transform: translate(0, 50px);
/* Output */
transform: translate(100px);
transform: translateY(50px);Converts translate3d functions to translateZ when X and Y are zero.
Transformation Rule:
translate3d(0, 0, tz) → translateZ(tz)Example:
/* Input */
transform: translate3d(0, 0, 10px);
/* Output */
transform: translateZ(10px);declare function reduceTransforms(): import("postcss").Plugin;
declare namespace reduceTransforms {
let postcss: true;
}The plugin preserves CSS variables (var() and env() functions) without modification:
/* Input */
transform: scale(var(--scale-x), var(--scale-y));
/* Output - preserved as-is */
transform: scale(var(--scale-x), var(--scale-y));All transform function names are processed case-insensitively:
/* Input */
transform: ROTATEZ(90deg);
transform: SCALE3D(2, 1, 1);
/* Output */
transform: rotate(90deg);
transform: scaleX(2);The plugin includes internal caching to avoid re-processing identical transform values, improving performance when the same transform declarations appear multiple times in a stylesheet.