minify.js 11 KB

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