utils.js
9.95 KB
1
2
3
4
5
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
"use strict";
/** @typedef {import("./index.js").Input} Input */
/** @typedef {import("source-map").RawSourceMap} RawSourceMap */
/** @typedef {import("source-map").SourceMapGenerator} SourceMapGenerator */
/** @typedef {import("./index.js").MinimizedResult} MinimizedResult */
/** @typedef {import("./index.js").CustomOptions} CustomOptions */
/** @typedef {import("postcss").ProcessOptions} ProcessOptions */
/** @typedef {import("postcss").Postcss} Postcss */
const notSettled = Symbol(`not-settled`);
/**
* @template T
* @typedef {() => Promise<T>} Task
*/
/**
* Run tasks with limited concurency.
* @template T
* @param {number} limit - Limit of tasks that run at once.
* @param {Task<T>[]} tasks - List of tasks to run.
* @returns {Promise<T[]>} A promise that fulfills to an array of the results
*/
function throttleAll(limit, tasks) {
if (!Number.isInteger(limit) || limit < 1) {
throw new TypeError(`Expected \`limit\` to be a finite number > 0, got \`${limit}\` (${typeof limit})`);
}
if (!Array.isArray(tasks) || !tasks.every(task => typeof task === `function`)) {
throw new TypeError(`Expected \`tasks\` to be a list of functions returning a promise`);
}
return new Promise((resolve, reject) => {
const result = Array(tasks.length).fill(notSettled);
const entries = tasks.entries();
const next = () => {
const {
done,
value
} = entries.next();
if (done) {
const isLast = !result.includes(notSettled);
if (isLast) resolve(result);
return;
}
const [index, task] = value;
/**
* @param {T} x
*/
const onFulfilled = x => {
result[index] = x;
next();
};
task().then(onFulfilled, reject);
};
Array(limit).fill(0).forEach(next);
});
}
/* istanbul ignore next */
/**
* @param {Input} input
* @param {RawSourceMap | undefined} sourceMap
* @param {CustomOptions} minimizerOptions
* @return {Promise<MinimizedResult>}
*/
async function cssnanoMinify(input, sourceMap, minimizerOptions = {
preset: "default"
}) {
/**
* @template T
* @param {string} module
* @returns {Promise<T>}
*/
const load = async module => {
let exports;
try {
// eslint-disable-next-line import/no-dynamic-require, global-require
exports = require(module);
return exports;
} catch (requireError) {
let importESM;
try {
// eslint-disable-next-line no-new-func
importESM = new Function("id", "return import(id);");
} catch (e) {
importESM = null;
}
if (
/** @type {Error & {code: string}} */
requireError.code === "ERR_REQUIRE_ESM" && importESM) {
exports = await importESM(module);
return exports.default;
}
throw requireError;
}
};
const [[name, code]] = Object.entries(input);
/** @type {ProcessOptions} */
const postcssOptions = {
from: name,
...minimizerOptions.processorOptions
};
if (typeof postcssOptions.parser === "string") {
try {
postcssOptions.parser = await load(postcssOptions.parser);
} catch (error) {
throw new Error(`Loading PostCSS "${postcssOptions.parser}" parser failed: ${
/** @type {Error} */
error.message}\n\n(@${name})`);
}
}
if (typeof postcssOptions.stringifier === "string") {
try {
postcssOptions.stringifier = await load(postcssOptions.stringifier);
} catch (error) {
throw new Error(`Loading PostCSS "${postcssOptions.stringifier}" stringifier failed: ${
/** @type {Error} */
error.message}\n\n(@${name})`);
}
}
if (typeof postcssOptions.syntax === "string") {
try {
postcssOptions.syntax = await load(postcssOptions.syntax);
} catch (error) {
throw new Error(`Loading PostCSS "${postcssOptions.syntax}" syntax failed: ${
/** @type {Error} */
error.message}\n\n(@${name})`);
}
}
if (sourceMap) {
postcssOptions.map = {
annotation: false
};
}
/** @type {Postcss} */
// eslint-disable-next-line global-require
const postcss = require("postcss").default; // @ts-ignore
// eslint-disable-next-line global-require
const cssnano = require("cssnano"); // @ts-ignore
// Types are broken
const result = await postcss([cssnano(minimizerOptions)]).process(code, postcssOptions);
return {
code: result.css,
map: result.map ? result.map.toJSON() : // eslint-disable-next-line no-undefined
undefined,
warnings: result.warnings().map(String)
};
}
/* istanbul ignore next */
/**
* @param {Input} input
* @param {RawSourceMap | undefined} sourceMap
* @param {CustomOptions} minimizerOptions
* @return {Promise<MinimizedResult>}
*/
async function cssoMinify(input, sourceMap, minimizerOptions) {
// eslint-disable-next-line global-require,import/no-extraneous-dependencies
const csso = require("csso");
const [[filename, code]] = Object.entries(input);
const result = csso.minify(code, {
filename,
sourceMap: Boolean(sourceMap),
...minimizerOptions
});
return {
code: result.css,
map: result.map ?
/** @type {SourceMapGenerator & { toJSON(): RawSourceMap }} */
result.map.toJSON() : // eslint-disable-next-line no-undefined
undefined
};
}
/* istanbul ignore next */
/**
* @param {Input} input
* @param {RawSourceMap | undefined} sourceMap
* @param {CustomOptions} minimizerOptions
* @return {Promise<MinimizedResult>}
*/
async function cleanCssMinify(input, sourceMap, minimizerOptions) {
// eslint-disable-next-line global-require,import/no-extraneous-dependencies
const CleanCSS = require("clean-css");
const [[name, code]] = Object.entries(input);
const result = await new CleanCSS({
sourceMap: Boolean(sourceMap),
...minimizerOptions,
returnPromise: true
}).minify({
[name]: {
styles: code
}
});
const generatedSourceMap = result.sourceMap &&
/** @type {SourceMapGenerator & { toJSON(): RawSourceMap }} */
result.sourceMap.toJSON(); // workaround for source maps on windows
if (generatedSourceMap) {
// eslint-disable-next-line global-require
const isWindowsPathSep = require("path").sep === "\\";
generatedSourceMap.sources = generatedSourceMap.sources.map(
/**
* @param {string} item
* @returns {string}
*/
item => isWindowsPathSep ? item.replace(/\\/g, "/") : item);
}
return {
code: result.styles,
map: generatedSourceMap,
warnings: result.warnings
};
}
/* istanbul ignore next */
/**
* @param {Input} input
* @param {RawSourceMap | undefined} sourceMap
* @param {CustomOptions} minimizerOptions
* @return {Promise<MinimizedResult>}
*/
async function esbuildMinify(input, sourceMap, minimizerOptions) {
/**
* @param {import("esbuild").TransformOptions} [esbuildOptions={}]
* @returns {import("esbuild").TransformOptions}
*/
const buildEsbuildOptions = (esbuildOptions = {}) => {
// Need deep copy objects to avoid https://github.com/terser/terser/issues/366
return {
loader: "css",
minify: true,
legalComments: "inline",
...esbuildOptions,
sourcemap: false
};
}; // eslint-disable-next-line import/no-extraneous-dependencies, global-require
const esbuild = require("esbuild"); // Copy `esbuild` options
const esbuildOptions = buildEsbuildOptions(minimizerOptions); // Let `esbuild` generate a SourceMap
if (sourceMap) {
esbuildOptions.sourcemap = true;
esbuildOptions.sourcesContent = false;
}
const [[filename, code]] = Object.entries(input);
esbuildOptions.sourcefile = filename;
const result = await esbuild.transform(code, esbuildOptions);
return {
code: result.code,
// eslint-disable-next-line no-undefined
map: result.map ? JSON.parse(result.map) : undefined,
warnings: result.warnings.length > 0 ? result.warnings.map(item => {
return {
source: item.location && item.location.file,
// eslint-disable-next-line no-undefined
line: item.location && item.location.line ? item.location.line : undefined,
// eslint-disable-next-line no-undefined
column: item.location && item.location.column ? item.location.column : undefined,
plugin: item.pluginName,
message: `${item.text}${item.detail ? `\nDetails:\n${item.detail}` : ""}${item.notes.length > 0 ? `\n\nNotes:\n${item.notes.map(note => `${note.location ? `[${note.location.file}:${note.location.line}:${note.location.column}] ` : ""}${note.text}${note.location ? `\nSuggestion: ${note.location.suggestion}` : ""}${note.location ? `\nLine text:\n${note.location.lineText}\n` : ""}`).join("\n")}` : ""}`
};
}) : []
};
}
/* istanbul ignore next */
/**
* @param {Input} input
* @param {RawSourceMap | undefined} sourceMap
* @param {CustomOptions} minimizerOptions
* @return {Promise<MinimizedResult>}
*/
async function parcelCssMinify(input, sourceMap, minimizerOptions) {
const [[filename, code]] = Object.entries(input);
/**
* @param {Partial<import("@parcel/css").TransformOptions>} [parcelCssOptions={}]
* @returns {import("@parcel/css").TransformOptions}
*/
const buildParcelCssOptions = (parcelCssOptions = {}) => {
// Need deep copy objects to avoid https://github.com/terser/terser/issues/366
return {
minify: true,
...parcelCssOptions,
sourceMap: false,
filename,
code: Buffer.from(code)
};
}; // eslint-disable-next-line import/no-extraneous-dependencies, global-require
const parcelCss = require("@parcel/css"); // Copy `esbuild` options
const parcelCssOptions = buildParcelCssOptions(minimizerOptions); // Let `esbuild` generate a SourceMap
if (sourceMap) {
parcelCssOptions.sourceMap = true;
}
const result = await parcelCss.transform(parcelCssOptions);
return {
code: result.code.toString(),
// eslint-disable-next-line no-undefined
map: result.map ? JSON.parse(result.map.toString()) : undefined
};
}
module.exports = {
throttleAll,
cssnanoMinify,
cssoMinify,
cleanCssMinify,
esbuildMinify,
parcelCssMinify
};