uglifyjs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. #! /usr/bin/env node
  2. // -*- js -*-
  3. "use strict";
  4. require("../tools/tty");
  5. var fs = require("fs");
  6. var info = require("../package.json");
  7. var path = require("path");
  8. var UglifyJS = require("../tools/node");
  9. var skip_keys = [ "cname", "fixed", "inlined", "parent_scope", "scope", "uses_eval", "uses_with" ];
  10. var files = {};
  11. var options = {};
  12. var short_forms = {
  13. b: "beautify",
  14. c: "compress",
  15. d: "define",
  16. e: "enclose",
  17. h: "help",
  18. m: "mangle",
  19. o: "output",
  20. O: "output-opts",
  21. p: "parse",
  22. v: "version",
  23. V: "version",
  24. };
  25. var args = process.argv.slice(2);
  26. var paths = [];
  27. var output, nameCache;
  28. var specified = {};
  29. while (args.length) {
  30. var arg = args.shift();
  31. if (arg[0] != "-") {
  32. paths.push(arg);
  33. } else if (arg == "--") {
  34. paths = paths.concat(args);
  35. break;
  36. } else if (arg[1] == "-") {
  37. process_option(arg.slice(2));
  38. } else [].forEach.call(arg.slice(1), function(letter, index, arg) {
  39. if (!(letter in short_forms)) fatal("invalid option -" + letter);
  40. process_option(short_forms[letter], index + 1 < arg.length);
  41. });
  42. }
  43. function process_option(name, no_value) {
  44. specified[name] = true;
  45. switch (name) {
  46. case "help":
  47. switch (read_value()) {
  48. case "ast":
  49. print(UglifyJS.describe_ast());
  50. break;
  51. case "options":
  52. var text = [];
  53. var toplevels = [];
  54. var padding = "";
  55. var defaults = UglifyJS.default_options();
  56. for (var name in defaults) {
  57. var option = defaults[name];
  58. if (option && typeof option == "object") {
  59. text.push("--" + ({
  60. output: "beautify",
  61. sourceMap: "source-map",
  62. }[name] || name) + " options:");
  63. text.push(format_object(option));
  64. text.push("");
  65. } else {
  66. if (padding.length < name.length) padding = Array(name.length + 1).join(" ");
  67. toplevels.push([ {
  68. keep_fnames: "keep-fnames",
  69. nameCache: "name-cache",
  70. }[name] || name, option ]);
  71. }
  72. }
  73. toplevels.forEach(function(tokens) {
  74. text.push("--" + tokens[0] + padding.slice(tokens[0].length - 2) + tokens[1]);
  75. });
  76. print(text.join("\n"));
  77. break;
  78. default:
  79. print([
  80. "Usage: uglifyjs [files...] [options]",
  81. "",
  82. "Options:",
  83. " -h, --help Print usage information.",
  84. " `--help options` for details on available options.",
  85. " -v, -V, --version Print version number.",
  86. " -p, --parse <options> Specify parser options.",
  87. " -c, --compress [options] Enable compressor/specify compressor options.",
  88. " -m, --mangle [options] Mangle names/specify mangler options.",
  89. " --mangle-props [options] Mangle properties/specify mangler options.",
  90. " -b, --beautify [options] Beautify output/specify output options.",
  91. " -O, --output-opts <options> Output options (beautify disabled).",
  92. " -o, --output <file> Output file (default STDOUT).",
  93. " --annotations Process and preserve comment annotations.",
  94. " --no-annotations Ignore and discard comment annotations.",
  95. " --comments [filter] Preserve copyright comments in the output.",
  96. " --config-file <file> Read minify() options from JSON file.",
  97. " -d, --define <expr>[=value] Global definitions.",
  98. " -e, --enclose [arg[,...][:value[,...]]] Embed everything in a big function, with configurable argument(s) & value(s).",
  99. " --ie8 Support non-standard Internet Explorer 8.",
  100. " --keep-fnames Do not mangle/drop function names. Useful for code relying on Function.prototype.name.",
  101. " --name-cache <file> File to hold mangled name mappings.",
  102. " --rename Force symbol expansion.",
  103. " --no-rename Disable symbol expansion.",
  104. " --self Build UglifyJS as a library (implies --wrap UglifyJS)",
  105. " --source-map [options] Enable source map/specify source map options.",
  106. " --timings Display operations run time on STDERR.",
  107. " --toplevel Compress and/or mangle variables in toplevel scope.",
  108. " --validate Perform validation during AST manipulations.",
  109. " --verbose Print diagnostic messages.",
  110. " --warn Print warning messages.",
  111. " --webkit Support non-standard Safari/Webkit.",
  112. " --wrap <name> Embed everything as a function with “exports” corresponding to “name” globally.",
  113. "",
  114. "(internal debug use only)",
  115. " --in-situ Warning: replaces original source files with minified output.",
  116. " --reduce-test Reduce a standalone test case (assumes cloned repository).",
  117. ].join("\n"));
  118. }
  119. process.exit();
  120. case "version":
  121. print(info.name + " " + info.version);
  122. process.exit();
  123. case "config-file":
  124. var config = JSON.parse(read_file(read_value(true)));
  125. if (config.mangle && config.mangle.properties && config.mangle.properties.regex) {
  126. config.mangle.properties.regex = UglifyJS.parse(config.mangle.properties.regex, {
  127. expression: true,
  128. }).value;
  129. }
  130. for (var key in config) if (!(key in options)) options[key] = config[key];
  131. break;
  132. case "compress":
  133. case "mangle":
  134. options[name] = parse_js(read_value(), options[name]);
  135. break;
  136. case "source-map":
  137. options.sourceMap = parse_js(read_value(), options.sourceMap);
  138. break;
  139. case "enclose":
  140. options[name] = read_value();
  141. break;
  142. case "annotations":
  143. case "ie8":
  144. case "timings":
  145. case "toplevel":
  146. case "validate":
  147. case "webkit":
  148. options[name] = true;
  149. break;
  150. case "no-annotations":
  151. options.annotations = false;
  152. break;
  153. case "keep-fnames":
  154. options.keep_fnames = true;
  155. break;
  156. case "wrap":
  157. options[name] = read_value(true);
  158. break;
  159. case "verbose":
  160. options.warnings = "verbose";
  161. break;
  162. case "warn":
  163. if (!options.warnings) options.warnings = true;
  164. break;
  165. case "beautify":
  166. options.output = parse_js(read_value(), options.output);
  167. if (!("beautify" in options.output)) options.output.beautify = true;
  168. break;
  169. case "output-opts":
  170. options.output = parse_js(read_value(true), options.output);
  171. break;
  172. case "comments":
  173. if (typeof options.output != "object") options.output = {};
  174. options.output.comments = read_value();
  175. if (options.output.comments === true) options.output.comments = "some";
  176. break;
  177. case "define":
  178. if (typeof options.compress != "object") options.compress = {};
  179. options.compress.global_defs = parse_js(read_value(true), options.compress.global_defs, "define");
  180. break;
  181. case "mangle-props":
  182. if (typeof options.mangle != "object") options.mangle = {};
  183. options.mangle.properties = parse_js(read_value(), options.mangle.properties);
  184. break;
  185. case "name-cache":
  186. nameCache = read_value(true);
  187. options.nameCache = JSON.parse(read_file(nameCache, "{}"));
  188. break;
  189. case "output":
  190. output = read_value(true);
  191. break;
  192. case "parse":
  193. options.parse = parse_js(read_value(true), options.parse);
  194. break;
  195. case "rename":
  196. options.rename = true;
  197. break;
  198. case "no-rename":
  199. options.rename = false;
  200. break;
  201. case "in-situ":
  202. case "reduce-test":
  203. case "self":
  204. break;
  205. default:
  206. fatal("invalid option --" + name);
  207. }
  208. function read_value(required) {
  209. if (no_value || !args.length || args[0][0] == "-") {
  210. if (required) fatal("missing option argument for --" + name);
  211. return true;
  212. }
  213. return args.shift();
  214. }
  215. }
  216. if (!output && options.sourceMap && options.sourceMap.url != "inline") fatal("cannot write source map to STDOUT");
  217. if (specified["beautify"] && specified["output-opts"]) fatal("--beautify cannot be used with --output-opts");
  218. [ "compress", "mangle" ].forEach(function(name) {
  219. if (!(name in options)) options[name] = false;
  220. });
  221. if (options.mangle && options.mangle.properties) {
  222. if (options.mangle.properties.domprops) {
  223. delete options.mangle.properties.domprops;
  224. } else {
  225. if (typeof options.mangle.properties != "object") options.mangle.properties = {};
  226. if (!Array.isArray(options.mangle.properties.reserved)) options.mangle.properties.reserved = [];
  227. require("../tools/domprops").forEach(function(name) {
  228. UglifyJS.push_uniq(options.mangle.properties.reserved, name);
  229. });
  230. }
  231. }
  232. if (/^ast|spidermonkey$/.test(output)) {
  233. if (typeof options.output != "object") options.output = {};
  234. options.output.ast = true;
  235. options.output.code = false;
  236. }
  237. if (options.parse && (options.parse.acorn || options.parse.spidermonkey)
  238. && options.sourceMap && options.sourceMap.content == "inline") {
  239. fatal("inline source map only works with built-in parser");
  240. }
  241. if (options.warnings) {
  242. UglifyJS.AST_Node.log_function(print_error, options.warnings == "verbose");
  243. delete options.warnings;
  244. }
  245. var convert_path = function(name) {
  246. return name;
  247. };
  248. if (typeof options.sourceMap == "object" && "base" in options.sourceMap) {
  249. convert_path = function() {
  250. var base = options.sourceMap.base;
  251. delete options.sourceMap.base;
  252. return function(name) {
  253. return path.relative(base, name);
  254. };
  255. }();
  256. }
  257. if (specified["self"]) {
  258. if (paths.length) UglifyJS.AST_Node.warn("Ignoring input files since --self was passed");
  259. if (!options.wrap) options.wrap = "UglifyJS";
  260. paths = UglifyJS.FILES;
  261. }
  262. if (specified["in-situ"]) {
  263. if (output && output != "spidermonkey" || specified["reduce-test"] || specified["self"]) {
  264. fatal("incompatible options specified");
  265. }
  266. paths.forEach(function(name) {
  267. print(name);
  268. if (/^ast|spidermonkey$/.test(name)) fatal("invalid file name specified");
  269. files = {};
  270. files[convert_path(name)] = read_file(name);
  271. output = name;
  272. run();
  273. });
  274. } else if (paths.length) {
  275. simple_glob(paths).forEach(function(name) {
  276. files[convert_path(name)] = read_file(name);
  277. });
  278. run();
  279. } else {
  280. var timerId = process.stdin.isTTY && process.argv.length < 3 && setTimeout(function() {
  281. print_error("Waiting for input... (use `--help` to print usage information)");
  282. }, 1500);
  283. var chunks = [];
  284. process.stdin.setEncoding("utf8");
  285. process.stdin.once("data", function() {
  286. clearTimeout(timerId);
  287. }).on("data", function(chunk) {
  288. chunks.push(chunk);
  289. }).on("end", function() {
  290. files = { STDIN: chunks.join("") };
  291. run();
  292. });
  293. process.stdin.resume();
  294. }
  295. function convert_ast(fn) {
  296. return UglifyJS.AST_Node.from_mozilla_ast(Object.keys(files).reduce(fn, null));
  297. }
  298. function run() {
  299. var content = options.sourceMap && options.sourceMap.content;
  300. if (content && content != "inline") {
  301. UglifyJS.AST_Node.info("Using input source map: {content}", {
  302. content : content,
  303. });
  304. options.sourceMap.content = read_file(content, content);
  305. }
  306. try {
  307. if (options.parse) {
  308. if (options.parse.acorn) {
  309. var annotations = Object.create(null);
  310. files = convert_ast(function(toplevel, name) {
  311. var content = files[name];
  312. var list = annotations[name] = [];
  313. var prev = -1;
  314. return require("acorn").parse(content, {
  315. allowHashBang: true,
  316. ecmaVersion: "latest",
  317. locations: true,
  318. onComment: function(block, text, start, end) {
  319. var match = /[@#]__PURE__/.exec(text);
  320. if (!match) {
  321. if (start != prev) return;
  322. match = [ list[prev] ];
  323. }
  324. while (/\s/.test(content[end])) end++;
  325. list[end] = match[0];
  326. prev = end;
  327. },
  328. preserveParens: true,
  329. program: toplevel,
  330. sourceFile: name,
  331. sourceType: "module",
  332. });
  333. });
  334. files.walk(new UglifyJS.TreeWalker(function(node) {
  335. if (!(node instanceof UglifyJS.AST_Call)) return;
  336. var list = annotations[node.start.file];
  337. var pure = list[node.start.pos];
  338. if (!pure) {
  339. var pos = node.start.parens;
  340. if (pos) for (var i = 0; !pure && i < pos.length; i++) {
  341. pure = list[pos[i]];
  342. }
  343. }
  344. if (pure) node.pure = pure;
  345. }));
  346. } else if (options.parse.spidermonkey) {
  347. files = convert_ast(function(toplevel, name) {
  348. var obj = JSON.parse(files[name]);
  349. if (!toplevel) return obj;
  350. toplevel.body = toplevel.body.concat(obj.body);
  351. return toplevel;
  352. });
  353. }
  354. }
  355. } catch (ex) {
  356. fatal(ex);
  357. }
  358. var result;
  359. if (specified["reduce-test"]) {
  360. // load on demand - assumes cloned repository
  361. var reduce_test = require("../test/reduce");
  362. if (Object.keys(files).length != 1) fatal("can only test on a single file");
  363. result = reduce_test(files[Object.keys(files)[0]], options, {
  364. log: print_error,
  365. verbose: true,
  366. });
  367. } else {
  368. result = UglifyJS.minify(files, options);
  369. }
  370. if (result.error) {
  371. var ex = result.error;
  372. if (ex.name == "SyntaxError") {
  373. print_error("Parse error at " + ex.filename + ":" + ex.line + "," + ex.col);
  374. var file = files[ex.filename];
  375. if (file) {
  376. var col = ex.col;
  377. var lines = file.split(/\r?\n/);
  378. var line = lines[ex.line - 1];
  379. if (!line && !col) {
  380. line = lines[ex.line - 2];
  381. col = line.length;
  382. }
  383. if (line) {
  384. var limit = 70;
  385. if (col > limit) {
  386. line = line.slice(col - limit);
  387. col = limit;
  388. }
  389. print_error(line.slice(0, 80));
  390. print_error(line.slice(0, col).replace(/\S/g, " ") + "^");
  391. }
  392. }
  393. } else if (ex.defs) {
  394. print_error("Supported options:");
  395. print_error(format_object(ex.defs));
  396. }
  397. fatal(ex);
  398. } else if (output == "ast") {
  399. if (!options.compress && !options.mangle) {
  400. var toplevel = result.ast;
  401. if (!(toplevel instanceof UglifyJS.AST_Toplevel)) {
  402. if (!(toplevel instanceof UglifyJS.AST_Statement)) toplevel = new UglifyJS.AST_SimpleStatement({
  403. body: toplevel,
  404. });
  405. toplevel = new UglifyJS.AST_Toplevel({
  406. body: [ toplevel ],
  407. });
  408. }
  409. toplevel.figure_out_scope({});
  410. }
  411. print(JSON.stringify(result.ast, function(key, value) {
  412. if (value) switch (key) {
  413. case "enclosed":
  414. return value.length ? value.map(symdef) : undefined;
  415. case "functions":
  416. case "globals":
  417. case "variables":
  418. return value.size() ? value.map(symdef) : undefined;
  419. case "thedef":
  420. return symdef(value);
  421. }
  422. if (skip_key(key)) return;
  423. if (value instanceof UglifyJS.AST_Token) return;
  424. if (value instanceof UglifyJS.Dictionary) return;
  425. if (value instanceof UglifyJS.AST_Node) {
  426. var result = {
  427. _class: "AST_" + value.TYPE
  428. };
  429. value.CTOR.PROPS.forEach(function(prop) {
  430. result[prop] = value[prop];
  431. });
  432. return result;
  433. }
  434. return value;
  435. }, 2));
  436. } else if (output == "spidermonkey") {
  437. print(JSON.stringify(result.ast.to_mozilla_ast(), null, 2));
  438. } else if (output) {
  439. var code;
  440. if (result.ast) {
  441. var opts = {};
  442. for (var name in options.output) {
  443. if (!/^ast|code$/.test(name)) opts[name] = options.output[name];
  444. }
  445. code = UglifyJS.AST_Node.from_mozilla_ast(result.ast.to_mozilla_ast()).print_to_string(opts);
  446. } else {
  447. code = result.code;
  448. }
  449. fs.writeFileSync(output, code);
  450. if (result.map) fs.writeFileSync(output + ".map", result.map);
  451. } else {
  452. print(result.code);
  453. }
  454. if (nameCache) fs.writeFileSync(nameCache, JSON.stringify(options.nameCache));
  455. if (result.timings) for (var phase in result.timings) {
  456. print_error("- " + phase + ": " + result.timings[phase].toFixed(3) + "s");
  457. }
  458. }
  459. function fatal(message) {
  460. if (message instanceof Error) {
  461. message = message.stack.replace(/^\S*?Error:/, "ERROR:")
  462. } else {
  463. message = "ERROR: " + message;
  464. }
  465. print_error(message);
  466. process.exit(1);
  467. }
  468. // A file glob function that only supports "*" and "?" wildcards in the basename.
  469. // Example: "foo/bar/*baz??.*.js"
  470. // Argument `glob` may be a string or an array of strings.
  471. // Returns an array of strings. Garbage in, garbage out.
  472. function simple_glob(glob) {
  473. if (Array.isArray(glob)) {
  474. return [].concat.apply([], glob.map(simple_glob));
  475. }
  476. if (glob.match(/\*|\?/)) {
  477. var dir = path.dirname(glob);
  478. try {
  479. var entries = fs.readdirSync(dir);
  480. } catch (ex) {}
  481. if (entries) {
  482. var pattern = "^" + path.basename(glob)
  483. .replace(/[.+^$[\]\\(){}]/g, "\\$&")
  484. .replace(/\*/g, "[^/\\\\]*")
  485. .replace(/\?/g, "[^/\\\\]") + "$";
  486. var mod = process.platform === "win32" ? "i" : "";
  487. var rx = new RegExp(pattern, mod);
  488. var results = entries.sort().filter(function(name) {
  489. return rx.test(name);
  490. }).map(function(name) {
  491. return path.join(dir, name);
  492. });
  493. if (results.length) return results;
  494. }
  495. }
  496. return [ glob ];
  497. }
  498. function read_file(path, default_value) {
  499. try {
  500. return fs.readFileSync(path, "utf8");
  501. } catch (ex) {
  502. if (ex.code == "ENOENT" && default_value != null) return default_value;
  503. fatal(ex);
  504. }
  505. }
  506. function parse_js(value, options, flag) {
  507. if (!options || typeof options != "object") options = {};
  508. if (typeof value == "string") try {
  509. UglifyJS.parse(value, {
  510. expression: true
  511. }).walk(new UglifyJS.TreeWalker(function(node) {
  512. if (node instanceof UglifyJS.AST_Assign) {
  513. var name = node.left.print_to_string();
  514. var value = node.right;
  515. if (flag) {
  516. options[name] = value;
  517. } else if (value instanceof UglifyJS.AST_Array) {
  518. options[name] = value.elements.map(to_string);
  519. } else {
  520. options[name] = to_string(value);
  521. }
  522. return true;
  523. }
  524. if (node instanceof UglifyJS.AST_Symbol || node instanceof UglifyJS.AST_PropAccess) {
  525. var name = node.print_to_string();
  526. options[name] = true;
  527. return true;
  528. }
  529. if (!(node instanceof UglifyJS.AST_Sequence)) throw node;
  530. function to_string(value) {
  531. return value instanceof UglifyJS.AST_Constant ? value.value : value.print_to_string({
  532. quote_keys: true
  533. });
  534. }
  535. }));
  536. } catch (ex) {
  537. if (flag) {
  538. fatal("cannot parse arguments for '" + flag + "': " + value);
  539. } else {
  540. options[value] = null;
  541. }
  542. }
  543. return options;
  544. }
  545. function skip_key(key) {
  546. return skip_keys.indexOf(key) >= 0;
  547. }
  548. function symdef(def) {
  549. var ret = (1e6 + def.id) + " " + def.name;
  550. if (def.mangled_name) ret += " " + def.mangled_name;
  551. return ret;
  552. }
  553. function format_object(obj) {
  554. var lines = [];
  555. var padding = "";
  556. Object.keys(obj).map(function(name) {
  557. if (padding.length < name.length) padding = Array(name.length + 1).join(" ");
  558. return [ name, JSON.stringify(obj[name]) ];
  559. }).forEach(function(tokens) {
  560. lines.push(" " + tokens[0] + padding.slice(tokens[0].length - 2) + tokens[1]);
  561. });
  562. return lines.join("\n");
  563. }
  564. function print_error(msg) {
  565. process.stderr.write(msg);
  566. process.stderr.write("\n");
  567. }
  568. function print(txt) {
  569. process.stdout.write(txt);
  570. process.stdout.write("\n");
  571. }