minify.js 10 KB

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