minify.js 11 KB

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