1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = worker;
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | async function worker(options) {
|
18 |
|
19 | let result = {
|
20 | data: options.input,
|
21 | filename: options.filename,
|
22 | warnings: [],
|
23 | errors: [],
|
24 | info: {}
|
25 | };
|
26 |
|
27 | if (!result.data) {
|
28 | result.errors.push(new Error("Empty input"));
|
29 | return result;
|
30 | }
|
31 | |
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | const normalizeProcessedResult = (item, filename) => {
|
39 | if (!item.info) {
|
40 | item.info = {};
|
41 | }
|
42 |
|
43 | if (!item.filename) {
|
44 | item.filename = options.filename;
|
45 | }
|
46 |
|
47 | if (!item.errors) {
|
48 | item.errors = [];
|
49 | }
|
50 |
|
51 | if (!item.warnings) {
|
52 | item.warnings = [];
|
53 | }
|
54 |
|
55 | if (options.severityError === "off") {
|
56 | item.warnings = [];
|
57 | item.errors = [];
|
58 | } else if (options.severityError === "warning") {
|
59 | item.warnings = [...item.warnings, ...item.errors];
|
60 | item.errors = [];
|
61 | }
|
62 |
|
63 | if (typeof filename !== "undefined" && typeof options.generateFilename !== "undefined") {
|
64 | item.filename = options.generateFilename(filename, {
|
65 | filename: item.filename
|
66 | });
|
67 | }
|
68 |
|
69 | return item;
|
70 | };
|
71 |
|
72 | const transformers = Array.isArray(options.transformer) ? options.transformer : [options.transformer];
|
73 |
|
74 | for (let i = 0; i <= transformers.length - 1; i++) {
|
75 | if (transformers[i].filter &&
|
76 | !transformers[i].filter(
|
77 |
|
78 | options.input, options.filename)) {
|
79 | continue;
|
80 | }
|
81 |
|
82 |
|
83 |
|
84 | let processedResult;
|
85 |
|
86 | try {
|
87 |
|
88 | processedResult = await transformers[i].implementation(result, transformers[i].options);
|
89 | } catch (error) {
|
90 | result.errors.push(error instanceof Error ? error : new Error(
|
91 |
|
92 | error));
|
93 | return result;
|
94 | }
|
95 |
|
96 | if (!processedResult || !Buffer.isBuffer(processedResult.data)) {
|
97 | result.errors.push(new Error("minimizer function doesn't return the 'data' property or result is not a 'Buffer' value"));
|
98 | return result;
|
99 | }
|
100 |
|
101 | result = normalizeProcessedResult(processedResult, transformers[i].filename);
|
102 | }
|
103 |
|
104 | return result;
|
105 | } |
\ | No newline at end of file |