support minify() output as AST (#1878)

- `options.output.ast` (default `false`)
- `options.output.code` (default `true`)
This commit is contained in:
Alex Lam S.L
2017-05-08 02:11:45 +08:00
committed by GitHub
parent 2d99d06601
commit e54748365c
2 changed files with 49 additions and 28 deletions

View File

@@ -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, {
expression: true parse: {
}).walk(new UglifyJS.TreeWalker(function(node) { expression: true
},
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;

View File

@@ -108,32 +108,38 @@ function minify(files, options) {
toplevel = mangle_properties(toplevel, options.mangle.properties); toplevel = mangle_properties(toplevel, options.mangle.properties);
} }
} }
if (options.sourceMap) { var result = {};
if (typeof options.sourceMap.content == "string") { if (options.output.ast) {
options.sourceMap.content = JSON.parse(options.sourceMap.content); result.ast = toplevel;
} }
options.output.source_map = SourceMap({ if (!HOP(options.output, "code") || options.output.code) {
file: options.sourceMap.filename, if (options.sourceMap) {
orig: options.sourceMap.content, if (typeof options.sourceMap.content == "string") {
root: options.sourceMap.root options.sourceMap.content = JSON.parse(options.sourceMap.content);
}); }
if (options.sourceMap.includeSources) { options.output.source_map = SourceMap({
for (var name in files) { file: options.sourceMap.filename,
options.output.source_map.get().setSourceContent(name, files[name]); 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]);
}
} }
} }
} delete options.output.ast;
var stream = OutputStream(options.output); delete options.output.code;
toplevel.print(stream); var stream = OutputStream(options.output);
var result = { toplevel.print(stream);
code: stream.get() result.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") { result.code += "\n//# sourceMappingURL=data:application/json;charset=utf-8;base64," + to_base64(result.map);
result.code += "\n//# sourceMappingURL=data:application/json;charset=utf-8;base64," + to_base64(result.map); } else if (options.sourceMap.url) {
} else if (options.sourceMap.url) { result.code += "\n//# sourceMappingURL=" + options.sourceMap.url;
result.code += "\n//# sourceMappingURL=" + options.sourceMap.url; }
} }
} }
if (warnings.length) { if (warnings.length) {