A very light minifier Node.js module.
Support:
- Babili
- Butternut
- YUI Compressor
- Google Closure Compiler
- UglifyJS
- 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.
npm install node-minify
var compressor = require('node-minify');
// Using Google Closure Compiler
compressor.minify({
compressor: 'gcc',
input: 'foo.js',
output: 'bar.js',
callback: function (err, min) {}
});
// Using UglifyJS
compressor.minify({
compressor: 'uglifyjs',
input: './**/*.js',
output: 'bar.js',
callback: function (err, min) {}
});
// Using Promise
var promise = compressor.minify({
compressor: 'uglifyjs',
input: './**/*.js',
output: 'bar.js'
});
promise.then(function(min) {});
In order to concatenate files, simply pass in an array with the type no-compress
.
compressor.minify({
compressor: 'no-compress',
input: ['foo.js', 'foo2.js', 'foo3.js'],
output: 'bar.js',
callback: function (err, min) {}
});
compressor.minify({
compressor: 'gcc',
input: 'public/**/*.js',
output: 'bar.js',
callback: function (err, min) {}
});
compressor.minify({
compressor: 'yui-js',
input: 'foo.js',
output: 'bar.js',
sync: true,
callback: function (err, min) {}
});
publicFolder
allow you to specify an input and output folder.
It avoids you to specify the folder for each file.
compressor.minify({
compressor: 'gcc',
publicFolder: './public/',
input: ['foo.js', 'foo2.js'],
output: 'bar.js',
callback: function (err, 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:
compressor.minify({
compressor: 'gcc',
input: 'foo.js',
output: 'bar.js',
sync: true,
buffer: 1000 * 1024,
callback: function (err, min) {}
});
You can pass an object to the compressor.
Please check available options.
compressor.minify({
compressor: 'babili',
input: 'foo.js',
output: 'bar.js',
options: {
babelrc: 'public/.babelrc',
presets: ['es2015']
},
callback: function (err, min) {}
});
compressor.minify({
compressor: 'butternut',
input: 'foo.js',
output: 'bar.js',
options: {
check: false,
allowDangerousEval: false,
sourceMap: true
},
callback: function (err, min) {}
});
compressor.minify({
compressor: 'yui-js',
input: 'foo.js',
output: 'bar.js',
options: {
'line-break': 80,
charset: 'utf8'
... // See more information link below
},
callback: function (err, min) {}
});
compressor.minify({
compressor: 'gcc',
input: 'foo.js',
output: 'bar.js',
options: {
compilation_level: 'WHITESPACE_ONLY',
language: 'ECMASCRIPT6'
... // See more information link below
},
callback: function (err, min) {}
});
compressor.minify({
compressor: 'uglifyjs',
input: 'foo.js',
output: 'bar.js',
options: {
warnings: true, // pass true to display compressor warnings.
mangle: false // pass false to skip mangling names.
output: {} // pass an object if you wish to specify additional output options. The defaults are optimized for best compression.
compress: false // pass false to skip compressing entirely. Pass an object to specify custom compressor options.
},
callback: function (err, min) {}
});
compressor.minify({
compressor: 'clean-css',
input: 'foo.css',
output: 'bar.css',
options: {
advanced: false, // set to false to disable advanced optimizations - selector & property merging, reduction, etc.
aggressiveMerging: false // set to false to disable aggressive merging of properties.
... // See more information link below
},
callback: function (err, min) {}
});
compressor.minify({
compressor: 'csso',
input: 'foo.css',
output: 'bar.css',
options: {
restructureOff: true // turns structure minimization off
},
callback: function (err, min) {}
});
compressor.minify({
compressor: 'sqwish',
input: 'foo.css',
output: 'bar.css',
options: {
strict: true // strict optimizations
},
callback: function (err, min) {}
});
Babili can compress only JavaScript files.
https://github.com/babel/babili
Butternut can compress only JavaScript files.
https://github.com/Rich-Harris/butternut
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.
GCC latest version requires Java 1.8 You can use the legacy version that use Java 1.6
var compressor = require('node-minify');
// Using Google Closure Compiler legacy version for Java 1.6
compressor.minify({
compressor: 'gcc-legacy',
input: 'foo.js',
output: 'bar.js',
callback: function (err, min) {}
});
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/UglifyJS2
Clean-css can compress only CSS files.
https://github.com/GoalSmashers/clean-css
CSSO can compress only CSS files.
Sqwish can compress only CSS files.
It assumes that you have Java installed on your environment for both GCC and YUI Compressor. To check, run:
java -version
How to install:
Mac: https://java.com/en/download/help/mac_install.xml
Windows: https://java.com/en/download/help/windows_manual_download.xml
Linux: https://www.java.com/en/download/help/linux_x64_install.xml
Since v0.5.0, a windows support is available for the no-compress option and uglify-js (thanks to pieces029 and benpusherhq)