uglify-save-license
A support module for UglifyJS to detect and preserve license comments
// Backbone.js 1.1.2 // (c) 2010-2014 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors// Backbone may be freely distributed under the MIT license.// For all details and documentation:// http://backbonejs.org { // Set up Backbone appropriately for the environment. Start with AMD. if typeof define === 'function' && defineamd
↓
// Backbone.js 1.1.2// (c) 2010-2014 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors// Backbone may be freely distributed under the MIT license.// For all details and documentation:// http://backbonejs.org!{if"function"==typeof define&&defineamd;else if"undefined"!=typeof exports...
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.
Installation
Install with npm. (Make sure you have installed Node.)
npm install --save-dev uglify-save-license
Usage
First of all, load uglify-save-license
module.
var saveLicense = ;
UglifyJS
Use withPass this module to the comments
option.
var result = UglifyJS;
grunt-contrib-uglify
Use withPass this module to the preserveComments
option.
grunt;
How it works
uglify-save-license checks each comment token of a 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 regexp for license statement matches the string of the comment. It matches, for example,
MIT
andCopyright
. - There is a comment at the previous line, and it matches 1. 2. or 3.
Examples
CLI tool example
uglify-example.js
)
Main script (#!/usr/bin/env node var UglifyJS = saveLicense = ; var minified = UglifyJScode; console;
Target file
// First line // (c) 2014 John <- contains '(c)'// The previous line is preserved // This line won't be preserved. { var str = 'Hello World! :' + doctitle; // This line will not, too. console;}window document;
Command
node uglify-example.js <target filename>
Output
// First line// (c) 2014 John <- contains '(c)'// The previous line is preserved!{var n="Hello World! :"+ltitle;console}windowdocument;
Gruntfile.coffee example
= gruntloadNpmTasks 'grunt-contrib-uglify' gruntloadNpmTasks 'grunt-contrib-concat' gruntloadNpmTasks 'grunt-contrib-clean' gruntinitConfig 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' gruntregisterTask 'default' 'uglify''concat''clean'
Acknowledgements
uglify-save-license is inspired by grunt-license-saver and I used it as reference. Thanks, kyo-ago.
License
Copyright (c) 2013 - 2014 Shinnosuke Watanabe
Licensed under the MIT license.