minify.js 10 KB

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