uglifyjs 23 KB

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