node-minify 0 eslint travis-ci npm

Light Node.js module that compress javascript and css files with Babili / Yui Compressor / Google Closure Compiler / UglifyJS / Clean-css / CSSO / Sqwish

Build Status Build status Coverage Status Dependency Status devDependency Status NPM version NPM downloads

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/.

Installation

npm install node-minify

Quick Start

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',
  callback: function(err, min){
    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',
  callback: function(err, min){
    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',
    callback: function(err, min){
      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',
  callback: function(err, min){
    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',
  callback: function(err, min){
    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',
  callback: function(err, min){
    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',
    callback: function(err, min){
      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',
    callback: function(err, min){
      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',
    callback: function(err, min){
      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',
    callback: function(err, min){
      console.log('CSSO');
      console.log(err);
      //console.log(min);
    }
});

Concatenate Files

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', ...]

Using sync option

new compressor.minify({
  type: 'yui-js',
  publicFolder: 'public/js/',
  fileIn: 'base.js',
  fileOut: 'public/js-dist/base-min-yui-publicfolder.js',
  sync: true,
  callback: function(err, min) {
    console.log('YUI JS with publicFolder option');
    console.log(err);
    //console.log(min);
  }
});

Using wildcards

new compressor.minify({
  type: 'gcc',
  fileIn: 'public/**/*.js',
  fileOut: 'public/js-dist/wildcards-match-gcc.js',
  callback: function(err, min){
    console.log('wildcards match GCC');
    console.log(err);
    //console.log(min);
  }
});

Passing options

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'],
  callback: function(err, min){
    console.log('GCC jquery 2.0');
    console.log(err);
    //console.log(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:

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

Temp Path

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/',
  callback: function(err){
    console.log(err);
  }
});

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.

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/UglifyJS

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 you have Java installed on your environment for both GCC and YUI Compressor. To check, run:

java -version

Windows support

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

Related Repositories

node-minify

node-minify

Light Node.js module that compress javascript and css files with Babili / Yui Compressor / Google Closure Compiler / UglifyJS / Clean-css / CSSO / Sqwish ...

packnode

packnode

Obfuscate, minify and/or encrypt node modules for private use ...

assets-builder

assets-builder

Build modern assets, JS, CSS, SVGs, transpile ES6 to ES5, CSSNext to CSS3, compress, minify, etc with simple node scripts and CLI. ...

ender-minify

ender-minify

Bundled minifier utilities for Node, designed primarily for use with the Ender CLI. ...

Robol

Robol

Compile, concatenate, lint and minify JavaScript, CoffeeScript, CSS and LESS files using Node.js ...


Top Contributors

srod sebbo2002 brianberlin mathiasbynens alexbardas dsego icoloma stefanoortisi

Dependencies

package version
babel-core 6.25.0
babel-preset-babili 0.1.4
butternut 0.4.6
clean-css 4.1.7
csso 3.1.1
depd 1.1.0
glob 7.1.2
google-closure-compiler-js 20170626.0.0
mkdirp 0.5.1
node-version 1.0.0
sqwish 0.2.2
uglify-js 3.0.25
xtend 4.0.1
dev babel-preset-es2015 6.24.1
eslint 4.2.0
jest 20.0.4

Releases

-   2.0.0-beta.2 zip tar
-   2.0.0-beta.1 zip tar
-   2.0.0-beta.0 zip tar
-   1.3.9 zip tar
-   1.3.8 zip tar
-   1.3.7 zip tar
-   1.3.6 zip tar
-   1.3.5 zip tar
-   1.3.4 zip tar
-   1.3.3 zip tar
-   1.3.2 zip tar
-   1.3.1 zip tar
-   1.3.0 zip tar
-   1.2.1 zip tar
-   1.2.0 zip tar
-   1.1.0 zip tar
-   1.0.2 zip tar
-   1.0.1 zip tar
-   1.0.0 zip tar
-   0.13.1 zip tar
-   0.12.1 zip tar
-   0.11.1 zip tar
-   0.10.5 zip tar
-   0.10.4 zip tar
-   0.10.3 zip tar
-   0.10.2 zip tar
-   0.10.1 zip tar
-   0.9.4 zip tar
-   0.9.3 zip tar
-   0.9.2 zip tar