uglifyjs binary: Make read_whole_file async and don't attempt to read STDIN synchronously.
This commit is contained in:
committed by
Mihai Bazon
parent
7628bcac01
commit
69dde0462b
209
bin/uglifyjs
209
bin/uglifyjs
@@ -7,6 +7,7 @@ var UglifyJS = require("../tools/node");
|
|||||||
var sys = require("util");
|
var sys = require("util");
|
||||||
var optimist = require("optimist");
|
var optimist = require("optimist");
|
||||||
var fs = require("fs");
|
var fs = require("fs");
|
||||||
|
var async = require("async");
|
||||||
var acorn;
|
var acorn;
|
||||||
var ARGS = optimist
|
var ARGS = optimist
|
||||||
.usage("$0 input1.js [input2.js ...] [options]\n\
|
.usage("$0 input1.js [input2.js ...] [options]\n\
|
||||||
@@ -215,110 +216,116 @@ try {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
files.forEach(function(file) {
|
async.eachLimit(files, 1, function (file, cb) {
|
||||||
var code = read_whole_file(file);
|
read_whole_file(file, function (err, code) {
|
||||||
if (ARGS.p != null) {
|
if (err) {
|
||||||
file = file.replace(/^\/+/, "").split(/\/+/).slice(ARGS.p).join("/");
|
sys.error("ERROR: can't read file: " + filename);
|
||||||
}
|
process.exit(1);
|
||||||
time_it("parse", function(){
|
|
||||||
if (ARGS.spidermonkey) {
|
|
||||||
var program = JSON.parse(code);
|
|
||||||
if (!TOPLEVEL) TOPLEVEL = program;
|
|
||||||
else TOPLEVEL.body = TOPLEVEL.body.concat(program.body);
|
|
||||||
}
|
}
|
||||||
else if (ARGS.acorn) {
|
if (ARGS.p != null) {
|
||||||
TOPLEVEL = acorn.parse(code, {
|
file = file.replace(/^\/+/, "").split(/\/+/).slice(ARGS.p).join("/");
|
||||||
locations : true,
|
|
||||||
trackComments : true,
|
|
||||||
sourceFile : file,
|
|
||||||
program : TOPLEVEL
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
else {
|
time_it("parse", function(){
|
||||||
TOPLEVEL = UglifyJS.parse(code, {
|
if (ARGS.spidermonkey) {
|
||||||
filename: file,
|
var program = JSON.parse(code);
|
||||||
toplevel: TOPLEVEL
|
if (!TOPLEVEL) TOPLEVEL = program;
|
||||||
});
|
else TOPLEVEL.body = TOPLEVEL.body.concat(program.body);
|
||||||
};
|
}
|
||||||
|
else if (ARGS.acorn) {
|
||||||
|
TOPLEVEL = acorn.parse(code, {
|
||||||
|
locations : true,
|
||||||
|
trackComments : true,
|
||||||
|
sourceFile : file,
|
||||||
|
program : TOPLEVEL
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
TOPLEVEL = UglifyJS.parse(code, {
|
||||||
|
filename: file,
|
||||||
|
toplevel: TOPLEVEL
|
||||||
|
});
|
||||||
|
};
|
||||||
|
});
|
||||||
|
cb();
|
||||||
|
});
|
||||||
|
}, function () {
|
||||||
|
if (ARGS.acorn || ARGS.spidermonkey) time_it("convert_ast", function(){
|
||||||
|
TOPLEVEL = UglifyJS.AST_Node.from_mozilla_ast(TOPLEVEL);
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
if (ARGS.acorn || ARGS.spidermonkey) time_it("convert_ast", function(){
|
if (ARGS.wrap) {
|
||||||
TOPLEVEL = UglifyJS.AST_Node.from_mozilla_ast(TOPLEVEL);
|
TOPLEVEL = TOPLEVEL.wrap_commonjs(ARGS.wrap, ARGS.export_all);
|
||||||
});
|
|
||||||
|
|
||||||
if (ARGS.wrap) {
|
|
||||||
TOPLEVEL = TOPLEVEL.wrap_commonjs(ARGS.wrap, ARGS.export_all);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ARGS.enclose) {
|
|
||||||
var arg_parameter_list = ARGS.enclose;
|
|
||||||
|
|
||||||
if (!(arg_parameter_list instanceof Array)) {
|
|
||||||
arg_parameter_list = [arg_parameter_list];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TOPLEVEL = TOPLEVEL.wrap_enclose(arg_parameter_list);
|
if (ARGS.enclose) {
|
||||||
}
|
var arg_parameter_list = ARGS.enclose;
|
||||||
|
|
||||||
var SCOPE_IS_NEEDED = COMPRESS || MANGLE || ARGS.lint;
|
if (!(arg_parameter_list instanceof Array)) {
|
||||||
|
arg_parameter_list = [arg_parameter_list];
|
||||||
if (SCOPE_IS_NEEDED) {
|
|
||||||
time_it("scope", function(){
|
|
||||||
TOPLEVEL.figure_out_scope({ screw_ie8: ARGS.screw_ie8 });
|
|
||||||
if (ARGS.lint) {
|
|
||||||
TOPLEVEL.scope_warnings();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TOPLEVEL = TOPLEVEL.wrap_enclose(arg_parameter_list);
|
||||||
|
}
|
||||||
|
|
||||||
|
var SCOPE_IS_NEEDED = COMPRESS || MANGLE || ARGS.lint;
|
||||||
|
|
||||||
|
if (SCOPE_IS_NEEDED) {
|
||||||
|
time_it("scope", function(){
|
||||||
|
TOPLEVEL.figure_out_scope({ screw_ie8: ARGS.screw_ie8 });
|
||||||
|
if (ARGS.lint) {
|
||||||
|
TOPLEVEL.scope_warnings();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (COMPRESS) {
|
||||||
|
time_it("squeeze", function(){
|
||||||
|
TOPLEVEL = TOPLEVEL.transform(compressor);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (SCOPE_IS_NEEDED) {
|
||||||
|
time_it("scope", function(){
|
||||||
|
TOPLEVEL.figure_out_scope({ screw_ie8: ARGS.screw_ie8 });
|
||||||
|
if (MANGLE) {
|
||||||
|
TOPLEVEL.compute_char_frequency(MANGLE);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (MANGLE) time_it("mangle", function(){
|
||||||
|
TOPLEVEL.mangle_names(MANGLE);
|
||||||
});
|
});
|
||||||
}
|
time_it("generate", function(){
|
||||||
|
TOPLEVEL.print(output);
|
||||||
if (COMPRESS) {
|
|
||||||
time_it("squeeze", function(){
|
|
||||||
TOPLEVEL = TOPLEVEL.transform(compressor);
|
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
if (SCOPE_IS_NEEDED) {
|
output = output.get();
|
||||||
time_it("scope", function(){
|
|
||||||
TOPLEVEL.figure_out_scope({ screw_ie8: ARGS.screw_ie8 });
|
|
||||||
if (MANGLE) {
|
|
||||||
TOPLEVEL.compute_char_frequency(MANGLE);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (MANGLE) time_it("mangle", function(){
|
if (SOURCE_MAP) {
|
||||||
TOPLEVEL.mangle_names(MANGLE);
|
fs.writeFileSync(ARGS.source_map, SOURCE_MAP, "utf8");
|
||||||
});
|
output += "\n/*\n//@ sourceMappingURL=" + (ARGS.source_map_url || ARGS.source_map) + "\n*/";
|
||||||
time_it("generate", function(){
|
}
|
||||||
TOPLEVEL.print(output);
|
|
||||||
});
|
|
||||||
|
|
||||||
output = output.get();
|
if (OUTPUT_FILE) {
|
||||||
|
fs.writeFileSync(OUTPUT_FILE, output, "utf8");
|
||||||
|
} else {
|
||||||
|
sys.print(output);
|
||||||
|
sys.error("\n");
|
||||||
|
}
|
||||||
|
|
||||||
if (SOURCE_MAP) {
|
if (ARGS.stats) {
|
||||||
fs.writeFileSync(ARGS.source_map, SOURCE_MAP, "utf8");
|
sys.error(UglifyJS.string_template("Timing information (compressed {count} files):", {
|
||||||
output += "\n/*\n//@ sourceMappingURL=" + (ARGS.source_map_url || ARGS.source_map) + "\n*/";
|
count: files.length
|
||||||
}
|
|
||||||
|
|
||||||
if (OUTPUT_FILE) {
|
|
||||||
fs.writeFileSync(OUTPUT_FILE, output, "utf8");
|
|
||||||
} else {
|
|
||||||
sys.print(output);
|
|
||||||
sys.error("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ARGS.stats) {
|
|
||||||
sys.error(UglifyJS.string_template("Timing information (compressed {count} files):", {
|
|
||||||
count: files.length
|
|
||||||
}));
|
|
||||||
for (var i in STATS) if (STATS.hasOwnProperty(i)) {
|
|
||||||
sys.error(UglifyJS.string_template("- {name}: {time}s", {
|
|
||||||
name: i,
|
|
||||||
time: (STATS[i] / 1000).toFixed(3)
|
|
||||||
}));
|
}));
|
||||||
|
for (var i in STATS) if (STATS.hasOwnProperty(i)) {
|
||||||
|
sys.error(UglifyJS.string_template("- {name}: {time}s", {
|
||||||
|
name: i,
|
||||||
|
time: (STATS[i] / 1000).toFixed(3)
|
||||||
|
}));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
/* -----[ functions ]----- */
|
/* -----[ functions ]----- */
|
||||||
|
|
||||||
@@ -363,20 +370,18 @@ function getOptions(x, constants) {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
function read_whole_file(filename) {
|
function read_whole_file(filename, cb) {
|
||||||
try {
|
if (filename == "-") {
|
||||||
if (filename == "-") {
|
var chunks = [];
|
||||||
var chunks = [];
|
process.stdin.setEncoding('utf-8');
|
||||||
do {
|
process.stdin.on('data', function (chunk) {
|
||||||
var chunk = fs.readFileSync("/dev/stdin", "utf8");
|
chunks.push(chunk);
|
||||||
chunks.push(chunk);
|
}).on('end', function () {
|
||||||
} while (chunk.length);
|
cb(null, chunks.join(""));
|
||||||
return chunks.join("");
|
});
|
||||||
}
|
process.openStdin();
|
||||||
return fs.readFileSync(filename, "utf8");
|
} else {
|
||||||
} catch(ex) {
|
fs.readFile(filename, "utf-8", cb);
|
||||||
sys.error("ERROR: can't read file: " + filename);
|
|
||||||
process.exit(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,8 +15,9 @@
|
|||||||
"url": "https://github.com/mishoo/UglifyJS2.git"
|
"url": "https://github.com/mishoo/UglifyJS2.git"
|
||||||
}],
|
}],
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"async" : "~0.2.6",
|
||||||
"source-map" : "~0.1.7",
|
"source-map" : "~0.1.7",
|
||||||
"optimist" : "~0.3.5"
|
"optimist" : "~0.3.5",
|
||||||
},
|
},
|
||||||
"bin": {
|
"bin": {
|
||||||
"uglifyjs" : "bin/uglifyjs"
|
"uglifyjs" : "bin/uglifyjs"
|
||||||
|
|||||||
Reference in New Issue
Block a user