From e54748365cba0509c82c089cdc2ef6a8bb1a724b Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Mon, 8 May 2017 02:11:45 +0800 Subject: [PATCH] support `minify()` output as AST (#1878) - `options.output.ast` (default `false`) - `options.output.code` (default `true`) --- bin/uglifyjs | 23 ++++++++++++++++++---- lib/minify.js | 54 ++++++++++++++++++++++++++++----------------------- 2 files changed, 49 insertions(+), 28 deletions(-) diff --git a/bin/uglifyjs b/bin/uglifyjs index 57e33d9b..c4f9dc5d 100755 --- a/bin/uglifyjs +++ b/bin/uglifyjs @@ -212,7 +212,14 @@ function run() { fatal("ERROR: " + ex.message); } if (program.output == "spidermonkey") { - console.log(JSON.stringify(UglifyJS.parse(result.code).to_mozilla_ast(), null, 2)); + console.log(JSON.stringify(UglifyJS.minify(result.code, { + compress: false, + mangle: false, + output: { + ast: true, + code: false + } + }).ast.to_mozilla_ast(), null, 2)); } else if (program.output) { fs.writeFileSync(program.output, result.code); if (result.map) { @@ -278,9 +285,17 @@ function parse_js(flag, constants) { return function(value, options) { options = options || {}; try { - UglifyJS.parse(value, { - expression: true - }).walk(new UglifyJS.TreeWalker(function(node) { + UglifyJS.minify(value, { + parse: { + expression: true + }, + compress: false, + mangle: false, + output: { + ast: true, + code: false + } + }).ast.walk(new UglifyJS.TreeWalker(function(node) { if (node instanceof UglifyJS.AST_Assign) { var name = node.left.print_to_string(); var value = node.right; diff --git a/lib/minify.js b/lib/minify.js index fe762db6..27cc9118 100644 --- a/lib/minify.js +++ b/lib/minify.js @@ -108,32 +108,38 @@ function minify(files, options) { toplevel = mangle_properties(toplevel, options.mangle.properties); } } - if (options.sourceMap) { - if (typeof options.sourceMap.content == "string") { - options.sourceMap.content = JSON.parse(options.sourceMap.content); - } - options.output.source_map = SourceMap({ - file: options.sourceMap.filename, - orig: options.sourceMap.content, - root: options.sourceMap.root - }); - if (options.sourceMap.includeSources) { - for (var name in files) { - options.output.source_map.get().setSourceContent(name, files[name]); + var result = {}; + if (options.output.ast) { + result.ast = toplevel; + } + if (!HOP(options.output, "code") || options.output.code) { + if (options.sourceMap) { + if (typeof options.sourceMap.content == "string") { + options.sourceMap.content = JSON.parse(options.sourceMap.content); + } + options.output.source_map = SourceMap({ + file: options.sourceMap.filename, + orig: options.sourceMap.content, + root: options.sourceMap.root + }); + if (options.sourceMap.includeSources) { + for (var name in files) { + options.output.source_map.get().setSourceContent(name, files[name]); + } } } - } - var stream = OutputStream(options.output); - toplevel.print(stream); - var result = { - code: stream.get() - }; - if (options.sourceMap) { - result.map = options.output.source_map.toString(); - if (options.sourceMap.url == "inline") { - result.code += "\n//# sourceMappingURL=data:application/json;charset=utf-8;base64," + to_base64(result.map); - } else if (options.sourceMap.url) { - result.code += "\n//# sourceMappingURL=" + options.sourceMap.url; + delete options.output.ast; + delete options.output.code; + var stream = OutputStream(options.output); + toplevel.print(stream); + result.code = stream.get(); + if (options.sourceMap) { + result.map = options.output.source_map.toString(); + if (options.sourceMap.url == "inline") { + result.code += "\n//# sourceMappingURL=data:application/json;charset=utf-8;base64," + to_base64(result.map); + } else if (options.sourceMap.url) { + result.code += "\n//# sourceMappingURL=" + options.sourceMap.url; + } } } if (warnings.length) { -- 2.34.1