parse command line internally (#3961)
This commit is contained in:
489
bin/uglifyjs
489
bin/uglifyjs
@@ -8,185 +8,251 @@ require("../tools/exit");
|
|||||||
var fs = require("fs");
|
var fs = require("fs");
|
||||||
var info = require("../package.json");
|
var info = require("../package.json");
|
||||||
var path = require("path");
|
var path = require("path");
|
||||||
var program = require("commander");
|
|
||||||
var UglifyJS = require("../tools/node");
|
var UglifyJS = require("../tools/node");
|
||||||
|
|
||||||
var skip_keys = [ "cname", "inlined", "parent_scope", "scope", "uses_eval", "uses_with" ];
|
var skip_keys = [ "cname", "inlined", "parent_scope", "scope", "uses_eval", "uses_with" ];
|
||||||
var files = {};
|
var files = {};
|
||||||
var options = {
|
var options = {};
|
||||||
compress: false,
|
var short_forms = {
|
||||||
mangle: false
|
b: "beautify",
|
||||||
|
c: "compress",
|
||||||
|
d: "define",
|
||||||
|
e: "enclose",
|
||||||
|
h: "help",
|
||||||
|
m: "mangle",
|
||||||
|
o: "output",
|
||||||
|
O: "output-opts",
|
||||||
|
p: "parse",
|
||||||
|
v: "version",
|
||||||
|
V: "version",
|
||||||
};
|
};
|
||||||
program.version(info.name + " " + info.version);
|
var args = process.argv.slice(2);
|
||||||
program.parseArgv = program.parse;
|
var paths = [];
|
||||||
program.parse = undefined;
|
var output, nameCache;
|
||||||
if (process.argv.indexOf("ast") >= 0) program.helpInformation = UglifyJS.describe_ast;
|
var specified = {};
|
||||||
else if (process.argv.indexOf("options") >= 0) program.helpInformation = function() {
|
while (args.length) {
|
||||||
var text = [];
|
var arg = args.shift();
|
||||||
var toplevels = [];
|
if (arg[0] != "-") {
|
||||||
var padding = "";
|
paths.push(arg);
|
||||||
var options = UglifyJS.default_options();
|
} else if (arg == "--") {
|
||||||
for (var name in options) {
|
paths = paths.concat(args);
|
||||||
var option = options[name];
|
break;
|
||||||
if (option && typeof option == "object") {
|
} else if (arg[1] == "-") {
|
||||||
text.push("--" + ({
|
process_option(arg.slice(2));
|
||||||
output: "beautify",
|
} else [].forEach.call(arg.slice(1), function(letter, index, arg) {
|
||||||
sourceMap: "source-map",
|
process_option(short_forms[letter], index + 1 < arg.length);
|
||||||
}[name] || name) + " options:");
|
|
||||||
text.push(format_object(option));
|
|
||||||
text.push("");
|
|
||||||
} else {
|
|
||||||
if (padding.length < name.length) padding = Array(name.length + 1).join(" ");
|
|
||||||
toplevels.push([ {
|
|
||||||
keep_fnames: "keep-fnames",
|
|
||||||
nameCache: "name-cache",
|
|
||||||
}[name] || name, option ]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
toplevels.forEach(function(tokens) {
|
|
||||||
text.push("--" + tokens[0] + padding.slice(tokens[0].length - 2) + tokens[1]);
|
|
||||||
});
|
});
|
||||||
return text.join("\n");
|
}
|
||||||
};
|
|
||||||
program.option("-p, --parse <options>", "Specify parser options.", parse_js());
|
function process_option(name, no_value) {
|
||||||
program.option("-c, --compress [options]", "Enable compressor/specify compressor options.", parse_js());
|
specified[name] = true;
|
||||||
program.option("-m, --mangle [options]", "Mangle names/specify mangler options.", parse_js());
|
switch (name) {
|
||||||
program.option("--mangle-props [options]", "Mangle properties/specify mangler options.", parse_js());
|
case "help":
|
||||||
program.option("-b, --beautify [options]", "Beautify output/specify output options.", parse_js());
|
switch (read_value()) {
|
||||||
program.option("-O, --output-opts [options]", "Output options (beautify disabled).", parse_js());
|
case "ast":
|
||||||
program.option("-o, --output <file>", "Output file (default STDOUT).");
|
print(UglifyJS.describe_ast());
|
||||||
program.option("--comments [filter]", "Preserve copyright comments in the output.");
|
break;
|
||||||
program.option("--config-file <file>", "Read minify() options from JSON file.");
|
case "options":
|
||||||
program.option("-d, --define <expr>[=value]", "Global definitions.", parse_js("define"));
|
var text = [];
|
||||||
program.option("-e, --enclose [arg[,...][:value[,...]]]", "Embed everything in a big function, with configurable argument(s) & value(s).");
|
var toplevels = [];
|
||||||
program.option("--ie8", "Support non-standard Internet Explorer 8.");
|
var padding = "";
|
||||||
program.option("--keep-fnames", "Do not mangle/drop function names. Useful for code relying on Function.prototype.name.");
|
var defaults = UglifyJS.default_options();
|
||||||
program.option("--name-cache <file>", "File to hold mangled name mappings.");
|
for (var name in defaults) {
|
||||||
program.option("--rename", "Force symbol expansion.");
|
var option = defaults[name];
|
||||||
program.option("--no-rename", "Disable symbol expansion.");
|
if (option && typeof option == "object") {
|
||||||
program.option("--self", "Build UglifyJS as a library (implies --wrap UglifyJS)");
|
text.push("--" + ({
|
||||||
program.option("--source-map [options]", "Enable source map/specify source map options.", parse_js());
|
output: "beautify",
|
||||||
program.option("--timings", "Display operations run time on STDERR.");
|
sourceMap: "source-map",
|
||||||
program.option("--toplevel", "Compress and/or mangle variables in toplevel scope.");
|
}[name] || name) + " options:");
|
||||||
program.option("--validate", "Perform validation during AST manipulations.");
|
text.push(format_object(option));
|
||||||
program.option("--verbose", "Print diagnostic messages.");
|
text.push("");
|
||||||
program.option("--warn", "Print warning messages.");
|
} else {
|
||||||
program.option("--wrap <name>", "Embed everything as a function with “exports” corresponding to “name” globally.");
|
if (padding.length < name.length) padding = Array(name.length + 1).join(" ");
|
||||||
program.option("--reduce-test", "Reduce a standalone `console.log` based test case.");
|
toplevels.push([ {
|
||||||
program.arguments("[files...]").parseArgv(process.argv);
|
keep_fnames: "keep-fnames",
|
||||||
if (program.configFile) {
|
nameCache: "name-cache",
|
||||||
options = JSON.parse(read_file(program.configFile));
|
}[name] || name, option ]);
|
||||||
if (options.mangle && options.mangle.properties && options.mangle.properties.regex) {
|
}
|
||||||
options.mangle.properties.regex = UglifyJS.parse(options.mangle.properties.regex, {
|
}
|
||||||
expression: true
|
toplevels.forEach(function(tokens) {
|
||||||
}).value;
|
text.push("--" + tokens[0] + padding.slice(tokens[0].length - 2) + tokens[1]);
|
||||||
|
});
|
||||||
|
print(text.join("\n"));
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
print([
|
||||||
|
"Usage: uglifyjs [files...] [options]",
|
||||||
|
"",
|
||||||
|
"Options:",
|
||||||
|
" -h, --help Print usage information.",
|
||||||
|
" `--help options` for details on available options.",
|
||||||
|
" -v, -V, --version Print version number.",
|
||||||
|
" -p, --parse <options> Specify parser options.",
|
||||||
|
" -c, --compress [options] Enable compressor/specify compressor options.",
|
||||||
|
" -m, --mangle [options] Mangle names/specify mangler options.",
|
||||||
|
" --mangle-props [options] Mangle properties/specify mangler options.",
|
||||||
|
" -b, --beautify [options] Beautify output/specify output options.",
|
||||||
|
" -O, --output-opts <options> Output options (beautify disabled).",
|
||||||
|
" -o, --output <file> Output file (default STDOUT).",
|
||||||
|
" --comments [filter] Preserve copyright comments in the output.",
|
||||||
|
" --config-file <file> Read minify() options from JSON file.",
|
||||||
|
" -d, --define <expr>[=value] Global definitions.",
|
||||||
|
" -e, --enclose [arg[,...][:value[,...]]] Embed everything in a big function, with configurable argument(s) & value(s).",
|
||||||
|
" --ie8 Support non-standard Internet Explorer 8.",
|
||||||
|
" --keep-fnames Do not mangle/drop function names. Useful for code relying on Function.prototype.name.",
|
||||||
|
" --name-cache <file> File to hold mangled name mappings.",
|
||||||
|
" --rename Force symbol expansion.",
|
||||||
|
" --no-rename Disable symbol expansion.",
|
||||||
|
" --self Build UglifyJS as a library (implies --wrap UglifyJS)",
|
||||||
|
" --source-map [options] Enable source map/specify source map options.",
|
||||||
|
" --timings Display operations run time on STDERR.",
|
||||||
|
" --toplevel Compress and/or mangle variables in toplevel scope.",
|
||||||
|
" --validate Perform validation during AST manipulations.",
|
||||||
|
" --verbose Print diagnostic messages.",
|
||||||
|
" --warn Print warning messages.",
|
||||||
|
" --wrap <name> Embed everything as a function with “exports” corresponding to “name” globally.",
|
||||||
|
" --reduce-test Reduce a standalone test case.",
|
||||||
|
].join("\n"));
|
||||||
|
}
|
||||||
|
process.exit();
|
||||||
|
case "version":
|
||||||
|
print(info.name + " " + info.version);
|
||||||
|
process.exit();
|
||||||
|
case "config-file":
|
||||||
|
var config = JSON.parse(read_file(read_value(true)));
|
||||||
|
if (config.mangle && config.mangle.properties && config.mangle.properties.regex) {
|
||||||
|
config.mangle.properties.regex = UglifyJS.parse(config.mangle.properties.regex, {
|
||||||
|
expression: true,
|
||||||
|
}).value;
|
||||||
|
}
|
||||||
|
for (var key in config) if (!(key in options)) options[key] = config[key];
|
||||||
|
break;
|
||||||
|
case "compress":
|
||||||
|
case "mangle":
|
||||||
|
options[name] = parse_js(read_value(), options[name]);
|
||||||
|
break;
|
||||||
|
case "source-map":
|
||||||
|
options.sourceMap = parse_js(read_value(), options.sourceMap);
|
||||||
|
break;
|
||||||
|
case "enclose":
|
||||||
|
options[name] = read_value();
|
||||||
|
break;
|
||||||
|
case "ie8":
|
||||||
|
case "timings":
|
||||||
|
case "toplevel":
|
||||||
|
case "validate":
|
||||||
|
options[name] = true;
|
||||||
|
break;
|
||||||
|
case "keep-fnames":
|
||||||
|
options.keep_fnames = true;
|
||||||
|
break;
|
||||||
|
case "wrap":
|
||||||
|
options[name] = read_value(true);
|
||||||
|
break;
|
||||||
|
case "verbose":
|
||||||
|
options.warnings = "verbose";
|
||||||
|
break;
|
||||||
|
case "warn":
|
||||||
|
if (!options.warnings) options.warnings = true;
|
||||||
|
break;
|
||||||
|
case "beautify":
|
||||||
|
options.output = parse_js(read_value(), options.output);
|
||||||
|
if (!("beautify" in options.output)) options.output.beautify = true;
|
||||||
|
break;
|
||||||
|
case "output-opts":
|
||||||
|
options.output = parse_js(read_value(true), options.output);
|
||||||
|
break;
|
||||||
|
case "comments":
|
||||||
|
if (typeof options.output != "object") options.output = {};
|
||||||
|
options.output.comments = read_value();
|
||||||
|
if (options.output.comments === true) options.output.comments = "some";
|
||||||
|
break;
|
||||||
|
case "define":
|
||||||
|
if (typeof options.compress != "object") options.compress = {};
|
||||||
|
options.compress.global_defs = parse_js(read_value(true), options.compress.global_defs, "define");
|
||||||
|
break;
|
||||||
|
case "mangle-props":
|
||||||
|
if (typeof options.mangle != "object") options.mangle = {};
|
||||||
|
options.mangle.properties = parse_js(read_value(), options.mangle.properties);
|
||||||
|
break;
|
||||||
|
case "name-cache":
|
||||||
|
nameCache = read_value(true);
|
||||||
|
options.nameCache = JSON.parse(read_file(nameCache, "{}"));
|
||||||
|
break;
|
||||||
|
case "output":
|
||||||
|
output = read_value(true);
|
||||||
|
break;
|
||||||
|
case "parse":
|
||||||
|
options.parse = parse_js(read_value(true), options.parse);
|
||||||
|
break;
|
||||||
|
case "rename":
|
||||||
|
options.rename = true;
|
||||||
|
break;
|
||||||
|
case "no-rename":
|
||||||
|
options.rename = false;
|
||||||
|
break;
|
||||||
|
case "reduce-test":
|
||||||
|
case "self":
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
fatal("invalid option --" + name);
|
||||||
|
}
|
||||||
|
|
||||||
|
function read_value(required) {
|
||||||
|
if (no_value || !args.length || args[0][0] == "-") {
|
||||||
|
if (required) fatal("missing option argument for --" + name);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return args.shift();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!program.output && program.sourceMap && program.sourceMap.url != "inline") {
|
if (!output && options.sourceMap && options.sourceMap.url != "inline") fatal("cannot write source map to STDOUT");
|
||||||
fatal("cannot write source map to STDOUT");
|
if (specified["beautify"] && specified["output-opts"]) fatal("--beautify cannot be used with --output-opts");
|
||||||
}
|
[ "compress", "mangle" ].forEach(function(name) {
|
||||||
[
|
if (!(name in options)) options[name] = false;
|
||||||
"compress",
|
|
||||||
"enclose",
|
|
||||||
"ie8",
|
|
||||||
"mangle",
|
|
||||||
"sourceMap",
|
|
||||||
"toplevel",
|
|
||||||
"validate",
|
|
||||||
"wrap"
|
|
||||||
].forEach(function(name) {
|
|
||||||
if (name in program) {
|
|
||||||
options[name] = program[name];
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
if (program.verbose) {
|
if (options.mangle && options.mangle.properties) {
|
||||||
options.warnings = "verbose";
|
if (options.mangle.properties.domprops) {
|
||||||
} else if (program.warn) {
|
delete options.mangle.properties.domprops;
|
||||||
options.warnings = true;
|
} else {
|
||||||
|
if (typeof options.mangle.properties != "object") options.mangle.properties = {};
|
||||||
|
if (!Array.isArray(options.mangle.properties.reserved)) options.mangle.properties.reserved = [];
|
||||||
|
require("../tools/domprops").forEach(function(name) {
|
||||||
|
UglifyJS.push_uniq(options.mangle.properties.reserved, name);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (output == "ast") options.output = {
|
||||||
|
ast: true,
|
||||||
|
code: false,
|
||||||
|
};
|
||||||
|
if (options.parse && (options.parse.acorn || options.parse.spidermonkey)
|
||||||
|
&& options.sourceMap && options.sourceMap.content == "inline") {
|
||||||
|
fatal("inline source map only works with built-in parser");
|
||||||
}
|
}
|
||||||
if (options.warnings) {
|
if (options.warnings) {
|
||||||
UglifyJS.AST_Node.log_function(print_error, options.warnings == "verbose");
|
UglifyJS.AST_Node.log_function(print_error, options.warnings == "verbose");
|
||||||
delete options.warnings;
|
delete options.warnings;
|
||||||
}
|
}
|
||||||
if (program.beautify) {
|
|
||||||
options.output = typeof program.beautify == "object" ? program.beautify : {};
|
|
||||||
if (!("beautify" in options.output)) {
|
|
||||||
options.output.beautify = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (program.outputOpts) {
|
|
||||||
if (program.beautify) fatal("--beautify cannot be used with --output-opts");
|
|
||||||
options.output = typeof program.outputOpts == "object" ? program.outputOpts : {};
|
|
||||||
}
|
|
||||||
if (program.comments) {
|
|
||||||
if (typeof options.output != "object") options.output = {};
|
|
||||||
options.output.comments = typeof program.comments == "string" ? program.comments : "some";
|
|
||||||
}
|
|
||||||
if (program.define) {
|
|
||||||
if (typeof options.compress != "object") options.compress = {};
|
|
||||||
if (typeof options.compress.global_defs != "object") options.compress.global_defs = {};
|
|
||||||
for (var expr in program.define) {
|
|
||||||
options.compress.global_defs[expr] = program.define[expr];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (program.keepFnames) {
|
|
||||||
options.keep_fnames = true;
|
|
||||||
}
|
|
||||||
if (program.mangleProps) {
|
|
||||||
if (program.mangleProps.domprops) {
|
|
||||||
delete program.mangleProps.domprops;
|
|
||||||
} else {
|
|
||||||
if (typeof program.mangleProps != "object") program.mangleProps = {};
|
|
||||||
if (!Array.isArray(program.mangleProps.reserved)) program.mangleProps.reserved = [];
|
|
||||||
require("../tools/domprops").forEach(function(name) {
|
|
||||||
UglifyJS.push_uniq(program.mangleProps.reserved, name);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (typeof options.mangle != "object") options.mangle = {};
|
|
||||||
options.mangle.properties = program.mangleProps;
|
|
||||||
}
|
|
||||||
if (program.nameCache) {
|
|
||||||
options.nameCache = JSON.parse(read_file(program.nameCache, "{}"));
|
|
||||||
}
|
|
||||||
if (program.output == "ast") {
|
|
||||||
options.output = {
|
|
||||||
ast: true,
|
|
||||||
code: false
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if (program.parse) {
|
|
||||||
if (!program.parse.acorn && !program.parse.spidermonkey) {
|
|
||||||
options.parse = program.parse;
|
|
||||||
} else if (program.sourceMap && program.sourceMap.content == "inline") {
|
|
||||||
fatal("inline source map only works with built-in parser");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (~program.rawArgs.indexOf("--rename")) {
|
|
||||||
options.rename = true;
|
|
||||||
} else if (!program.rename) {
|
|
||||||
options.rename = false;
|
|
||||||
}
|
|
||||||
var convert_path = function(name) {
|
var convert_path = function(name) {
|
||||||
return name;
|
return name;
|
||||||
};
|
};
|
||||||
if (typeof program.sourceMap == "object" && "base" in program.sourceMap) {
|
if (typeof options.sourceMap == "object" && "base" in options.sourceMap) {
|
||||||
convert_path = function() {
|
convert_path = function() {
|
||||||
var base = program.sourceMap.base;
|
var base = options.sourceMap.base;
|
||||||
delete options.sourceMap.base;
|
delete options.sourceMap.base;
|
||||||
return function(name) {
|
return function(name) {
|
||||||
return path.relative(base, name);
|
return path.relative(base, name);
|
||||||
};
|
};
|
||||||
}();
|
}();
|
||||||
}
|
}
|
||||||
if (program.self) {
|
if (specified["self"]) {
|
||||||
if (program.args.length) UglifyJS.AST_Node.warn("Ignoring input files since --self was passed");
|
if (paths.length) UglifyJS.AST_Node.warn("Ignoring input files since --self was passed");
|
||||||
if (!options.wrap) options.wrap = "UglifyJS";
|
if (!options.wrap) options.wrap = "UglifyJS";
|
||||||
simple_glob(UglifyJS.FILES).forEach(function(name) {
|
paths = UglifyJS.FILES;
|
||||||
files[convert_path(name)] = read_file(name);
|
}
|
||||||
});
|
if (paths.length) {
|
||||||
run();
|
simple_glob(paths).forEach(function(name) {
|
||||||
} else if (program.args.length) {
|
|
||||||
simple_glob(program.args).forEach(function(name) {
|
|
||||||
files[convert_path(name)] = read_file(name);
|
files[convert_path(name)] = read_file(name);
|
||||||
});
|
});
|
||||||
run();
|
run();
|
||||||
@@ -207,15 +273,14 @@ function convert_ast(fn) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function run() {
|
function run() {
|
||||||
var content = program.sourceMap && program.sourceMap.content;
|
var content = options.sourceMap && options.sourceMap.content;
|
||||||
if (content && content != "inline") {
|
if (content && content != "inline") {
|
||||||
UglifyJS.AST_Node.info("Using input source map: " + content);
|
UglifyJS.AST_Node.info("Using input source map: " + content);
|
||||||
options.sourceMap.content = read_file(content, content);
|
options.sourceMap.content = read_file(content, content);
|
||||||
}
|
}
|
||||||
if (program.timings) options.timings = true;
|
|
||||||
try {
|
try {
|
||||||
if (program.parse) {
|
if (options.parse) {
|
||||||
if (program.parse.acorn) {
|
if (options.parse.acorn) {
|
||||||
files = convert_ast(function(toplevel, name) {
|
files = convert_ast(function(toplevel, name) {
|
||||||
return require("acorn").parse(files[name], {
|
return require("acorn").parse(files[name], {
|
||||||
locations: true,
|
locations: true,
|
||||||
@@ -223,7 +288,7 @@ function run() {
|
|||||||
sourceFile: name
|
sourceFile: name
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
} else if (program.parse.spidermonkey) {
|
} else if (options.parse.spidermonkey) {
|
||||||
files = convert_ast(function(toplevel, name) {
|
files = convert_ast(function(toplevel, name) {
|
||||||
var obj = JSON.parse(files[name]);
|
var obj = JSON.parse(files[name]);
|
||||||
if (!toplevel) return obj;
|
if (!toplevel) return obj;
|
||||||
@@ -235,17 +300,17 @@ function run() {
|
|||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
fatal(ex);
|
fatal(ex);
|
||||||
}
|
}
|
||||||
if (program.reduceTest) {
|
var result;
|
||||||
|
if (specified["reduce-test"]) {
|
||||||
// load on demand - assumes dev tree checked out
|
// load on demand - assumes dev tree checked out
|
||||||
var reduce_test = require("../test/reduce");
|
var reduce_test = require("../test/reduce");
|
||||||
var testcase = files[0] || files[Object.keys(files)[0]];
|
if (Object.keys(files).length != 1) fatal("can only test on a single file");
|
||||||
var result = reduce_test(testcase, options, {
|
result = reduce_test(files[Object.keys(files)[0]], options, {
|
||||||
log: print_error,
|
log: print_error,
|
||||||
verbose: true,
|
verbose: true,
|
||||||
});
|
});
|
||||||
}
|
} else {
|
||||||
else {
|
result = UglifyJS.minify(files, options);
|
||||||
var result = UglifyJS.minify(files, options);
|
|
||||||
}
|
}
|
||||||
if (result.error) {
|
if (result.error) {
|
||||||
var ex = result.error;
|
var ex = result.error;
|
||||||
@@ -275,10 +340,8 @@ function run() {
|
|||||||
print_error(format_object(ex.defs));
|
print_error(format_object(ex.defs));
|
||||||
}
|
}
|
||||||
fatal(ex);
|
fatal(ex);
|
||||||
} else if (program.output == "ast") {
|
} else if (output == "ast") {
|
||||||
if (!options.compress && !options.mangle) {
|
if (!options.compress && !options.mangle) result.ast.figure_out_scope({});
|
||||||
result.ast.figure_out_scope({});
|
|
||||||
}
|
|
||||||
print(JSON.stringify(result.ast, function(key, value) {
|
print(JSON.stringify(result.ast, function(key, value) {
|
||||||
if (value) switch (key) {
|
if (value) switch (key) {
|
||||||
case "thedef":
|
case "thedef":
|
||||||
@@ -304,26 +367,22 @@ function run() {
|
|||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
}, 2));
|
}, 2));
|
||||||
} else if (program.output == "spidermonkey") {
|
} else if (output == "spidermonkey") {
|
||||||
print(JSON.stringify(UglifyJS.minify(result.code, {
|
print(JSON.stringify(UglifyJS.minify(result.code, {
|
||||||
compress: false,
|
compress: false,
|
||||||
mangle: false,
|
mangle: false,
|
||||||
output: {
|
output: {
|
||||||
ast: true,
|
ast: true,
|
||||||
code: false
|
code: false
|
||||||
}
|
},
|
||||||
}).ast.to_mozilla_ast(), null, 2));
|
}).ast.to_mozilla_ast(), null, 2));
|
||||||
} else if (program.output) {
|
} else if (output) {
|
||||||
fs.writeFileSync(program.output, result.code);
|
fs.writeFileSync(output, result.code);
|
||||||
if (result.map) {
|
if (result.map) fs.writeFileSync(output + ".map", result.map);
|
||||||
fs.writeFileSync(program.output + ".map", result.map);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
print(result.code);
|
print(result.code);
|
||||||
}
|
}
|
||||||
if (program.nameCache) {
|
if (nameCache) fs.writeFileSync(nameCache, JSON.stringify(options.nameCache));
|
||||||
fs.writeFileSync(program.nameCache, JSON.stringify(options.nameCache));
|
|
||||||
}
|
|
||||||
if (result.timings) for (var phase in result.timings) {
|
if (result.timings) for (var phase in result.timings) {
|
||||||
print_error("- " + phase + ": " + result.timings[phase].toFixed(3) + "s");
|
print_error("- " + phase + ": " + result.timings[phase].toFixed(3) + "s");
|
||||||
}
|
}
|
||||||
@@ -379,47 +438,45 @@ function read_file(path, default_value) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function parse_js(flag) {
|
function parse_js(value, options, flag) {
|
||||||
return function(value, options) {
|
if (!options || typeof options != "object") options = {};
|
||||||
options = options || {};
|
if (typeof value == "string") try {
|
||||||
try {
|
UglifyJS.parse(value, {
|
||||||
UglifyJS.parse(value, {
|
expression: true
|
||||||
expression: true
|
}).walk(new UglifyJS.TreeWalker(function(node) {
|
||||||
}).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;
|
if (flag) {
|
||||||
if (flag) {
|
options[name] = value;
|
||||||
options[name] = value;
|
} else if (value instanceof UglifyJS.AST_Array) {
|
||||||
} else if (value instanceof UglifyJS.AST_Array) {
|
options[name] = value.elements.map(to_string);
|
||||||
options[name] = value.elements.map(to_string);
|
} else {
|
||||||
} else {
|
options[name] = to_string(value);
|
||||||
options[name] = to_string(value);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
if (node instanceof UglifyJS.AST_Symbol || node instanceof UglifyJS.AST_PropAccess) {
|
return true;
|
||||||
var name = node.print_to_string();
|
|
||||||
options[name] = true;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (!(node instanceof UglifyJS.AST_Sequence)) throw node;
|
|
||||||
|
|
||||||
function to_string(value) {
|
|
||||||
return value instanceof UglifyJS.AST_Constant ? value.value : value.print_to_string({
|
|
||||||
quote_keys: true
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
} catch (ex) {
|
|
||||||
if (flag) {
|
|
||||||
fatal("cannot parse arguments for '" + flag + "': " + value);
|
|
||||||
} else {
|
|
||||||
options[value] = null;
|
|
||||||
}
|
}
|
||||||
|
if (node instanceof UglifyJS.AST_Symbol || node instanceof UglifyJS.AST_PropAccess) {
|
||||||
|
var name = node.print_to_string();
|
||||||
|
options[name] = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (!(node instanceof UglifyJS.AST_Sequence)) throw node;
|
||||||
|
|
||||||
|
function to_string(value) {
|
||||||
|
return value instanceof UglifyJS.AST_Constant ? value.value : value.print_to_string({
|
||||||
|
quote_keys: true
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
} catch (ex) {
|
||||||
|
if (flag) {
|
||||||
|
fatal("cannot parse arguments for '" + flag + "': " + value);
|
||||||
|
} else {
|
||||||
|
options[value] = null;
|
||||||
}
|
}
|
||||||
return options;
|
|
||||||
}
|
}
|
||||||
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
function skip_key(key) {
|
function skip_key(key) {
|
||||||
|
|||||||
@@ -22,9 +22,6 @@
|
|||||||
"tools",
|
"tools",
|
||||||
"LICENSE"
|
"LICENSE"
|
||||||
],
|
],
|
||||||
"dependencies": {
|
|
||||||
"commander": "~2.20.3"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"acorn": "~7.1.0",
|
"acorn": "~7.1.0",
|
||||||
"semver": "~6.3.0"
|
"semver": "~6.3.0"
|
||||||
|
|||||||
@@ -249,7 +249,7 @@ describe("bin/uglifyjs", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
it("Should work with `--output-opts`", function(done) {
|
it("Should work with `--output-opts`", function(done) {
|
||||||
var command = uglifyjscmd + ' test/input/issue-1482/input.js -O';
|
var command = uglifyjscmd + ' test/input/issue-1482/input.js -O ascii_only';
|
||||||
exec(command, function(err, stdout) {
|
exec(command, function(err, stdout) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
assert.strictEqual(stdout, read("test/input/issue-1482/default.js"));
|
assert.strictEqual(stdout, read("test/input/issue-1482/default.js"));
|
||||||
@@ -257,7 +257,7 @@ describe("bin/uglifyjs", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
it("Should fail when both --beautify & --output-opts are specified", function(done) {
|
it("Should fail when both --beautify & --output-opts are specified", function(done) {
|
||||||
var command = uglifyjscmd + " test/input/issue-520/input.js -bO";
|
var command = uglifyjscmd + " test/input/issue-520/input.js -bO ascii_only";
|
||||||
exec(command, function(err, stdout, stderr) {
|
exec(command, function(err, stdout, stderr) {
|
||||||
assert.ok(err);
|
assert.ok(err);
|
||||||
assert.strictEqual(stderr, "ERROR: --beautify cannot be used with --output-opts\n");
|
assert.strictEqual(stderr, "ERROR: --beautify cannot be used with --output-opts\n");
|
||||||
@@ -717,7 +717,7 @@ describe("bin/uglifyjs", function() {
|
|||||||
var command = uglifyjscmd + " test/input/issue-3315/input.js --config-file test/input/issue-3315/config.json";
|
var command = uglifyjscmd + " test/input/issue-3315/input.js --config-file test/input/issue-3315/config.json";
|
||||||
exec(command, function(err, stdout) {
|
exec(command, function(err, stdout) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
assert.strictEqual(stdout, 'function f(){"aaaaaaaaaa";var a={prop:1,a:2};return a.prop+a.a}\n');
|
assert.strictEqual(stdout, 'function f(){"aaaaaaaaaa";var a={prop:1,t:2};return a.prop+a.t}\n');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user