minify.js 9.9 KB

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