minify.js 11 KB

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