add default index.js, restore bin/uglifyjs origional formating

This commit is contained in:
Moshe Brevda 2022-12-19 12:12:55 +02:00
parent 84464b7750
commit 32dd204b33
3 changed files with 518 additions and 626 deletions

3
.gitignore vendored
View File

@ -3,5 +3,4 @@
tmp/ tmp/
# files generated at build time # files generated at build time
index.js index.js.map
*.js.map

View File

@ -10,25 +10,8 @@ var info = require("../package.json");
var path = require("path"); var path = require("path");
var UglifyJS = require(".."); var UglifyJS = require("..");
var skip_keys = [ var skip_keys = [ "cname", "fixed", "in_arg", "inlined", "length_read", "parent_scope", "redef", "scope", "unused" ];
"cname", var truthy_keys = [ "optional", "pure", "terminal", "uses_arguments", "uses_eval", "uses_with" ];
"fixed",
"in_arg",
"inlined",
"length_read",
"parent_scope",
"redef",
"scope",
"unused",
];
var truthy_keys = [
"optional",
"pure",
"terminal",
"uses_arguments",
"uses_eval",
"uses_with",
];
var files = {}; var files = {};
var options = {}; var options = {};
@ -58,8 +41,7 @@ while (args.length) {
break; break;
} else if (arg[1] == "-") { } else if (arg[1] == "-") {
process_option(arg.slice(2)); process_option(arg.slice(2));
} else } else [].forEach.call(arg.slice(1), function(letter, index, arg) {
[].forEach.call(arg.slice(1), function (letter, index, arg) {
if (!(letter in short_forms)) fatal("invalid option -" + letter); if (!(letter in short_forms)) fatal("invalid option -" + letter);
process_option(short_forms[letter], index + 1 < arg.length); process_option(short_forms[letter], index + 1 < arg.length);
}); });
@ -81,39 +63,28 @@ function process_option(name, no_value) {
for (var name in defaults) { for (var name in defaults) {
var option = defaults[name]; var option = defaults[name];
if (option && typeof option == "object") { if (option && typeof option == "object") {
text.push( text.push("--" + ({
"--" +
({
output: "beautify", output: "beautify",
sourceMap: "source-map", sourceMap: "source-map",
}[name] || name) + }[name] || name) + " options:");
" options:"
);
text.push(format_object(option)); text.push(format_object(option));
text.push(""); text.push("");
} else { } else {
if (padding.length < name.length) if (padding.length < name.length) padding = Array(name.length + 1).join(" ");
padding = Array(name.length + 1).join(" "); toplevels.push([ {
toplevels.push([
{
keep_fargs: "keep-fargs", keep_fargs: "keep-fargs",
keep_fnames: "keep-fnames", keep_fnames: "keep-fnames",
nameCache: "name-cache", nameCache: "name-cache",
}[name] || name, }[name] || name, option ]);
option,
]);
} }
} }
toplevels.forEach(function (tokens) { toplevels.forEach(function(tokens) {
text.push( text.push("--" + tokens[0] + padding.slice(tokens[0].length - 2) + tokens[1]);
"--" + tokens[0] + padding.slice(tokens[0].length - 2) + tokens[1]
);
}); });
print(text.join("\n")); print(text.join("\n"));
break; break;
default: default:
print( print([
[
"Usage: uglifyjs [files...] [options]", "Usage: uglifyjs [files...] [options]",
"", "",
"Options:", "Options:",
@ -156,8 +127,7 @@ function process_option(name, no_value) {
"(internal debug use only)", "(internal debug use only)",
" --in-situ Warning: replaces original source files with minified output.", " --in-situ Warning: replaces original source files with minified output.",
" --reduce-test Reduce a standalone test case (assumes cloned repository).", " --reduce-test Reduce a standalone test case (assumes cloned repository).",
].join("\n") ].join("\n"));
);
} }
process.exit(); process.exit();
case "version": case "version":
@ -165,17 +135,10 @@ function process_option(name, no_value) {
process.exit(); process.exit();
case "config-file": case "config-file":
var config = JSON.parse(read_file(read_value(true))); var config = JSON.parse(read_file(read_value(true)));
if ( if (config.mangle && config.mangle.properties && config.mangle.properties.regex) {
config.mangle && config.mangle.properties.regex = UglifyJS.parse(config.mangle.properties.regex, {
config.mangle.properties &&
config.mangle.properties.regex
) {
config.mangle.properties.regex = UglifyJS.parse(
config.mangle.properties.regex,
{
expression: true, expression: true,
} }).value;
).value;
} }
for (var key in config) if (!(key in options)) options[key] = config[key]; for (var key in config) if (!(key in options)) options[key] = config[key];
break; break;
@ -232,18 +195,11 @@ function process_option(name, no_value) {
break; break;
case "define": case "define":
if (typeof options.compress != "object") options.compress = {}; if (typeof options.compress != "object") options.compress = {};
options.compress.global_defs = parse_js( options.compress.global_defs = parse_js(read_value(true), options.compress.global_defs, "define");
read_value(true),
options.compress.global_defs,
"define"
);
break; break;
case "mangle-props": case "mangle-props":
if (typeof options.mangle != "object") options.mangle = {}; if (typeof options.mangle != "object") options.mangle = {};
options.mangle.properties = parse_js( options.mangle.properties = parse_js(read_value(), options.mangle.properties);
read_value(),
options.mangle.properties
);
break; break;
case "module": case "module":
options.module = true; options.module = true;
@ -283,11 +239,9 @@ function process_option(name, no_value) {
return args.shift(); return args.shift();
} }
} }
if (!output && options.sourceMap && options.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");
if (specified["beautify"] && specified["output-opts"]) [ "compress", "mangle" ].forEach(function(name) {
fatal("--beautify cannot be used with --output-opts");
["compress", "mangle"].forEach(function (name) {
if (!(name in options)) options[name] = false; if (!(name in options)) options[name] = false;
}); });
if (/^ast|spidermonkey$/.test(output)) { if (/^ast|spidermonkey$/.test(output)) {
@ -295,47 +249,38 @@ if (/^ast|spidermonkey$/.test(output)) {
options.output.ast = true; options.output.ast = true;
options.output.code = false; options.output.code = false;
} }
if ( if (options.parse && (options.parse.acorn || options.parse.spidermonkey)
options.parse && && options.sourceMap && options.sourceMap.content == "inline") {
(options.parse.acorn || options.parse.spidermonkey) &&
options.sourceMap &&
options.sourceMap.content == "inline"
) {
fatal("inline source map only works with built-in parser"); 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;
} }
var convert_path = function (name) { var convert_path = function(name) {
return name; return name;
}; };
if (typeof options.sourceMap == "object" && "base" in options.sourceMap) { if (typeof options.sourceMap == "object" && "base" in options.sourceMap) {
convert_path = (function () { convert_path = function() {
var base = options.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 (specified["self"]) { if (specified["self"]) {
if (paths.length) if (paths.length) UglifyJS.AST_Node.warn("Ignoring input files since --self was passed");
UglifyJS.AST_Node.warn("Ignoring input files since --self was passed");
if (!options.wrap) options.wrap = "UglifyJS"; if (!options.wrap) options.wrap = "UglifyJS";
paths = UglifyJS.FILES; paths = UglifyJS.FILES;
} else if (paths.length) { } else if (paths.length) {
paths = simple_glob(paths); paths = simple_glob(paths);
} }
if (specified["in-situ"]) { if (specified["in-situ"]) {
if ( if (output && output != "spidermonkey" || specified["reduce-test"] || specified["self"]) {
(output && output != "spidermonkey") ||
specified["reduce-test"] ||
specified["self"]
) {
fatal("incompatible options specified"); fatal("incompatible options specified");
} }
paths.forEach(function (name) { paths.forEach(function(name) {
print(name); print(name);
if (/^ast|spidermonkey$/.test(name)) fatal("invalid file name specified"); if (/^ast|spidermonkey$/.test(name)) fatal("invalid file name specified");
files = {}; files = {};
@ -344,29 +289,19 @@ if (specified["in-situ"]) {
run(); run();
}); });
} else if (paths.length) { } else if (paths.length) {
paths.forEach(function (name) { paths.forEach(function(name) {
files[convert_path(name)] = read_file(name); files[convert_path(name)] = read_file(name);
}); });
run(); run();
} else { } else {
var timerId = var timerId = process.stdin.isTTY && process.argv.length < 3 && setTimeout(function() {
process.stdin.isTTY && print_error("Waiting for input... (use `--help` to print usage information)");
process.argv.length < 3 &&
setTimeout(function () {
print_error(
"Waiting for input... (use `--help` to print usage information)"
);
}, 1500); }, 1500);
var chunks = []; var chunks = [];
process.stdin.setEncoding("utf8"); process.stdin.setEncoding("utf8");
process.stdin process.stdin.once("data", function() {
.once("data", function () {
clearTimeout(timerId); clearTimeout(timerId);
}) }).on("data", process.stdin.isTTY ? function(chunk) {
.on(
"data",
process.stdin.isTTY
? function (chunk) {
// emulate console input termination via Ctrl+D / Ctrl+Z // emulate console input termination via Ctrl+D / Ctrl+Z
var match = /[\x04\x1a]\r?\n?$/.exec(chunk); var match = /[\x04\x1a]\r?\n?$/.exec(chunk);
if (match) { if (match) {
@ -376,12 +311,9 @@ if (specified["in-situ"]) {
} else { } else {
chunks.push(chunk); chunks.push(chunk);
} }
} } : function(chunk) {
: function (chunk) {
chunks.push(chunk); chunks.push(chunk);
} }).once("end", function() {
)
.once("end", function () {
files = { STDIN: chunks.join("") }; files = { STDIN: chunks.join("") };
run(); run();
}); });
@ -389,16 +321,14 @@ if (specified["in-situ"]) {
} }
function convert_ast(fn) { function convert_ast(fn) {
return UglifyJS.AST_Node.from_mozilla_ast( return UglifyJS.AST_Node.from_mozilla_ast(Object.keys(files).reduce(fn, null));
Object.keys(files).reduce(fn, null)
);
} }
function run() { function run() {
var content = options.sourceMap && options.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}", {
content: content, content : content,
}); });
options.sourceMap.content = read_file(content, content); options.sourceMap.content = read_file(content, content);
} }
@ -406,19 +336,19 @@ function run() {
if (options.parse) { if (options.parse) {
if (options.parse.acorn) { if (options.parse.acorn) {
var annotations = Object.create(null); var annotations = Object.create(null);
files = convert_ast(function (toplevel, name) { files = convert_ast(function(toplevel, name) {
var content = files[name]; var content = files[name];
var list = (annotations[name] = []); var list = annotations[name] = [];
var prev = -1; var prev = -1;
return require("acorn").parse(content, { return require("acorn").parse(content, {
allowHashBang: true, allowHashBang: true,
ecmaVersion: "latest", ecmaVersion: "latest",
locations: true, locations: true,
onComment: function (block, text, start, end) { onComment: function(block, text, start, end) {
var match = /[@#]__PURE__/.exec(text); var match = /[@#]__PURE__/.exec(text);
if (!match) { if (!match) {
if (start != prev) return; if (start != prev) return;
match = [list[prev]]; match = [ list[prev] ];
} }
while (/\s/.test(content[end])) end++; while (/\s/.test(content[end])) end++;
list[end] = match[0]; list[end] = match[0];
@ -430,23 +360,20 @@ function run() {
sourceType: "module", sourceType: "module",
}); });
}); });
files.walk( files.walk(new UglifyJS.TreeWalker(function(node) {
new UglifyJS.TreeWalker(function (node) {
if (!(node instanceof UglifyJS.AST_Call)) return; if (!(node instanceof UglifyJS.AST_Call)) return;
var list = annotations[node.start.file]; var list = annotations[node.start.file];
var pure = list[node.start.pos]; var pure = list[node.start.pos];
if (!pure) { if (!pure) {
var tokens = node.start.parens; var tokens = node.start.parens;
if (tokens) if (tokens) for (var i = 0; !pure && i < tokens.length; i++) {
for (var i = 0; !pure && i < tokens.length; i++) {
pure = list[tokens[i].pos]; pure = list[tokens[i].pos];
} }
} }
if (pure) node.pure = pure; if (pure) node.pure = pure;
}) }));
);
} else if (options.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;
toplevel.body = toplevel.body.concat(obj.body); toplevel.body = toplevel.body.concat(obj.body);
@ -472,9 +399,7 @@ function run() {
if (result.error) { if (result.error) {
var ex = result.error; var ex = result.error;
if (ex.name == "SyntaxError") { if (ex.name == "SyntaxError") {
print_error( print_error("Parse error at " + ex.filename + ":" + ex.line + "," + ex.col);
"Parse error at " + ex.filename + ":" + ex.line + "," + ex.col
);
var file = files[ex.filename]; var file = files[ex.filename];
if (file) { if (file) {
var col = ex.col; var col = ex.col;
@ -503,22 +428,17 @@ function run() {
if (!options.compress && !options.mangle) { if (!options.compress && !options.mangle) {
var toplevel = result.ast; var toplevel = result.ast;
if (!(toplevel instanceof UglifyJS.AST_Toplevel)) { if (!(toplevel instanceof UglifyJS.AST_Toplevel)) {
if (!(toplevel instanceof UglifyJS.AST_Statement)) if (!(toplevel instanceof UglifyJS.AST_Statement)) toplevel = new UglifyJS.AST_SimpleStatement({
toplevel = new UglifyJS.AST_SimpleStatement({
body: toplevel, body: toplevel,
}); });
toplevel = new UglifyJS.AST_Toplevel({ toplevel = new UglifyJS.AST_Toplevel({
body: [toplevel], body: [ toplevel ],
}); });
} }
toplevel.figure_out_scope({}); toplevel.figure_out_scope({});
} }
print( print(JSON.stringify(result.ast, function(key, value) {
JSON.stringify( if (value) switch (key) {
result.ast,
function (key, value) {
if (value)
switch (key) {
case "enclosed": case "enclosed":
return value.length ? value.map(symdef) : undefined; return value.length ? value.map(symdef) : undefined;
case "functions": case "functions":
@ -533,18 +453,15 @@ function run() {
if (value instanceof UglifyJS.Dictionary) return; if (value instanceof UglifyJS.Dictionary) return;
if (value instanceof UglifyJS.AST_Node) { if (value instanceof UglifyJS.AST_Node) {
var result = { var result = {
_class: "AST_" + value.TYPE, _class: "AST_" + value.TYPE
}; };
value.CTOR.PROPS.forEach(function (prop) { value.CTOR.PROPS.forEach(function(prop) {
result[prop] = value[prop]; result[prop] = value[prop];
}); });
return result; return result;
} }
return value; return value;
}, }, 2));
2
)
);
} else if (output == "spidermonkey") { } else if (output == "spidermonkey") {
print(JSON.stringify(result.ast.to_mozilla_ast(), null, 2)); print(JSON.stringify(result.ast.to_mozilla_ast(), null, 2));
} else if (output) { } else if (output) {
@ -554,9 +471,7 @@ function run() {
for (var name in options.output) { for (var name in options.output) {
if (!/^ast|code$/.test(name)) opts[name] = options.output[name]; if (!/^ast|code$/.test(name)) opts[name] = options.output[name];
} }
code = UglifyJS.AST_Node.from_mozilla_ast( code = UglifyJS.AST_Node.from_mozilla_ast(result.ast.to_mozilla_ast()).print_to_string(opts);
result.ast.to_mozilla_ast()
).print_to_string(opts);
} else { } else {
code = result.code; code = result.code;
} }
@ -566,15 +481,14 @@ function run() {
print(result.code); print(result.code);
} }
if (nameCache) fs.writeFileSync(nameCache, JSON.stringify(options.nameCache)); if (nameCache) fs.writeFileSync(nameCache, JSON.stringify(options.nameCache));
if (result.timings) if (result.timings) for (var phase in 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");
} }
} }
function fatal(message) { function fatal(message) {
if (message instanceof Error) { if (message instanceof Error) {
message = message.stack.replace(/^\S*?Error:/, "ERROR:"); message = message.stack.replace(/^\S*?Error:/, "ERROR:")
} else { } else {
message = "ERROR: " + message; message = "ERROR: " + message;
} }
@ -587,11 +501,11 @@ function fatal(message) {
// Argument `paths` must be an array of strings. // Argument `paths` must be an array of strings.
// Returns an array of strings. Garbage in, garbage out. // Returns an array of strings. Garbage in, garbage out.
function simple_glob(paths) { function simple_glob(paths) {
return paths.reduce(function (paths, glob) { return paths.reduce(function(paths, glob) {
if (/\*|\?/.test(glob)) { if (/\*|\?/.test(glob)) {
var dir = path.dirname(glob); var dir = path.dirname(glob);
try { try {
var entries = fs.readdirSync(dir).filter(function (name) { var entries = fs.readdirSync(dir).filter(function(name) {
try { try {
return fs.statSync(path.join(dir, name)).isFile(); return fs.statSync(path.join(dir, name)).isFile();
} catch (ex) { } catch (ex) {
@ -600,22 +514,15 @@ function simple_glob(paths) {
}); });
} catch (ex) {} } catch (ex) {}
if (entries) { if (entries) {
var pattern = var pattern = "^" + path.basename(glob)
"^" +
path
.basename(glob)
.replace(/[.+^$[\]\\(){}]/g, "\\$&") .replace(/[.+^$[\]\\(){}]/g, "\\$&")
.replace(/\*/g, "[^/\\\\]*") .replace(/\*/g, "[^/\\\\]*")
.replace(/\?/g, "[^/\\\\]") + .replace(/\?/g, "[^/\\\\]") + "$";
"$";
var mod = process.platform === "win32" ? "i" : ""; var mod = process.platform === "win32" ? "i" : "";
var rx = new RegExp(pattern, mod); var rx = new RegExp(pattern, mod);
var results = entries var results = entries.filter(function(name) {
.filter(function (name) {
return rx.test(name); return rx.test(name);
}) }).sort().map(function(name) {
.sort()
.map(function (name) {
return path.join(dir, name); return path.join(dir, name);
}); });
if (results.length) { if (results.length) {
@ -640,12 +547,10 @@ function read_file(path, default_value) {
function parse_js(value, options, flag) { function parse_js(value, options, flag) {
if (!options || typeof options != "object") options = Object.create(null); if (!options || typeof options != "object") options = Object.create(null);
if (typeof value == "string") if (typeof value == "string") try {
try {
UglifyJS.parse(value, { UglifyJS.parse(value, {
expression: true, expression: true
}).walk( }).walk(new UglifyJS.TreeWalker(function(node) {
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;
@ -658,10 +563,7 @@ function parse_js(value, options, flag) {
} }
return true; return true;
} }
if ( if (node instanceof UglifyJS.AST_Symbol || node instanceof UglifyJS.AST_PropAccess) {
node instanceof UglifyJS.AST_Symbol ||
node instanceof UglifyJS.AST_PropAccess
) {
var name = node.print_to_string(); var name = node.print_to_string();
options[name] = true; options[name] = true;
return true; return true;
@ -669,14 +571,11 @@ function parse_js(value, options, flag) {
if (!(node instanceof UglifyJS.AST_Sequence)) throw node; if (!(node instanceof UglifyJS.AST_Sequence)) throw node;
function to_string(value) { function to_string(value) {
return value instanceof UglifyJS.AST_Constant return value instanceof UglifyJS.AST_Constant ? value.value : value.print_to_string({
? value.value quote_keys: true
: value.print_to_string({
quote_keys: true,
}); });
} }
}) }));
);
} catch (ex) { } catch (ex) {
if (flag) { if (flag) {
fatal("cannot parse arguments for '" + flag + "': " + value); fatal("cannot parse arguments for '" + flag + "': " + value);
@ -688,15 +587,13 @@ function parse_js(value, options, flag) {
} }
function skip_property(key, value) { function skip_property(key, value) {
return ( return skip_keys.indexOf(key) >= 0
skip_keys.indexOf(key) >= 0 ||
// only skip truthy_keys if their value is falsy // only skip truthy_keys if their value is falsy
(truthy_keys.indexOf(key) >= 0 && !value) || truthy_keys.indexOf(key) >= 0 && !value;
);
} }
function symdef(def) { function symdef(def) {
var ret = 1e6 + def.id + " " + def.name; var ret = (1e6 + def.id) + " " + def.name;
if (def.mangled_name) ret += " " + def.mangled_name; if (def.mangled_name) ret += " " + def.mangled_name;
return ret; return ret;
} }
@ -704,16 +601,11 @@ function symdef(def) {
function format_object(obj) { function format_object(obj) {
var lines = []; var lines = [];
var padding = ""; var padding = "";
Object.keys(obj) Object.keys(obj).map(function(name) {
.map(function (name) { if (padding.length < name.length) padding = Array(name.length + 1).join(" ");
if (padding.length < name.length) return [ name, JSON.stringify(obj[name]) ];
padding = Array(name.length + 1).join(" "); }).forEach(function(tokens) {
return [name, JSON.stringify(obj[name])]; lines.push(" " + tokens[0] + padding.slice(tokens[0].length - 2) + tokens[1]);
})
.forEach(function (tokens) {
lines.push(
" " + tokens[0] + padding.slice(tokens[0].length - 2) + tokens[1]
);
}); });
return lines.join("\n"); return lines.join("\n");
} }

1
index.js Normal file
View File

@ -0,0 +1 @@
module.exports = require("./tools/node.js");