Trim whitespace inside and around CSS rules & declarations.
npx @tessl/cli install tessl/npm-postcss-normalize-whitespace@7.0.0PostCSS Normalize Whitespace is a PostCSS plugin that normalizes and optimizes whitespace in CSS code by trimming unnecessary spaces inside and around CSS rules and declarations. It intelligently handles various CSS constructs including calc() functions, CSS custom properties (variables), and preserves necessary spacing while removing redundant whitespace.
npm install postcss-normalize-whitespaceconst normalizeWhitespace = require("postcss-normalize-whitespace");For ES modules (using default import due to CommonJS export):
import normalizeWhitespace from "postcss-normalize-whitespace";const postcss = require("postcss");
const normalizeWhitespace = require("postcss-normalize-whitespace");
// Use with PostCSS
const result = await postcss([normalizeWhitespace()])
.process(css, { from: "input.css", to: "output.css" });
// Example transformation
const input = `
h1 {
width: calc(10px - ( 100px / var(--test) ))
}
`;
const output = `
h1{
width: calc(10px - 100px / var(--test))
}
`;The plugin follows the standard PostCSS plugin architecture:
Creates a PostCSS plugin instance for whitespace normalization.
/**
* Creates a PostCSS plugin for normalizing whitespace in CSS
* @returns {import('postcss').Plugin} PostCSS plugin object
*/
function normalizeWhitespace(): import('postcss').Plugin;
// PostCSS plugin identifier (static property)
normalizeWhitespace.postcss = true;The plugin automatically performs the following optimizations:
Input CSS:
h1 {
width: calc(10px - ( 100px / var(--test) )) ;
--custom-prop: ;
color: red !important ;
}Output CSS:
h1{
width:calc(10px - 100px / var(--test));
--custom-prop: ;
color:red!important
}This plugin accepts no configuration options - it applies whitespace normalization automatically according to CSS optimization best practices.
The plugin is designed to be safe and non-destructive: