minify.js 9.3 KB

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