Skip to content

Commit

Permalink
Merge pull request yui#22 from louisbuchbinder/upgrade-uglify
Browse files Browse the repository at this point in the history
upgrade uglify-js
  • Loading branch information
davglass authored Jul 14, 2017
2 parents 4e30ee4 + 906a776 commit 376ba09
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ CVS/
*~
.com.apple.timemachine.supported
tests/assets/yql/build/*/*.js
yuglify-*.tgz
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v0.10
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
language: node_js
node_js:
- "0.8"
- "0.10"
12 changes: 5 additions & 7 deletions bin/yuglify
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ if (parsed.terminal) {
compfn(data, function(err, smashed) {
if (err) {
throw(err);
process.exit(1);
}
if (parsed.output) {
fs.writeFileSync(parsed.output, smashed, 'utf8');
Expand All @@ -92,10 +91,10 @@ if (parsed.terminal) {
files = parsed.argv.remain;

// Uglify is blocking, so we will do this in sync.
files.forEach(function(file, index) {
files.forEach(function(file) {

file = path.resolve(file);

if (exists(file)) {

var data = fs.readFileSync(file, 'utf8'),
Expand All @@ -109,9 +108,8 @@ if (parsed.terminal) {

if (err) {
throw(err);
process.exit(1);
}

if(parsed.combine) {

// Fill the buffer content by its type.
Expand All @@ -130,7 +128,7 @@ if (parsed.terminal) {
}
console.log(log);
}

});
} else {
console.log('Failed to find:', file);
Expand Down
28 changes: 8 additions & 20 deletions lib/jsminify.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
* Ender is licensed under MIT - copyright 2012 Dustin Diaz & Jacob Thornton
* http://ender.no.de/
*/
var parser = require('uglify-js').parser,
uglify = require('uglify-js').uglify;

var UglifyJS = require('uglify-js');

exports.config = {
mangle: true,
Expand All @@ -29,39 +29,27 @@ exports.jsminify = function (code, config, callback) {
}
config = config || exports.config;
var comments = [],
token = '"yUglify: preserved comment block"',
// 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'),
ast;
reTokens = new RegExp(token, 'g');

try {
code = code.replace(reMultiComments, function (comment) {
comments.push(comment);
return ';' + token + ';';
});

config.ascii_only = true; //Force ascii
ast = parser.parse(code, config.semicolon || false);

if (config.mangle) {
ast = uglify.ast_mangle(ast, config);
}
if (config.squeeze) {
ast = uglify.ast_squeeze(ast, config);
}

code = uglify.gen_code(ast, config);
config.ascii_only = true; // Force ascii
config.fromString = true; // Force from string

//Limit the number of characters on a single line
if (!config.beautify && config.max_line_length) {
code = uglify.split_lines(code, config.max_line_length);
}
code = UglifyJS.minify(code, config).code;

//First pass with comma (comment inside code somewhere)
code = code.replace(reTokens1, function () {
Expand Down
1 change: 1 addition & 0 deletions mocha-tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The mocha-tests directory contains tests that will validate refactoring steps.
2 changes: 2 additions & 0 deletions mocha-tests/lib/jsminify/badSyntax.js.ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var a = function () {
return true
3 changes: 3 additions & 0 deletions mocha-tests/lib/jsminify/goodSyntax.js.ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var a = function () {
return true;
};
7 changes: 7 additions & 0 deletions mocha-tests/lib/jsminify/goodSyntaxWithLicense.js.ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*! This is a License Comment
* It should persist through the uglification
*/

var a = function () {
return true;
};
58 changes: 58 additions & 0 deletions mocha-tests/lib/jsminify/jsminify.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

var assert = require('assert');
var fs = require('fs');
var path = require('path');
var module = require('../../../lib/jsminify.js');
var config = module.config;
var jsminify = module.jsminify;
var good = String(fs.readFileSync(path.join(__dirname, 'goodSyntax.js.ignore')));
var goodWithLicense = String(fs.readFileSync(path.join(__dirname, 'goodSyntaxWithLicense.js.ignore')));
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]);
});
});
it('should have a jsminify function', function () {
assert.strictEqual(typeof jsminify, 'function');
});
it('should minify js code', function (done) {
jsminify(good, null, function (err, code) {
if (err) {
return done(err);
}
assert.strictEqual(code, 'var a=function(){return!0};\n');
done();
});
});
it('should preserve license comments in js code', function (done) {
jsminify(goodWithLicense, null, function (err, code) {
if (err) {
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');
done();
});
});
it('should error on bad syntax', function (done) {
jsminify(bad, null, function (err, code) {
assert(err, 'Expected an error');
done();
});
});
});
17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
{
"name": "yuglify",
"description": "cli wrapper for uglify and cssmin used by YUI",
"version": "0.1.4",
"version": "1.0.0",
"dependencies": {
"uglify-js": "~1.3.4",
"ycssmin": "~1.0.1",
"nopt": "~2.1.1"
"mocha": "^3.4.2",
"nopt": "~2.1.1",
"uglify-js": "^2.8.29",
"ycssmin": "~1.0.1"
},
"devDependencies": {
"yui-lint": "~0.1.1",
"jshint": "~0.9.0",
"vows": "*"
"jshint": "^2.9.5",
"vows": "*",
"yui-lint": "^0.2.0"
},
"main": "./lib/index.js",
"bin": {
"yuglify": "./bin/yuglify"
},
"scripts": {
"pretest": "jshint --config ./node_modules/yui-lint/jshint.json ./lib/*.js ./bin/yuglify",
"test": "vows --spec ./tests/*.js"
"test": "vows --spec ./tests/*.js && mocha mocha-tests --recursive"
},
"preferGlobal": "true",
"bugs": {
Expand Down

0 comments on commit 376ba09

Please sign in to comment.