Tim van der Lippe | 652ccb7 | 2021-05-27 17:07:12 +0100 | [diff] [blame^] | 1 | "use strict"; |
| 2 | |
| 3 | var to_ascii, to_base64; |
| 4 | if (typeof Buffer == "undefined") { |
| 5 | to_ascii = atob; |
| 6 | to_base64 = btoa; |
| 7 | } else if (typeof Buffer.alloc == "undefined") { |
| 8 | to_ascii = function(b64) { |
| 9 | return new Buffer(b64, "base64").toString(); |
| 10 | }; |
| 11 | to_base64 = function(str) { |
| 12 | return new Buffer(str).toString("base64"); |
| 13 | }; |
| 14 | } else { |
| 15 | to_ascii = function(b64) { |
| 16 | return Buffer.from(b64, "base64").toString(); |
| 17 | }; |
| 18 | to_base64 = function(str) { |
| 19 | return Buffer.from(str).toString("base64"); |
| 20 | }; |
| 21 | } |
| 22 | |
| 23 | function read_source_map(name, toplevel) { |
| 24 | var comments = toplevel.end.comments_after; |
| 25 | for (var i = comments.length; --i >= 0;) { |
| 26 | var comment = comments[i]; |
| 27 | if (comment.type != "comment1") break; |
| 28 | var match = /^# ([^\s=]+)=(\S+)\s*$/.exec(comment.value); |
| 29 | if (!match) break; |
| 30 | if (match[1] == "sourceMappingURL") { |
| 31 | match = /^data:application\/json(;.*?)?;base64,(\S+)$/.exec(match[2]); |
| 32 | if (!match) break; |
| 33 | return to_ascii(match[2]); |
| 34 | } |
| 35 | } |
| 36 | AST_Node.warn("inline source map not found: {name}", { |
| 37 | name: name, |
| 38 | }); |
| 39 | } |
| 40 | |
| 41 | function parse_source_map(content) { |
| 42 | try { |
| 43 | return JSON.parse(content); |
| 44 | } catch (ex) { |
| 45 | throw new Error("invalid input source map: " + content); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | function set_shorthand(name, options, keys) { |
| 50 | keys.forEach(function(key) { |
| 51 | if (options[key]) { |
| 52 | if (typeof options[key] != "object") options[key] = {}; |
| 53 | if (!(name in options[key])) options[key][name] = options[name]; |
| 54 | } |
| 55 | }); |
| 56 | } |
| 57 | |
| 58 | function init_cache(cache) { |
| 59 | if (!cache) return; |
| 60 | if (!("props" in cache)) { |
| 61 | cache.props = new Dictionary(); |
| 62 | } else if (!(cache.props instanceof Dictionary)) { |
| 63 | cache.props = Dictionary.fromObject(cache.props); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | function to_json(cache) { |
| 68 | return { |
| 69 | props: cache.props.toObject() |
| 70 | }; |
| 71 | } |
| 72 | |
| 73 | function minify(files, options) { |
| 74 | try { |
| 75 | options = defaults(options, { |
| 76 | annotations: undefined, |
| 77 | compress: {}, |
| 78 | enclose: false, |
| 79 | ie8: false, |
| 80 | keep_fnames: false, |
| 81 | mangle: {}, |
| 82 | nameCache: null, |
| 83 | output: {}, |
| 84 | parse: {}, |
| 85 | rename: undefined, |
| 86 | sourceMap: false, |
| 87 | timings: false, |
| 88 | toplevel: false, |
| 89 | v8: false, |
| 90 | validate: false, |
| 91 | warnings: false, |
| 92 | webkit: false, |
| 93 | wrap: false, |
| 94 | }, true); |
| 95 | if (options.validate) AST_Node.enable_validation(); |
| 96 | var timings = options.timings && { start: Date.now() }; |
| 97 | if (options.rename === undefined) options.rename = options.compress && options.mangle; |
| 98 | if (options.annotations !== undefined) set_shorthand("annotations", options, [ "compress", "output" ]); |
| 99 | if (options.ie8) set_shorthand("ie8", options, [ "compress", "mangle", "output" ]); |
| 100 | if (options.keep_fnames) set_shorthand("keep_fnames", options, [ "compress", "mangle" ]); |
| 101 | if (options.toplevel) set_shorthand("toplevel", options, [ "compress", "mangle" ]); |
| 102 | if (options.v8) set_shorthand("v8", options, [ "mangle", "output" ]); |
| 103 | if (options.webkit) set_shorthand("webkit", options, [ "mangle", "output" ]); |
| 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 | ie8: false, |
| 110 | keep_fnames: false, |
| 111 | properties: false, |
| 112 | reserved: [], |
| 113 | toplevel: false, |
| 114 | v8: false, |
| 115 | webkit: false, |
| 116 | }, true); |
| 117 | if (options.mangle.properties) { |
| 118 | if (typeof options.mangle.properties != "object") { |
| 119 | options.mangle.properties = {}; |
| 120 | } |
| 121 | if (options.mangle.properties.keep_quoted) { |
| 122 | quoted_props = options.mangle.properties.reserved; |
| 123 | if (!Array.isArray(quoted_props)) quoted_props = []; |
| 124 | options.mangle.properties.reserved = quoted_props; |
| 125 | } |
| 126 | if (options.nameCache && !("cache" in options.mangle.properties)) { |
| 127 | options.mangle.properties.cache = options.nameCache.props || {}; |
| 128 | } |
| 129 | } |
| 130 | init_cache(options.mangle.cache); |
| 131 | init_cache(options.mangle.properties.cache); |
| 132 | } |
| 133 | if (options.sourceMap) { |
| 134 | options.sourceMap = defaults(options.sourceMap, { |
| 135 | content: null, |
| 136 | filename: null, |
| 137 | includeSources: false, |
| 138 | names: true, |
| 139 | root: null, |
| 140 | url: null, |
| 141 | }, true); |
| 142 | } |
| 143 | var warnings = []; |
| 144 | if (options.warnings) AST_Node.log_function(function(warning) { |
| 145 | warnings.push(warning); |
| 146 | }, options.warnings == "verbose"); |
| 147 | if (timings) timings.parse = Date.now(); |
| 148 | var toplevel; |
| 149 | if (files instanceof AST_Toplevel) { |
| 150 | toplevel = files; |
| 151 | } else { |
| 152 | if (typeof files == "string") { |
| 153 | files = [ files ]; |
| 154 | } |
| 155 | options.parse = options.parse || {}; |
| 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) { |
| 168 | options.sourceMap.orig[name] = parse_source_map(inlined_content); |
| 169 | } |
| 170 | } else if (source_map_content) { |
| 171 | options.sourceMap.orig[name] = source_map_content; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | if (quoted_props) { |
| 176 | reserve_quoted_keys(toplevel, quoted_props); |
| 177 | } |
| 178 | [ "enclose", "wrap" ].forEach(function(action) { |
| 179 | var option = options[action]; |
| 180 | if (!option) return; |
| 181 | var orig = toplevel.print_to_string().slice(0, -1); |
| 182 | toplevel = toplevel[action](option); |
| 183 | files[toplevel.start.file] = toplevel.print_to_string().replace(orig, ""); |
| 184 | }); |
| 185 | if (options.validate) toplevel.validate_ast(); |
| 186 | if (timings) timings.rename = Date.now(); |
| 187 | if (options.rename) { |
| 188 | toplevel.figure_out_scope(options.mangle); |
| 189 | toplevel.expand_names(options.mangle); |
| 190 | } |
| 191 | if (timings) timings.compress = Date.now(); |
| 192 | if (options.compress) { |
| 193 | toplevel = new Compressor(options.compress).compress(toplevel); |
| 194 | if (options.validate) toplevel.validate_ast(); |
| 195 | } |
| 196 | if (timings) timings.scope = Date.now(); |
| 197 | if (options.mangle) toplevel.figure_out_scope(options.mangle); |
| 198 | if (timings) timings.mangle = Date.now(); |
| 199 | if (options.mangle) { |
| 200 | toplevel.compute_char_frequency(options.mangle); |
| 201 | toplevel.mangle_names(options.mangle); |
| 202 | } |
| 203 | if (timings) timings.properties = Date.now(); |
| 204 | if (options.mangle && options.mangle.properties) mangle_properties(toplevel, options.mangle.properties); |
| 205 | if (timings) timings.output = Date.now(); |
| 206 | var result = {}; |
| 207 | var output = defaults(options.output, { |
| 208 | ast: false, |
| 209 | code: true, |
| 210 | }); |
| 211 | if (output.ast) result.ast = toplevel; |
| 212 | if (output.code) { |
| 213 | if (options.sourceMap) { |
| 214 | output.source_map = SourceMap(options.sourceMap); |
| 215 | if (options.sourceMap.includeSources) { |
| 216 | if (files instanceof AST_Toplevel) { |
| 217 | throw new Error("original source content unavailable"); |
| 218 | } else for (var name in files) if (HOP(files, name)) { |
| 219 | output.source_map.setSourceContent(name, files[name]); |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | delete output.ast; |
| 224 | delete output.code; |
| 225 | var stream = OutputStream(output); |
| 226 | toplevel.print(stream); |
| 227 | result.code = stream.get(); |
| 228 | if (options.sourceMap) { |
| 229 | result.map = output.source_map.toString(); |
| 230 | var url = options.sourceMap.url; |
| 231 | if (url) { |
| 232 | result.code = result.code.replace(/\n\/\/# sourceMappingURL=\S+\s*$/, ""); |
| 233 | if (url == "inline") { |
| 234 | result.code += "\n//# sourceMappingURL=data:application/json;charset=utf-8;base64," + to_base64(result.map); |
| 235 | } else { |
| 236 | result.code += "\n//# sourceMappingURL=" + url; |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | if (options.nameCache && options.mangle) { |
| 242 | if (options.mangle.cache) options.nameCache.vars = to_json(options.mangle.cache); |
| 243 | if (options.mangle.properties && options.mangle.properties.cache) { |
| 244 | options.nameCache.props = to_json(options.mangle.properties.cache); |
| 245 | } |
| 246 | } |
| 247 | if (timings) { |
| 248 | timings.end = Date.now(); |
| 249 | result.timings = { |
| 250 | parse: 1e-3 * (timings.rename - timings.parse), |
| 251 | rename: 1e-3 * (timings.compress - timings.rename), |
| 252 | compress: 1e-3 * (timings.scope - timings.compress), |
| 253 | scope: 1e-3 * (timings.mangle - timings.scope), |
| 254 | mangle: 1e-3 * (timings.properties - timings.mangle), |
| 255 | properties: 1e-3 * (timings.output - timings.properties), |
| 256 | output: 1e-3 * (timings.end - timings.output), |
| 257 | total: 1e-3 * (timings.end - timings.start) |
| 258 | }; |
| 259 | } |
| 260 | if (warnings.length) { |
| 261 | result.warnings = warnings; |
| 262 | } |
| 263 | return result; |
| 264 | } catch (ex) { |
| 265 | return { error: ex }; |
| 266 | } finally { |
| 267 | AST_Node.log_function(); |
| 268 | AST_Node.disable_validation(); |
| 269 | } |
| 270 | } |