Clean-css – A Node.js CSS Compressor
Today’s feature is GoalSmashers’ node.js library called clean-css. It’s a nice and very simple utility for minifying and concatenating CSS files. It strips unnecessary whitespace, removes comments, deletes the last semicolon of every selector, and more. Basically, it does the same job as YUI Compressor’s CSS minifier but much faster.
Using the clean-css library is fairly easy (and again, simple).
Installation:
npm install clean-css
Example usage (minifying a css file named ‘public.css’):
cleancss -o public-min.css public.css
To minify the same above file into standard output, skip the –o parameter:
cleancss public.css
Alternatively, you could do it this way:
cat one.css two.css three.css | cleancss -o merged-and-minified.css
Using clean-css via NodeJA apps (i.e. within your JavaScript code):
# Get the clean-css package
var cleanCSS = require('clean-css');
# Read in the source of a file or use hard-coded CSS...
var source = "body { color: red; font-weight: bold; }"; # Minify! var minifiedCSS = cleanCSS.process(source);
If you want to preserve a comment block:
/*!
Important comments included in minified output. */
To run a clean-css test:
make test
For more info and details about this library, check out its GitHub page.