1 | "use strict";
|
2 |
|
3 | const {
|
4 | minify: terserMinify
|
5 | } = require('terser');
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | function buildTerserOptions(terserOptions = {}) {
|
55 | return { ...terserOptions,
|
56 | mangle: terserOptions.mangle == null ? true : typeof terserOptions.mangle === 'boolean' ? terserOptions.mangle : { ...terserOptions.mangle
|
57 | },
|
58 |
|
59 |
|
60 | sourceMap: undefined,
|
61 |
|
62 | ...(terserOptions.format ? {
|
63 | format: {
|
64 | beautify: false,
|
65 | ...terserOptions.format
|
66 | }
|
67 | } : {
|
68 | output: {
|
69 | beautify: false,
|
70 | ...terserOptions.output
|
71 | }
|
72 | })
|
73 | };
|
74 | }
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | function isObject(value) {
|
82 | const type = typeof value;
|
83 | return value != null && (type === 'object' || type === 'function');
|
84 | }
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | function buildComments(extractComments, terserOptions, extractedComments) {
|
94 |
|
95 | const condition = {};
|
96 | let comments;
|
97 |
|
98 | if (terserOptions.format) {
|
99 | ({
|
100 | comments
|
101 | } = terserOptions.format);
|
102 | } else if (terserOptions.output) {
|
103 | ({
|
104 | comments
|
105 | } = terserOptions.output);
|
106 | }
|
107 |
|
108 | condition.preserve = typeof comments !== 'undefined' ? comments : false;
|
109 |
|
110 | if (typeof extractComments === 'boolean' && extractComments) {
|
111 | condition.extract = 'some';
|
112 | } else if (typeof extractComments === 'string' || extractComments instanceof RegExp) {
|
113 | condition.extract = extractComments;
|
114 | } else if (typeof extractComments === 'function') {
|
115 | condition.extract = extractComments;
|
116 | } else if (extractComments && isObject(extractComments)) {
|
117 | condition.extract = typeof extractComments.condition === 'boolean' && extractComments.condition ? 'some' : typeof extractComments.condition !== 'undefined' ? extractComments.condition : 'some';
|
118 | } else {
|
119 |
|
120 |
|
121 | condition.preserve = typeof comments !== 'undefined' ? comments : 'some';
|
122 | condition.extract = false;
|
123 | }
|
124 |
|
125 |
|
126 | ['preserve', 'extract'].forEach(key => {
|
127 |
|
128 | let regexStr;
|
129 |
|
130 |
|
131 | let regex;
|
132 |
|
133 | switch (typeof condition[key]) {
|
134 | case 'boolean':
|
135 | condition[key] = condition[key] ? () => true : () => false;
|
136 | break;
|
137 |
|
138 | case 'function':
|
139 | break;
|
140 |
|
141 | case 'string':
|
142 | if (condition[key] === 'all') {
|
143 | condition[key] = () => true;
|
144 |
|
145 | break;
|
146 | }
|
147 |
|
148 | if (condition[key] === 'some') {
|
149 | condition[key] =
|
150 |
|
151 | (astNode, comment) => {
|
152 | return (comment.type === 'comment2' || comment.type === 'comment1') && /@preserve|@lic|@cc_on|^\**!/i.test(comment.value);
|
153 | };
|
154 |
|
155 | break;
|
156 | }
|
157 |
|
158 | regexStr =
|
159 |
|
160 | condition[key];
|
161 |
|
162 | condition[key] =
|
163 |
|
164 | (astNode, comment) => {
|
165 | return new RegExp(
|
166 |
|
167 | regexStr).test(comment.value);
|
168 | };
|
169 |
|
170 | break;
|
171 |
|
172 | default:
|
173 | regex =
|
174 |
|
175 | condition[key];
|
176 |
|
177 | condition[key] =
|
178 |
|
179 | (astNode, comment) =>
|
180 |
|
181 | regex.test(comment.value);
|
182 |
|
183 | }
|
184 | });
|
185 |
|
186 |
|
187 | return (astNode, comment) => {
|
188 | if (
|
189 |
|
190 | condition.extract(astNode, comment)) {
|
191 | const commentText = comment.type === 'comment2' ? `/*${comment.value}*/` : `//${comment.value}`;
|
192 |
|
193 | if (!extractedComments.includes(commentText)) {
|
194 | extractedComments.push(commentText);
|
195 | }
|
196 | }
|
197 |
|
198 | return (
|
199 |
|
200 | condition.preserve(astNode, comment)
|
201 | );
|
202 | };
|
203 | }
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 | async function minify(options) {
|
211 | const {
|
212 | name,
|
213 | input,
|
214 | inputSourceMap,
|
215 | minify: minifyFn,
|
216 | minifyOptions
|
217 | } = options;
|
218 |
|
219 | if (minifyFn) {
|
220 | return minifyFn({
|
221 | [name]: input
|
222 | }, inputSourceMap, minifyOptions);
|
223 | }
|
224 |
|
225 |
|
226 | const terserOptions = buildTerserOptions(minifyOptions);
|
227 |
|
228 | if (inputSourceMap) {
|
229 |
|
230 | terserOptions.sourceMap = {
|
231 | asObject: true
|
232 | };
|
233 | }
|
234 |
|
235 |
|
236 |
|
237 | const extractedComments = [];
|
238 | const {
|
239 | extractComments
|
240 | } = options;
|
241 |
|
242 | if (terserOptions.output) {
|
243 | terserOptions.output.comments = buildComments(extractComments, terserOptions, extractedComments);
|
244 | } else if (terserOptions.format) {
|
245 | terserOptions.format.comments = buildComments(extractComments, terserOptions, extractedComments);
|
246 | }
|
247 |
|
248 | const result = await terserMinify({
|
249 | [name]: input
|
250 | }, terserOptions);
|
251 | return { ...result,
|
252 | extractedComments
|
253 | };
|
254 | }
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 | function transform(options) {
|
262 |
|
263 |
|
264 |
|
265 | const evaluatedOptions =
|
266 |
|
267 |
|
268 | new Function('exports', 'require', 'module', '__filename', '__dirname', `'use strict'\nreturn ${options}`)(exports, require, module, __filename, __dirname);
|
269 | return minify(evaluatedOptions);
|
270 | }
|
271 |
|
272 | module.exports.minify = minify;
|
273 | module.exports.transform = transform; |
\ | No newline at end of file |