minify.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 } 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. } from "./propmangle.js";
  20. var to_ascii = typeof atob == "undefined" ? function(b64) {
  21. return Buffer.from(b64, "base64").toString();
  22. } : atob;
  23. var to_base64 = typeof btoa == "undefined" ? function(str) {
  24. return Buffer.from(str).toString("base64");
  25. } : btoa;
  26. function read_source_map(code) {
  27. var match = /(?:^|[^.])\/\/# sourceMappingURL=data:application\/json(;[\w=-]*)?;base64,([+/0-9A-Za-z]*=*)\s*$/.exec(code);
  28. if (!match) {
  29. console.warn("inline source map not found");
  30. return null;
  31. }
  32. return to_ascii(match[2]);
  33. }
  34. function set_shorthand(name, options, keys) {
  35. if (options[name]) {
  36. keys.forEach(function(key) {
  37. if (options[key]) {
  38. if (typeof options[key] != "object") options[key] = {};
  39. if (!(name in options[key])) options[key][name] = options[name];
  40. }
  41. });
  42. }
  43. }
  44. function init_cache(cache) {
  45. if (!cache) return;
  46. if (!("props" in cache)) {
  47. cache.props = new Map();
  48. } else if (!(cache.props instanceof Map)) {
  49. cache.props = map_from_object(cache.props);
  50. }
  51. }
  52. function cache_to_json(cache) {
  53. return {
  54. props: map_to_object(cache.props)
  55. };
  56. }
  57. async function minify(files, options) {
  58. options = defaults(options, {
  59. compress: {},
  60. ecma: undefined,
  61. enclose: false,
  62. ie8: false,
  63. keep_classnames: undefined,
  64. keep_fnames: false,
  65. mangle: {},
  66. module: false,
  67. nameCache: null,
  68. output: null,
  69. format: null,
  70. parse: {},
  71. rename: undefined,
  72. safari10: false,
  73. sourceMap: false,
  74. spidermonkey: false,
  75. timings: false,
  76. toplevel: false,
  77. warnings: false,
  78. wrap: false,
  79. }, true);
  80. var timings = options.timings && {
  81. start: Date.now()
  82. };
  83. if (options.keep_classnames === undefined) {
  84. options.keep_classnames = options.keep_fnames;
  85. }
  86. if (options.rename === undefined) {
  87. options.rename = options.compress && options.mangle;
  88. }
  89. if (options.output && options.format) {
  90. throw new Error("Please only specify either output or format option, preferrably format.");
  91. }
  92. options.format = options.format || options.output || {};
  93. set_shorthand("ecma", options, [ "parse", "compress", "format" ]);
  94. set_shorthand("ie8", options, [ "compress", "mangle", "format" ]);
  95. set_shorthand("keep_classnames", options, [ "compress", "mangle" ]);
  96. set_shorthand("keep_fnames", options, [ "compress", "mangle" ]);
  97. set_shorthand("module", options, [ "parse", "compress", "mangle" ]);
  98. set_shorthand("safari10", options, [ "mangle", "format" ]);
  99. set_shorthand("toplevel", options, [ "compress", "mangle" ]);
  100. set_shorthand("warnings", options, [ "compress" ]); // legacy
  101. var quoted_props;
  102. if (options.mangle) {
  103. options.mangle = defaults(options.mangle, {
  104. cache: options.nameCache && (options.nameCache.vars || {}),
  105. eval: false,
  106. ie8: false,
  107. keep_classnames: false,
  108. keep_fnames: false,
  109. module: false,
  110. nth_identifier: base54,
  111. properties: false,
  112. reserved: [],
  113. safari10: false,
  114. toplevel: false,
  115. }, true);
  116. if (options.mangle.properties) {
  117. if (typeof options.mangle.properties != "object") {
  118. options.mangle.properties = {};
  119. }
  120. if (options.mangle.properties.keep_quoted) {
  121. quoted_props = options.mangle.properties.reserved;
  122. if (!Array.isArray(quoted_props)) quoted_props = [];
  123. options.mangle.properties.reserved = quoted_props;
  124. }
  125. if (options.nameCache && !("cache" in options.mangle.properties)) {
  126. options.mangle.properties.cache = options.nameCache.props || {};
  127. }
  128. }
  129. init_cache(options.mangle.cache);
  130. init_cache(options.mangle.properties.cache);
  131. }
  132. if (options.sourceMap) {
  133. options.sourceMap = defaults(options.sourceMap, {
  134. asObject: false,
  135. content: null,
  136. filename: null,
  137. includeSources: false,
  138. root: null,
  139. url: null,
  140. }, true);
  141. }
  142. if (timings) timings.parse = Date.now();
  143. var toplevel;
  144. if (files instanceof AST_Toplevel) {
  145. toplevel = files;
  146. } else {
  147. if (typeof files == "string" || (options.parse.spidermonkey && !Array.isArray(files))) {
  148. files = [ files ];
  149. }
  150. options.parse = options.parse || {};
  151. options.parse.toplevel = null;
  152. if (options.parse.spidermonkey) {
  153. options.parse.toplevel = AST_Node.from_mozilla_ast(Object.keys(files).reduce(function(toplevel, name) {
  154. if (!toplevel) return files[name];
  155. toplevel.body = toplevel.body.concat(files[name].body);
  156. return toplevel;
  157. }, null));
  158. } else {
  159. delete options.parse.spidermonkey;
  160. for (var name in files) if (HOP(files, name)) {
  161. options.parse.filename = name;
  162. options.parse.toplevel = parse(files[name], options.parse);
  163. if (options.sourceMap && options.sourceMap.content == "inline") {
  164. if (Object.keys(files).length > 1)
  165. throw new Error("inline source map only works with singular input");
  166. options.sourceMap.content = read_source_map(files[name]);
  167. }
  168. }
  169. }
  170. toplevel = options.parse.toplevel;
  171. }
  172. if (quoted_props && options.mangle.properties.keep_quoted !== "strict") {
  173. reserve_quoted_keys(toplevel, quoted_props);
  174. }
  175. if (options.wrap) {
  176. toplevel = toplevel.wrap_commonjs(options.wrap);
  177. }
  178. if (options.enclose) {
  179. toplevel = toplevel.wrap_enclose(options.enclose);
  180. }
  181. if (timings) timings.rename = Date.now();
  182. // disable rename on harmony due to expand_names bug in for-of loops
  183. // https://github.com/mishoo/UglifyJS2/issues/2794
  184. if (0 && options.rename) {
  185. toplevel.figure_out_scope(options.mangle);
  186. toplevel.expand_names(options.mangle);
  187. }
  188. if (timings) timings.compress = Date.now();
  189. if (options.compress) {
  190. toplevel = new Compressor(options.compress, {
  191. mangle_options: options.mangle
  192. }).compress(toplevel);
  193. }
  194. if (timings) timings.scope = Date.now();
  195. if (options.mangle) toplevel.figure_out_scope(options.mangle);
  196. if (timings) timings.mangle = Date.now();
  197. if (options.mangle) {
  198. toplevel.compute_char_frequency(options.mangle);
  199. toplevel.mangle_names(options.mangle);
  200. toplevel = mangle_private_properties(toplevel, options.mangle);
  201. }
  202. if (timings) timings.properties = Date.now();
  203. if (options.mangle && options.mangle.properties) {
  204. toplevel = mangle_properties(toplevel, options.mangle.properties);
  205. }
  206. if (timings) timings.format = Date.now();
  207. var result = {};
  208. if (options.format.ast) {
  209. result.ast = toplevel;
  210. }
  211. if (options.format.spidermonkey) {
  212. result.ast = toplevel.to_mozilla_ast();
  213. }
  214. if (!HOP(options.format, "code") || options.format.code) {
  215. if (options.sourceMap) {
  216. options.format.source_map = await SourceMap({
  217. file: options.sourceMap.filename,
  218. orig: options.sourceMap.content,
  219. root: options.sourceMap.root
  220. });
  221. if (options.sourceMap.includeSources) {
  222. if (files instanceof AST_Toplevel) {
  223. throw new Error("original source content unavailable");
  224. } else for (var name in files) if (HOP(files, name)) {
  225. options.format.source_map.get().setSourceContent(name, files[name]);
  226. }
  227. }
  228. }
  229. delete options.format.ast;
  230. delete options.format.code;
  231. delete options.format.spidermonkey;
  232. var stream = OutputStream(options.format);
  233. toplevel.print(stream);
  234. result.code = stream.get();
  235. if (options.sourceMap) {
  236. if(options.sourceMap.asObject) {
  237. result.map = options.format.source_map.get().toJSON();
  238. } else {
  239. result.map = options.format.source_map.toString();
  240. }
  241. if (options.sourceMap.url == "inline") {
  242. var sourceMap = typeof result.map === "object" ? JSON.stringify(result.map) : result.map;
  243. result.code += "\n//# sourceMappingURL=data:application/json;charset=utf-8;base64," + to_base64(sourceMap);
  244. } else if (options.sourceMap.url) {
  245. result.code += "\n//# sourceMappingURL=" + options.sourceMap.url;
  246. }
  247. }
  248. }
  249. if (options.nameCache && options.mangle) {
  250. if (options.mangle.cache) options.nameCache.vars = cache_to_json(options.mangle.cache);
  251. if (options.mangle.properties && options.mangle.properties.cache) {
  252. options.nameCache.props = cache_to_json(options.mangle.properties.cache);
  253. }
  254. }
  255. if (options.format && options.format.source_map) {
  256. options.format.source_map.destroy();
  257. }
  258. if (timings) {
  259. timings.end = Date.now();
  260. result.timings = {
  261. parse: 1e-3 * (timings.rename - timings.parse),
  262. rename: 1e-3 * (timings.compress - timings.rename),
  263. compress: 1e-3 * (timings.scope - timings.compress),
  264. scope: 1e-3 * (timings.mangle - timings.scope),
  265. mangle: 1e-3 * (timings.properties - timings.mangle),
  266. properties: 1e-3 * (timings.format - timings.properties),
  267. format: 1e-3 * (timings.end - timings.format),
  268. total: 1e-3 * (timings.end - timings.start)
  269. };
  270. }
  271. return result;
  272. }
  273. export {
  274. minify,
  275. to_ascii,
  276. };