source: trip-planner-front/node_modules/terser/lib/minify.js@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 10.1 KB
Line 
1"use strict";
2/* eslint-env browser, es6, node */
3
4import {
5 defaults,
6 map_from_object,
7 map_to_object,
8 HOP,
9} from "./utils/index.js";
10import { AST_Toplevel, AST_Node } from "./ast.js";
11import { parse } from "./parse.js";
12import { OutputStream } from "./output.js";
13import { Compressor } from "./compress/index.js";
14import { base54 } from "./scope.js";
15import { SourceMap } from "./sourcemap.js";
16import {
17 mangle_properties,
18 reserve_quoted_keys,
19} from "./propmangle.js";
20
21var to_ascii = typeof atob == "undefined" ? function(b64) {
22 return Buffer.from(b64, "base64").toString();
23} : atob;
24var to_base64 = typeof btoa == "undefined" ? function(str) {
25 return Buffer.from(str).toString("base64");
26} : btoa;
27
28function read_source_map(code) {
29 var match = /(?:^|[^.])\/\/# sourceMappingURL=data:application\/json(;[\w=-]*)?;base64,([+/0-9A-Za-z]*=*)\s*$/.exec(code);
30 if (!match) {
31 console.warn("inline source map not found");
32 return null;
33 }
34 return to_ascii(match[2]);
35}
36
37function set_shorthand(name, options, keys) {
38 if (options[name]) {
39 keys.forEach(function(key) {
40 if (options[key]) {
41 if (typeof options[key] != "object") options[key] = {};
42 if (!(name in options[key])) options[key][name] = options[name];
43 }
44 });
45 }
46}
47
48function init_cache(cache) {
49 if (!cache) return;
50 if (!("props" in cache)) {
51 cache.props = new Map();
52 } else if (!(cache.props instanceof Map)) {
53 cache.props = map_from_object(cache.props);
54 }
55}
56
57function cache_to_json(cache) {
58 return {
59 props: map_to_object(cache.props)
60 };
61}
62
63async function minify(files, options) {
64 options = defaults(options, {
65 compress: {},
66 ecma: undefined,
67 enclose: false,
68 ie8: false,
69 keep_classnames: undefined,
70 keep_fnames: false,
71 mangle: {},
72 module: false,
73 nameCache: null,
74 output: null,
75 format: null,
76 parse: {},
77 rename: undefined,
78 safari10: false,
79 sourceMap: false,
80 spidermonkey: false,
81 timings: false,
82 toplevel: false,
83 warnings: false,
84 wrap: false,
85 }, true);
86 var timings = options.timings && {
87 start: Date.now()
88 };
89 if (options.keep_classnames === undefined) {
90 options.keep_classnames = options.keep_fnames;
91 }
92 if (options.rename === undefined) {
93 options.rename = options.compress && options.mangle;
94 }
95 if (options.output && options.format) {
96 throw new Error("Please only specify either output or format option, preferrably format.");
97 }
98 options.format = options.format || options.output || {};
99 set_shorthand("ecma", options, [ "parse", "compress", "format" ]);
100 set_shorthand("ie8", options, [ "compress", "mangle", "format" ]);
101 set_shorthand("keep_classnames", options, [ "compress", "mangle" ]);
102 set_shorthand("keep_fnames", options, [ "compress", "mangle" ]);
103 set_shorthand("module", options, [ "parse", "compress", "mangle" ]);
104 set_shorthand("safari10", options, [ "mangle", "format" ]);
105 set_shorthand("toplevel", options, [ "compress", "mangle" ]);
106 set_shorthand("warnings", options, [ "compress" ]); // legacy
107 var quoted_props;
108 if (options.mangle) {
109 options.mangle = defaults(options.mangle, {
110 cache: options.nameCache && (options.nameCache.vars || {}),
111 eval: false,
112 ie8: false,
113 keep_classnames: false,
114 keep_fnames: false,
115 module: false,
116 properties: false,
117 reserved: [],
118 safari10: false,
119 toplevel: false,
120 }, true);
121 if (options.mangle.properties) {
122 if (typeof options.mangle.properties != "object") {
123 options.mangle.properties = {};
124 }
125 if (options.mangle.properties.keep_quoted) {
126 quoted_props = options.mangle.properties.reserved;
127 if (!Array.isArray(quoted_props)) quoted_props = [];
128 options.mangle.properties.reserved = quoted_props;
129 }
130 if (options.nameCache && !("cache" in options.mangle.properties)) {
131 options.mangle.properties.cache = options.nameCache.props || {};
132 }
133 }
134 init_cache(options.mangle.cache);
135 init_cache(options.mangle.properties.cache);
136 }
137 if (options.sourceMap) {
138 options.sourceMap = defaults(options.sourceMap, {
139 asObject: false,
140 content: null,
141 filename: null,
142 includeSources: false,
143 root: null,
144 url: null,
145 }, true);
146 }
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" || (options.parse.spidermonkey && !Array.isArray(files))) {
153 files = [ files ];
154 }
155 options.parse = options.parse || {};
156 options.parse.toplevel = null;
157
158 if (options.parse.spidermonkey) {
159 options.parse.toplevel = AST_Node.from_mozilla_ast(Object.keys(files).reduce(function(toplevel, name) {
160 if (!toplevel) return files[name];
161 toplevel.body = toplevel.body.concat(files[name].body);
162 return toplevel;
163 }, null));
164 } else {
165 delete options.parse.spidermonkey;
166
167 for (var name in files) if (HOP(files, name)) {
168 options.parse.filename = name;
169 options.parse.toplevel = parse(files[name], options.parse);
170 if (options.sourceMap && options.sourceMap.content == "inline") {
171 if (Object.keys(files).length > 1)
172 throw new Error("inline source map only works with singular input");
173 options.sourceMap.content = read_source_map(files[name]);
174 }
175 }
176 }
177
178 toplevel = options.parse.toplevel;
179 }
180 if (quoted_props && options.mangle.properties.keep_quoted !== "strict") {
181 reserve_quoted_keys(toplevel, quoted_props);
182 }
183 if (options.wrap) {
184 toplevel = toplevel.wrap_commonjs(options.wrap);
185 }
186 if (options.enclose) {
187 toplevel = toplevel.wrap_enclose(options.enclose);
188 }
189 if (timings) timings.rename = Date.now();
190 // disable rename on harmony due to expand_names bug in for-of loops
191 // https://github.com/mishoo/UglifyJS2/issues/2794
192 if (0 && options.rename) {
193 toplevel.figure_out_scope(options.mangle);
194 toplevel.expand_names(options.mangle);
195 }
196 if (timings) timings.compress = Date.now();
197 if (options.compress) {
198 toplevel = new Compressor(options.compress, {
199 mangle_options: options.mangle
200 }).compress(toplevel);
201 }
202 if (timings) timings.scope = Date.now();
203 if (options.mangle) toplevel.figure_out_scope(options.mangle);
204 if (timings) timings.mangle = Date.now();
205 if (options.mangle) {
206 base54.reset();
207 toplevel.compute_char_frequency(options.mangle);
208 toplevel.mangle_names(options.mangle);
209 }
210 if (timings) timings.properties = Date.now();
211 if (options.mangle && options.mangle.properties) {
212 toplevel = mangle_properties(toplevel, options.mangle.properties);
213 }
214 if (timings) timings.format = Date.now();
215 var result = {};
216 if (options.format.ast) {
217 result.ast = toplevel;
218 }
219 if (options.format.spidermonkey) {
220 result.ast = toplevel.to_mozilla_ast();
221 }
222 if (!HOP(options.format, "code") || options.format.code) {
223 if (options.sourceMap) {
224 options.format.source_map = await SourceMap({
225 file: options.sourceMap.filename,
226 orig: options.sourceMap.content,
227 root: options.sourceMap.root
228 });
229 if (options.sourceMap.includeSources) {
230 if (files instanceof AST_Toplevel) {
231 throw new Error("original source content unavailable");
232 } else for (var name in files) if (HOP(files, name)) {
233 options.format.source_map.get().setSourceContent(name, files[name]);
234 }
235 }
236 }
237 delete options.format.ast;
238 delete options.format.code;
239 delete options.format.spidermonkey;
240 var stream = OutputStream(options.format);
241 toplevel.print(stream);
242 result.code = stream.get();
243 if (options.sourceMap) {
244 if(options.sourceMap.asObject) {
245 result.map = options.format.source_map.get().toJSON();
246 } else {
247 result.map = options.format.source_map.toString();
248 }
249 if (options.sourceMap.url == "inline") {
250 var sourceMap = typeof result.map === "object" ? JSON.stringify(result.map) : result.map;
251 result.code += "\n//# sourceMappingURL=data:application/json;charset=utf-8;base64," + to_base64(sourceMap);
252 } else if (options.sourceMap.url) {
253 result.code += "\n//# sourceMappingURL=" + options.sourceMap.url;
254 }
255 }
256 }
257 if (options.nameCache && options.mangle) {
258 if (options.mangle.cache) options.nameCache.vars = cache_to_json(options.mangle.cache);
259 if (options.mangle.properties && options.mangle.properties.cache) {
260 options.nameCache.props = cache_to_json(options.mangle.properties.cache);
261 }
262 }
263 if (options.format && options.format.source_map) {
264 options.format.source_map.destroy();
265 }
266 if (timings) {
267 timings.end = Date.now();
268 result.timings = {
269 parse: 1e-3 * (timings.rename - timings.parse),
270 rename: 1e-3 * (timings.compress - timings.rename),
271 compress: 1e-3 * (timings.scope - timings.compress),
272 scope: 1e-3 * (timings.mangle - timings.scope),
273 mangle: 1e-3 * (timings.properties - timings.mangle),
274 properties: 1e-3 * (timings.format - timings.properties),
275 format: 1e-3 * (timings.end - timings.format),
276 total: 1e-3 * (timings.end - timings.start)
277 };
278 }
279 return result;
280}
281
282export {
283 minify,
284 to_ascii,
285};
Note: See TracBrowser for help on using the repository browser.