minify.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. ie8: false,
  47. keep_fnames: false,
  48. mangle: {},
  49. nameCache: null,
  50. output: {},
  51. parse: {},
  52. sourceMap: false,
  53. timings: false,
  54. toplevel: false,
  55. warnings: false,
  56. wrap: false,
  57. }, true);
  58. var timings = options.timings && {
  59. start: Date.now()
  60. };
  61. set_shorthand("ie8", options, [ "compress", "mangle", "output" ]);
  62. set_shorthand("keep_fnames", options, [ "compress", "mangle" ]);
  63. set_shorthand("toplevel", options, [ "compress", "mangle" ]);
  64. set_shorthand("warnings", options, [ "compress" ]);
  65. var quoted_props;
  66. if (options.mangle) {
  67. options.mangle = defaults(options.mangle, {
  68. cache: options.nameCache && (options.nameCache.vars || {}),
  69. eval: false,
  70. ie8: false,
  71. keep_fnames: false,
  72. properties: false,
  73. reserved: [],
  74. toplevel: false,
  75. }, true);
  76. if (options.mangle.properties) {
  77. if (typeof options.mangle.properties != "object") {
  78. options.mangle.properties = {};
  79. }
  80. if (options.mangle.properties.keep_quoted) {
  81. quoted_props = options.mangle.properties.reserved;
  82. if (!Array.isArray(quoted_props)) quoted_props = [];
  83. options.mangle.properties.reserved = quoted_props;
  84. }
  85. if (options.nameCache && !("cache" in options.mangle.properties)) {
  86. options.mangle.properties.cache = options.nameCache.props || {};
  87. }
  88. }
  89. init_cache(options.mangle.cache);
  90. init_cache(options.mangle.properties.cache);
  91. }
  92. if (options.sourceMap) {
  93. options.sourceMap = defaults(options.sourceMap, {
  94. content: null,
  95. filename: null,
  96. includeSources: false,
  97. root: null,
  98. url: null,
  99. }, true);
  100. }
  101. var warnings = [];
  102. if (options.warnings && !AST_Node.warn_function) {
  103. AST_Node.warn_function = function(warning) {
  104. warnings.push(warning);
  105. };
  106. }
  107. if (timings) timings.parse = Date.now();
  108. var toplevel;
  109. if (files instanceof AST_Toplevel) {
  110. toplevel = files;
  111. } else {
  112. if (typeof files == "string") {
  113. files = [ files ];
  114. }
  115. options.parse = options.parse || {};
  116. options.parse.toplevel = null;
  117. for (var name in files) if (HOP(files, name)) {
  118. options.parse.filename = name;
  119. options.parse.toplevel = parse(files[name], options.parse);
  120. if (options.sourceMap && options.sourceMap.content == "inline") {
  121. if (Object.keys(files).length > 1)
  122. throw new Error("inline source map only works with singular input");
  123. options.sourceMap.content = read_source_map(files[name]);
  124. }
  125. }
  126. toplevel = options.parse.toplevel;
  127. }
  128. if (quoted_props) {
  129. reserve_quoted_keys(toplevel, quoted_props);
  130. }
  131. if (options.wrap) {
  132. toplevel = toplevel.wrap_commonjs(options.wrap);
  133. }
  134. if (timings) timings.scope1 = Date.now();
  135. if (options.compress) toplevel.figure_out_scope(options.mangle);
  136. if (timings) timings.compress = Date.now();
  137. if (options.compress) toplevel = new Compressor(options.compress).compress(toplevel);
  138. if (timings) timings.scope2 = Date.now();
  139. if (options.mangle) toplevel.figure_out_scope(options.mangle);
  140. if (timings) timings.mangle = Date.now();
  141. if (options.mangle) {
  142. base54.reset();
  143. toplevel.compute_char_frequency(options.mangle);
  144. toplevel.mangle_names(options.mangle);
  145. }
  146. if (timings) timings.properties = Date.now();
  147. if (options.mangle && options.mangle.properties) {
  148. toplevel = mangle_properties(toplevel, options.mangle.properties);
  149. }
  150. if (timings) timings.output = Date.now();
  151. var result = {};
  152. if (options.output.ast) {
  153. result.ast = toplevel;
  154. }
  155. if (!HOP(options.output, "code") || options.output.code) {
  156. if (options.sourceMap) {
  157. if (typeof options.sourceMap.content == "string") {
  158. options.sourceMap.content = JSON.parse(options.sourceMap.content);
  159. }
  160. options.output.source_map = SourceMap({
  161. file: options.sourceMap.filename,
  162. orig: options.sourceMap.content,
  163. root: options.sourceMap.root
  164. });
  165. if (options.sourceMap.includeSources) {
  166. if (files instanceof AST_Toplevel) {
  167. throw new Error("original source content unavailable");
  168. } else for (var name in files) if (HOP(files, name)) {
  169. options.output.source_map.get().setSourceContent(name, files[name]);
  170. }
  171. }
  172. }
  173. delete options.output.ast;
  174. delete options.output.code;
  175. var stream = OutputStream(options.output);
  176. toplevel.print(stream);
  177. result.code = stream.get();
  178. if (options.sourceMap) {
  179. result.map = options.output.source_map.toString();
  180. if (options.sourceMap.url == "inline") {
  181. result.code += "\n//# sourceMappingURL=data:application/json;charset=utf-8;base64," + to_base64(result.map);
  182. } else if (options.sourceMap.url) {
  183. result.code += "\n//# sourceMappingURL=" + options.sourceMap.url;
  184. }
  185. }
  186. }
  187. if (options.nameCache && options.mangle) {
  188. if (options.mangle.cache) options.nameCache.vars = to_json(options.mangle.cache);
  189. if (options.mangle.properties && options.mangle.properties.cache) {
  190. options.nameCache.props = to_json(options.mangle.properties.cache);
  191. }
  192. }
  193. if (timings) {
  194. timings.end = Date.now();
  195. result.timings = {
  196. parse: 1e-3 * (timings.scope1 - timings.parse),
  197. scope: 1e-3 * (timings.compress - timings.scope1 + timings.mangle - timings.scope2),
  198. compress: 1e-3 * (timings.scope2 - timings.compress),
  199. mangle: 1e-3 * (timings.properties - timings.mangle),
  200. properties: 1e-3 * (timings.output - timings.properties),
  201. output: 1e-3 * (timings.end - timings.output),
  202. total: 1e-3 * (timings.end - timings.start)
  203. }
  204. }
  205. if (warnings.length) {
  206. result.warnings = warnings;
  207. }
  208. return result;
  209. } catch (ex) {
  210. return { error: ex };
  211. } finally {
  212. AST_Node.warn_function = warn_function;
  213. }
  214. }