uglifyjs 23 KB

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