minify.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. "use strict";
  2. /* eslint-env browser, es6, node */
  3. import {
  4. defaults,
  5. map_from_object,
  6. map_to_object,
  7. HOP,
  8. } from "./utils/index.js";
  9. import { AST_Toplevel, AST_Node, walk, AST_Scope } from "./ast.js";
  10. import { parse } from "./parse.js";
  11. import { OutputStream } from "./output.js";
  12. import { Compressor } from "./compress/index.js";
  13. import { base54 } from "./scope.js";
  14. import { SourceMap } from "./sourcemap.js";
  15. import {
  16. mangle_properties,
  17. mangle_private_properties,
  18. reserve_quoted_keys,
  19. find_annotated_props,
  20. } from "./propmangle.js";
  21. // to/from base64 functions
  22. // Prefer built-in Buffer, if available, then use hack
  23. // https://developer.mozilla.org/en-US/docs/Glossary/Base64#The_Unicode_Problem
  24. var to_ascii = typeof Buffer !== "undefined"
  25. ? (b64) => Buffer.from(b64, "base64").toString()
  26. : (b64) => decodeURIComponent(escape(atob(b64)));
  27. var to_base64 = typeof Buffer !== "undefined"
  28. ? (str) => Buffer.from(str).toString("base64")
  29. : (str) => btoa(unescape(encodeURIComponent(str)));
  30. function read_source_map(code) {
  31. var match = /(?:^|[^.])\/\/# sourceMappingURL=data:application\/json(;[\w=-]*)?;base64,([+/0-9A-Za-z]*=*)\s*$/.exec(code);
  32. if (!match) {
  33. console.warn("inline source map not found");
  34. return null;
  35. }
  36. return to_ascii(match[2]);
  37. }
  38. function set_shorthand(name, options, keys) {
  39. if (options[name]) {
  40. keys.forEach(function(key) {
  41. if (options[key]) {
  42. if (typeof options[key] != "object") options[key] = {};
  43. if (!(name in options[key])) options[key][name] = options[name];
  44. }
  45. });
  46. }
  47. }
  48. function init_cache(cache) {
  49. if (!cache) return;
  50. if (!("props" in cache)) {
  51. cache.props = new Map();
  52. } else if (!(cache.props instanceof Map)) {
  53. cache.props = map_from_object(cache.props);
  54. }
  55. }
  56. function cache_to_json(cache) {
  57. return {
  58. props: map_to_object(cache.props)
  59. };
  60. }
  61. function log_input(files, options, fs, debug_folder) {
  62. if (!(fs && fs.writeFileSync && fs.mkdirSync)) {
  63. return;
  64. }
  65. try {
  66. fs.mkdirSync(debug_folder);
  67. } catch (e) {
  68. if (e.code !== "EEXIST") throw e;
  69. }
  70. const log_path = `${debug_folder}/terser-debug-${(Math.random() * 9999999) | 0}.log`;
  71. options = options || {};
  72. const options_str = JSON.stringify(options, (_key, thing) => {
  73. if (typeof thing === "function") return "[Function " + thing.toString() + "]";
  74. if (thing instanceof RegExp) return "[RegExp " + thing.toString() + "]";
  75. return thing;
  76. }, 4);
  77. const files_str = (file) => {
  78. if (typeof file === "object" && options.parse && options.parse.spidermonkey) {
  79. return JSON.stringify(file, null, 2);
  80. } else if (typeof file === "object") {
  81. return Object.keys(file)
  82. .map((key) => key + ": " + files_str(file[key]))
  83. .join("\n\n");
  84. } else if (typeof file === "string") {
  85. return "```\n" + file + "\n```";
  86. } else {
  87. return file; // What do?
  88. }
  89. };
  90. fs.writeFileSync(log_path, "Options: \n" + options_str + "\n\nInput files:\n\n" + files_str(files) + "\n");
  91. }
  92. function* minify_sync_or_async(files, options, _fs_module) {
  93. if (
  94. _fs_module
  95. && typeof process === "object"
  96. && process.env
  97. && typeof process.env.TERSER_DEBUG_DIR === "string"
  98. ) {
  99. log_input(files, options, _fs_module, process.env.TERSER_DEBUG_DIR);
  100. }
  101. options = defaults(options, {
  102. compress: {},
  103. ecma: undefined,
  104. enclose: false,
  105. ie8: false,
  106. keep_classnames: undefined,
  107. keep_fnames: false,
  108. mangle: {},
  109. module: false,
  110. nameCache: null,
  111. output: null,
  112. format: null,
  113. parse: {},
  114. rename: undefined,
  115. safari10: false,
  116. sourceMap: false,
  117. spidermonkey: false,
  118. timings: false,
  119. toplevel: false,
  120. warnings: false,
  121. wrap: false,
  122. }, true);
  123. var timings = options.timings && {
  124. start: Date.now()
  125. };
  126. if (options.keep_classnames === undefined) {
  127. options.keep_classnames = options.keep_fnames;
  128. }
  129. if (options.rename === undefined) {
  130. options.rename = options.compress && options.mangle;
  131. }
  132. if (options.output && options.format) {
  133. throw new Error("Please only specify either output or format option, preferrably format.");
  134. }
  135. options.format = options.format || options.output || {};
  136. set_shorthand("ecma", options, [ "parse", "compress", "format" ]);
  137. set_shorthand("ie8", options, [ "compress", "mangle", "format" ]);
  138. set_shorthand("keep_classnames", options, [ "compress", "mangle" ]);
  139. set_shorthand("keep_fnames", options, [ "compress", "mangle" ]);
  140. set_shorthand("module", options, [ "parse", "compress", "mangle" ]);
  141. set_shorthand("safari10", options, [ "mangle", "format" ]);
  142. set_shorthand("toplevel", options, [ "compress", "mangle" ]);
  143. set_shorthand("warnings", options, [ "compress" ]); // legacy
  144. var quoted_props;
  145. if (options.mangle) {
  146. options.mangle = defaults(options.mangle, {
  147. cache: options.nameCache && (options.nameCache.vars || {}),
  148. eval: false,
  149. ie8: false,
  150. keep_classnames: false,
  151. keep_fnames: false,
  152. module: false,
  153. nth_identifier: base54,
  154. properties: false,
  155. reserved: [],
  156. safari10: false,
  157. toplevel: false,
  158. }, true);
  159. if (options.mangle.properties) {
  160. if (typeof options.mangle.properties != "object") {
  161. options.mangle.properties = {};
  162. }
  163. if (options.mangle.properties.keep_quoted) {
  164. quoted_props = options.mangle.properties.reserved;
  165. if (!Array.isArray(quoted_props)) quoted_props = [];
  166. options.mangle.properties.reserved = quoted_props;
  167. }
  168. if (options.nameCache && !("cache" in options.mangle.properties)) {
  169. options.mangle.properties.cache = options.nameCache.props || {};
  170. }
  171. }
  172. init_cache(options.mangle.cache);
  173. init_cache(options.mangle.properties.cache);
  174. }
  175. if (options.sourceMap) {
  176. options.sourceMap = defaults(options.sourceMap, {
  177. asObject: false,
  178. content: null,
  179. filename: null,
  180. includeSources: false,
  181. root: null,
  182. url: null,
  183. }, true);
  184. }
  185. // -- Parse phase --
  186. if (timings) timings.parse = Date.now();
  187. var toplevel;
  188. if (files instanceof AST_Toplevel) {
  189. toplevel = files;
  190. } else {
  191. if (typeof files == "string" || (options.parse.spidermonkey && !Array.isArray(files))) {
  192. files = [ files ];
  193. }
  194. options.parse = options.parse || {};
  195. options.parse.toplevel = null;
  196. if (options.parse.spidermonkey) {
  197. options.parse.toplevel = AST_Node.from_mozilla_ast(Object.keys(files).reduce(function(toplevel, name) {
  198. if (!toplevel) return files[name];
  199. toplevel.body = toplevel.body.concat(files[name].body);
  200. return toplevel;
  201. }, null));
  202. } else {
  203. delete options.parse.spidermonkey;
  204. for (var name in files) if (HOP(files, name)) {
  205. options.parse.filename = name;
  206. options.parse.toplevel = parse(files[name], options.parse);
  207. if (options.sourceMap && options.sourceMap.content == "inline") {
  208. if (Object.keys(files).length > 1)
  209. throw new Error("inline source map only works with singular input");
  210. options.sourceMap.content = read_source_map(files[name]);
  211. }
  212. }
  213. }
  214. if (options.parse.toplevel === null) {
  215. throw new Error("no source file given");
  216. }
  217. toplevel = options.parse.toplevel;
  218. }
  219. if (quoted_props && options.mangle.properties.keep_quoted !== "strict") {
  220. reserve_quoted_keys(toplevel, quoted_props);
  221. }
  222. var annotated_props;
  223. if (options.mangle && options.mangle.properties) {
  224. annotated_props = find_annotated_props(toplevel);
  225. }
  226. if (options.wrap) {
  227. toplevel = toplevel.wrap_commonjs(options.wrap);
  228. }
  229. if (options.enclose) {
  230. toplevel = toplevel.wrap_enclose(options.enclose);
  231. }
  232. if (timings) timings.rename = Date.now();
  233. // disable rename on harmony due to expand_names bug in for-of loops
  234. // https://github.com/mishoo/UglifyJS2/issues/2794
  235. if (0 && options.rename) {
  236. toplevel.figure_out_scope(options.mangle);
  237. toplevel.expand_names(options.mangle);
  238. }
  239. // -- Compress phase --
  240. if (timings) timings.compress = Date.now();
  241. if (options.compress) {
  242. toplevel = new Compressor(options.compress, {
  243. mangle_options: options.mangle
  244. }).compress(toplevel);
  245. }
  246. // -- Mangle phase --
  247. if (timings) timings.scope = Date.now();
  248. if (options.mangle) toplevel.figure_out_scope(options.mangle);
  249. if (timings) timings.mangle = Date.now();
  250. if (options.mangle) {
  251. toplevel.compute_char_frequency(options.mangle);
  252. toplevel.mangle_names(options.mangle);
  253. toplevel = mangle_private_properties(toplevel, options.mangle);
  254. }
  255. if (timings) timings.properties = Date.now();
  256. if (options.mangle && options.mangle.properties) {
  257. toplevel = mangle_properties(toplevel, options.mangle.properties, annotated_props);
  258. }
  259. // Format phase
  260. if (timings) timings.format = Date.now();
  261. var result = {};
  262. if (options.format.ast) {
  263. result.ast = toplevel;
  264. }
  265. if (options.format.spidermonkey) {
  266. result.ast = toplevel.to_mozilla_ast();
  267. }
  268. let format_options;
  269. if (!HOP(options.format, "code") || options.format.code) {
  270. // Make a shallow copy so that we can modify without mutating the user's input.
  271. format_options = {...options.format};
  272. if (!format_options.ast) {
  273. // Destroy stuff to save RAM. (unless the deprecated `ast` option is on)
  274. format_options._destroy_ast = true;
  275. walk(toplevel, node => {
  276. if (node instanceof AST_Scope) {
  277. node.variables = undefined;
  278. node.enclosed = undefined;
  279. node.parent_scope = undefined;
  280. }
  281. if (node.block_scope) {
  282. node.block_scope.variables = undefined;
  283. node.block_scope.enclosed = undefined;
  284. node.parent_scope = undefined;
  285. }
  286. });
  287. }
  288. if (options.sourceMap) {
  289. if (options.sourceMap.includeSources && files instanceof AST_Toplevel) {
  290. throw new Error("original source content unavailable");
  291. }
  292. format_options.source_map = yield* SourceMap({
  293. file: options.sourceMap.filename,
  294. orig: options.sourceMap.content,
  295. root: options.sourceMap.root,
  296. files: options.sourceMap.includeSources ? files : null,
  297. });
  298. }
  299. delete format_options.ast;
  300. delete format_options.code;
  301. delete format_options.spidermonkey;
  302. var stream = OutputStream(format_options);
  303. toplevel.print(stream);
  304. result.code = stream.get();
  305. if (options.sourceMap) {
  306. Object.defineProperty(result, "map", {
  307. configurable: true,
  308. enumerable: true,
  309. get() {
  310. const map = format_options.source_map.getEncoded();
  311. return (result.map = options.sourceMap.asObject ? map : JSON.stringify(map));
  312. },
  313. set(value) {
  314. Object.defineProperty(result, "map", {
  315. value,
  316. writable: true,
  317. });
  318. }
  319. });
  320. result.decoded_map = format_options.source_map.getDecoded();
  321. if (options.sourceMap.url == "inline") {
  322. var sourceMap = typeof result.map === "object" ? JSON.stringify(result.map) : result.map;
  323. result.code += "\n//# sourceMappingURL=data:application/json;charset=utf-8;base64," + to_base64(sourceMap);
  324. } else if (options.sourceMap.url) {
  325. result.code += "\n//# sourceMappingURL=" + options.sourceMap.url;
  326. }
  327. }
  328. }
  329. if (options.nameCache && options.mangle) {
  330. if (options.mangle.cache) options.nameCache.vars = cache_to_json(options.mangle.cache);
  331. if (options.mangle.properties && options.mangle.properties.cache) {
  332. options.nameCache.props = cache_to_json(options.mangle.properties.cache);
  333. }
  334. }
  335. if (format_options && format_options.source_map) {
  336. format_options.source_map.destroy();
  337. }
  338. if (timings) {
  339. timings.end = Date.now();
  340. result.timings = {
  341. parse: 1e-3 * (timings.rename - timings.parse),
  342. rename: 1e-3 * (timings.compress - timings.rename),
  343. compress: 1e-3 * (timings.scope - timings.compress),
  344. scope: 1e-3 * (timings.mangle - timings.scope),
  345. mangle: 1e-3 * (timings.properties - timings.mangle),
  346. properties: 1e-3 * (timings.format - timings.properties),
  347. format: 1e-3 * (timings.end - timings.format),
  348. total: 1e-3 * (timings.end - timings.start)
  349. };
  350. }
  351. return result;
  352. }
  353. async function minify(files, options, _fs_module) {
  354. const gen = minify_sync_or_async(files, options, _fs_module);
  355. let yielded;
  356. let val;
  357. do {
  358. val = gen.next(await yielded);
  359. yielded = val.value;
  360. } while (!val.done);
  361. return val.value;
  362. }
  363. function minify_sync(files, options, _fs_module) {
  364. const gen = minify_sync_or_async(files, options, _fs_module);
  365. let yielded;
  366. let val;
  367. do {
  368. if (yielded && typeof yielded.then === "function") {
  369. throw new Error("minify_sync cannot be used with the legacy source-map module");
  370. }
  371. val = gen.next(yielded);
  372. yielded = val.value;
  373. } while (!val.done);
  374. return val.value;
  375. }
  376. export {
  377. minify,
  378. minify_sync,
  379. to_ascii,
  380. };