minify.js 10 KB

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