-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
47 lines (39 loc) · 1.13 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
import through2 from 'through2-concurrent';
import PluginError from 'plugin-error';
import optimize from './lib/optimize.js';
import log from './lib/log.js';
const image = (options = {}) => through2.obj({
maxConcurrency: options.concurrent,
}, async (file, enc, callback) => {
if (file.isNull()) {
return callback(null, file);
}
if (file.isStream()) {
return callback(new Error('gulp-image: Streaming is not supported'));
}
try {
const originalBuffer = file.contents;
const optimizedBuffer = await optimize(originalBuffer, {
pngquant: true,
optipng: false,
zopflipng: true,
jpegRecompress: false,
mozjpeg: true,
gifsicle: true,
svgo: true,
...options,
});
const originalBytes = originalBuffer.length;
const optimizedBytes = optimizedBuffer.length;
if (!options.quiet) {
log(file.relative, originalBytes, optimizedBytes);
}
if (originalBytes - optimizedBytes > 0) {
file.contents = optimizedBuffer;
}
callback(null, file);
} catch (error) {
callback(new PluginError('gulp-image', error));
}
});
export default image;