UNPKG

9.7 kBJavaScriptView Raw
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 } 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 timings: false,
81 toplevel: false,
82 warnings: false,
83 wrap: false,
84 }, true);
85 var timings = options.timings && {
86 start: Date.now()
87 };
88 if (options.keep_classnames === undefined) {
89 options.keep_classnames = options.keep_fnames;
90 }
91 if (options.rename === undefined) {
92 options.rename = options.compress && options.mangle;
93 }
94 if (options.output && options.format) {
95 throw new Error("Please only specify either output or format option, preferrably format.");
96 }
97 options.format = options.format || options.output || {};
98 set_shorthand("ecma", options, [ "parse", "compress", "format" ]);
99 set_shorthand("ie8", options, [ "compress", "mangle", "format" ]);
100 set_shorthand("keep_classnames", options, [ "compress", "mangle" ]);
101 set_shorthand("keep_fnames", options, [ "compress", "mangle" ]);
102 set_shorthand("module", options, [ "parse", "compress", "mangle" ]);
103 set_shorthand("safari10", options, [ "mangle", "format" ]);
104 set_shorthand("toplevel", options, [ "compress", "mangle" ]);
105 set_shorthand("warnings", options, [ "compress" ]); // legacy
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 ie8: false,
112 keep_classnames: false,
113 keep_fnames: false,
114 module: false,
115 properties: false,
116 reserved: [],
117 safari10: false,
118 toplevel: false,
119 }, true);
120 if (options.mangle.properties) {
121 if (typeof options.mangle.properties != "object") {
122 options.mangle.properties = {};
123 }
124 if (options.mangle.properties.keep_quoted) {
125 quoted_props = options.mangle.properties.reserved;
126 if (!Array.isArray(quoted_props)) quoted_props = [];
127 options.mangle.properties.reserved = quoted_props;
128 }
129 if (options.nameCache && !("cache" in options.mangle.properties)) {
130 options.mangle.properties.cache = options.nameCache.props || {};
131 }
132 }
133 init_cache(options.mangle.cache);
134 init_cache(options.mangle.properties.cache);
135 }
136 if (options.sourceMap) {
137 options.sourceMap = defaults(options.sourceMap, {
138 asObject: false,
139 content: null,
140 filename: null,
141 includeSources: false,
142 root: null,
143 url: null,
144 }, true);
145 }
146 if (timings) timings.parse = Date.now();
147 var toplevel;
148 if (files instanceof AST_Toplevel) {
149 toplevel = files;
150 } else {
151 if (typeof files == "string") {
152 files = [ files ];
153 }
154 options.parse = options.parse || {};
155 options.parse.toplevel = null;
156 for (var name in files) if (HOP(files, name)) {
157 options.parse.filename = name;
158 options.parse.toplevel = parse(files[name], options.parse);
159 if (options.sourceMap && options.sourceMap.content == "inline") {
160 if (Object.keys(files).length > 1)
161 throw new Error("inline source map only works with singular input");
162 options.sourceMap.content = read_source_map(files[name]);
163 }
164 }
165 toplevel = options.parse.toplevel;
166 }
167 if (quoted_props && options.mangle.properties.keep_quoted !== "strict") {
168 reserve_quoted_keys(toplevel, quoted_props);
169 }
170 if (options.wrap) {
171 toplevel = toplevel.wrap_commonjs(options.wrap);
172 }
173 if (options.enclose) {
174 toplevel = toplevel.wrap_enclose(options.enclose);
175 }
176 if (timings) timings.rename = Date.now();
177 // disable rename on harmony due to expand_names bug in for-of loops
178 // https://github.com/mishoo/UglifyJS2/issues/2794
179 if (0 && options.rename) {
180 toplevel.figure_out_scope(options.mangle);
181 toplevel.expand_names(options.mangle);
182 }
183 if (timings) timings.compress = Date.now();
184 if (options.compress) {
185 toplevel = new Compressor(options.compress, {
186 mangle_options: options.mangle
187 }).compress(toplevel);
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 base54.reset();
194 toplevel.compute_char_frequency(options.mangle);
195 toplevel.mangle_names(options.mangle);
196 }
197 if (timings) timings.properties = Date.now();
198 if (options.mangle && options.mangle.properties) {
199 toplevel = mangle_properties(toplevel, options.mangle.properties);
200 }
201 if (timings) timings.format = Date.now();
202 var result = {};
203 if (options.format.ast) {
204 result.ast = toplevel;
205 }
206 if (!HOP(options.format, "code") || options.format.code) {
207 if (options.sourceMap) {
208 options.format.source_map = await SourceMap({
209 file: options.sourceMap.filename,
210 orig: options.sourceMap.content,
211 root: options.sourceMap.root
212 });
213 if (options.sourceMap.includeSources) {
214 if (files instanceof AST_Toplevel) {
215 throw new Error("original source content unavailable");
216 } else for (var name in files) if (HOP(files, name)) {
217 options.format.source_map.get().setSourceContent(name, files[name]);
218 }
219 }
220 }
221 delete options.format.ast;
222 delete options.format.code;
223 var stream = OutputStream(options.format);
224 toplevel.print(stream);
225 result.code = stream.get();
226 if (options.sourceMap) {
227 if(options.sourceMap.asObject) {
228 result.map = options.format.source_map.get().toJSON();
229 } else {
230 result.map = options.format.source_map.toString();
231 }
232 if (options.sourceMap.url == "inline") {
233 var sourceMap = typeof result.map === "object" ? JSON.stringify(result.map) : result.map;
234 result.code += "\n//# sourceMappingURL=data:application/json;charset=utf-8;base64," + to_base64(sourceMap);
235 } else if (options.sourceMap.url) {
236 result.code += "\n//# sourceMappingURL=" + options.sourceMap.url;
237 }
238 }
239 }
240 if (options.nameCache && options.mangle) {
241 if (options.mangle.cache) options.nameCache.vars = cache_to_json(options.mangle.cache);
242 if (options.mangle.properties && options.mangle.properties.cache) {
243 options.nameCache.props = cache_to_json(options.mangle.properties.cache);
244 }
245 }
246 if (options.format && options.format.source_map) {
247 options.format.source_map.destroy();
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.format - timings.properties),
258 format: 1e-3 * (timings.end - timings.format),
259 total: 1e-3 * (timings.end - timings.start)
260 };
261 }
262 return result;
263}
264
265export {
266 minify,
267 to_ascii,
268};