From b684160ffa873e7837079f0ff0c1622b0d1b21bc Mon Sep 17 00:00:00 2001 From: Lei Kang Date: Mon, 19 Aug 2013 14:58:32 +1200 Subject: [PATCH 1/2] Uses minimal "switch" statement instead of simple string as the token that replaces all the copyright comments. --- lib/jsminify.js | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/lib/jsminify.js b/lib/jsminify.js index 5560b0e..c68ad2b 100644 --- a/lib/jsminify.js +++ b/lib/jsminify.js @@ -29,15 +29,9 @@ exports.jsminify = function (code, config, callback) { } config = config || exports.config; var comments = [], - token = '"yUglify: preserved comment block"', + token = 'switch("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'), + reTokens = new RegExp(token.replace(/[(){}]/g, '\\$&'), 'g'), ast; try { @@ -63,12 +57,6 @@ exports.jsminify = function (code, config, callback) { code = uglify.split_lines(code, config.max_line_length); } - //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'; }); From bc7bef9caa5f635c273b6200f7bea80ae2b89c1b Mon Sep 17 00:00:00 2001 From: Lei Kang Date: Thu, 27 Feb 2014 10:32:33 +1300 Subject: [PATCH 2/2] Fixed the issue that if there are any global function ( function foo() { ... } ) declarations in the raw file, in the output file they will be pulled onto the top and potentially before the preserved comment block. --- lib/jsminify.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/jsminify.js b/lib/jsminify.js index c68ad2b..009e4c3 100644 --- a/lib/jsminify.js +++ b/lib/jsminify.js @@ -29,9 +29,9 @@ exports.jsminify = function (code, config, callback) { } config = config || exports.config; var comments = [], - token = 'switch("yUglify: preserved comment block"){}', + token = 'function fn(){"yUglify: preserved comment block";}', reMultiComments = /\/\*![\s\S]*?\*\//g, - reTokens = new RegExp(token.replace(/[(){}]/g, '\\$&'), 'g'), + reTokens = new RegExp('function \\w+\\(\\)\\{\\"yUglify: preserved comment block\\";\\}', 'g'), ast; try {