support minify() output as AST (#1878)
- `options.output.ast` (default `false`) - `options.output.code` (default `true`)
This commit is contained in:
21
bin/uglifyjs
21
bin/uglifyjs
@@ -212,7 +212,14 @@ function run() {
|
|||||||
fatal("ERROR: " + ex.message);
|
fatal("ERROR: " + ex.message);
|
||||||
}
|
}
|
||||||
if (program.output == "spidermonkey") {
|
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) {
|
} else if (program.output) {
|
||||||
fs.writeFileSync(program.output, result.code);
|
fs.writeFileSync(program.output, result.code);
|
||||||
if (result.map) {
|
if (result.map) {
|
||||||
@@ -278,9 +285,17 @@ function parse_js(flag, constants) {
|
|||||||
return function(value, options) {
|
return function(value, options) {
|
||||||
options = options || {};
|
options = options || {};
|
||||||
try {
|
try {
|
||||||
UglifyJS.parse(value, {
|
UglifyJS.minify(value, {
|
||||||
|
parse: {
|
||||||
expression: true
|
expression: true
|
||||||
}).walk(new UglifyJS.TreeWalker(function(node) {
|
},
|
||||||
|
compress: false,
|
||||||
|
mangle: false,
|
||||||
|
output: {
|
||||||
|
ast: true,
|
||||||
|
code: false
|
||||||
|
}
|
||||||
|
}).ast.walk(new UglifyJS.TreeWalker(function(node) {
|
||||||
if (node instanceof UglifyJS.AST_Assign) {
|
if (node instanceof UglifyJS.AST_Assign) {
|
||||||
var name = node.left.print_to_string();
|
var name = node.left.print_to_string();
|
||||||
var value = node.right;
|
var value = node.right;
|
||||||
|
|||||||
@@ -108,6 +108,11 @@ function minify(files, options) {
|
|||||||
toplevel = mangle_properties(toplevel, options.mangle.properties);
|
toplevel = mangle_properties(toplevel, options.mangle.properties);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
var result = {};
|
||||||
|
if (options.output.ast) {
|
||||||
|
result.ast = toplevel;
|
||||||
|
}
|
||||||
|
if (!HOP(options.output, "code") || options.output.code) {
|
||||||
if (options.sourceMap) {
|
if (options.sourceMap) {
|
||||||
if (typeof options.sourceMap.content == "string") {
|
if (typeof options.sourceMap.content == "string") {
|
||||||
options.sourceMap.content = JSON.parse(options.sourceMap.content);
|
options.sourceMap.content = JSON.parse(options.sourceMap.content);
|
||||||
@@ -123,11 +128,11 @@ function minify(files, options) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
delete options.output.ast;
|
||||||
|
delete options.output.code;
|
||||||
var stream = OutputStream(options.output);
|
var stream = OutputStream(options.output);
|
||||||
toplevel.print(stream);
|
toplevel.print(stream);
|
||||||
var result = {
|
result.code = stream.get();
|
||||||
code: stream.get()
|
|
||||||
};
|
|
||||||
if (options.sourceMap) {
|
if (options.sourceMap) {
|
||||||
result.map = options.output.source_map.toString();
|
result.map = options.output.source_map.toString();
|
||||||
if (options.sourceMap.url == "inline") {
|
if (options.sourceMap.url == "inline") {
|
||||||
@@ -136,6 +141,7 @@ function minify(files, options) {
|
|||||||
result.code += "\n//# sourceMappingURL=" + options.sourceMap.url;
|
result.code += "\n//# sourceMappingURL=" + options.sourceMap.url;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (warnings.length) {
|
if (warnings.length) {
|
||||||
result.warnings = warnings;
|
result.warnings = warnings;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user