minify.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 (!("props" in cache)) {
  29. cache.props = new Dictionary();
  30. } else if (!(cache.props instanceof Dictionary)) {
  31. cache.props = Dictionary.fromObject(cache.props);
  32. }
  33. }
  34. function to_json(cache) {
  35. return {
  36. props: cache.props.toObject()
  37. };
  38. }
  39. function minify(files, options) {
  40. var warn_function = AST_Node.warn_function;
  41. try {
  42. options = defaults(options, {
  43. compress: {},
  44. ecma: undefined,
  45. ie8: false,
  46. keep_classnames: undefined,
  47. keep_fnames: false,
  48. mangle: {},
  49. nameCache: null,
  50. output: {},
  51. parse: {},
  52. rename: undefined,
  53. safari10: false,
  54. sourceMap: false,
  55. timings: false,
  56. toplevel: false,
  57. warnings: false,
  58. wrap: false,
  59. }, true);
  60. var timings = options.timings && {
  61. start: Date.now()
  62. };
  63. if (options.keep_classnames === undefined) {
  64. options.keep_classnames = options.keep_fnames;
  65. }
  66. if (options.rename === undefined) {
  67. options.rename = options.compress && options.mangle;
  68. }
  69. set_shorthand("ecma", options, [ "parse", "compress", "output" ]);
  70. set_shorthand("ie8", options, [ "compress", "mangle", "output" ]);
  71. set_shorthand("keep_classnames", options, [ "compress", "mangle" ]);
  72. set_shorthand("keep_fnames", options, [ "compress", "mangle" ]);
  73. set_shorthand("safari10", options, [ "mangle", "output" ]);
  74. set_shorthand("toplevel", options, [ "compress", "mangle" ]);
  75. set_shorthand("warnings", options, [ "compress" ]);
  76. var quoted_props;
  77. if (options.mangle) {
  78. options.mangle = defaults(options.mangle, {
  79. cache: options.nameCache && (options.nameCache.vars || {}),
  80. eval: false,
  81. ie8: false,
  82. keep_classnames: false,
  83. keep_fnames: false,
  84. properties: false,
  85. reserved: [],
  86. safari10: false,
  87. toplevel: false,
  88. }, true);
  89. if (options.mangle.properties) {
  90. if (typeof options.mangle.properties != "object") {
  91. options.mangle.properties = {};
  92. }
  93. if (options.mangle.properties.keep_quoted) {
  94. quoted_props = options.mangle.properties.reserved;
  95. if (!Array.isArray(quoted_props)) quoted_props = [];
  96. options.mangle.properties.reserved = quoted_props;
  97. }
  98. if (options.nameCache && !("cache" in options.mangle.properties)) {
  99. options.mangle.properties.cache = options.nameCache.props || {};
  100. }
  101. }
  102. init_cache(options.mangle.cache);
  103. init_cache(options.mangle.properties.cache);
  104. }
  105. if (options.sourceMap) {
  106. options.sourceMap = defaults(options.sourceMap, {
  107. content: null,
  108. filename: null,
  109. includeSources: false,
  110. root: null,
  111. url: null,
  112. }, true);
  113. }
  114. var warnings = [];
  115. if (options.warnings && !AST_Node.warn_function) {
  116. AST_Node.warn_function = function(warning) {
  117. warnings.push(warning);
  118. };
  119. }
  120. if (timings) timings.parse = Date.now();
  121. var toplevel;
  122. if (files instanceof AST_Toplevel) {
  123. toplevel = files;
  124. } else {
  125. if (typeof files == "string") {
  126. files = [ files ];
  127. }
  128. options.parse = options.parse || {};
  129. options.parse.toplevel = null;
  130. for (var name in files) if (HOP(files, name)) {
  131. options.parse.filename = name;
  132. options.parse.toplevel = parse(files[name], options.parse);
  133. if (options.sourceMap && options.sourceMap.content == "inline") {
  134. if (Object.keys(files).length > 1)
  135. throw new Error("inline source map only works with singular input");
  136. options.sourceMap.content = read_source_map(files[name]);
  137. }
  138. }
  139. toplevel = options.parse.toplevel;
  140. }
  141. if (quoted_props) {
  142. reserve_quoted_keys(toplevel, quoted_props);
  143. }
  144. if (options.wrap) {
  145. toplevel = toplevel.wrap_commonjs(options.wrap);
  146. }
  147. if (timings) timings.rename = Date.now();
  148. // disable rename on harmony due to expand_names bug in for-of loops
  149. // https://github.com/mishoo/UglifyJS2/issues/2794
  150. if (0 && 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. }