-
Notifications
You must be signed in to change notification settings - Fork 58
/
index.js
118 lines (90 loc) · 2.29 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import {Buffer} from 'node:buffer';
import {execa} from 'execa';
import isJpg from 'is-jpg';
import mozjpeg from 'mozjpeg';
const imageminMozjpeg = options => async buffer => {
options = {
trellis: true,
trellisDC: true,
overshoot: true,
...options,
};
if (!Buffer.isBuffer(buffer)) {
return Promise.reject(new TypeError('Expected a buffer'));
}
if (!isJpg(buffer)) {
return Promise.resolve(buffer);
}
// TODO: Remove these sometime far in the future
if (options.fastcrush) {
return Promise.reject(new Error('Option `fastcrush` was renamed to `fastCrush`'));
}
if (options.maxmemory) {
return Promise.reject(new Error('Option `maxmemory` was renamed to `maxMemory`'));
}
if (options.notrellis) {
return Promise.reject(new Error('Option `notrellis` was renamed to `trellis` and inverted'));
}
if (options.noovershoot) {
return Promise.reject(new Error('Option `noovershoot` was renamed to `overshoot` and inverted'));
}
const args = [];
if (typeof options.quality !== 'undefined') {
args.push('-quality', options.quality);
}
if (options.progressive === false) {
args.push('-baseline');
}
if (options.targa) {
args.push('-targa');
}
if (options.revert) {
args.push('-revert');
}
if (options.fastCrush) {
args.push('-fastcrush');
}
if (typeof options.dcScanOpt !== 'undefined') {
args.push('-dc-scan-opt', options.dcScanOpt);
}
if (!options.trellis) {
args.push('-notrellis');
}
if (!options.trellisDC) {
args.push('-notrellis-dc');
}
if (options.tune) {
args.push(`-tune-${options.tune}`);
}
if (!options.overshoot) {
args.push('-noovershoot');
}
if (options.arithmetic) {
args.push('-arithmetic');
}
if (options.dct) {
args.push('-dct', options.dct);
}
if (options.quantBaseline) {
args.push('-quant-baseline', options.quantBaseline);
}
if (typeof options.quantTable !== 'undefined') {
args.push('-quant-table', options.quantTable);
}
if (options.smooth) {
args.push('-smooth', options.smooth);
}
if (options.maxMemory) {
args.push('-maxmemory', options.maxMemory);
}
if (options.sample) {
args.push('-sample', options.sample.join(','));
}
const {stdout} = await execa(mozjpeg, args, {
encoding: null,
input: buffer,
maxBuffer: Number.POSITIVE_INFINITY,
});
return stdout;
};
export default imageminMozjpeg;