-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[TIMOB-12127] Upgraded Uglify from v1 to v2. Added uglify error handling... #3741
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ var ti = require('titanium-sdk'), | |
afs = appc.fs, | ||
xml = appc.xml, | ||
parallel = appc.async.parallel, | ||
uglify = require('uglify-js'), | ||
UglifyJS = require('uglify-js'), | ||
fs = require('fs'), | ||
path = require('path'), | ||
wrench = require('wrench'), | ||
|
@@ -39,6 +39,9 @@ var ti = require('titanium-sdk'), | |
'.jpeg': 'image/jpg' | ||
}; | ||
|
||
// silence uglify's default warn mechanism | ||
UglifyJS.AST_Node.warn_function = function () {}; | ||
|
||
exports.config = function (logger, config, cli) { | ||
return { | ||
options: { | ||
|
@@ -524,12 +527,37 @@ build.prototype = { | |
|
||
if (/^url\:/.test(moduleName)) { | ||
if (this.minifyJS) { | ||
var pro = uglify.uglify, | ||
source = file + '.uncompressed.js'; | ||
|
||
var source = file + '.uncompressed.js'; | ||
fs.renameSync(file, source); | ||
this.logger.debug(__('Minifying include %s', file)); | ||
fs.writeFileSync(file, pro.gen_code(pro.ast_squeeze(pro.ast_mangle(uglify.parser.parse(fs.readFileSync(source).toString()))))); | ||
try { | ||
fs.writeFileSync(file, UglifyJS.minify(source).code); | ||
} catch (ex) { | ||
this.logger.error(__('Failed to minify %s', source)); | ||
if (ex.line) { | ||
this.logger.error(__('%s [line %s, column %s]', ex.message, ex.line, ex.col)); | ||
} else { | ||
this.logger.error(__('%s', ex.message)); | ||
} | ||
try { | ||
var contents = fs.readFileSync(source).toString().split('\n'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can move this read inside the if statement, that way we don't have to read the file if we don't have a line. Also, what about windows newlines? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Won't fix. Contents is needed to check if the line # is valid. |
||
if (ex.line && ex.line <= contents.length) { | ||
this.logger.error(''); | ||
this.logger.error(' ' + contents[ex.line-1]); | ||
if (ex.col) { | ||
var i = 0, | ||
len = ex.col; | ||
buffer = ' '; | ||
for (; i < len; i++) { | ||
buffer += '-'; | ||
} | ||
this.logger.error(buffer + '^'); | ||
} | ||
this.logger.log(); | ||
} | ||
} catch (ex2) {} | ||
process.exit(1); | ||
} | ||
} | ||
tiJS.push('"' + moduleName + '":"' + fs.readFileSync(file).toString().trim().replace(/\\/g, '\\\\').replace(/\n/g, '\\n\\\n').replace(/"/g, '\\"') + '"'); | ||
} else if (isCommonJS) { | ||
|
@@ -589,8 +617,7 @@ build.prototype = { | |
minifyJavaScript: function () { | ||
if (this.minifyJS) { | ||
this.logger.info(__('Minifying JavaScript')); | ||
var pro = uglify.uglify, | ||
self = this; | ||
var self = this; | ||
(function walk(dir) { | ||
fs.readdirSync(dir).sort().forEach(function (dest) { | ||
var stat = fs.statSync(dir + '/' + dest); | ||
|
@@ -601,10 +628,37 @@ build.prototype = { | |
var source = dest + '.uncompressed.js'; | ||
fs.renameSync(dest, source); | ||
self.logger.debug(__('Minifying include %s', dest)); | ||
fs.writeFileSync(dest, pro.gen_code(pro.ast_squeeze(pro.ast_mangle(uglify.parser.parse(fs.readFileSync(source).toString()))))); | ||
try { | ||
fs.writeFileSync(dest, UglifyJS.minify(source).code); | ||
} catch (ex) { | ||
self.logger.error(__('Failed to minify %s', dest)); | ||
if (ex.line) { | ||
self.logger.error(__('%s [line %s, column %s]', ex.message, ex.line, ex.col)); | ||
} else { | ||
self.logger.error(__('%s', ex.message)); | ||
} | ||
try { | ||
var contents = fs.readFileSync(source).toString().split('\n'); | ||
if (ex.line && ex.line <= contents.length) { | ||
self.logger.error(''); | ||
self.logger.error(' ' + contents[ex.line-1]); | ||
if (ex.col) { | ||
var i = 0, | ||
len = ex.col; | ||
buffer = ' '; | ||
for (; i < len; i++) { | ||
buffer += '-'; | ||
} | ||
self.logger.error(buffer + '^'); | ||
} | ||
self.logger.log(); | ||
} | ||
} catch (ex2) {} | ||
process.exit(1); | ||
} | ||
} | ||
}); | ||
}(this.buildDir)) | ||
}(this.buildDir)); | ||
} | ||
}, | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this just finding variable/function identifiers? If so, you can accomplish it a lot easier using figure_out_scope. http://lisperator.net/uglifyjs/scope
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I'll try playing around with the scope stuff tomorrow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed!