breezewish / express-minify

Automatically minify and cache your javascript and css files.
https://npmjs.org/package/express-minify
MIT License
86 stars 18 forks source link
express minify uglifyjs

express-minify

Automatically minify (and cache) your JavaScript, CSS and JSON responses without pain. It also supports LESS/SASS/Stylus/CoffeeScript compiling and minifying.

Build Status npm version npm download counter

Dependency Status devDependency Status

NodeICO

Installation

npm install express-minify

Basic Usage

express-minify takes care of all responses. You don't even need to pass a source directory as other minifying middlewares.

var minify = require('express-minify');
app.use(minify());

It's very easy and elegant to integrate express-minify with express.static and compression:

app.use(compression());
app.use(minify());
app.use(express.static(__dirname + '/static'));

Note that the order of the middlewares is important. In the example above, we want to: serve static files → for JS & CSS: minify → GZip → send to user, so we have such orders.

Options

Default:

app.use(minify({
  cache: false,
  uglifyJsModule: null,
  errorHandler: null,
  jsMatch: /javascript/,
  cssMatch: /css/,
  jsonMatch: /json/,
  sassMatch: /scss/,
  lessMatch: /less/,
  stylusMatch: /stylus/,
  coffeeScriptMatch: /coffeescript/,
}));

Per-response Options

Options below can be supplied for specific response:

Examples

Enable File Cache

By default, express-minify uses memory cache. You can change to file cache:

app.use(minify({cache: __dirname + '/cache'}));

Compile and Minify CoffeeScript/LESS/SASS/Stylus

express-minify can automatically compile your files and minify it without the need of specifying a source file directory. Currently it supports CoffeeScript, SASS, LESS and Stylus.

To enable this feature, first of all you need to install those modules by yourself:

# You needn't install all of these. Only choose what you need.
npm install coffee-script less node-sass stylus --save

Then you need to define MIME for those files:

// visit http://localhost/test.coffee

express.static.mime.define(
{
  'text/coffeescript':  ['coffee'],
  'text/less':          ['less'],
  'text/x-scss':        ['scss'],
  'text/stylus':        ['styl']
});

app.use(minify());

Customize Error Behavior

Errors may thrown at the compiling stage (for CoffeeScript/LESS/SASS/Stylus) or at the minifying stage (for JSON/UglifyJS/CleanCSS). The default behavior is returning the error message for compiling errors and returning original content for minifying errors.

You can customize this behavior or get notified about the error by providing errorHandler in options:

var minify = require('express-minify');

var myErrorHandler = function (errorInfo, callback) {
  console.log(errorInfo);
  // below is the default implementation (minify.Minifier.defaultErrorHandler)
  if (errorInfo.stage === 'compile') {
    callback(errorInfo.error, JSON.stringify(errorInfo.error));
    return;
  }
  callback(errorInfo.error, errorInfo.body);
};

app.use(minify({ errorHandler: myErrorHandler }));

The structure of errorInfo is:

Use Uglify-ES

You can pass the uglify-es module in options to replace the built-in UglifyJS 3 module.

var uglifyEs = require('uglify-es');
app.use(minify({
  uglifyJsModule: uglifyEs,
}));

Remember to invalidate file caches after switching a UglifyJS module. They won't be invalidated automatically.

Specify UglifyJS Options

Example 1: Disable Mangling for AngularJS Source Files

app.use(function(req, res, next)
{
  // do not mangle -angular.js files
  if (/-angular\.js$/.test(req.url)) {
    res.minifyOptions = res.minifyOptions || {};
    res.minifyOptions.js = { mangle: true };
  }
  next();
});
app.use(minify());

Example 2: Preserve Comments for Specific JavaScript Files

app.use(function(req, res, next)
{
  if (/\.(user|meta)\.js$/.test(req.url)) {
    res.minifyOptions = res.minifyOptions || {};
    res.minifyOptions.js = { output: { comments: true } };
  }
  next();
});

Dynamic Response

express-minify is able to handle all kind of responses, including dynamic responses.

var responseJS =
  "(function(window, undefined)\n" +
  "{\n" +
  "\n" +
  "    var hello = 'hello';\n" +
  "\n" +
  "    var world = 'world';\n" +
  "\n" +
  "    alert(hello + world);\n" +
  "\n" +
  "})(window);"
app.use(minify());
app.get('/response.js', function(req, res)
{
  res.setHeader('Content-Type', 'application/javascript');
  res.end(responseJS);
});

Disable Minifying or Caching for Specific Response

If you don't want to minify a specific response, just set response.minifyOptions.minify = false.

If you want to minify a response but don't want to cache it (for example, dynamic response data), set response.minifyOptions.cache = false.

Example 1. Disable Minification for Minified Assets

app.use(function(req, res, next)
{
  if (/\.min\.(css|js)$/.test(req.url)) {
    res.minifyOptions = res.minifyOptions || {};
    res.minifyOptions.minify = false;
  }
  next();
});
app.use(minify());

Example 2. Disable Caching for Dynamic Content

app.use(minify());
app.get('/server_time_min.jsonp', function(req, res)
{
  var obj = {
    'ok': true,
    'data': {
      'timestamp': new Date().getTime(),
    },
  };

  // minify this response, but do not cache it
  res.minifyOptions = res.minifyOptions || {};
  res.minifyOptions.cache = false;
  res.setHeader('Content-Type', 'application/javascript');
  res.send("callback(" + JSON.stringify(obj, null, 4) + ");");
});

app.get('/server_time.jsonp', function(req, res)
{
  var obj = {
    'ok': true,
    'data': {
      'timestamp': new Date().getTime(),
    },
  };

  // do not minify (and do not cache) this response
  res.minifyOptions = res.minifyOptions || {};
  res.minifyOptions.minify = false;
  res.setHeader('Content-Type', 'application/javascript');
  res.send("callback(" + JSON.stringify(obj, null, 4) + ");");
});

Change Log

1.0.0

0.2.0

0.1.7

0.1.6

0.1.5

0.1.4

0.1.3

0.1.2

0.1.1

0.1.0

0.0.11

0.0.10

0.0.9

0.0.8

0.0.7

0.0.6

0.0.5

0.0.4

0.0.3

0.0.2

License

The MIT License (MIT)

Copyright (c) 2017 Breezewish

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.