a zip code crypto-currency system good for red ONLY

uglifyjs 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. #! /usr/bin/env node
  2. // -*- js -*-
  3. "use strict";
  4. // workaround for tty output truncation upon process.exit()
  5. [process.stdout, process.stderr].forEach(function(stream){
  6. if (stream._handle && stream._handle.setBlocking)
  7. stream._handle.setBlocking(true);
  8. });
  9. var fs = require("fs");
  10. var info = require("../package.json");
  11. var path = require("path");
  12. var program = require("commander");
  13. var UglifyJS = require("../tools/node");
  14. var skip_keys = [ "cname", "enclosed", "inlined", "parent_scope", "scope", "thedef", "uses_eval", "uses_with" ];
  15. var files = {};
  16. var options = {
  17. compress: false,
  18. mangle: false
  19. };
  20. program.version(info.name + " " + info.version);
  21. program.parseArgv = program.parse;
  22. program.parse = undefined;
  23. if (process.argv.indexOf("ast") >= 0) program.helpInformation = UglifyJS.describe_ast;
  24. else if (process.argv.indexOf("options") >= 0) program.helpInformation = function() {
  25. var text = [];
  26. var options = UglifyJS.default_options();
  27. for (var option in options) {
  28. text.push("--" + (option == "output" ? "beautify" : option == "sourceMap" ? "source-map" : option) + " options:");
  29. text.push(format_object(options[option]));
  30. text.push("");
  31. }
  32. return text.join("\n");
  33. };
  34. program.option("-p, --parse <options>", "Specify parser options.", parse_js());
  35. program.option("-c, --compress [options]", "Enable compressor/specify compressor options.", parse_js());
  36. program.option("-m, --mangle [options]", "Mangle names/specify mangler options.", parse_js());
  37. program.option("--mangle-props [options]", "Mangle properties/specify mangler options.", parse_js());
  38. program.option("-b, --beautify [options]", "Beautify output/specify output options.", parse_js());
  39. program.option("-o, --output <file>", "Output file (default STDOUT).");
  40. program.option("--comments [filter]", "Preserve copyright comments in the output.");
  41. program.option("--config-file <file>", "Read minify() options from JSON file.");
  42. program.option("-d, --define <expr>[=value]", "Global definitions.", parse_js("define"));
  43. program.option("--ecma <version>", "Specify ECMAScript release: 5, 6, 7 or 8.");
  44. program.option("--ie8", "Support non-standard Internet Explorer 8.");
  45. program.option("--keep-classnames", "Do not mangle/drop class names.");
  46. program.option("--keep-fnames", "Do not mangle/drop function names. Useful for code relying on Function.prototype.name.");
  47. program.option("--name-cache <file>", "File to hold mangled name mappings.");
  48. program.option("--no-rename", "Disable symbol expansion.");
  49. program.option("--safari10", "Support non-standard Safari 10.");
  50. program.option("--self", "Build UglifyJS as a library (implies --wrap UglifyJS)");
  51. program.option("--source-map [options]", "Enable source map/specify source map options.", parse_source_map());
  52. program.option("--timings", "Display operations run time on STDERR.")
  53. program.option("--toplevel", "Compress and/or mangle variables in toplevel scope.");
  54. program.option("--verbose", "Print diagnostic messages.");
  55. program.option("--warn", "Print warning messages.");
  56. program.option("--wrap <name>", "Embed everything as a function with “exports” corresponding to “name” globally.");
  57. program.arguments("[files...]").parseArgv(process.argv);
  58. if (program.configFile) {
  59. options = JSON.parse(read_file(program.configFile));
  60. }
  61. if (!program.output && program.sourceMap && program.sourceMap.url != "inline") {
  62. fatal("ERROR: cannot write source map to STDOUT");
  63. }
  64. [
  65. "compress",
  66. "ie8",
  67. "mangle",
  68. "rename",
  69. "safari10",
  70. "sourceMap",
  71. "toplevel",
  72. "wrap"
  73. ].forEach(function(name) {
  74. if (name in program) {
  75. if (name == "rename" && program[name]) return;
  76. options[name] = program[name];
  77. }
  78. });
  79. if ("ecma" in program) {
  80. if (program.ecma != (program.ecma | 0)) fatal("ERROR: ecma must be an integer");
  81. options.ecma = program.ecma | 0;
  82. }
  83. if (program.beautify) {
  84. options.output = typeof program.beautify == "object" ? program.beautify : {};
  85. if (!("beautify" in options.output)) {
  86. options.output.beautify = true;
  87. }
  88. }
  89. if (program.comments) {
  90. if (typeof options.output != "object") options.output = {};
  91. options.output.comments = typeof program.comments == "string" ? program.comments : "some";
  92. }
  93. if (program.define) {
  94. if (typeof options.compress != "object") options.compress = {};
  95. if (typeof options.compress.global_defs != "object") options.compress.global_defs = {};
  96. for (var expr in program.define) {
  97. options.compress.global_defs[expr] = program.define[expr];
  98. }
  99. }
  100. if (program.keepClassnames) {
  101. options.keep_classnames = true;
  102. }
  103. if (program.keepFnames) {
  104. options.keep_fnames = true;
  105. }
  106. if (program.mangleProps) {
  107. if (program.mangleProps.domprops) {
  108. delete program.mangleProps.domprops;
  109. } else {
  110. if (typeof program.mangleProps != "object") program.mangleProps = {};
  111. if (!Array.isArray(program.mangleProps.reserved)) program.mangleProps.reserved = [];
  112. require("../tools/domprops").forEach(function(name) {
  113. UglifyJS._push_uniq(program.mangleProps.reserved, name);
  114. });
  115. }
  116. if (typeof options.mangle != "object") options.mangle = {};
  117. options.mangle.properties = program.mangleProps;
  118. }
  119. if (program.nameCache) {
  120. options.nameCache = JSON.parse(read_file(program.nameCache, "{}"));
  121. }
  122. if (program.output == "ast") {
  123. options.output = {
  124. ast: true,
  125. code: false
  126. };
  127. }
  128. if (program.parse) {
  129. if (!program.parse.acorn && !program.parse.spidermonkey) {
  130. options.parse = program.parse;
  131. } else if (program.sourceMap && program.sourceMap.content == "inline") {
  132. fatal("ERROR: inline source map only works with built-in parser");
  133. }
  134. }
  135. var convert_path = function(name) {
  136. return name;
  137. };
  138. if (typeof program.sourceMap == "object" && "base" in program.sourceMap) {
  139. convert_path = function() {
  140. var base = program.sourceMap.base;
  141. delete options.sourceMap.base;
  142. return function(name) {
  143. return path.relative(base, name);
  144. };
  145. }();
  146. }
  147. if (program.verbose) {
  148. options.warnings = "verbose";
  149. } else if (program.warn) {
  150. options.warnings = true;
  151. }
  152. if (program.self) {
  153. if (program.args.length) {
  154. print_error("WARN: Ignoring input files since --self was passed");
  155. }
  156. if (!options.wrap) options.wrap = "UglifyJS";
  157. simple_glob(UglifyJS.FILES).forEach(function(name) {
  158. files[convert_path(name)] = read_file(name);
  159. });
  160. run();
  161. } else if (program.args.length) {
  162. simple_glob(program.args).forEach(function(name) {
  163. files[convert_path(name)] = read_file(name);
  164. });
  165. run();
  166. } else {
  167. var chunks = [];
  168. process.stdin.setEncoding("utf8");
  169. process.stdin.on("data", function(chunk) {
  170. chunks.push(chunk);
  171. }).on("end", function() {
  172. files = [ chunks.join("") ];
  173. run();
  174. });
  175. process.stdin.resume();
  176. }
  177. function convert_ast(fn) {
  178. return UglifyJS.AST_Node.from_mozilla_ast(Object.keys(files).reduce(fn, null));
  179. }
  180. function run() {
  181. UglifyJS.AST_Node.warn_function = function(msg) {
  182. print_error("WARN: " + msg);
  183. };
  184. if (program.timings) options.timings = true;
  185. try {
  186. if (program.parse) {
  187. if (program.parse.acorn) {
  188. files = convert_ast(function(toplevel, name) {
  189. return require("acorn").parse(files[name], {
  190. locations: true,
  191. program: toplevel,
  192. sourceFile: name
  193. });
  194. });
  195. } else if (program.parse.spidermonkey) {
  196. files = convert_ast(function(toplevel, name) {
  197. var obj = JSON.parse(files[name]);
  198. if (!toplevel) return obj;
  199. toplevel.body = toplevel.body.concat(obj.body);
  200. return toplevel;
  201. });
  202. }
  203. }
  204. } catch (ex) {
  205. fatal(ex);
  206. }
  207. var result = UglifyJS.minify(files, options);
  208. if (result.error) {
  209. var ex = result.error;
  210. if (ex.name == "SyntaxError") {
  211. print_error("Parse error at " + ex.filename + ":" + ex.line + "," + ex.col);
  212. var col = ex.col;
  213. var lines = files[ex.filename].split(/\r?\n/);
  214. var line = lines[ex.line - 1];
  215. if (!line && !col) {
  216. line = lines[ex.line - 2];
  217. col = line.length;
  218. }
  219. if (line) {
  220. var limit = 70;
  221. if (col > limit) {
  222. line = line.slice(col - limit);
  223. col = limit;
  224. }
  225. print_error(line.slice(0, 80));
  226. print_error(line.slice(0, col).replace(/\S/g, " ") + "^");
  227. }
  228. }
  229. if (ex.defs) {
  230. print_error("Supported options:");
  231. print_error(format_object(ex.defs));
  232. }
  233. fatal(ex);
  234. } else if (program.output == "ast") {
  235. print(JSON.stringify(result.ast, function(key, value) {
  236. if (skip_key(key)) return;
  237. if (value instanceof UglifyJS.AST_Token) return;
  238. if (value instanceof UglifyJS.Dictionary) return;
  239. if (value instanceof UglifyJS.AST_Node) {
  240. var result = {
  241. _class: "AST_" + value.TYPE
  242. };
  243. value.CTOR.PROPS.forEach(function(prop) {
  244. result[prop] = value[prop];
  245. });
  246. return result;
  247. }
  248. return value;
  249. }, 2));
  250. } else if (program.output == "spidermonkey") {
  251. print(JSON.stringify(UglifyJS.minify(result.code, {
  252. compress: false,
  253. mangle: false,
  254. output: {
  255. ast: true,
  256. code: false
  257. }
  258. }).ast.to_mozilla_ast(), null, 2));
  259. } else if (program.output) {
  260. fs.writeFileSync(program.output, result.code);
  261. if (result.map) {
  262. fs.writeFileSync(program.output + ".map", result.map);
  263. }
  264. } else {
  265. print(result.code);
  266. }
  267. if (program.nameCache) {
  268. fs.writeFileSync(program.nameCache, JSON.stringify(options.nameCache));
  269. }
  270. if (result.timings) for (var phase in result.timings) {
  271. print_error("- " + phase + ": " + result.timings[phase].toFixed(3) + "s");
  272. }
  273. }
  274. function fatal(message) {
  275. if (message instanceof Error) message = message.stack.replace(/^\S*?Error:/, "ERROR:")
  276. print_error(message);
  277. process.exit(1);
  278. }
  279. // A file glob function that only supports "*" and "?" wildcards in the basename.
  280. // Example: "foo/bar/*baz??.*.js"
  281. // Argument `glob` may be a string or an array of strings.
  282. // Returns an array of strings. Garbage in, garbage out.
  283. function simple_glob(glob) {
  284. if (Array.isArray(glob)) {
  285. return [].concat.apply([], glob.map(simple_glob));
  286. }
  287. if (glob.match(/\*|\?/)) {
  288. var dir = path.dirname(glob);
  289. try {
  290. var entries = fs.readdirSync(dir);
  291. } catch (ex) {}
  292. if (entries) {
  293. var pattern = "^" + path.basename(glob)
  294. .replace(/[.+^$[\]\\(){}]/g, "\\$&")
  295. .replace(/\*/g, "[^/\\\\]*")
  296. .replace(/\?/g, "[^/\\\\]") + "$";
  297. var mod = process.platform === "win32" ? "i" : "";
  298. var rx = new RegExp(pattern, mod);
  299. var results = entries.filter(function(name) {
  300. return rx.test(name);
  301. }).map(function(name) {
  302. return path.join(dir, name);
  303. });
  304. if (results.length) return results;
  305. }
  306. }
  307. return [ glob ];
  308. }
  309. function read_file(path, default_value) {
  310. try {
  311. return fs.readFileSync(path, "utf8");
  312. } catch (ex) {
  313. if (ex.code == "ENOENT" && default_value != null) return default_value;
  314. fatal(ex);
  315. }
  316. }
  317. function parse_js(flag) {
  318. return function(value, options) {
  319. options = options || {};
  320. try {
  321. UglifyJS.minify(value, {
  322. parse: {
  323. expression: true
  324. },
  325. compress: false,
  326. mangle: false,
  327. output: {
  328. ast: true,
  329. code: false
  330. }
  331. }).ast.walk(new UglifyJS.TreeWalker(function(node) {
  332. if (node instanceof UglifyJS.AST_Assign) {
  333. var name = node.left.print_to_string();
  334. var value = node.right;
  335. if (flag) {
  336. options[name] = value;
  337. } else if (value instanceof UglifyJS.AST_Array) {
  338. options[name] = value.elements.map(to_string);
  339. } else {
  340. options[name] = to_string(value);
  341. }
  342. return true;
  343. }
  344. if (node instanceof UglifyJS.AST_Symbol || node instanceof UglifyJS.AST_PropAccess) {
  345. var name = node.print_to_string();
  346. options[name] = true;
  347. return true;
  348. }
  349. if (!(node instanceof UglifyJS.AST_Sequence)) throw node;
  350. function to_string(value) {
  351. return value instanceof UglifyJS.AST_Constant ? value.getValue() : value.print_to_string({
  352. quote_keys: true
  353. });
  354. }
  355. }));
  356. } catch(ex) {
  357. if (flag) {
  358. fatal("Error parsing arguments for '" + flag + "': " + value);
  359. } else {
  360. options[value] = null;
  361. }
  362. }
  363. return options;
  364. }
  365. }
  366. function parse_source_map() {
  367. var parse = parse_js();
  368. return function(value, options) {
  369. var hasContent = options && "content" in options;
  370. var settings = parse(value, options);
  371. if (!hasContent && settings.content && settings.content != "inline") {
  372. print_error("INFO: Using input source map: " + settings.content);
  373. settings.content = read_file(settings.content, settings.content);
  374. }
  375. return settings;
  376. }
  377. }
  378. function skip_key(key) {
  379. return skip_keys.indexOf(key) >= 0;
  380. }
  381. function format_object(obj) {
  382. var lines = [];
  383. var padding = "";
  384. Object.keys(obj).map(function(name) {
  385. if (padding.length < name.length) padding = Array(name.length + 1).join(" ");
  386. return [ name, JSON.stringify(obj[name]) ];
  387. }).forEach(function(tokens) {
  388. lines.push(" " + tokens[0] + padding.slice(tokens[0].length - 2) + tokens[1]);
  389. });
  390. return lines.join("\n");
  391. }
  392. function print_error(msg) {
  393. process.stderr.write(msg);
  394. process.stderr.write("\n");
  395. }
  396. function print(txt) {
  397. process.stdout.write(txt);
  398. process.stdout.write("\n");
  399. }