-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
index.js
98 lines (82 loc) · 2.78 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
'use strict';
const path = require('path');
const gutil = require('gulp-util');
const through = require('through2-concurrent');
const prettyBytes = require('pretty-bytes');
const chalk = require('chalk');
const imagemin = require('imagemin');
const imageminGifsicle = require('imagemin-gifsicle');
const imageminJpegtran = require('imagemin-jpegtran');
const imageminOptipng = require('imagemin-optipng');
const imageminSvgo = require('imagemin-svgo');
const plur = require('plur');
module.exports = (plugins, opts) => {
if (typeof plugins === 'object' && !Array.isArray(plugins)) {
opts = plugins;
plugins = null;
}
opts = Object.assign({
// TODO: remove this when gulp get's a real logger with levels
verbose: process.argv.indexOf('--verbose') !== -1
}, opts);
const validExts = ['.jpg', '.jpeg', '.png', '.gif', '.svg'];
let totalBytes = 0;
let totalSavedBytes = 0;
let totalFiles = 0;
return through.obj((file, enc, cb) => {
if (file.isNull()) {
cb(null, file);
return;
}
if (file.isStream()) {
cb(new gutil.PluginError('gulp-imagemin', 'Streaming not supported'));
return;
}
if (validExts.indexOf(path.extname(file.path).toLowerCase()) === -1) {
if (opts.verbose) {
gutil.log(`gulp-imagemin: Skipping unsupported image ${chalk.blue(file.relative)}`);
}
cb(null, file);
return;
}
const use = plugins || [
imageminGifsicle(),
imageminJpegtran(),
imageminOptipng(),
imageminSvgo()
];
imagemin.buffer(file.contents, {use})
.then(data => {
const originalSize = file.contents.length;
const optimizedSize = data.length;
const saved = originalSize - optimizedSize;
const percent = originalSize > 0 ? (saved / originalSize) * 100 : 0;
const savedMsg = `saved ${prettyBytes(saved)} - ${percent.toFixed(1).replace(/\.0$/, '')}%`;
const msg = saved > 0 ? savedMsg : 'already optimized';
totalBytes += originalSize;
totalSavedBytes += saved;
totalFiles++;
if (opts.verbose) {
gutil.log('gulp-imagemin:', chalk.green('✔ ') + file.relative + chalk.gray(` (${msg})`));
}
file.contents = data;
cb(null, file);
})
.catch(err => {
// TODO: remove this setImmediate when gulp 4 is targeted
setImmediate(cb, new gutil.PluginError('gulp-imagemin', err, {fileName: file.path}));
});
}, cb => {
const percent = totalBytes > 0 ? (totalSavedBytes / totalBytes) * 100 : 0;
let msg = `Minified ${totalFiles} ${plur('image', totalFiles)}`;
if (totalFiles > 0) {
msg += chalk.gray(` (saved ${prettyBytes(totalSavedBytes)} - ${percent.toFixed(1).replace(/\.0$/, '')}%)`);
}
gutil.log('gulp-imagemin:', msg);
cb();
});
};
module.exports.gifsicle = imageminGifsicle;
module.exports.jpegtran = imageminJpegtran;
module.exports.optipng = imageminOptipng;
module.exports.svgo = imageminSvgo;