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