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