source: trip-planner-front/node_modules/terser-webpack-plugin/dist/minify.js@ 188ee53

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

initial commit

  • Property mode set to 100644
File size: 7.9 KB
RevLine 
[6a3a178]1"use strict";
2
3const {
4 minify: terserMinify
5} = require("terser");
6/** @typedef {import("source-map").RawSourceMap} RawSourceMap */
7
8/** @typedef {import("./index.js").ExtractCommentsOptions} ExtractCommentsOptions */
9
10/** @typedef {import("./index.js").CustomMinifyFunction} CustomMinifyFunction */
11
12/** @typedef {import("terser").MinifyOptions} TerserMinifyOptions */
13
14/** @typedef {import("terser").MinifyOutput} MinifyOutput */
15
16/** @typedef {import("terser").FormatOptions} FormatOptions */
17
18/** @typedef {import("terser").MangleOptions} MangleOptions */
19
20/** @typedef {import("./index.js").ExtractCommentsFunction} ExtractCommentsFunction */
21
22/** @typedef {import("./index.js").ExtractCommentsCondition} ExtractCommentsCondition */
23
24/**
25 * @typedef {Object.<any, any>} CustomMinifyOptions
26 */
27
28/**
29 * @typedef {Object} InternalMinifyOptions
30 * @property {string} name
31 * @property {string} input
32 * @property {RawSourceMap} [inputSourceMap]
33 * @property {ExtractCommentsOptions} extractComments
34 * @property {CustomMinifyFunction} [minify]
35 * @property {TerserMinifyOptions | CustomMinifyOptions} minifyOptions
36 */
37
38/**
39 * @typedef {Array<string>} ExtractedComments
40 */
41
42/**
43 * @typedef {Promise<MinifyOutput & { extractedComments?: ExtractedComments}>} InternalMinifyResult
44 */
45
46/**
47 * @typedef {TerserMinifyOptions & { sourceMap: undefined } & ({ output: FormatOptions & { beautify: boolean } } | { format: FormatOptions & { beautify: boolean } })} NormalizedTerserMinifyOptions
48 */
49
50/**
51 * @param {TerserMinifyOptions} [terserOptions={}]
52 * @returns {NormalizedTerserMinifyOptions}
53 */
54
55
56function buildTerserOptions(terserOptions = {}) {
57 // Need deep copy objects to avoid https://github.com/terser/terser/issues/366
58 return { ...terserOptions,
59 compress: typeof terserOptions.compress === "boolean" ? terserOptions.compress : { ...terserOptions.compress
60 },
61 // ecma: terserOptions.ecma,
62 // ie8: terserOptions.ie8,
63 // keep_classnames: terserOptions.keep_classnames,
64 // keep_fnames: terserOptions.keep_fnames,
65 mangle: terserOptions.mangle == null ? true : typeof terserOptions.mangle === "boolean" ? terserOptions.mangle : { ...terserOptions.mangle
66 },
67 // module: terserOptions.module,
68 // nameCache: { ...terserOptions.toplevel },
69 // the `output` option is deprecated
70 ...(terserOptions.format ? {
71 format: {
72 beautify: false,
73 ...terserOptions.format
74 }
75 } : {
76 output: {
77 beautify: false,
78 ...terserOptions.output
79 }
80 }),
81 parse: { ...terserOptions.parse
82 },
83 // safari10: terserOptions.safari10,
84 // Ignoring sourceMap from options
85 // eslint-disable-next-line no-undefined
86 sourceMap: undefined // toplevel: terserOptions.toplevel
87
88 };
89}
90/**
91 * @param {any} value
92 * @returns {boolean}
93 */
94
95
96function isObject(value) {
97 const type = typeof value;
98 return value != null && (type === "object" || type === "function");
99}
100/**
101 * @param {ExtractCommentsOptions} extractComments
102 * @param {NormalizedTerserMinifyOptions} terserOptions
103 * @param {ExtractedComments} extractedComments
104 * @returns {ExtractCommentsFunction}
105 */
106
107
108function buildComments(extractComments, terserOptions, extractedComments) {
109 /** @type {{ [index: string]: ExtractCommentsCondition }} */
110 const condition = {};
111 let comments;
112
113 if (terserOptions.format) {
114 ({
115 comments
116 } = terserOptions.format);
117 } else if (terserOptions.output) {
118 ({
119 comments
120 } = terserOptions.output);
121 }
122
123 condition.preserve = typeof comments !== "undefined" ? comments : false;
124
125 if (typeof extractComments === "boolean" && extractComments) {
126 condition.extract = "some";
127 } else if (typeof extractComments === "string" || extractComments instanceof RegExp) {
128 condition.extract = extractComments;
129 } else if (typeof extractComments === "function") {
130 condition.extract = extractComments;
131 } else if (extractComments && isObject(extractComments)) {
132 condition.extract = typeof extractComments.condition === "boolean" && extractComments.condition ? "some" : typeof extractComments.condition !== "undefined" ? extractComments.condition : "some";
133 } else {
134 // No extract
135 // Preserve using "commentsOpts" or "some"
136 condition.preserve = typeof comments !== "undefined" ? comments : "some";
137 condition.extract = false;
138 } // Ensure that both conditions are functions
139
140
141 ["preserve", "extract"].forEach(key => {
142 /** @type {undefined | string} */
143 let regexStr;
144 /** @type {undefined | RegExp} */
145
146 let regex;
147
148 switch (typeof condition[key]) {
149 case "boolean":
150 condition[key] = condition[key] ? () => true : () => false;
151 break;
152
153 case "function":
154 break;
155
156 case "string":
157 if (condition[key] === "all") {
158 condition[key] = () => true;
159
160 break;
161 }
162
163 if (condition[key] === "some") {
164 condition[key] =
165 /** @type {ExtractCommentsFunction} */
166 (astNode, comment) => (comment.type === "comment2" || comment.type === "comment1") && /@preserve|@lic|@cc_on|^\**!/i.test(comment.value);
167
168 break;
169 }
170
171 regexStr =
172 /** @type {string} */
173 condition[key];
174
175 condition[key] =
176 /** @type {ExtractCommentsFunction} */
177 (astNode, comment) => new RegExp(
178 /** @type {string} */
179 regexStr).test(comment.value);
180
181 break;
182
183 default:
184 regex =
185 /** @type {RegExp} */
186 condition[key];
187
188 condition[key] =
189 /** @type {ExtractCommentsFunction} */
190 (astNode, comment) =>
191 /** @type {RegExp} */
192 regex.test(comment.value);
193
194 }
195 }); // Redefine the comments function to extract and preserve
196 // comments according to the two conditions
197
198 return (astNode, comment) => {
199 if (
200 /** @type {{ extract: ExtractCommentsFunction }} */
201 condition.extract(astNode, comment)) {
202 const commentText = comment.type === "comment2" ? `/*${comment.value}*/` : `//${comment.value}`; // Don't include duplicate comments
203
204 if (!extractedComments.includes(commentText)) {
205 extractedComments.push(commentText);
206 }
207 }
208
209 return (
210 /** @type {{ preserve: ExtractCommentsFunction }} */
211 condition.preserve(astNode, comment)
212 );
213 };
214}
215/**
216 * @param {InternalMinifyOptions} options
217 * @returns {InternalMinifyResult}
218 */
219
220
221async function minify(options) {
222 const {
223 name,
224 input,
225 inputSourceMap,
226 minify: minifyFn,
227 minifyOptions
228 } = options;
229
230 if (minifyFn) {
231 return minifyFn({
232 [name]: input
233 }, inputSourceMap, minifyOptions);
234 } // Copy terser options
235
236
237 const terserOptions = buildTerserOptions(minifyOptions); // Let terser generate a SourceMap
238
239 if (inputSourceMap) {
240 // @ts-ignore
241 terserOptions.sourceMap = {
242 asObject: true
243 };
244 }
245 /** @type {ExtractedComments} */
246
247
248 const extractedComments = [];
249 const {
250 extractComments
251 } = options;
252
253 if (terserOptions.output) {
254 terserOptions.output.comments = buildComments(extractComments, terserOptions, extractedComments);
255 } else if (terserOptions.format) {
256 terserOptions.format.comments = buildComments(extractComments, terserOptions, extractedComments);
257 }
258
259 const result = await terserMinify({
260 [name]: input
261 }, terserOptions);
262 return { ...result,
263 extractedComments
264 };
265}
266/**
267 * @param {string} options
268 * @returns {InternalMinifyResult}
269 */
270
271
272function transform(options) {
273 // 'use strict' => this === undefined (Clean Scope)
274 // Safer for possible security issues, albeit not critical at all here
275 // eslint-disable-next-line no-param-reassign
276 const evaluatedOptions =
277 /** @type {InternalMinifyOptions} */
278 // eslint-disable-next-line no-new-func
279 new Function("exports", "require", "module", "__filename", "__dirname", `'use strict'\nreturn ${options}`)(exports, require, module, __filename, __dirname);
280 return minify(evaluatedOptions);
281}
282
283module.exports.minify = minify;
284module.exports.transform = transform;
Note: See TracBrowser for help on using the repository browser.