How to minify CSS files with clean-css

clean-css is a nodejs program that minimizes CSS files.

clean-css has two things, one is a CLI tool and the other is a library that you can use it in your program.

CLI Tool

you can minify CSS files with clean-css-cli in the terminal.

Install the CLI Tool with the following command

$ sudo npm install clean-css-cli -g

You can minify your CSS file. the next command is to minify the local main.css and make it the main.min.css file.

$ cleancss -o main.min.css main.css

If you look at the generated file main.min.css, you will see that it's minified.

nodejs library

To use it in nodejs, install the package with the following command

$ npm install --save-dev clean-css

You can use it like this.

var CleanCSS = require('clean-css');
var input = 'a{font-weight:bold;}';
var options = { /* options */ };
var output = new CleanCSS(options).minify(input);

There are more options available in the clean-css page

References

codechachaCopyright ©2019 codechacha