From 32e2921013525867d8b61eb100409f4de0e0ad41 Mon Sep 17 00:00:00 2001 From: Nathan Vander Wilt Date: Tue, 17 Oct 2017 10:43:25 -0700 Subject: [PATCH] migrate to UglifyJS v3 and use its comment preservation feature instead of a custom regex one. this should fix a number of open issues (probably #19 and #20; not sure about #21) --- lib/jsminify.js | 75 +++++------------------ mocha-tests/lib/jsminify/jsminify.test.js | 20 ++---- package.json | 2 +- 3 files changed, 21 insertions(+), 76 deletions(-) diff --git a/lib/jsminify.js b/lib/jsminify.js index ca2052a..5cac219 100644 --- a/lib/jsminify.js +++ b/lib/jsminify.js @@ -3,76 +3,33 @@ * Copyrights licensed under the New BSD License. * See the accompanying LICENSE file for terms. */ -/* - * The comment/license workaround is 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/ -*/ var UglifyJS = require('uglify-js'); exports.config = { mangle: true, - squeeze: true, - semicolon: false, - lift_vars: true, - mangle_toplevel: true, - no_mangle_functions: true, - max_line_length: 6000 + output: { + semicolons: false, + max_line_len: 6000, + comments: /^!/, + }, + compress: { + hoist_vars: true, + }, }; exports.jsminify = function (code, config, callback) { if (typeof config === 'function') { callback = config; - config = exports.config; + config = null; } config = config || exports.config; - var comments = [], - // trick UglifyJS.minify to preserve the token by assigning it to a variable - token = 'a="yUglify: preserved comment block"', - reMultiComments = /\/\*![\s\S]*?\*\//g, - /* - In some cases Uglify adds a comma, in others it doesn't - So we have to process the tokens twice, first with the comma - then without it to catch both cases and to be clear about it. - */ - reTokens1 = new RegExp(token + ',', 'g'), - reTokens = new RegExp(token, 'g'); - - try { - code = code.replace(reMultiComments, function (comment) { - comments.push(comment); - return ';' + token + ';'; - }); - - config.ascii_only = true; // Force ascii - config.fromString = true; // Force from string - - code = UglifyJS.minify(code, config).code; - - //First pass with comma (comment inside code somewhere) - code = code.replace(reTokens1, function () { - return '\n' + comments.shift() + '\n'; - }); - - //Second pass without the comma to catch normal comments - code = code.replace(reTokens, function () { - return '\n' + comments.shift() + '\n'; - }); - - if ((code.substr(code.length - 1) === ')') || - (code.substr(code.length - 1) === '}')) { - code += ';'; - } - - //Trim spaces at the beginning of the code - code = code.replace(/^\s+/, ''); - - code += '\n'; - - callback(null, code); - } catch (e) { - callback(e); + + var result = UglifyJS.minify(code, config); + if (result.error) { + callback(result.error); + } + else { + callback(null, result.code); } }; diff --git a/mocha-tests/lib/jsminify/jsminify.test.js b/mocha-tests/lib/jsminify/jsminify.test.js index 6fcc123..484610b 100644 --- a/mocha-tests/lib/jsminify/jsminify.test.js +++ b/mocha-tests/lib/jsminify/jsminify.test.js @@ -11,21 +11,9 @@ var bad = String(fs.readFileSync(path.join(__dirname, 'badSyntax.js.ignore'))); describe('JSminify Unit Tests', function () { it('should have a default config object', function () { - var expected = { - mangle: true, - squeeze: true, - semicolon: false, - lift_vars: true, - mangle_toplevel: true, - no_mangle_functions: true, - max_line_length: 6000 - }; - assert.strictEqual(typeof config, 'object'); - - Object.keys(expected).forEach(function (key) { - assert.strictEqual(config[key], expected[key]); - }); + assert.strictEqual(typeof config.output, 'object'); + assert.strictEqual(typeof config.compress, 'object'); }); it('should have a jsminify function', function () { assert.strictEqual(typeof jsminify, 'function'); @@ -35,7 +23,7 @@ describe('JSminify Unit Tests', function () { if (err) { return done(err); } - assert.strictEqual(code, 'var a=function(){return!0};\n'); + assert.strictEqual(code, 'var a=function(){return!0}\n'); done(); }); }); @@ -45,7 +33,7 @@ describe('JSminify Unit Tests', function () { return done(err); } - assert.strictEqual(code, '/*! This is a License Comment\n\t* It should persist through the uglification\n*/\n;var a=function(){return!0};\n'); + assert.strictEqual(code, '/*! This is a License Comment\n\t* It should persist through the uglification\n*/\nvar a=function(){return!0}\n'); done(); }); }); diff --git a/package.json b/package.json index 0cf9a6e..cf16399 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "version": "1.0.1", "dependencies": { "nopt": "~2.1.1", - "uglify-js": "^2.8.29", + "uglify-js": "^3.1.4", "ycssmin": "~1.0.1" }, "devDependencies": {