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