cli.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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.", 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.format || program.beautify) {
  92. const chosenOption = program.format || program.beautify;
  93. options.format = typeof chosenOption === "object" ? chosenOption : {};
  94. }
  95. if (program.comments) {
  96. if (typeof options.format != "object") options.format = {};
  97. options.format.comments = typeof program.comments == "string" ? (program.comments == "false" ? false : program.comments) : "some";
  98. }
  99. if (program.define) {
  100. if (typeof options.compress != "object") options.compress = {};
  101. if (typeof options.compress.global_defs != "object") options.compress.global_defs = {};
  102. for (var expr in program.define) {
  103. options.compress.global_defs[expr] = program.define[expr];
  104. }
  105. }
  106. if (program.keepClassnames) {
  107. options.keep_classnames = true;
  108. }
  109. if (program.keepFnames) {
  110. options.keep_fnames = true;
  111. }
  112. if (program.mangleProps) {
  113. if (program.mangleProps.domprops) {
  114. delete program.mangleProps.domprops;
  115. } else {
  116. if (typeof program.mangleProps != "object") program.mangleProps = {};
  117. if (!Array.isArray(program.mangleProps.reserved)) program.mangleProps.reserved = [];
  118. }
  119. if (typeof options.mangle != "object") options.mangle = {};
  120. options.mangle.properties = program.mangleProps;
  121. }
  122. if (program.nameCache) {
  123. options.nameCache = JSON.parse(read_file(program.nameCache, "{}"));
  124. }
  125. if (program.output == "ast") {
  126. options.format = {
  127. ast: true,
  128. code: false
  129. };
  130. }
  131. if (program.parse) {
  132. if (!program.parse.acorn && !program.parse.spidermonkey) {
  133. options.parse = program.parse;
  134. } else if (program.sourceMap && program.sourceMap.content == "inline") {
  135. fatal("ERROR: inline source map only works with built-in parser");
  136. }
  137. }
  138. if (~program.rawArgs.indexOf("--rename")) {
  139. options.rename = true;
  140. } else if (!program.rename) {
  141. options.rename = false;
  142. }
  143. let convert_path = name => name;
  144. if (typeof program.sourceMap == "object" && "base" in program.sourceMap) {
  145. convert_path = function() {
  146. var base = program.sourceMap.base;
  147. delete options.sourceMap.base;
  148. return function(name) {
  149. return path.relative(base, name);
  150. };
  151. }();
  152. }
  153. let filesList;
  154. if (options.files && options.files.length) {
  155. filesList = options.files;
  156. delete options.files;
  157. } else if (program.args.length) {
  158. filesList = program.args;
  159. }
  160. if (filesList) {
  161. simple_glob(filesList).forEach(function(name) {
  162. files[convert_path(name)] = read_file(name);
  163. });
  164. } else {
  165. await new Promise((resolve) => {
  166. var chunks = [];
  167. process.stdin.setEncoding("utf8");
  168. process.stdin.on("data", function(chunk) {
  169. chunks.push(chunk);
  170. }).on("end", function() {
  171. files = [ chunks.join("") ];
  172. resolve();
  173. });
  174. process.stdin.resume();
  175. });
  176. }
  177. await run_cli();
  178. function convert_ast(fn) {
  179. return AST_Node.from_mozilla_ast(Object.keys(files).reduce(fn, null));
  180. }
  181. async function run_cli() {
  182. var content = program.sourceMap && program.sourceMap.content;
  183. if (content && content !== "inline") {
  184. options.sourceMap.content = read_file(content, content);
  185. }
  186. if (program.timings) options.timings = true;
  187. try {
  188. if (program.parse) {
  189. if (program.parse.acorn) {
  190. files = convert_ast(function(toplevel, name) {
  191. return require("acorn").parse(files[name], {
  192. ecmaVersion: 2018,
  193. locations: true,
  194. program: toplevel,
  195. sourceFile: name,
  196. sourceType: options.module || program.parse.module ? "module" : "script"
  197. });
  198. });
  199. } else if (program.parse.spidermonkey) {
  200. files = convert_ast(function(toplevel, name) {
  201. var obj = JSON.parse(files[name]);
  202. if (!toplevel) return obj;
  203. toplevel.body = toplevel.body.concat(obj.body);
  204. return toplevel;
  205. });
  206. }
  207. }
  208. } catch (ex) {
  209. fatal(ex);
  210. }
  211. let result;
  212. try {
  213. result = await minify(files, options, fs);
  214. } catch (ex) {
  215. if (ex.name == "SyntaxError") {
  216. print_error("Parse error at " + ex.filename + ":" + ex.line + "," + ex.col);
  217. var col = ex.col;
  218. var lines = files[ex.filename].split(/\r?\n/);
  219. var line = lines[ex.line - 1];
  220. if (!line && !col) {
  221. line = lines[ex.line - 2];
  222. col = line.length;
  223. }
  224. if (line) {
  225. var limit = 70;
  226. if (col > limit) {
  227. line = line.slice(col - limit);
  228. col = limit;
  229. }
  230. print_error(line.slice(0, 80));
  231. print_error(line.slice(0, col).replace(/\S/g, " ") + "^");
  232. }
  233. }
  234. if (ex.defs) {
  235. print_error("Supported options:");
  236. print_error(format_object(ex.defs));
  237. }
  238. fatal(ex);
  239. return;
  240. }
  241. if (program.output == "ast") {
  242. if (!options.compress && !options.mangle) {
  243. result.ast.figure_out_scope({});
  244. }
  245. console.log(JSON.stringify(result.ast, function(key, value) {
  246. if (value) switch (key) {
  247. case "thedef":
  248. return symdef(value);
  249. case "enclosed":
  250. return value.length ? value.map(symdef) : undefined;
  251. case "variables":
  252. case "globals":
  253. return value.size ? collect_from_map(value, symdef) : undefined;
  254. }
  255. if (skip_keys.has(key)) return;
  256. if (value instanceof AST_Token) return;
  257. if (value instanceof Map) return;
  258. if (value instanceof AST_Node) {
  259. var result = {
  260. _class: "AST_" + value.TYPE
  261. };
  262. if (value.block_scope) {
  263. result.variables = value.block_scope.variables;
  264. result.enclosed = value.block_scope.enclosed;
  265. }
  266. value.CTOR.PROPS.forEach(function(prop) {
  267. if (prop !== "block_scope") {
  268. result[prop] = value[prop];
  269. }
  270. });
  271. return result;
  272. }
  273. return value;
  274. }, 2));
  275. } else if (program.output == "spidermonkey") {
  276. try {
  277. const minified = await minify(
  278. result.code,
  279. {
  280. compress: false,
  281. mangle: false,
  282. format: {
  283. ast: true,
  284. code: false
  285. }
  286. },
  287. fs
  288. );
  289. console.log(JSON.stringify(minified.ast.to_mozilla_ast(), null, 2));
  290. } catch (ex) {
  291. fatal(ex);
  292. return;
  293. }
  294. } else if (program.output) {
  295. fs.writeFileSync(program.output, result.code);
  296. if (options.sourceMap && options.sourceMap.url !== "inline" && result.map) {
  297. fs.writeFileSync(program.output + ".map", result.map);
  298. }
  299. } else {
  300. console.log(result.code);
  301. }
  302. if (program.nameCache) {
  303. fs.writeFileSync(program.nameCache, JSON.stringify(options.nameCache));
  304. }
  305. if (result.timings) for (var phase in result.timings) {
  306. print_error("- " + phase + ": " + result.timings[phase].toFixed(3) + "s");
  307. }
  308. }
  309. function fatal(message) {
  310. if (message instanceof Error) message = message.stack.replace(/^\S*?Error:/, "ERROR:");
  311. print_error(message);
  312. process.exit(1);
  313. }
  314. // A file glob function that only supports "*" and "?" wildcards in the basename.
  315. // Example: "foo/bar/*baz??.*.js"
  316. // Argument `glob` may be a string or an array of strings.
  317. // Returns an array of strings. Garbage in, garbage out.
  318. function simple_glob(glob) {
  319. if (Array.isArray(glob)) {
  320. return [].concat.apply([], glob.map(simple_glob));
  321. }
  322. if (glob && glob.match(/[*?]/)) {
  323. var dir = path.dirname(glob);
  324. try {
  325. var entries = fs.readdirSync(dir);
  326. } catch (ex) {}
  327. if (entries) {
  328. var pattern = "^" + path.basename(glob)
  329. .replace(/[.+^$[\]\\(){}]/g, "\\$&")
  330. .replace(/\*/g, "[^/\\\\]*")
  331. .replace(/\?/g, "[^/\\\\]") + "$";
  332. var mod = process.platform === "win32" ? "i" : "";
  333. var rx = new RegExp(pattern, mod);
  334. var results = entries.filter(function(name) {
  335. return rx.test(name);
  336. }).map(function(name) {
  337. return path.join(dir, name);
  338. });
  339. if (results.length) return results;
  340. }
  341. }
  342. return [ glob ];
  343. }
  344. function read_file(path, default_value) {
  345. try {
  346. return fs.readFileSync(path, "utf8");
  347. } catch (ex) {
  348. if ((ex.code == "ENOENT" || ex.code == "ENAMETOOLONG") && default_value != null) return default_value;
  349. fatal(ex);
  350. }
  351. }
  352. function parse_js(flag) {
  353. return function(value, options) {
  354. options = options || {};
  355. try {
  356. walk(parse(value, { expression: true }), node => {
  357. if (node instanceof AST_Assign) {
  358. var name = node.left.print_to_string();
  359. var value = node.right;
  360. if (flag) {
  361. options[name] = value;
  362. } else if (value instanceof AST_Array) {
  363. options[name] = value.elements.map(to_string);
  364. } else if (value instanceof AST_RegExp) {
  365. value = value.value;
  366. options[name] = new RegExp(value.source, value.flags);
  367. } else {
  368. options[name] = to_string(value);
  369. }
  370. return true;
  371. }
  372. if (node instanceof AST_Symbol || node instanceof AST_PropAccess) {
  373. var name = node.print_to_string();
  374. options[name] = true;
  375. return true;
  376. }
  377. if (!(node instanceof AST_Sequence)) throw node;
  378. function to_string(value) {
  379. return value instanceof AST_Constant ? value.getValue() : value.print_to_string({
  380. quote_keys: true
  381. });
  382. }
  383. });
  384. } catch(ex) {
  385. if (flag) {
  386. fatal("Error parsing arguments for '" + flag + "': " + value);
  387. } else {
  388. options[value] = null;
  389. }
  390. }
  391. return options;
  392. };
  393. }
  394. function symdef(def) {
  395. var ret = (1e6 + def.id) + " " + def.name;
  396. if (def.mangled_name) ret += " " + def.mangled_name;
  397. return ret;
  398. }
  399. function collect_from_map(map, callback) {
  400. var result = [];
  401. map.forEach(function (def) {
  402. result.push(callback(def));
  403. });
  404. return result;
  405. }
  406. function format_object(obj) {
  407. var lines = [];
  408. var padding = "";
  409. Object.keys(obj).map(function(name) {
  410. if (padding.length < name.length) padding = Array(name.length + 1).join(" ");
  411. return [ name, JSON.stringify(obj[name]) ];
  412. }).forEach(function(tokens) {
  413. lines.push(" " + tokens[0] + padding.slice(tokens[0].length - 2) + tokens[1]);
  414. });
  415. return lines.join("\n");
  416. }
  417. function print_error(msg) {
  418. process.stderr.write(msg);
  419. process.stderr.write("\n");
  420. }
  421. function describe_ast() {
  422. var out = OutputStream({ beautify: true });
  423. function doitem(ctor) {
  424. out.print("AST_" + ctor.TYPE);
  425. const props = ctor.SELF_PROPS.filter(prop => !/^\$/.test(prop));
  426. if (props.length > 0) {
  427. out.space();
  428. out.with_parens(function() {
  429. props.forEach(function(prop, i) {
  430. if (i) out.space();
  431. out.print(prop);
  432. });
  433. });
  434. }
  435. if (ctor.documentation) {
  436. out.space();
  437. out.print_string(ctor.documentation);
  438. }
  439. if (ctor.SUBCLASSES.length > 0) {
  440. out.space();
  441. out.with_block(function() {
  442. ctor.SUBCLASSES.forEach(function(ctor) {
  443. out.indent();
  444. doitem(ctor);
  445. out.newline();
  446. });
  447. });
  448. }
  449. }
  450. doitem(AST_Node);
  451. return out + "\n";
  452. }
  453. }