uglify-save-license
Tiny license detector for UglifyJS
Overview
This module enables us to preserve license comments when using UglifyJS.
Even if the license statement is in multiple line comments, or the comment has no directive such as @license
and /*!
, this module keeps them readable.
uglify-save-license is inspired by grunt-license-saver and I used it as reference.
Thanks, kyo-ago.
Installation
npm install uglify-save-license
Make sure you have installed Node before running this command.
Usage
First of all, load the uglify-save-license
module.
var saveLicense = require('uglify-save-license');
Pass this module to the comments
option.
var result = UglifyJS.minify('file1.js', {
output: {
comments: saveLicense
}
});
Pass this module to the preserveComments
option.
grunt.initConfig({
uglify: {
my_target: {
options: {
preserveComments: saveLicense
},
src: ['src/app.js'],
dest: 'dest/app.min.js'
}
}
});
How does it works
uglify-save-license checks each comment node of JavaScript file.
The comment will be regarded as a license statement and preserved after compression, if it meets at least one of the following requirements:
- The comment is in the first line of a file.
- The regular expression for license statements matches the comment string. The regexp matches, for example,
MIT
and Copyright
. (See the source code for details.) - There is a comment at the previous line, and it matches 1. 2. or 3.
Examples
CLI tool example
Main script (uglify-example.js
)
#!/usr/bin/env node
var UglifyJS = require('uglify-js'),
saveLicense = require('uglify-save-license');
var minified = UglifyJS.minify(process.argv[2], {
output: {
comments: saveLicense
}
}).code;
console.log(minified);
Target file
(function(win, doc) {
var str = 'Hello World! :' + doc.title;
console.log(str);
}(window, document));
Command
node uglify-example.js <target filename>
Output
!function(o,l){var n="Hello World! :"+l.title;console.log(n)}(window,document);
module.exports = (grunt) ->
grunt.loadNpmTasks 'grunt-contrib-uglify'
grunt.loadNpmTasks 'grunt-contrib-concat'
grunt.loadNpmTasks 'grunt-contrib-clean'
grunt.initConfig
uglify:
target:
options:
preserveComments: require 'uglify-save-license'
files: [
expand: true
flatten: true
cwd: 'path/to/src'
src: ["**/*.js"]
dest: 'tmp/'
]
concat:
js:
src: ['tmp/*.js']
dest: 'path/to/build/app.js'
clean:
tmpdir: ['tmp']
grunt.registerTask 'default' ['uglify', 'concat', 'clean']
License
Copyright (c) 2013 - 2014 Shinnosuke Watanabe All rights reserved.
Licensed under the MIT license.