Light Node.js module that compress javascript and css files with Babili / Yui Compressor / Google Closure Compiler / UglifyJS / Clean-css / CSSO / Sqwish
JavaScript
Latest commit b7719c7 Nov 27, 2016 @srod committed on GitHub Merge pull request #125 from srod/develop
Merge Develop to Master : 2.0.2

Readme.md

NPM Version NPM Downloads Linux Build Windows Build Coverage Status

Dependency Status devDependency Status

Node-minify

A very light minifier Node.js module.

Support:

  • Babili
  • 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.

Installation

npm install node-minify

Quick Start

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) {});

More examples

Concatenate Files

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) {}
});

Using wildcards

compressor.minify({
  compressor: 'gcc',
  input: 'public/**/*.js',
  output: 'bar.js',
  callback: function (err, min) {}
});

Using sync option

compressor.minify({
  compressor: 'yui-js',
  input: 'foo.js',
  output: 'bar.js',
  sync: true,
  callback: function (err, min) {}
});

Using public folder

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) {}
});

Max Buffer Size

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) {}
});

Passing options

You can pass an object to the compressor.

Please check available options.

Options for Babili

compressor.minify({
  compressor: 'babili',
  input: 'foo.js',
  output: 'bar.js',
  options: {
    babelrc: 'public/.babelrc',
    presets: ['es2015']
  },
  callback: function (err, min) {}
});

More informations

Options for YUI Compressor

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) {}
});

More informations

Options for Google Closure Compiler

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) {}
});

More informations

Options for UglifyJS

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) {}
});

More informations

Options for clean-css

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) {}
});

More informations

Options for CSSO

compressor.minify({
  compressor: 'csso',
  input: 'foo.css',
  output: 'bar.css',
  options: {
    restructureOff: true // turns structure minimization off
  },
  callback: function (err, min) {}
});

More informations

Options for Sqwish

compressor.minify({
  compressor: 'sqwish',
  input: 'foo.css',
  output: 'bar.css',
  options: {
    strict: true // strict optimizations
  },
  callback: function (err, min) {}
});

More informations

Babili

Babili can compress only JavaScript files.

https://github.com/babel/babili

YUI Compressor

Yahoo Compressor can compress both JavaScript and CSS files.

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

Google Closure Compiler

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

UglifyJS can compress only JavaScript files.

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

https://github.com/mishoo/UglifyJS2

Clean-css

Clean-css can compress only CSS files.

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

CSSO

CSSO can compress only CSS files.

https://github.com/css/csso

Sqwish

Sqwish can compress only CSS files.

https://github.com/ded/sqwish

Warning

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

Windows support

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

License

MIT