UNPKG

9.77 kBJavaScriptView Raw
1module.exports =
2/******/ (function() { // webpackBootstrap
3/******/ "use strict";
4/******/ var __webpack_modules__ = ({
5
6/***/ 149:
7/***/ (function(module, exports, __nccwpck_require__) {
8
9/* module decorator */ module = __nccwpck_require__.nmd(module);
10
11
12const {
13 minify: terserMinify
14} = __nccwpck_require__(775);
15/** @typedef {import("source-map").RawSourceMap} RawSourceMap */
16
17/** @typedef {import("./index.js").ExtractCommentsOptions} ExtractCommentsOptions */
18
19/** @typedef {import("./index.js").CustomMinifyFunction} CustomMinifyFunction */
20
21/** @typedef {import("./index.js").MinifyOptions} MinifyOptions */
22
23/** @typedef {import("terser").MinifyOptions} TerserMinifyOptions */
24
25/** @typedef {import("terser").MinifyOutput} MinifyOutput */
26
27/** @typedef {import("terser").FormatOptions} FormatOptions */
28
29/** @typedef {import("terser").MangleOptions} MangleOptions */
30
31/** @typedef {import("./index.js").ExtractCommentsFunction} ExtractCommentsFunction */
32
33/** @typedef {import("./index.js").ExtractCommentsCondition} ExtractCommentsCondition */
34
35/**
36 * @typedef {Object} InternalMinifyOptions
37 * @property {string} name
38 * @property {string} input
39 * @property {RawSourceMap | undefined} inputSourceMap
40 * @property {ExtractCommentsOptions} extractComments
41 * @property {CustomMinifyFunction | undefined} minify
42 * @property {MinifyOptions} minifyOptions
43 */
44
45/**
46 * @typedef {Array<string>} ExtractedComments
47 */
48
49/**
50 * @typedef {Promise<MinifyOutput & { extractedComments?: ExtractedComments}>} InternalMinifyResult
51 */
52
53/**
54 * @typedef {TerserMinifyOptions & { sourceMap: undefined } & ({ output: FormatOptions & { beautify: boolean } } | { format: FormatOptions & { beautify: boolean } })} NormalizedTerserMinifyOptions
55 */
56
57/**
58 * @param {TerserMinifyOptions} [terserOptions={}]
59 * @returns {NormalizedTerserMinifyOptions}
60 */
61
62
63function buildTerserOptions(terserOptions = {}) {
64 return { ...terserOptions,
65 mangle: terserOptions.mangle == null ? true : typeof terserOptions.mangle === "boolean" ? terserOptions.mangle : { ...terserOptions.mangle
66 },
67 // Ignoring sourceMap from options
68 // eslint-disable-next-line no-undefined
69 sourceMap: undefined,
70 // the `output` option is deprecated
71 ...(terserOptions.format ? {
72 format: {
73 beautify: false,
74 ...terserOptions.format
75 }
76 } : {
77 output: {
78 beautify: false,
79 ...terserOptions.output
80 }
81 })
82 };
83}
84/**
85 * @param {any} value
86 * @returns {boolean}
87 */
88
89
90function isObject(value) {
91 const type = typeof value;
92 return value != null && (type === "object" || type === "function");
93}
94/**
95 * @param {ExtractCommentsOptions} extractComments
96 * @param {NormalizedTerserMinifyOptions} terserOptions
97 * @param {ExtractedComments} extractedComments
98 * @returns {ExtractCommentsFunction}
99 */
100
101
102function buildComments(extractComments, terserOptions, extractedComments) {
103 /** @type {{ [index: string]: ExtractCommentsCondition }} */
104 const condition = {};
105 let comments;
106
107 if (terserOptions.format) {
108 ({
109 comments
110 } = terserOptions.format);
111 } else if (terserOptions.output) {
112 ({
113 comments
114 } = terserOptions.output);
115 }
116
117 condition.preserve = typeof comments !== "undefined" ? comments : false;
118
119 if (typeof extractComments === "boolean" && extractComments) {
120 condition.extract = "some";
121 } else if (typeof extractComments === "string" || extractComments instanceof RegExp) {
122 condition.extract = extractComments;
123 } else if (typeof extractComments === "function") {
124 condition.extract = extractComments;
125 } else if (extractComments && isObject(extractComments)) {
126 condition.extract = typeof extractComments.condition === "boolean" && extractComments.condition ? "some" : typeof extractComments.condition !== "undefined" ? extractComments.condition : "some";
127 } else {
128 // No extract
129 // Preserve using "commentsOpts" or "some"
130 condition.preserve = typeof comments !== "undefined" ? comments : "some";
131 condition.extract = false;
132 } // Ensure that both conditions are functions
133
134
135 ["preserve", "extract"].forEach(key => {
136 /** @type {undefined | string} */
137 let regexStr;
138 /** @type {undefined | RegExp} */
139
140 let regex;
141
142 switch (typeof condition[key]) {
143 case "boolean":
144 condition[key] = condition[key] ? () => true : () => false;
145 break;
146
147 case "function":
148 break;
149
150 case "string":
151 if (condition[key] === "all") {
152 condition[key] = () => true;
153
154 break;
155 }
156
157 if (condition[key] === "some") {
158 condition[key] =
159 /** @type {ExtractCommentsFunction} */
160 (astNode, comment) => (comment.type === "comment2" || comment.type === "comment1") && /@preserve|@lic|@cc_on|^\**!/i.test(comment.value);
161
162 break;
163 }
164
165 regexStr =
166 /** @type {string} */
167 condition[key];
168
169 condition[key] =
170 /** @type {ExtractCommentsFunction} */
171 (astNode, comment) => new RegExp(
172 /** @type {string} */
173 regexStr).test(comment.value);
174
175 break;
176
177 default:
178 regex =
179 /** @type {RegExp} */
180 condition[key];
181
182 condition[key] =
183 /** @type {ExtractCommentsFunction} */
184 (astNode, comment) =>
185 /** @type {RegExp} */
186 regex.test(comment.value);
187
188 }
189 }); // Redefine the comments function to extract and preserve
190 // comments according to the two conditions
191
192 return (astNode, comment) => {
193 if (
194 /** @type {{ extract: ExtractCommentsFunction }} */
195 condition.extract(astNode, comment)) {
196 const commentText = comment.type === "comment2" ? `/*${comment.value}*/` : `//${comment.value}`; // Don't include duplicate comments
197
198 if (!extractedComments.includes(commentText)) {
199 extractedComments.push(commentText);
200 }
201 }
202
203 return (
204 /** @type {{ preserve: ExtractCommentsFunction }} */
205 condition.preserve(astNode, comment)
206 );
207 };
208}
209/**
210 * @param {InternalMinifyOptions} options
211 * @returns {InternalMinifyResult}
212 */
213
214
215async function minify(options) {
216 const {
217 name,
218 input,
219 inputSourceMap,
220 minify: minifyFn,
221 minifyOptions
222 } = options;
223
224 if (minifyFn) {
225 return minifyFn({
226 [name]: input
227 }, inputSourceMap, minifyOptions);
228 } // Copy terser options
229
230
231 const terserOptions = buildTerserOptions(minifyOptions); // Let terser generate a SourceMap
232
233 if (inputSourceMap) {
234 // @ts-ignore
235 terserOptions.sourceMap = {
236 asObject: true
237 };
238 }
239 /** @type {ExtractedComments} */
240
241
242 const extractedComments = [];
243 const {
244 extractComments
245 } = options;
246
247 if (terserOptions.output) {
248 terserOptions.output.comments = buildComments(extractComments, terserOptions, extractedComments);
249 } else if (terserOptions.format) {
250 terserOptions.format.comments = buildComments(extractComments, terserOptions, extractedComments);
251 }
252
253 const result = await terserMinify({
254 [name]: input
255 }, terserOptions);
256 return { ...result,
257 extractedComments
258 };
259}
260/**
261 * @param {string} options
262 * @returns {InternalMinifyResult}
263 */
264
265
266function transform(options) {
267 // 'use strict' => this === undefined (Clean Scope)
268 // Safer for possible security issues, albeit not critical at all here
269 // eslint-disable-next-line no-param-reassign
270 const evaluatedOptions =
271 /** @type {InternalMinifyOptions} */
272 // eslint-disable-next-line no-new-func
273 new Function("exports", "require", "module", "__filename", "__dirname", `'use strict'\nreturn ${options}`)(exports, require, module, __filename, __dirname);
274 return minify(evaluatedOptions);
275}
276
277module.exports.minify = minify;
278module.exports.transform = transform;
279
280/***/ }),
281
282/***/ 775:
283/***/ (function(module) {
284
285module.exports = require("next/dist/compiled/terser");;
286
287/***/ })
288
289/******/ });
290/************************************************************************/
291/******/ // The module cache
292/******/ var __webpack_module_cache__ = {};
293/******/
294/******/ // The require function
295/******/ function __nccwpck_require__(moduleId) {
296/******/ // Check if module is in cache
297/******/ if(__webpack_module_cache__[moduleId]) {
298/******/ return __webpack_module_cache__[moduleId].exports;
299/******/ }
300/******/ // Create a new module (and put it into the cache)
301/******/ var module = __webpack_module_cache__[moduleId] = {
302/******/ id: moduleId,
303/******/ loaded: false,
304/******/ exports: {}
305/******/ };
306/******/
307/******/ // Execute the module function
308/******/ var threw = true;
309/******/ try {
310/******/ __webpack_modules__[moduleId](module, module.exports, __nccwpck_require__);
311/******/ threw = false;
312/******/ } finally {
313/******/ if(threw) delete __webpack_module_cache__[moduleId];
314/******/ }
315/******/
316/******/ // Flag the module as loaded
317/******/ module.loaded = true;
318/******/
319/******/ // Return the exports of the module
320/******/ return module.exports;
321/******/ }
322/******/
323/************************************************************************/
324/******/ /* webpack/runtime/node module decorator */
325/******/ !function() {
326/******/ __nccwpck_require__.nmd = function(module) {
327/******/ module.paths = [];
328/******/ if (!module.children) module.children = [];
329/******/ return module;
330/******/ };
331/******/ }();
332/******/
333/******/ /* webpack/runtime/compat */
334/******/
335/******/ __nccwpck_require__.ab = __dirname + "/";/************************************************************************/
336/******/ // module exports must be returned from runtime so entry inlining is disabled
337/******/ // startup
338/******/ // Load entry module and return exports
339/******/ return __nccwpck_require__(149);
340/******/ })()
341;
\No newline at end of file