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