2017-05-03 15:35:00 +02:00
|
|
|
#! /usr/bin/env node
|
|
|
|
// -*- js -*-
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
// workaround for tty output truncation upon process.exit()
|
|
|
|
[process.stdout, process.stderr].forEach(function(stream){
|
|
|
|
if (stream._handle && stream._handle.setBlocking)
|
|
|
|
stream._handle.setBlocking(true);
|
|
|
|
});
|
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
var fs = require("fs");
|
2017-08-14 05:01:11 +02:00
|
|
|
var info = require("../package.json");
|
2017-05-03 15:35:00 +02:00
|
|
|
var path = require("path");
|
2017-08-14 05:01:11 +02:00
|
|
|
var program = require("commander");
|
|
|
|
var UglifyJS = require("../tools/node");
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
var skip_keys = [ "cname", "enclosed", "parent_scope", "scope", "thedef", "uses_eval", "uses_with" ];
|
|
|
|
var files = {};
|
|
|
|
var options = {
|
|
|
|
compress: false,
|
|
|
|
mangle: false
|
|
|
|
};
|
|
|
|
program.version(info.name + " " + info.version);
|
|
|
|
program.parseArgv = program.parse;
|
|
|
|
program.parse = undefined;
|
|
|
|
if (process.argv.indexOf("ast") >= 0) program.helpInformation = UglifyJS.describe_ast;
|
|
|
|
else if (process.argv.indexOf("options") >= 0) program.helpInformation = function() {
|
|
|
|
var text = [];
|
|
|
|
var options = UglifyJS.default_options();
|
|
|
|
for (var option in options) {
|
|
|
|
text.push("--" + (option == "output" ? "beautify" : option == "sourceMap" ? "source-map" : option) + " options:");
|
|
|
|
text.push(format_object(options[option]));
|
|
|
|
text.push("");
|
|
|
|
}
|
|
|
|
return text.join("\n");
|
|
|
|
};
|
|
|
|
program.option("-p, --parse <options>", "Specify parser options.", parse_js());
|
|
|
|
program.option("-c, --compress [options]", "Enable compressor/specify compressor options.", parse_js());
|
|
|
|
program.option("-m, --mangle [options]", "Mangle names/specify mangler options.", parse_js());
|
|
|
|
program.option("--mangle-props [options]", "Mangle properties/specify mangler options.", parse_js());
|
|
|
|
program.option("-b, --beautify [options]", "Beautify output/specify output options.", parse_js());
|
|
|
|
program.option("-o, --output <file>", "Output file (default STDOUT).");
|
|
|
|
program.option("--comments [filter]", "Preserve copyright comments in the output.");
|
|
|
|
program.option("--config-file <file>", "Read minify() options from JSON file.");
|
|
|
|
program.option("-d, --define <expr>[=value]", "Global definitions.", parse_js("define"));
|
|
|
|
program.option("--ie8", "Support non-standard Internet Explorer 8.");
|
|
|
|
program.option("--keep-fnames", "Do not mangle/drop function names. Useful for code relying on Function.prototype.name.");
|
|
|
|
program.option("--name-cache <file>", "File to hold mangled name mappings.");
|
|
|
|
program.option("--self", "Build UglifyJS as a library (implies --wrap UglifyJS)");
|
|
|
|
program.option("--source-map [options]", "Enable source map/specify source map options.", parse_source_map());
|
|
|
|
program.option("--timings", "Display operations run time on STDERR.")
|
|
|
|
program.option("--toplevel", "Compress and/or mangle variables in toplevel scope.");
|
|
|
|
program.option("--verbose", "Print diagnostic messages.");
|
|
|
|
program.option("--warn", "Print warning messages.");
|
|
|
|
program.option("--wrap <name>", "Embed everything as a function with “exports” corresponding to “name” globally.");
|
|
|
|
program.arguments("[files...]").parseArgv(process.argv);
|
|
|
|
if (program.configFile) {
|
|
|
|
options = JSON.parse(read_file(program.configFile));
|
|
|
|
}
|
|
|
|
if (!program.output && program.sourceMap && program.sourceMap.url != "inline") {
|
|
|
|
fatal("ERROR: cannot write source map to STDOUT");
|
|
|
|
}
|
|
|
|
[
|
|
|
|
"compress",
|
|
|
|
"ie8",
|
|
|
|
"mangle",
|
|
|
|
"sourceMap",
|
|
|
|
"toplevel",
|
|
|
|
"wrap"
|
|
|
|
].forEach(function(name) {
|
|
|
|
if (name in program) {
|
|
|
|
options[name] = program[name];
|
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
});
|
2017-08-14 05:01:11 +02:00
|
|
|
if (program.beautify) {
|
|
|
|
options.output = typeof program.beautify == "object" ? program.beautify : {};
|
|
|
|
if (!("beautify" in options.output)) {
|
|
|
|
options.output.beautify = true;
|
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
if (program.comments) {
|
|
|
|
if (typeof options.output != "object") options.output = {};
|
|
|
|
options.output.comments = typeof program.comments == "string" ? program.comments : "some";
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
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];
|
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
if (program.keepFnames) {
|
|
|
|
options.keep_fnames = true;
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
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;
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
if (program.nameCache) {
|
|
|
|
options.nameCache = JSON.parse(read_file(program.nameCache, "{}"));
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
if (program.output == "ast") {
|
|
|
|
options.output = {
|
|
|
|
ast: true,
|
|
|
|
code: false
|
|
|
|
};
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
if (program.parse) {
|
|
|
|
if (!program.parse.acorn && !program.parse.spidermonkey) {
|
|
|
|
options.parse = program.parse;
|
|
|
|
} else if (program.sourceMap && program.sourceMap.content == "inline") {
|
|
|
|
fatal("ERROR: inline source map only works with built-in parser");
|
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
var convert_path = function(name) {
|
|
|
|
return name;
|
2017-05-03 15:35:00 +02:00
|
|
|
};
|
2017-08-14 05:01:11 +02:00
|
|
|
if (typeof program.sourceMap == "object" && "base" in program.sourceMap) {
|
|
|
|
convert_path = function() {
|
|
|
|
var base = program.sourceMap.base;
|
|
|
|
delete options.sourceMap.base;
|
|
|
|
return function(name) {
|
|
|
|
return path.relative(base, name);
|
|
|
|
};
|
|
|
|
}();
|
|
|
|
}
|
|
|
|
if (program.verbose) {
|
|
|
|
options.warnings = "verbose";
|
|
|
|
} else if (program.warn) {
|
|
|
|
options.warnings = true;
|
|
|
|
}
|
|
|
|
if (program.self) {
|
|
|
|
if (program.args.length) {
|
2017-05-03 15:35:00 +02:00
|
|
|
print_error("WARN: Ignoring input files since --self was passed");
|
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
if (!options.wrap) options.wrap = "UglifyJS";
|
|
|
|
simple_glob(UglifyJS.FILES).forEach(function(name) {
|
|
|
|
files[convert_path(name)] = read_file(name);
|
|
|
|
});
|
|
|
|
run();
|
|
|
|
} else if (program.args.length) {
|
|
|
|
simple_glob(program.args).forEach(function(name) {
|
|
|
|
files[convert_path(name)] = read_file(name);
|
|
|
|
});
|
|
|
|
run();
|
|
|
|
} else {
|
|
|
|
var chunks = [];
|
|
|
|
process.stdin.setEncoding("utf8");
|
|
|
|
process.stdin.on("data", function(chunk) {
|
|
|
|
chunks.push(chunk);
|
|
|
|
}).on("end", function() {
|
|
|
|
files = [ chunks.join("") ];
|
|
|
|
run();
|
|
|
|
});
|
|
|
|
process.stdin.resume();
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
function convert_ast(fn) {
|
|
|
|
return UglifyJS.AST_Node.from_mozilla_ast(Object.keys(files).reduce(fn, null));
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
function run() {
|
|
|
|
UglifyJS.AST_Node.warn_function = function(msg) {
|
|
|
|
print_error("WARN: " + msg);
|
|
|
|
};
|
|
|
|
if (program.timings) options.timings = true;
|
|
|
|
try {
|
|
|
|
if (program.parse) {
|
|
|
|
if (program.parse.acorn) {
|
|
|
|
files = convert_ast(function(toplevel, name) {
|
|
|
|
return require("acorn").parse(files[name], {
|
|
|
|
locations: true,
|
|
|
|
program: toplevel,
|
|
|
|
sourceFile: name
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else if (program.parse.spidermonkey) {
|
|
|
|
files = convert_ast(function(toplevel, name) {
|
|
|
|
var obj = JSON.parse(files[name]);
|
|
|
|
if (!toplevel) return obj;
|
|
|
|
toplevel.body = toplevel.body.concat(obj.body);
|
|
|
|
return toplevel;
|
2017-05-03 15:35:00 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
} catch (ex) {
|
|
|
|
fatal(ex);
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
var result = UglifyJS.minify(files, options);
|
|
|
|
if (result.error) {
|
|
|
|
var ex = result.error;
|
|
|
|
if (ex.name == "SyntaxError") {
|
|
|
|
print_error("Parse error at " + ex.filename + ":" + ex.line + "," + ex.col);
|
|
|
|
var col = ex.col;
|
|
|
|
var lines = files[ex.filename].split(/\r?\n/);
|
|
|
|
var line = lines[ex.line - 1];
|
|
|
|
if (!line && !col) {
|
|
|
|
line = lines[ex.line - 2];
|
|
|
|
col = line.length;
|
|
|
|
}
|
|
|
|
if (line) {
|
|
|
|
var limit = 70;
|
|
|
|
if (col > limit) {
|
|
|
|
line = line.slice(col - limit);
|
|
|
|
col = limit;
|
|
|
|
}
|
|
|
|
print_error(line.slice(0, 80));
|
|
|
|
print_error(line.slice(0, col).replace(/\S/g, " ") + "^");
|
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
if (ex.defs) {
|
|
|
|
print_error("Supported options:");
|
|
|
|
print_error(format_object(ex.defs));
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
fatal(ex);
|
|
|
|
} else if (program.output == "ast") {
|
|
|
|
print(JSON.stringify(result.ast, function(key, value) {
|
|
|
|
if (skip_key(key)) return;
|
|
|
|
if (value instanceof UglifyJS.AST_Token) return;
|
|
|
|
if (value instanceof UglifyJS.Dictionary) return;
|
|
|
|
if (value instanceof UglifyJS.AST_Node) {
|
|
|
|
var result = {
|
|
|
|
_class: "AST_" + value.TYPE
|
|
|
|
};
|
|
|
|
value.CTOR.PROPS.forEach(function(prop) {
|
|
|
|
result[prop] = value[prop];
|
|
|
|
});
|
|
|
|
return result;
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
return value;
|
|
|
|
}, 2));
|
|
|
|
} else if (program.output == "spidermonkey") {
|
|
|
|
print(JSON.stringify(UglifyJS.minify(result.code, {
|
|
|
|
compress: false,
|
|
|
|
mangle: false,
|
|
|
|
output: {
|
|
|
|
ast: true,
|
|
|
|
code: false
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
}).ast.to_mozilla_ast(), null, 2));
|
|
|
|
} else if (program.output) {
|
|
|
|
fs.writeFileSync(program.output, result.code);
|
|
|
|
if (result.map) {
|
|
|
|
fs.writeFileSync(program.output + ".map", result.map);
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
} else {
|
2017-08-14 05:01:11 +02:00
|
|
|
print(result.code);
|
|
|
|
}
|
|
|
|
if (program.nameCache) {
|
|
|
|
fs.writeFileSync(program.nameCache, JSON.stringify(options.nameCache));
|
|
|
|
}
|
|
|
|
if (result.timings) for (var phase in result.timings) {
|
|
|
|
print_error("- " + phase + ": " + result.timings[phase].toFixed(3) + "s");
|
|
|
|
}
|
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
function fatal(message) {
|
|
|
|
if (message instanceof Error) message = message.stack.replace(/^\S*?Error:/, "ERROR:")
|
|
|
|
print_error(message);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
// A file glob function that only supports "*" and "?" wildcards in the basename.
|
|
|
|
// Example: "foo/bar/*baz??.*.js"
|
|
|
|
// Argument `glob` may be a string or an array of strings.
|
|
|
|
// Returns an array of strings. Garbage in, garbage out.
|
|
|
|
function simple_glob(glob) {
|
|
|
|
if (Array.isArray(glob)) {
|
|
|
|
return [].concat.apply([], glob.map(simple_glob));
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
if (glob.match(/\*|\?/)) {
|
|
|
|
var dir = path.dirname(glob);
|
|
|
|
try {
|
|
|
|
var entries = fs.readdirSync(dir);
|
|
|
|
} catch (ex) {}
|
|
|
|
if (entries) {
|
|
|
|
var pattern = "^" + path.basename(glob)
|
|
|
|
.replace(/[.+^$[\]\\(){}]/g, "\\$&")
|
|
|
|
.replace(/\*/g, "[^/\\\\]*")
|
|
|
|
.replace(/\?/g, "[^/\\\\]") + "$";
|
|
|
|
var mod = process.platform === "win32" ? "i" : "";
|
|
|
|
var rx = new RegExp(pattern, mod);
|
|
|
|
var results = entries.filter(function(name) {
|
|
|
|
return rx.test(name);
|
|
|
|
}).map(function(name) {
|
|
|
|
return path.join(dir, name);
|
|
|
|
});
|
|
|
|
if (results.length) return results;
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
return [ glob ];
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
function read_file(path, default_value) {
|
|
|
|
try {
|
|
|
|
return fs.readFileSync(path, "utf8");
|
|
|
|
} catch (ex) {
|
|
|
|
if (ex.code == "ENOENT" && default_value != null) return default_value;
|
|
|
|
fatal(ex);
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
function parse_js(flag) {
|
|
|
|
return function(value, options) {
|
|
|
|
options = options || {};
|
2017-05-03 15:35:00 +02:00
|
|
|
try {
|
2017-08-14 05:01:11 +02:00
|
|
|
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;
|
|
|
|
if (flag) {
|
|
|
|
options[name] = value;
|
|
|
|
} else if (value instanceof UglifyJS.AST_Array) {
|
|
|
|
options[name] = value.elements.map(to_string);
|
|
|
|
} else {
|
|
|
|
options[name] = to_string(value);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
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.getValue() : value.print_to_string({
|
|
|
|
quote_keys: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}));
|
2017-05-03 15:35:00 +02:00
|
|
|
} catch(ex) {
|
2017-08-14 05:01:11 +02:00
|
|
|
if (flag) {
|
|
|
|
fatal("Error parsing arguments for '" + flag + "': " + value);
|
|
|
|
} else {
|
|
|
|
options[value] = null;
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
return options;
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
function parse_source_map() {
|
|
|
|
var parse = parse_js();
|
|
|
|
return function(value, options) {
|
|
|
|
var hasContent = options && "content" in options;
|
|
|
|
var settings = parse(value, options);
|
|
|
|
if (!hasContent && settings.content && settings.content != "inline") {
|
|
|
|
print_error("INFO: Using input source map: " + settings.content);
|
|
|
|
settings.content = read_file(settings.content, settings.content);
|
|
|
|
}
|
|
|
|
return settings;
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
function skip_key(key) {
|
|
|
|
return skip_keys.indexOf(key) >= 0;
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
function format_object(obj) {
|
|
|
|
var lines = [];
|
|
|
|
var padding = "";
|
|
|
|
Object.keys(obj).map(function(name) {
|
|
|
|
if (padding.length < name.length) padding = Array(name.length + 1).join(" ");
|
|
|
|
return [ name, JSON.stringify(obj[name]) ];
|
|
|
|
}).forEach(function(tokens) {
|
|
|
|
lines.push(" " + tokens[0] + padding.slice(tokens[0].length - 2) + tokens[1]);
|
|
|
|
});
|
|
|
|
return lines.join("\n");
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function print_error(msg) {
|
2017-08-14 05:01:11 +02:00
|
|
|
process.stderr.write(msg);
|
|
|
|
process.stderr.write("\n");
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function print(txt) {
|
2017-08-14 05:01:11 +02:00
|
|
|
process.stdout.write(txt);
|
|
|
|
process.stdout.write("\n");
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|