minify.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 } 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. timings: false,
  74. toplevel: false,
  75. warnings: false,
  76. wrap: false,
  77. }, true);
  78. var timings = options.timings && {
  79. start: Date.now()
  80. };
  81. if (options.keep_classnames === undefined) {
  82. options.keep_classnames = options.keep_fnames;
  83. }
  84. if (options.rename === undefined) {
  85. options.rename = options.compress && options.mangle;
  86. }
  87. if (options.output && options.format) {
  88. throw new Error("Please only specify either output or format option, preferrably format.");
  89. }
  90. options.format = options.format || options.output || {};
  91. set_shorthand("ecma", options, [ "parse", "compress", "format" ]);
  92. set_shorthand("ie8", options, [ "compress", "mangle", "format" ]);
  93. set_shorthand("keep_classnames", options, [ "compress", "mangle" ]);
  94. set_shorthand("keep_fnames", options, [ "compress", "mangle" ]);
  95. set_shorthand("module", options, [ "parse", "compress", "mangle" ]);
  96. set_shorthand("safari10", options, [ "mangle", "format" ]);
  97. set_shorthand("toplevel", options, [ "compress", "mangle" ]);
  98. set_shorthand("warnings", options, [ "compress" ]); // legacy
  99. var quoted_props;
  100. if (options.mangle) {
  101. options.mangle = defaults(options.mangle, {
  102. cache: options.nameCache && (options.nameCache.vars || {}),
  103. eval: false,
  104. ie8: false,
  105. keep_classnames: false,
  106. keep_fnames: false,
  107. module: false,
  108. properties: false,
  109. reserved: [],
  110. safari10: false,
  111. toplevel: false,
  112. }, true);
  113. if (options.mangle.properties) {
  114. if (typeof options.mangle.properties != "object") {
  115. options.mangle.properties = {};
  116. }
  117. if (options.mangle.properties.keep_quoted) {
  118. quoted_props = options.mangle.properties.reserved;
  119. if (!Array.isArray(quoted_props)) quoted_props = [];
  120. options.mangle.properties.reserved = quoted_props;
  121. }
  122. if (options.nameCache && !("cache" in options.mangle.properties)) {
  123. options.mangle.properties.cache = options.nameCache.props || {};
  124. }
  125. }
  126. init_cache(options.mangle.cache);
  127. init_cache(options.mangle.properties.cache);
  128. }
  129. if (options.sourceMap) {
  130. options.sourceMap = defaults(options.sourceMap, {
  131. asObject: false,
  132. content: null,
  133. filename: null,
  134. includeSources: false,
  135. root: null,
  136. url: null,
  137. }, true);
  138. }
  139. if (timings) timings.parse = Date.now();
  140. var toplevel;
  141. if (files instanceof AST_Toplevel) {
  142. toplevel = files;
  143. } else {
  144. if (typeof files == "string") {
  145. files = [ files ];
  146. }
  147. options.parse = options.parse || {};
  148. options.parse.toplevel = null;
  149. for (var name in files) if (HOP(files, name)) {
  150. options.parse.filename = name;
  151. options.parse.toplevel = parse(files[name], options.parse);
  152. if (options.sourceMap && options.sourceMap.content == "inline") {
  153. if (Object.keys(files).length > 1)
  154. throw new Error("inline source map only works with singular input");
  155. options.sourceMap.content = read_source_map(files[name]);
  156. }
  157. }
  158. toplevel = options.parse.toplevel;
  159. }
  160. if (quoted_props && options.mangle.properties.keep_quoted !== "strict") {
  161. reserve_quoted_keys(toplevel, quoted_props);
  162. }
  163. if (options.wrap) {
  164. toplevel = toplevel.wrap_commonjs(options.wrap);
  165. }
  166. if (options.enclose) {
  167. toplevel = toplevel.wrap_enclose(options.enclose);
  168. }
  169. if (timings) timings.rename = Date.now();
  170. // disable rename on harmony due to expand_names bug in for-of loops
  171. // https://github.com/mishoo/UglifyJS2/issues/2794
  172. if (0 && options.rename) {
  173. toplevel.figure_out_scope(options.mangle);
  174. toplevel.expand_names(options.mangle);
  175. }
  176. if (timings) timings.compress = Date.now();
  177. if (options.compress) {
  178. toplevel = new Compressor(options.compress, {
  179. mangle_options: options.mangle
  180. }).compress(toplevel);
  181. }
  182. if (timings) timings.scope = Date.now();
  183. if (options.mangle) toplevel.figure_out_scope(options.mangle);
  184. if (timings) timings.mangle = Date.now();
  185. if (options.mangle) {
  186. base54.reset();
  187. toplevel.compute_char_frequency(options.mangle);
  188. toplevel.mangle_names(options.mangle);
  189. }
  190. if (timings) timings.properties = Date.now();
  191. if (options.mangle && options.mangle.properties) {
  192. toplevel = mangle_properties(toplevel, options.mangle.properties);
  193. }
  194. if (timings) timings.format = Date.now();
  195. var result = {};
  196. if (options.format.ast) {
  197. result.ast = toplevel;
  198. }
  199. if (!HOP(options.format, "code") || options.format.code) {
  200. if (options.sourceMap) {
  201. options.format.source_map = await SourceMap({
  202. file: options.sourceMap.filename,
  203. orig: options.sourceMap.content,
  204. root: options.sourceMap.root
  205. });
  206. if (options.sourceMap.includeSources) {
  207. if (files instanceof AST_Toplevel) {
  208. throw new Error("original source content unavailable");
  209. } else for (var name in files) if (HOP(files, name)) {
  210. options.format.source_map.get().setSourceContent(name, files[name]);
  211. }
  212. }
  213. }
  214. delete options.format.ast;
  215. delete options.format.code;
  216. var stream = OutputStream(options.format);
  217. toplevel.print(stream);
  218. result.code = stream.get();
  219. if (options.sourceMap) {
  220. if(options.sourceMap.asObject) {
  221. result.map = options.format.source_map.get().toJSON();
  222. } else {
  223. result.map = options.format.source_map.toString();
  224. }
  225. if (options.sourceMap.url == "inline") {
  226. var sourceMap = typeof result.map === "object" ? JSON.stringify(result.map) : result.map;
  227. result.code += "\n//# sourceMappingURL=data:application/json;charset=utf-8;base64," + to_base64(sourceMap);
  228. } else if (options.sourceMap.url) {
  229. result.code += "\n//# sourceMappingURL=" + options.sourceMap.url;
  230. }
  231. }
  232. }
  233. if (options.nameCache && options.mangle) {
  234. if (options.mangle.cache) options.nameCache.vars = cache_to_json(options.mangle.cache);
  235. if (options.mangle.properties && options.mangle.properties.cache) {
  236. options.nameCache.props = cache_to_json(options.mangle.properties.cache);
  237. }
  238. }
  239. if (options.format && options.format.source_map) {
  240. options.format.source_map.destroy();
  241. }
  242. if (timings) {
  243. timings.end = Date.now();
  244. result.timings = {
  245. parse: 1e-3 * (timings.rename - timings.parse),
  246. rename: 1e-3 * (timings.compress - timings.rename),
  247. compress: 1e-3 * (timings.scope - timings.compress),
  248. scope: 1e-3 * (timings.mangle - timings.scope),
  249. mangle: 1e-3 * (timings.properties - timings.mangle),
  250. properties: 1e-3 * (timings.format - timings.properties),
  251. format: 1e-3 * (timings.end - timings.format),
  252. total: 1e-3 * (timings.end - timings.start)
  253. };
  254. }
  255. return result;
  256. }
  257. export {
  258. minify,
  259. to_ascii,
  260. };