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
85
bin/uglifyjs
85
bin/uglifyjs
@@ -7,6 +7,7 @@ var UglifyJS = require("../tools/node");
|
||||
var sys = require("util");
|
||||
var optimist = require("optimist");
|
||||
var fs = require("fs");
|
||||
var async = require("async");
|
||||
var acorn;
|
||||
var ARGS = optimist
|
||||
.usage("$0 input1.js [input2.js ...] [options]\n\
|
||||
@@ -215,8 +216,12 @@ try {
|
||||
}
|
||||
}
|
||||
|
||||
files.forEach(function(file) {
|
||||
var code = read_whole_file(file);
|
||||
async.eachLimit(files, 1, function (file, cb) {
|
||||
read_whole_file(file, function (err, code) {
|
||||
if (err) {
|
||||
sys.error("ERROR: can't read file: " + filename);
|
||||
process.exit(1);
|
||||
}
|
||||
if (ARGS.p != null) {
|
||||
file = file.replace(/^\/+/, "").split(/\/+/).slice(ARGS.p).join("/");
|
||||
}
|
||||
@@ -241,17 +246,18 @@ files.forEach(function(file) {
|
||||
});
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
if (ARGS.acorn || ARGS.spidermonkey) time_it("convert_ast", function(){
|
||||
cb();
|
||||
});
|
||||
}, function () {
|
||||
if (ARGS.acorn || ARGS.spidermonkey) time_it("convert_ast", function(){
|
||||
TOPLEVEL = UglifyJS.AST_Node.from_mozilla_ast(TOPLEVEL);
|
||||
});
|
||||
});
|
||||
|
||||
if (ARGS.wrap) {
|
||||
if (ARGS.wrap) {
|
||||
TOPLEVEL = TOPLEVEL.wrap_commonjs(ARGS.wrap, ARGS.export_all);
|
||||
}
|
||||
}
|
||||
|
||||
if (ARGS.enclose) {
|
||||
if (ARGS.enclose) {
|
||||
var arg_parameter_list = ARGS.enclose;
|
||||
|
||||
if (!(arg_parameter_list instanceof Array)) {
|
||||
@@ -259,56 +265,56 @@ if (ARGS.enclose) {
|
||||
}
|
||||
|
||||
TOPLEVEL = TOPLEVEL.wrap_enclose(arg_parameter_list);
|
||||
}
|
||||
}
|
||||
|
||||
var SCOPE_IS_NEEDED = COMPRESS || MANGLE || ARGS.lint;
|
||||
var SCOPE_IS_NEEDED = COMPRESS || MANGLE || ARGS.lint;
|
||||
|
||||
if (SCOPE_IS_NEEDED) {
|
||||
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) {
|
||||
if (COMPRESS) {
|
||||
time_it("squeeze", function(){
|
||||
TOPLEVEL = TOPLEVEL.transform(compressor);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (SCOPE_IS_NEEDED) {
|
||||
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(){
|
||||
if (MANGLE) time_it("mangle", function(){
|
||||
TOPLEVEL.mangle_names(MANGLE);
|
||||
});
|
||||
time_it("generate", function(){
|
||||
});
|
||||
time_it("generate", function(){
|
||||
TOPLEVEL.print(output);
|
||||
});
|
||||
});
|
||||
|
||||
output = output.get();
|
||||
output = output.get();
|
||||
|
||||
if (SOURCE_MAP) {
|
||||
if (SOURCE_MAP) {
|
||||
fs.writeFileSync(ARGS.source_map, SOURCE_MAP, "utf8");
|
||||
output += "\n/*\n//@ sourceMappingURL=" + (ARGS.source_map_url || ARGS.source_map) + "\n*/";
|
||||
}
|
||||
}
|
||||
|
||||
if (OUTPUT_FILE) {
|
||||
if (OUTPUT_FILE) {
|
||||
fs.writeFileSync(OUTPUT_FILE, output, "utf8");
|
||||
} else {
|
||||
} else {
|
||||
sys.print(output);
|
||||
sys.error("\n");
|
||||
}
|
||||
}
|
||||
|
||||
if (ARGS.stats) {
|
||||
if (ARGS.stats) {
|
||||
sys.error(UglifyJS.string_template("Timing information (compressed {count} files):", {
|
||||
count: files.length
|
||||
}));
|
||||
@@ -318,7 +324,8 @@ if (ARGS.stats) {
|
||||
time: (STATS[i] / 1000).toFixed(3)
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
/* -----[ functions ]----- */
|
||||
|
||||
@@ -363,20 +370,18 @@ function getOptions(x, constants) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
function read_whole_file(filename) {
|
||||
try {
|
||||
function read_whole_file(filename, cb) {
|
||||
if (filename == "-") {
|
||||
var chunks = [];
|
||||
do {
|
||||
var chunk = fs.readFileSync("/dev/stdin", "utf8");
|
||||
process.stdin.setEncoding('utf-8');
|
||||
process.stdin.on('data', function (chunk) {
|
||||
chunks.push(chunk);
|
||||
} while (chunk.length);
|
||||
return chunks.join("");
|
||||
}
|
||||
return fs.readFileSync(filename, "utf8");
|
||||
} catch(ex) {
|
||||
sys.error("ERROR: can't read file: " + filename);
|
||||
process.exit(1);
|
||||
}).on('end', function () {
|
||||
cb(null, chunks.join(""));
|
||||
});
|
||||
process.openStdin();
|
||||
} else {
|
||||
fs.readFile(filename, "utf-8", cb);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -15,8 +15,9 @@
|
||||
"url": "https://github.com/mishoo/UglifyJS2.git"
|
||||
}],
|
||||
"dependencies": {
|
||||
"async" : "~0.2.6",
|
||||
"source-map" : "~0.1.7",
|
||||
"optimist" : "~0.3.5"
|
||||
"optimist" : "~0.3.5",
|
||||
},
|
||||
"bin": {
|
||||
"uglifyjs" : "bin/uglifyjs"
|
||||
|
||||
Reference in New Issue
Block a user