-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
50 lines (41 loc) · 1.2 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
import path from 'node:path';
import replaceExt from 'replace-ext';
import PluginError from 'plugin-error';
import through from 'through2';
import execBuffer from 'exec-buffer';
import bin from 'dwebp-bin';
const booleanFlags = new Set(['bmp', 'tiff', 'pam', 'ppm', 'pgm', 'yuv', 'nofancy', 'nofilter', 'nodither', 'mt', 'flip', 'noasm']);
const dwebp = (options = {}) => through.obj(async (file, enc, callback) => {
if (file.isNull()) {
callback(null, file);
return;
}
if (file.isStream()) {
callback(new PluginError('gulp-dwebp', 'Streaming not supported'));
return;
}
if (!['.webp'].includes(path.extname(file.path).toLowerCase())) {
callback(null, file);
return;
}
const args = ['-o', execBuffer.output, execBuffer.input];
for (const key of Object.keys(options)) {
args.push(`-${key}`);
if (!booleanFlags.has(key)) {
args.push(options[key]);
}
}
try {
const buffer = await execBuffer({
input: file.contents,
bin,
args,
});
file.contents = buffer;
file.path = replaceExt(file.path, '.png');
callback(null, file);
} catch (error) {
callback(new PluginError('gulp-dwebp', error));
}
});
export default dwebp;