Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added fix from @davglass for uglify not preserving license comments

  • Loading branch information...
commit e66a1dabf202c53416f257eaf5a1ac4d282d359b 1 parent e2f8820
@twobit authored
View
44 build/gear-lib.js
@@ -24208,19 +24208,38 @@ var parser = require('uglify-js').parser,
/**
* Minify JS.
*
+ * Issue: we need to preserve license comments which is still an open issue in uglify-js:
+ * https://github.com/mishoo/UglifyJS/pull/332
+ *
+ * Fix based on the Ender workaround here:
+ * https://github.com/ender-js/Ender/blob/76961673be2a29e893d8d3dc9b97e3faf8b169a6/lib/ender.file.js#L25-58
+ * Ender is licensed under MIT - copyright 2012 Dustin Diaz & Jacob Thornton
+ * http://ender.no.de/
+ *
+ *
* @param options {Object} Task options.
* @param options.config {Object} Minify options.
* @param blob {Object} Incoming blob.
* @param done {Function} Callback on task completion.
*/
-exports.jsminify = function(options, blob, done) {
+exports.jsminify = function (options, blob, done) {
options = options || {};
var config = options.config || {},
+ comments = [],
+ token = '"jsminify task: preserved comment block"',
+ reMultiComments = /\/\*![\s\S]*?\*\//g,
+ reTokens = new RegExp(token, 'g'),
+ source = blob.result,
ast;
try {
- ast = parser.parse(blob.result, config.semicolon || false);
+ source = source.replace(reMultiComments, function (comment) {
+ comments.push(comment);
+ return ';' + token + ';';
+ });
+
+ ast = parser.parse(source, config.semicolon || false);
if (config.mangle) {
ast = uglify.ast_mangle(ast, config);
@@ -24229,11 +24248,26 @@ exports.jsminify = function(options, blob, done) {
ast = uglify.ast_squeeze(ast, config);
}
- done(null, new blob.constructor(uglify.gen_code(ast, config), blob));
+ source = uglify.gen_code(ast, config);
+
+ source = source.replace(reTokens, function () {
+ return '\n' + comments.shift() + '\n';
+ });
+
+ if (source.substr(source.length - 1) === ')') {
+ source += ';';
+ }
+ source += '\n';
} catch (e) {
- this._log(e);
- done('Minify failed, ' + (blob.name || 'file') + ' unparseable');
+ if (options.callback) {
+ options.callback(e);
+ }
+
+ done('Minify failed, ' + (blob.name || 'file') + ' unparseable.\nException:\n' + JSON.stringify(e));
+ return;
}
+
+ done(null, new blob.constructor(source, blob));
};
View
52 build/gear-lib.min.js
51 additions, 1 deletion not shown
View
44 lib/jsminify.js
@@ -9,19 +9,38 @@ var parser = require('uglify-js').parser,
/**
* Minify JS.
*
+ * Issue: we need to preserve license comments which is still an open issue in uglify-js:
+ * https://github.com/mishoo/UglifyJS/pull/332
+ *
+ * Fix based on the Ender workaround here:
+ * https://github.com/ender-js/Ender/blob/76961673be2a29e893d8d3dc9b97e3faf8b169a6/lib/ender.file.js#L25-58
+ * Ender is licensed under MIT - copyright 2012 Dustin Diaz & Jacob Thornton
+ * http://ender.no.de/
+ *
+ *
* @param options {Object} Task options.
* @param options.config {Object} Minify options.
* @param blob {Object} Incoming blob.
* @param done {Function} Callback on task completion.
*/
-exports.jsminify = function(options, blob, done) {
+exports.jsminify = function (options, blob, done) {
options = options || {};
var config = options.config || {},
+ comments = [],
+ token = '"jsminify task: preserved comment block"',
+ reMultiComments = /\/\*![\s\S]*?\*\//g,
+ reTokens = new RegExp(token, 'g'),
+ source = blob.result,
ast;
try {
- ast = parser.parse(blob.result, config.semicolon || false);
+ source = source.replace(reMultiComments, function (comment) {
+ comments.push(comment);
+ return ';' + token + ';';
+ });
+
+ ast = parser.parse(source, config.semicolon || false);
if (config.mangle) {
ast = uglify.ast_mangle(ast, config);
@@ -30,9 +49,24 @@ exports.jsminify = function(options, blob, done) {
ast = uglify.ast_squeeze(ast, config);
}
- done(null, new blob.constructor(uglify.gen_code(ast, config), blob));
+ source = uglify.gen_code(ast, config);
+
+ source = source.replace(reTokens, function () {
+ return '\n' + comments.shift() + '\n';
+ });
+
+ if (source.substr(source.length - 1) === ')') {
+ source += ';';
+ }
+ source += '\n';
} catch (e) {
- this._log(e);
- done('Minify failed, ' + (blob.name || 'file') + ' unparseable');
+ if (options.callback) {
+ options.callback(e);
+ }
+
+ done('Minify failed, ' + (blob.name || 'file') + ' unparseable.\nException:\n' + JSON.stringify(e));
+ return;
}
+
+ done(null, new blob.constructor(source, blob));
};
View
2  package.json
@@ -1,6 +1,6 @@
{
"name": "gear-lib",
- "version": "0.8.3",
+ "version": "0.8.4",
"description": "Collection of common Gear.js tasks",
"author": "Stephen Murphy <stephen@hypernaut.com>",
"keywords": ["gear", "task", "build"],
View
20 test/jsminify.js
@@ -2,7 +2,11 @@ var Blob = require('gear').Blob,
jsminify = require('../lib/jsminify').jsminify,
fixtures = {
js: new Blob('function test( x ) {console.log(x);;;;}'),
- min: 'function test(x){console.log(x);}'
+ min: 'function test(x){console.log(x);}\n',
+ license: new Blob('/*!\n* Copyright (C) 2012\n*/\nfunction foo() {}'),
+ license_min: '\n/*!\n* Copyright (C) 2012\n*/\n;function foo(){}\n',
+ comment: new Blob('/*\n* Copyright (C) 2012\n*/\nfunction foo() {}'),
+ comment_min: 'function foo(){}\n'
};
describe('jsminify()', function() {
@@ -12,4 +16,18 @@ describe('jsminify()', function() {
done(err);
});
});
+
+ it('should preserve licenses', function(done) {
+ jsminify({}, fixtures.license, function(err, res) {
+ res.result.should.equal(fixtures.license_min);
+ done(err);
+ });
+ });
+
+ it('should strip comments', function(done) {
+ jsminify({}, fixtures.comment, function(err, res) {
+ res.result.should.equal(fixtures.comment_min);
+ done(err);
+ });
+ });
});
Please sign in to comment.
Something went wrong with that request. Please try again.