4 global.sys = require(/^v0\.[012]/.test(process.version) ? "sys" : "util");
5 var fs = require("fs"),
6 jsp = require("./lib/parse-js"),
7 pro = require("./lib/process");
9 pro.set_logger(function(msg){
16 mangle_toplevel: false,
25 unsafe: false, // XXX: extra & unsafe? but maybe we don't want both, so....
32 output: true // stdout
35 var args = jsp.slice(process.argv, 2);
38 out: while (args.length > 0) {
43 options.beautify = true;
47 options.beautify_options.indent_level = args.shift();
51 options.beautify_options.quote_keys = true;
54 case "--mangle-toplevel":
55 options.mangle_toplevel = true;
59 options.mangle = false;
63 options.squeeze = false;
66 options.make_seqs = false;
68 case "--no-dead-code":
69 options.dead_code = false;
71 case "--no-copyright":
73 options.show_copyright = false;
77 options.output = args.shift();
80 options.out_same_file = true;
84 options.verbose = true;
93 options.unsafe = true;
102 fs.readFile(filename, "utf8", function(err, text){
106 output(squeeze_it(text));
109 var stdin = process.openStdin();
110 stdin.setEncoding("utf8");
112 stdin.on("data", function(chunk){
115 stdin.on("end", function() {
116 output(squeeze_it(text));
120 function output(text) {
122 if (options.out_same_file && filename)
123 options.output = filename;
124 if (options.output === true) {
125 out = process.stdout;
127 out = fs.createWriteStream(options.output, {
137 // --------- main ends here.
139 function show_copyright(comments) {
141 for (var i = 0; i < comments.length; ++i) {
143 if (c.type == "comment1") {
144 ret += "//" + c.value + "\n";
146 ret += "/*" + c.value + "*/";
152 function squeeze_it(code) {
154 if (options.show_copyright) {
155 var initial_comments = [];
156 // keep first comment
157 var tok = jsp.tokenizer(code, false), c;
160 while (/^comment/.test(c.type) && (!prev || prev == c.type)) {
161 initial_comments.push(c);
165 result += show_copyright(initial_comments);
168 var ast = time_it("parse", function(){ return jsp.parse(code); });
170 ast = time_it("mangle", function(){ return pro.ast_mangle(ast, options.mangle_toplevel); });
172 ast = time_it("squeeze", function(){
173 ast = pro.ast_squeeze(ast, {
174 make_seqs : options.make_seqs,
175 dead_code : options.dead_code,
176 extra : options.extra
179 ast = pro.ast_squeeze_more(ast);
183 return sys.inspect(ast, null, null);
184 result += time_it("generate", function(){ return pro.gen_code(ast, options.beautify && options.beautify_options) });
188 sys.debug(sys.inspect(ex));
189 sys.debug(JSON.stringify(ex));
193 function time_it(name, cont) {
194 if (!options.verbose)
196 var t1 = new Date().getTime();
197 try { return cont(); }
198 finally { sys.debug("// " + name + ": " + ((new Date().getTime() - t1) / 1000).toFixed(3) + " sec."); }