UNPKG

1.68 kBJavaScriptView Raw
1"use strict";
2
3var _imagemin = _interopRequireDefault(require("imagemin"));
4
5var _normalizeConfig = _interopRequireDefault(require("./utils/normalize-config"));
6
7function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
8
9async function minify(options = {}) {
10 const {
11 input,
12 filename,
13 severityError,
14 isProductionMode
15 } = options;
16 const result = {
17 input,
18 filename,
19 warnings: [],
20 errors: []
21 };
22
23 if (!result.input) {
24 result.errors.push(new Error('Empty input'));
25 return result;
26 }
27
28 result.input = input;
29 let output;
30 let minimizerOptions;
31
32 try {
33 // Implement autosearch config on root directory of project in future
34 minimizerOptions = (0, _normalizeConfig.default)(options.minimizerOptions, {
35 options,
36 result
37 });
38 output = await _imagemin.default.buffer(result.input, minimizerOptions);
39 } catch (error) {
40 const errored = error instanceof Error ? error : new Error(error);
41
42 switch (severityError) {
43 case 'off':
44 case false:
45 break;
46
47 case 'error':
48 case true:
49 result.errors.push(errored);
50 break;
51
52 case 'warning':
53 result.warnings.push(errored);
54 break;
55
56 case 'auto':
57 default:
58 if (isProductionMode) {
59 result.errors.push(errored);
60 } else {
61 result.warnings.push(errored);
62 }
63
64 }
65
66 return {
67 filename,
68 output: input,
69 warnings: result.warnings,
70 errors: result.errors
71 };
72 }
73
74 return {
75 filename,
76 output,
77 warnings: result.warnings,
78 errors: result.errors
79 };
80}
81
82module.exports = minify;
\No newline at end of file