cli.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. import { minify, _default_options } from "../main.js";
  2. import { parse } from "./parse.js";
  3. import {
  4. AST_Assign,
  5. AST_Array,
  6. AST_Constant,
  7. AST_Node,
  8. AST_PropAccess,
  9. AST_RegExp,
  10. AST_Sequence,
  11. AST_Symbol,
  12. AST_Token,
  13. walk
  14. } from "./ast.js";
  15. import { OutputStream } from "./output.js";
  16. export async function run_cli({ program, packageJson, fs, path }) {
  17. const skip_keys = new Set([ "cname", "parent_scope", "scope", "uses_eval", "uses_with" ]);
  18. var files = {};
  19. var options = {
  20. compress: false,
  21. mangle: false
  22. };
  23. const default_options = await _default_options();
  24. program.version(packageJson.name + " " + packageJson.version);
  25. program.parseArgv = program.parse;
  26. program.parse = undefined;
  27. if (process.argv.includes("ast")) program.helpInformation = describe_ast;
  28. else if (process.argv.includes("options")) program.helpInformation = function() {
  29. var text = [];
  30. for (var option in default_options) {
  31. text.push("--" + (option === "sourceMap" ? "source-map" : option) + " options:");
  32. text.push(format_object(default_options[option]));
  33. text.push("");
  34. }
  35. return text.join("\n");
  36. };
  37. program.option("-p, --parse <options>", "Specify parser options.", parse_js());
  38. program.option("-c, --compress [options]", "Enable compressor/specify compressor options.", parse_js());
  39. program.option("-m, --mangle [options]", "Mangle names/specify mangler options.", parse_js());
  40. program.option("--mangle-props [options]", "Mangle properties/specify mangler options.", parse_js());
  41. program.option("-f, --format [options]", "Format options.", parse_js());
  42. program.option("-b, --beautify [options]", "Alias for --format beautify=true.", parse_js());
  43. program.option("-o, --output <file>", "Output file (default STDOUT).");
  44. program.option("--comments [filter]", "Preserve copyright comments in the output.");
  45. program.option("--config-file <file>", "Read minify() options from JSON file.");
  46. program.option("-d, --define <expr>[=value]", "Global definitions.", parse_js("define"));
  47. program.option("--ecma <version>", "Specify ECMAScript release: 5, 2015, 2016 or 2017...");
  48. program.option("-e, --enclose [arg[,...][:value[,...]]]", "Embed output in a big function with configurable arguments and values.");
  49. program.option("--ie8", "Support non-standard Internet Explorer 8.");
  50. program.option("--keep-classnames", "Do not mangle/drop class names.");
  51. program.option("--keep-fnames", "Do not mangle/drop function names. Useful for code relying on Function.prototype.name.");
  52. program.option("--module", "Input is an ES6 module");
  53. program.option("--name-cache <file>", "File to hold mangled name mappings.");
  54. program.option("--rename", "Force symbol expansion.");
  55. program.option("--no-rename", "Disable symbol expansion.");
  56. program.option("--safari10", "Support non-standard Safari 10.");
  57. program.option("--source-map [options]", "Enable source map/specify source map options.", parse_js());
  58. program.option("--timings", "Display operations run time on STDERR.");
  59. program.option("--toplevel", "Compress and/or mangle variables in toplevel scope.");
  60. program.option("--wrap <name>", "Embed everything as a function with “exports” corresponding to “name” globally.");
  61. program.arguments("[files...]").parseArgv(process.argv);
  62. if (program.configFile) {
  63. options = JSON.parse(read_file(program.configFile));
  64. }
  65. if (!program.output && program.sourceMap && program.sourceMap.url != "inline") {
  66. fatal("ERROR: cannot write source map to STDOUT");
  67. }
  68. [
  69. "compress",
  70. "enclose",
  71. "ie8",
  72. "mangle",
  73. "module",
  74. "safari10",
  75. "sourceMap",
  76. "toplevel",
  77. "wrap"
  78. ].forEach(function(name) {
  79. if (name in program) {
  80. options[name] = program[name];
  81. }
  82. });
  83. if ("ecma" in program) {
  84. if (program.ecma != (program.ecma | 0)) fatal("ERROR: ecma must be an integer");
  85. const ecma = program.ecma | 0;
  86. if (ecma > 5 && ecma < 2015)
  87. options.ecma = ecma + 2009;
  88. else
  89. options.ecma = ecma;
  90. }
  91. if (program.beautify || program.format) {
  92. if (program.beautify && program.format) {
  93. fatal("Please only specify one of --beautify or --format");
  94. }
  95. if (program.beautify) {
  96. options.format = typeof program.beautify == "object" ? program.beautify : {};
  97. if (!("beautify" in options.format)) {
  98. options.format.beautify = true;
  99. }
  100. }
  101. if (program.format) {
  102. options.format = typeof program.format == "object" ? program.format : {};
  103. }
  104. }
  105. if (program.comments) {
  106. if (typeof options.format != "object") options.format = {};
  107. options.format.comments = typeof program.comments == "string" ? (program.comments == "false" ? false : program.comments) : "some";
  108. }
  109. if (program.define) {
  110. if (typeof options.compress != "object") options.compress = {};
  111. if (typeof options.compress.global_defs != "object") options.compress.global_defs = {};
  112. for (var expr in program.define) {
  113. options.compress.global_defs[expr] = program.define[expr];
  114. }
  115. }
  116. if (program.keepClassnames) {
  117. options.keep_classnames = true;
  118. }
  119. if (program.keepFnames) {
  120. options.keep_fnames = true;
  121. }
  122. if (program.mangleProps) {
  123. if (program.mangleProps.domprops) {
  124. delete program.mangleProps.domprops;
  125. } else {
  126. if (typeof program.mangleProps != "object") program.mangleProps = {};
  127. if (!Array.isArray(program.mangleProps.reserved)) program.mangleProps.reserved = [];
  128. }
  129. if (typeof options.mangle != "object") options.mangle = {};
  130. options.mangle.properties = program.mangleProps;
  131. }
  132. if (program.nameCache) {
  133. options.nameCache = JSON.parse(read_file(program.nameCache, "{}"));
  134. }
  135. if (program.output == "ast") {
  136. options.format = {
  137. ast: true,
  138. code: false
  139. };
  140. }
  141. if (program.parse) {
  142. if (!program.parse.acorn && !program.parse.spidermonkey) {
  143. options.parse = program.parse;
  144. } else if (program.sourceMap && program.sourceMap.content == "inline") {
  145. fatal("ERROR: inline source map only works with built-in parser");
  146. }
  147. }
  148. if (~program.rawArgs.indexOf("--rename")) {
  149. options.rename = true;
  150. } else if (!program.rename) {
  151. options.rename = false;
  152. }
  153. let convert_path = name => name;
  154. if (typeof program.sourceMap == "object" && "base" in program.sourceMap) {
  155. convert_path = function() {
  156. var base = program.sourceMap.base;
  157. delete options.sourceMap.base;
  158. return function(name) {
  159. return path.relative(base, name);
  160. };
  161. }();
  162. }
  163. let filesList;
  164. if (options.files && options.files.length) {
  165. filesList = options.files;
  166. delete options.files;
  167. } else if (program.args.length) {
  168. filesList = program.args;
  169. }
  170. if (filesList) {
  171. simple_glob(filesList).forEach(function(name) {
  172. files[convert_path(name)] = read_file(name);
  173. });
  174. } else {
  175. await new Promise((resolve) => {
  176. var chunks = [];
  177. process.stdin.setEncoding("utf8");
  178. process.stdin.on("data", function(chunk) {
  179. chunks.push(chunk);
  180. }).on("end", function() {
  181. files = [ chunks.join("") ];
  182. resolve();
  183. });
  184. process.stdin.resume();
  185. });
  186. }
  187. await run_cli();
  188. function convert_ast(fn) {
  189. return AST_Node.from_mozilla_ast(Object.keys(files).reduce(fn, null));
  190. }
  191. async function run_cli() {
  192. var content = program.sourceMap && program.sourceMap.content;
  193. if (content && content !== "inline") {
  194. options.sourceMap.content = read_file(content, content);
  195. }
  196. if (program.timings) options.timings = true;
  197. try {
  198. if (program.parse) {
  199. if (program.parse.acorn) {
  200. files = convert_ast(function(toplevel, name) {
  201. return require("acorn").parse(files[name], {
  202. ecmaVersion: 2018,
  203. locations: true,
  204. program: toplevel,
  205. sourceFile: name,
  206. sourceType: options.module || program.parse.module ? "module" : "script"
  207. });
  208. });
  209. } else if (program.parse.spidermonkey) {
  210. files = convert_ast(function(toplevel, name) {
  211. var obj = JSON.parse(files[name]);
  212. if (!toplevel) return obj;
  213. toplevel.body = toplevel.body.concat(obj.body);
  214. return toplevel;
  215. });
  216. }
  217. }
  218. } catch (ex) {
  219. fatal(ex);
  220. }
  221. let result;
  222. try {
  223. result = await minify(files, options);
  224. } catch (ex) {
  225. if (ex.name == "SyntaxError") {
  226. print_error("Parse error at " + ex.filename + ":" + ex.line + "," + ex.col);
  227. var col = ex.col;
  228. var lines = files[ex.filename].split(/\r?\n/);
  229. var line = lines[ex.line - 1];
  230. if (!line && !col) {
  231. line = lines[ex.line - 2];
  232. col = line.length;
  233. }
  234. if (line) {
  235. var limit = 70;
  236. if (col > limit) {
  237. line = line.slice(col - limit);
  238. col = limit;
  239. }
  240. print_error(line.slice(0, 80));
  241. print_error(line.slice(0, col).replace(/\S/g, " ") + "^");
  242. }
  243. }
  244. if (ex.defs) {
  245. print_error("Supported options:");
  246. print_error(format_object(ex.defs));
  247. }
  248. fatal(ex);
  249. return;
  250. }
  251. if (program.output == "ast") {
  252. if (!options.compress && !options.mangle) {
  253. result.ast.figure_out_scope({});
  254. }
  255. console.log(JSON.stringify(result.ast, function(key, value) {
  256. if (value) switch (key) {
  257. case "thedef":
  258. return symdef(value);
  259. case "enclosed":
  260. return value.length ? value.map(symdef) : undefined;
  261. case "variables":
  262. case "functions":
  263. case "globals":
  264. return value.size ? collect_from_map(value, symdef) : undefined;
  265. }
  266. if (skip_keys.has(key)) return;
  267. if (value instanceof AST_Token) return;
  268. if (value instanceof Map) return;
  269. if (value instanceof AST_Node) {
  270. var result = {
  271. _class: "AST_" + value.TYPE
  272. };
  273. if (value.block_scope) {
  274. result.variables = value.block_scope.variables;
  275. result.functions = value.block_scope.functions;
  276. result.enclosed = value.block_scope.enclosed;
  277. }
  278. value.CTOR.PROPS.forEach(function(prop) {
  279. result[prop] = value[prop];
  280. });
  281. return result;
  282. }
  283. return value;
  284. }, 2));
  285. } else if (program.output == "spidermonkey") {
  286. try {
  287. const minified = await minify(result.code, {
  288. compress: false,
  289. mangle: false,
  290. format: {
  291. ast: true,
  292. code: false
  293. }
  294. });
  295. console.log(JSON.stringify(minified.ast.to_mozilla_ast(), null, 2));
  296. } catch (ex) {
  297. fatal(ex);
  298. return;
  299. }
  300. } else if (program.output) {
  301. fs.writeFileSync(program.output, result.code);
  302. if (options.sourceMap && options.sourceMap.url !== "inline" && result.map) {
  303. fs.writeFileSync(program.output + ".map", result.map);
  304. }
  305. } else {
  306. console.log(result.code);
  307. }
  308. if (program.nameCache) {
  309. fs.writeFileSync(program.nameCache, JSON.stringify(options.nameCache));
  310. }
  311. if (result.timings) for (var phase in result.timings) {
  312. print_error("- " + phase + ": " + result.timings[phase].toFixed(3) + "s");
  313. }
  314. }
  315. function fatal(message) {
  316. if (message instanceof Error) message = message.stack.replace(/^\S*?Error:/, "ERROR:");
  317. print_error(message);
  318. process.exit(1);
  319. }
  320. // A file glob function that only supports "*" and "?" wildcards in the basename.
  321. // Example: "foo/bar/*baz??.*.js"
  322. // Argument `glob` may be a string or an array of strings.
  323. // Returns an array of strings. Garbage in, garbage out.
  324. function simple_glob(glob) {
  325. if (Array.isArray(glob)) {
  326. return [].concat.apply([], glob.map(simple_glob));
  327. }
  328. if (glob && glob.match(/[*?]/)) {
  329. var dir = path.dirname(glob);
  330. try {
  331. var entries = fs.readdirSync(dir);
  332. } catch (ex) {}
  333. if (entries) {
  334. var pattern = "^" + path.basename(glob)
  335. .replace(/[.+^$[\]\\(){}]/g, "\\$&")
  336. .replace(/\*/g, "[^/\\\\]*")
  337. .replace(/\?/g, "[^/\\\\]") + "$";
  338. var mod = process.platform === "win32" ? "i" : "";
  339. var rx = new RegExp(pattern, mod);
  340. var results = entries.filter(function(name) {
  341. return rx.test(name);
  342. }).map(function(name) {
  343. return path.join(dir, name);
  344. });
  345. if (results.length) return results;
  346. }
  347. }
  348. return [ glob ];
  349. }
  350. function read_file(path, default_value) {
  351. try {
  352. return fs.readFileSync(path, "utf8");
  353. } catch (ex) {
  354. if ((ex.code == "ENOENT" || ex.code == "ENAMETOOLONG") && default_value != null) return default_value;
  355. fatal(ex);
  356. }
  357. }
  358. function parse_js(flag) {
  359. return function(value, options) {
  360. options = options || {};
  361. try {
  362. walk(parse(value, { expression: true }), node => {
  363. if (node instanceof AST_Assign) {
  364. var name = node.left.print_to_string();
  365. var value = node.right;
  366. if (flag) {
  367. options[name] = value;
  368. } else if (value instanceof AST_Array) {
  369. options[name] = value.elements.map(to_string);
  370. } else if (value instanceof AST_RegExp) {
  371. value = value.value;
  372. options[name] = new RegExp(value.source, value.flags);
  373. } else {
  374. options[name] = to_string(value);
  375. }
  376. return true;
  377. }
  378. if (node instanceof AST_Symbol || node instanceof AST_PropAccess) {
  379. var name = node.print_to_string();
  380. options[name] = true;
  381. return true;
  382. }
  383. if (!(node instanceof AST_Sequence)) throw node;
  384. function to_string(value) {
  385. return value instanceof AST_Constant ? value.getValue() : value.print_to_string({
  386. quote_keys: true
  387. });
  388. }
  389. });
  390. } catch(ex) {
  391. if (flag) {
  392. fatal("Error parsing arguments for '" + flag + "': " + value);
  393. } else {
  394. options[value] = null;
  395. }
  396. }
  397. return options;
  398. };
  399. }
  400. function symdef(def) {
  401. var ret = (1e6 + def.id) + " " + def.name;
  402. if (def.mangled_name) ret += " " + def.mangled_name;
  403. return ret;
  404. }
  405. function collect_from_map(map, callback) {
  406. var result = [];
  407. map.forEach(function (def) {
  408. result.push(callback(def));
  409. });
  410. return result;
  411. }
  412. function format_object(obj) {
  413. var lines = [];
  414. var padding = "";
  415. Object.keys(obj).map(function(name) {
  416. if (padding.length < name.length) padding = Array(name.length + 1).join(" ");
  417. return [ name, JSON.stringify(obj[name]) ];
  418. }).forEach(function(tokens) {
  419. lines.push(" " + tokens[0] + padding.slice(tokens[0].length - 2) + tokens[1]);
  420. });
  421. return lines.join("\n");
  422. }
  423. function print_error(msg) {
  424. process.stderr.write(msg);
  425. process.stderr.write("\n");
  426. }
  427. function describe_ast() {
  428. var out = OutputStream({ beautify: true });
  429. function doitem(ctor) {
  430. out.print("AST_" + ctor.TYPE);
  431. const props = ctor.SELF_PROPS.filter(prop => !/^\$/.test(prop));
  432. if (props.length > 0) {
  433. out.space();
  434. out.with_parens(function() {
  435. props.forEach(function(prop, i) {
  436. if (i) out.space();
  437. out.print(prop);
  438. });
  439. });
  440. }
  441. if (ctor.documentation) {
  442. out.space();
  443. out.print_string(ctor.documentation);
  444. }
  445. if (ctor.SUBCLASSES.length > 0) {
  446. out.space();
  447. out.with_block(function() {
  448. ctor.SUBCLASSES.forEach(function(ctor) {
  449. out.indent();
  450. doitem(ctor);
  451. out.newline();
  452. });
  453. });
  454. }
  455. }
  456. doitem(AST_Node);
  457. return out + "\n";
  458. }
  459. }