UNPKG

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