Tizen 2.0 Release
[platform/framework/web/web-ui-fw.git] / libs / js / jquery-mobile-1.2.0 / node_modules / grunt / node_modules / nodeunit / node_modules / tap / node_modules / runforcover / node_modules / bunker / node_modules / burrito / node_modules / uglify-js / bin / uglifyjs
1 #! /usr/bin/env node
2 // -*- js -*-
3
4 global.sys = require(/^v0\.[012]/.test(process.version) ? "sys" : "util");
5 var fs = require("fs");
6 var uglify = require("uglify-js"), // symlink ~/.node_libraries/uglify-js.js to ../uglify-js.js
7     jsp = uglify.parser,
8     pro = uglify.uglify;
9
10 var options = {
11         ast: false,
12         mangle: true,
13         mangle_toplevel: false,
14         squeeze: true,
15         make_seqs: true,
16         dead_code: true,
17         verbose: false,
18         show_copyright: true,
19         out_same_file: false,
20         max_line_length: 32 * 1024,
21         unsafe: false,
22         reserved_names: null,
23         defines: { },
24         lift_vars: false,
25         codegen_options: {
26                 ascii_only: false,
27                 beautify: false,
28                 indent_level: 4,
29                 indent_start: 0,
30                 quote_keys: false,
31                 space_colon: false,
32                 inline_script: false
33         },
34         make: false,
35         output: true            // stdout
36 };
37
38 var args = jsp.slice(process.argv, 2);
39 var filename;
40
41 out: while (args.length > 0) {
42         var v = args.shift();
43         switch (v) {
44             case "-b":
45             case "--beautify":
46                 options.codegen_options.beautify = true;
47                 break;
48             case "-i":
49             case "--indent":
50                 options.codegen_options.indent_level = args.shift();
51                 break;
52             case "-q":
53             case "--quote-keys":
54                 options.codegen_options.quote_keys = true;
55                 break;
56             case "-mt":
57             case "--mangle-toplevel":
58                 options.mangle_toplevel = true;
59                 break;
60             case "--no-mangle":
61             case "-nm":
62                 options.mangle = false;
63                 break;
64             case "--no-squeeze":
65             case "-ns":
66                 options.squeeze = false;
67                 break;
68             case "--no-seqs":
69                 options.make_seqs = false;
70                 break;
71             case "--no-dead-code":
72                 options.dead_code = false;
73                 break;
74             case "--no-copyright":
75             case "-nc":
76                 options.show_copyright = false;
77                 break;
78             case "-o":
79             case "--output":
80                 options.output = args.shift();
81                 break;
82             case "--overwrite":
83                 options.out_same_file = true;
84                 break;
85             case "-v":
86             case "--verbose":
87                 options.verbose = true;
88                 break;
89             case "--ast":
90                 options.ast = true;
91                 break;
92             case "--unsafe":
93                 options.unsafe = true;
94                 break;
95             case "--max-line-len":
96                 options.max_line_length = parseInt(args.shift(), 10);
97                 break;
98             case "--reserved-names":
99                 options.reserved_names = args.shift().split(",");
100                 break;
101             case "--lift-vars":
102                 options.lift_vars = true;
103                 break;
104             case "-d":
105             case "--define":
106                  var defarg = args.shift();
107                  try {
108                      var defsym = function(sym) {
109                              // KEYWORDS_ATOM doesn't include NaN or Infinity - should we check
110                              // for them too ?? We don't check reserved words and the like as the
111                              // define values are only substituted AFTER parsing
112                              if (jsp.KEYWORDS_ATOM.hasOwnProperty(sym)) {
113                                  throw "Don't define values for inbuilt constant '"+sym+"'";
114                              }
115                              return sym;
116                          },
117                          defval = function(v) {
118                              if (v.match(/^"(.*)"$/) || v.match(/^'(.*)'$/)) {
119                                  return [ "string", RegExp.$1 ];
120                              }
121                              else if (!isNaN(parseFloat(v))) {
122                                  return [ "num", parseFloat(v) ];
123                              }
124                              else if (v.match(/^[a-z\$_][a-z\$_0-9]*$/i)) {
125                                  return [ "name", v ];
126                              }
127                              else if (!v.match(/"/)) {
128                                  return [ "string", v ];
129                              }
130                              else if (!v.match(/'/)) {
131                                  return [ "string", v ];
132                              }
133                              throw "Can't understand the specified value: "+v;
134                          };
135                      if (defarg.match(/^([a-z_\$][a-z_\$0-9]*)(=(.*))?$/i)) {
136                          var sym = defsym(RegExp.$1),
137                              val = RegExp.$2 ? defval(RegExp.$2.substr(1)) : [ 'name', 'true' ];
138                          options.defines[sym] = val;
139                      }
140                      else {
141                          throw "The --define option expects SYMBOL[=value]";
142                      }
143                  } catch(ex) {
144                      sys.print("ERROR: In option --define "+defarg+"\n"+ex+"\n");
145                      process.exit(1);
146                  }
147                  break;
148             case "--define-from-module":
149                 var defmodarg = args.shift(),
150                     defmodule = require(defmodarg),
151                     sym,
152                     val;
153                 for (sym in defmodule) {
154                     if (defmodule.hasOwnProperty(sym)) {
155                         options.defines[sym] = function(val) {
156                             if (typeof val == "string")
157                                 return [ "string", val ];
158                             if (typeof val == "number")
159                                 return [ "num", val ];
160                             if (val === true)
161                                 return [ 'name', 'true' ];
162                             if (val === false)
163                                 return [ 'name', 'false' ];
164                             if (val === null)
165                                 return [ 'name', 'null' ];
166                             if (val === undefined)
167                                 return [ 'name', 'undefined' ];
168                             sys.print("ERROR: In option --define-from-module "+defmodarg+"\n");
169                             sys.print("ERROR: Unknown object type for: "+sym+"="+val+"\n");
170                             process.exit(1);
171                             return null;
172                         }(defmodule[sym]);
173                     }
174                 }
175                 break;
176             case "--ascii":
177                 options.codegen_options.ascii_only = true;
178                 break;
179             case "--make":
180                 options.make = true;
181                 break;
182             case "--inline-script":
183                 options.codegen_options.inline_script = true;
184                 break;
185             default:
186                 filename = v;
187                 break out;
188         }
189 }
190
191 if (options.verbose) {
192         pro.set_logger(function(msg){
193                 sys.debug(msg);
194         });
195 }
196
197 jsp.set_logger(function(msg){
198         sys.debug(msg);
199 });
200
201 if (options.make) {
202         options.out_same_file = false; // doesn't make sense in this case
203         var makefile = JSON.parse(fs.readFileSync(filename || "Makefile.uglify.js").toString());
204         output(makefile.files.map(function(file){
205                 var code = fs.readFileSync(file.name);
206                 if (file.module) {
207                         code = "!function(exports, global){global = this;\n" + code + "\n;this." + file.module + " = exports;}({})";
208                 }
209                 else if (file.hide) {
210                         code = "(function(){" + code + "}());";
211                 }
212                 return squeeze_it(code);
213         }).join("\n"));
214 }
215 else if (filename) {
216         fs.readFile(filename, "utf8", function(err, text){
217                 if (err) throw err;
218                 output(squeeze_it(text));
219         });
220 }
221 else {
222         var stdin = process.openStdin();
223         stdin.setEncoding("utf8");
224         var text = "";
225         stdin.on("data", function(chunk){
226                 text += chunk;
227         });
228         stdin.on("end", function() {
229                 output(squeeze_it(text));
230         });
231 }
232
233 function output(text) {
234         var out;
235         if (options.out_same_file && filename)
236                 options.output = filename;
237         if (options.output === true) {
238                 out = process.stdout;
239         } else {
240                 out = fs.createWriteStream(options.output, {
241                         flags: "w",
242                         encoding: "utf8",
243                         mode: 0644
244                 });
245         }
246         out.write(text + ";");
247         if (options.output !== true) {
248                 out.end();
249         }
250 };
251
252 // --------- main ends here.
253
254 function show_copyright(comments) {
255         var ret = "";
256         for (var i = 0; i < comments.length; ++i) {
257                 var c = comments[i];
258                 if (c.type == "comment1") {
259                         ret += "//" + c.value + "\n";
260                 } else {
261                         ret += "/*" + c.value + "*/";
262                 }
263         }
264         return ret;
265 };
266
267 function squeeze_it(code) {
268         var result = "";
269         if (options.show_copyright) {
270                 var tok = jsp.tokenizer(code), c;
271                 c = tok();
272                 result += show_copyright(c.comments_before);
273         }
274         try {
275                 var ast = time_it("parse", function(){ return jsp.parse(code); });
276                 if (options.lift_vars) {
277                         ast = time_it("lift", function(){ return pro.ast_lift_variables(ast); });
278                 }
279                 if (options.mangle) ast = time_it("mangle", function(){
280                         return pro.ast_mangle(ast, {
281                                 toplevel: options.mangle_toplevel,
282                                 defines: options.defines,
283                                 except: options.reserved_names
284                         });
285                 });
286                 if (options.squeeze) ast = time_it("squeeze", function(){
287                         ast = pro.ast_squeeze(ast, {
288                                 make_seqs  : options.make_seqs,
289                                 dead_code  : options.dead_code,
290                                 keep_comps : !options.unsafe
291                         });
292                         if (options.unsafe)
293                                 ast = pro.ast_squeeze_more(ast);
294                         return ast;
295                 });
296                 if (options.ast)
297                         return sys.inspect(ast, null, null);
298                 result += time_it("generate", function(){ return pro.gen_code(ast, options.codegen_options) });
299                 if (!options.codegen_options.beautify && options.max_line_length) {
300                         result = time_it("split", function(){ return pro.split_lines(result, options.max_line_length) });
301                 }
302                 return result;
303         } catch(ex) {
304                 sys.debug(ex.stack);
305                 sys.debug(sys.inspect(ex));
306                 sys.debug(JSON.stringify(ex));
307                 process.exit(1);
308         }
309 };
310
311 function time_it(name, cont) {
312         if (!options.verbose)
313                 return cont();
314         var t1 = new Date().getTime();
315         try { return cont(); }
316         finally { sys.debug("// " + name + ": " + ((new Date().getTime() - t1) / 1000).toFixed(3) + " sec."); }
317 };