node-minify

Javascript / CSS minifier based on YUI Compressor / Google Closure Compiler / UglifyJS2 / Sqwish / Clean-css / CSSO

Node-minify

A very light minifier NodeJS module.

Support:

  • YUI Compressor --version 2.4.7
  • Google Closure Compiler --version v20130411
  • UglifyJS2
  • Clean-css
  • CSSO
  • Sqwish

It allow you to compress JavaScript and CSS files.

CSS benchmark : http://goalsmashers.github.io/css-minification-benchmark/

I recommend to execute it at boot time for production use.

See server.js in examples/.

npm install node-minify
var compressor = require('node-minify');
 
// Using Google Closure 
new compressor.minify({
    type: 'gcc',
    fileIn: 'public/js/base.js',
    fileOut: 'public/js-dist/base-min-gcc.js',
    callbackfunction(errmin){
        console.log(err);
//        console.log(min); 
    }
});
 
// Array 
new compressor.minify({
    type: 'gcc',
    fileIn: ['public/js/base.js', 'public/js/base2.js'],
    fileOut: 'public/js-dist/base-onefile-gcc.js',
    callbackfunction(errmin){
        console.log(err);
//        console.log(min); 
    }
});
 
// Only concatenation of files (no compression) 
new compressor.minify({
    type: 'no-compress',
    fileIn: ['public/js/base.js', 'public/js/base2.js'],
    fileOut: 'public/js-dist/base-onefile-gcc.js',
    callbackfunction(errmin){
        console.log(err);
//        console.log(min); 
    }
});
 
// Using YUI Compressor for CSS 
new compressor.minify({
    type: 'yui-css',
    fileIn: 'public/css/base.css',
    fileOut: 'public/css/base-min-yui.css',
    callbackfunction(errmin){
        console.log(err);
//        console.log(min); 
    }
});
 
// Using YUI Compressor for JS 
new compressor.minify({
    type: 'yui-js',
    fileIn: 'public/js/base.js',
    fileOut: 'public/js-dist/base-min-yui.js',
    callbackfunction(errmin){
        console.log(err);
//        console.log(min); 
    }
});
 
// Using UglifyJS for JS 
new compressor.minify({
    type: 'uglifyjs',
    fileIn: 'public/js/base.js',
    fileOut: 'public/js-dist/base-onefile-uglify.js',
    callbackfunction(errmin){
        console.log(err);
//        console.log(min); 
    }
});
 
// Using Sqwish for CSS 
new compressor.minify({
    type: 'sqwish',
    fileIn: ['public/css/base.css', 'public/css/base2.css'],
    fileOut: 'public/css/base-min-sqwish.css',
    callbackfunction(errmin){
        console.log('Sqwish');
        console.log(err);
//        console.log(min); 
    }
});
 
// Using public folder option 
new compressor.minify({
    type: 'yui-js',
    publicFolder: 'public/js/',
    fileIn: 'base.js',
    fileOut: 'public/js-dist/base-min-yui-publicfolder.js',
    callbackfunction(errmin){
        console.log('YUI JS with publicFolder option');
        console.log(err);
//        console.log(min); 
    }
});
 
// Using Clean-css for CSS 
new compressor.minify({
    type: 'clean-css',
    fileIn: ['public/css/base.css', 'public/css/base2.css'],
    fileOut: 'public/css/base-min-cleancss.css',
    callbackfunction(errmin){
        console.log('Clean-css');
        console.log(err);
//        console.log(min); 
    }
});
 
// Using CSSO for CSS 
new compressor.minify({
    type: 'csso',
    fileIn: ['public/css/base.css', 'public/css/base2.css'],
    fileOut: 'public/css/base-min-csso.css',
    callbackfunction(errmin){
        console.log('CSSO');
        console.log(err);
//        console.log(min); 
    }
});

In order to concatenate files, simply pass in an array with the file paths to fileIn.

fileIn: ['public/js/base.js', 'public/js/base2.js', ...]
new compressor.minify({
    type: 'gcc',
    fileIn: 'public/**/*.js',
    fileOut: 'public/js-dist/wildcards-match-gcc.js',
    callbackfunction(errmin){
        console.log('wildcards match GCC');
        console.log(err);
//      console.log(min); 
    }
});

You can pass any option/flag you want

options: ['--option=1', '--option=2']
 
new compressor.minify({
    type: 'gcc',
    language: 'ECMASCRIPT5',
    fileIn: 'public/js/jquery-2.0.3.js',
    fileOut: 'public/js-dist/jquery-2.0.3-gcc.js',
    options: ['--option=1', '--option=2'],
    callbackfunction(errmin){
        console.log('GCC jquery 2.0');
        console.log(err);
//      console.log(min); 
    }
});

In some cases you might need a bigger max buffer size (for example when minifying really large files). By default the buffer is 1000 * 1024 which should be enough. If you however need more buffer, you can simply pass in the desired buffer size as an argument to compressor.minify like so:

new compressor.minify({
    type: 'uglifyjs',
    fileIn: './public/css/base.css',
    fileOut: './public/css/base-min-uglifyjs.css',
    buffer: 1000 * 1024,
    callbackfunction(err){
        console.log(err);
    }
});

You can define a temporary folder where temporary files will be generated :

new compressor.minify({
    type: 'yui-js',
    fileIn: 'public/js/base.js',
    fileOut: 'public/js-dist/base-min-yui.js',
    tempPath: '/tmp/',
    callbackfunction(err){
        console.log(err);
    }
});

Yahoo Compressor can compress both JavaScript and CSS files.

http://developer.yahoo.com/yui/compressor/

Google Closure Compiler can compress only JavaScript files.

It will throw an error if you try with CSS files.

https://developers.google.com/closure/compiler/

UglifyJS can compress only JavaScript files.

It will throw an error if you try with CSS files.

https://github.com/mishoo/UglifyJS

Clean-css can compress only CSS files.

https://github.com/GoalSmashers/clean-css

CSSO can compress only CSS files.

https://github.com/css/csso

Sqwish can compress only CSS files.

https://github.com/ded/sqwish

It assumes you have Java installed on your environment for both GCC and YUI Compressor. To check, run:

java -version

Since v0.5.0, a windows support is available for the no-compress option and uglify-js (thanks to pieces029 and benpusherhq)