| [9af201e] | 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | /** @typedef {import("./index.js").MinimizedResult} MinimizedResult */
|
|---|
| 4 |
|
|---|
| 5 | /** @typedef {import("source-map").RawSourceMap} RawSourceMap */
|
|---|
| 6 |
|
|---|
| 7 | /** @typedef {import("./index.js").InternalResult} InternalResult */
|
|---|
| 8 |
|
|---|
| 9 | /**
|
|---|
| 10 | * @template T
|
|---|
| 11 | * @param {import("./index.js").InternalOptions<T>} options
|
|---|
| 12 | * @returns {Promise<InternalResult>}
|
|---|
| 13 | */
|
|---|
| 14 | const minify = async options => {
|
|---|
| 15 | const minifyFns = Array.isArray(options.minimizer.implementation) ? options.minimizer.implementation : [options.minimizer.implementation];
|
|---|
| 16 | /** @type {InternalResult} */
|
|---|
| 17 |
|
|---|
| 18 | const result = {
|
|---|
| 19 | outputs: [],
|
|---|
| 20 | warnings: [],
|
|---|
| 21 | errors: []
|
|---|
| 22 | };
|
|---|
| 23 | let needSourceMap = false;
|
|---|
| 24 |
|
|---|
| 25 | for (let i = 0; i <= minifyFns.length - 1; i++) {
|
|---|
| 26 | const minifyFn = minifyFns[i];
|
|---|
| 27 | const minifyOptions = Array.isArray(options.minimizer.options) ? options.minimizer.options[i] : options.minimizer.options;
|
|---|
| 28 | const prevResult = result.outputs.length > 0 ? result.outputs[result.outputs.length - 1] : {
|
|---|
| 29 | code: options.input,
|
|---|
| 30 | map: options.inputSourceMap
|
|---|
| 31 | };
|
|---|
| 32 | const {
|
|---|
| 33 | code,
|
|---|
| 34 | map
|
|---|
| 35 | } = prevResult; // eslint-disable-next-line no-await-in-loop
|
|---|
| 36 |
|
|---|
| 37 | const minifyResult = await minifyFn({
|
|---|
| 38 | [options.name]: code
|
|---|
| 39 | }, map, minifyOptions);
|
|---|
| 40 |
|
|---|
| 41 | if (typeof minifyResult.code !== "string") {
|
|---|
| 42 | throw new Error("minimizer function doesn't return the 'code' property or result is not a string value");
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | if (minifyResult.map) {
|
|---|
| 46 | needSourceMap = true;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | if (minifyResult.errors) {
|
|---|
| 50 | result.errors = result.errors.concat(minifyResult.errors);
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | if (minifyResult.warnings) {
|
|---|
| 54 | result.warnings = result.warnings.concat(minifyResult.warnings);
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | result.outputs.push({
|
|---|
| 58 | code: minifyResult.code,
|
|---|
| 59 | map: minifyResult.map
|
|---|
| 60 | });
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | if (!needSourceMap) {
|
|---|
| 64 | result.outputs = [result.outputs[result.outputs.length - 1]];
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | return result;
|
|---|
| 68 | };
|
|---|
| 69 | /**
|
|---|
| 70 | * @param {string} options
|
|---|
| 71 | * @returns {Promise<InternalResult>}
|
|---|
| 72 | */
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 | async function transform(options) {
|
|---|
| 76 | // 'use strict' => this === undefined (Clean Scope)
|
|---|
| 77 | // Safer for possible security issues, albeit not critical at all here
|
|---|
| 78 | // eslint-disable-next-line no-new-func, no-param-reassign
|
|---|
| 79 | const evaluatedOptions = new Function("exports", "require", "module", "__filename", "__dirname", `'use strict'\nreturn ${options}`)(exports, require, module, __filename, __dirname);
|
|---|
| 80 | return minify(evaluatedOptions);
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | module.exports = {
|
|---|
| 84 | minify,
|
|---|
| 85 | transform
|
|---|
| 86 | }; |
|---|