You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

minify.js 9.8KB

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