minify.js 9.4 KB

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