minify.js 9.2 KB

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