UNPKG

1.47 kBJavaScriptView Raw
1"use strict";
2
3const minify = async options => {
4 const minifyFns = typeof options.minify === "function" ? [options.minify] : options.minify;
5 const result = {
6 code: options.input,
7 warnings: [],
8 errors: []
9 };
10
11 for (let i = 0; i <= minifyFns.length - 1; i++) {
12 const minifyFn = minifyFns[i];
13 const minifyOptions = Array.isArray(options.minimizerOptions) ? options.minimizerOptions[i] : options.minimizerOptions; // eslint-disable-next-line no-await-in-loop
14
15 const minifyResult = await minifyFn({
16 [options.name]: result.code
17 }, minifyOptions);
18
19 if (Object.prototype.toString.call(minifyResult) === "[object Object]" && minifyResult !== null && "code" in minifyResult) {
20 result.code = minifyResult.code;
21 result.warnings = result.warnings.concat(minifyResult.warnings || []);
22 result.errors = result.errors.concat(minifyResult.errors || []);
23 } else {
24 result.code = minifyResult;
25 }
26 }
27
28 return result;
29};
30
31async function transform(options) {
32 // 'use strict' => this === undefined (Clean Scope)
33 // Safer for possible security issues, albeit not critical at all here
34 // eslint-disable-next-line no-new-func, no-param-reassign
35 options = new Function("exports", "require", "module", "__filename", "__dirname", `'use strict'\nreturn ${options}`)(exports, require, module, __filename, __dirname);
36 return minify(options);
37}
38
39module.exports.minify = minify;
40module.exports.transform = transform;
\No newline at end of file