#! /usr/bin/env node // -*- js -*- var UglifyJS = require("../tools/node"); var sys = require("util"); var optimist = require("optimist"); var fs = require("fs"); var ARGS = optimist .usage("uglifyjs2 [options] input1.js [input2.js ...]\n\ Maximum compression settings are on by default.\n\ Use a single dash to read input from the standard input.\ ") .describe("source-map", "Specify an output file where to generate source map") .describe("source-map-root", "The root of the original source to be included in the source map") .describe("p", "Skip prefix for original filenames that appear in source maps") .describe("o", "Output file (default STDOUT)") .describe("stats", "Display operations run time on STDERR") .describe("v", "Verbose") .describe("b", "Beautify output") .describe("m", "Don't mangle names") .describe("c", "Pass compressor options") .alias("p", "prefix") .alias("o", "output") .alias("v", "verbose") .alias("b", "beautify") .alias("c", "options") .alias("m", "no-mangle") .boolean("b") .boolean("v") .boolean("stats") .boolean("m") .string("c") .argv ; function normalize(o) { for (var i in o) if (o.hasOwnProperty(i) && /-/.test(i)) { o[i.replace(/-/g, "_")] = o[i]; } } normalize(ARGS); if (ARGS.h || ARGS.help) { sys.puts(optimist.help()); process.exit(0); } var COMPRESSOR_OPTIONS = {}; if (ARGS.c) { ARGS.c.replace(/^\s+|\s+$/g).split(/\s*,+\s*/).forEach(function(opt){ var a = opt.split(/\s*=\s*/); COMPRESSOR_OPTIONS[a[0]] = new Function("return(" + a[1] + ")")(); }); } var files = ARGS._.slice(); if (files.length == 0) files = [ "-" ]; if (files.indexOf("-") >= 0 && ARGS.source_map) { sys.error("ERROR: Source map doesn't work with input from STDIN"); process.exit(1); } if (files.filter(function(el){ return el == "-" }).length > 1) { sys.error("ERROR: Can read a single file from STDIN (two or more dashes specified)"); process.exit(1); } var STATS = {}; var OUTPUT_FILE = ARGS.o; var SOURCE_MAP = ARGS.source_map ? UglifyJS.SourceMap({ file: OUTPUT_FILE, root: ARGS.source_map_root }) : null; var output = UglifyJS.OutputStream({ beautify: ARGS.b, source_map: SOURCE_MAP }); files = files.map(do_file_1); files = files.map(do_file_2); UglifyJS.base54.sort(); files.forEach(do_file_3); if (ARGS.v) { sys.error("BASE54 digits: " + UglifyJS.base54.get()); //sys.error("Frequency: " + sys.inspect(UglifyJS.base54.freq())); } output = output.get(); if (SOURCE_MAP) { fs.writeFileSync(ARGS.source_map, SOURCE_MAP, "utf8"); output += "\n//@ sourceMappingURL=" + ARGS.source_map; } 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) })); } } /* -----[ functions ]----- */ function do_file_1(file) { if (ARGS.v) { sys.error("Compressing " + file); } var code = read_whole_file(file); var ast; time_it("parse", function(){ ast = UglifyJS.parse(code); }); time_it("scope", function(){ ast.figure_out_scope(); }); time_it("squeeze", function(){ var compressor = UglifyJS.Compressor(COMPRESSOR_OPTIONS); ast = ast.squeeze(compressor); }); ast.filename = file; return ast; } function do_file_2(ast) { time_it("scope", function(){ ast.figure_out_scope(); if (!ARGS.m) { ast.compute_char_frequency(); } }); return ast; } function do_file_3(ast) { var file = ast.filename; // if (ARGS.v) { // sys.error("Mangling/generating " + file); // } if (!ARGS.m) time_it("mangle", function(){ ast.mangle_names(); }); time_it("generate", function(){ if (SOURCE_MAP) { if (ARGS.p != null) { file = file.replace(/^\/+/, "").split(/\/+/).slice(ARGS.p).join("/"); } SOURCE_MAP.set_source(file); } ast.print(output); }); } function read_whole_file(filename) { if (filename == "-") { // XXX: this sucks. How does one read the whole STDIN // synchronously? filename = "/dev/stdin"; } try { return fs.readFileSync(filename, "utf8"); } catch(ex) { sys.error("ERROR: can't read file: " + filename); process.exit(1); } } function time_it(name, cont) { var t1 = new Date().getTime(); var ret = cont(); if (ARGS.stats) { var spent = new Date().getTime() - t1; if (STATS[name]) STATS[name] += spent; else STATS[name] = spent; } return ret; };