-
-
Notifications
You must be signed in to change notification settings - Fork 156
/
index.js
99 lines (80 loc) · 3.08 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
import path from 'node:path';
import process from 'node:process';
import prettyBytes from 'pretty-bytes';
import chalk from 'chalk';
import imagemin from 'imagemin';
import plur from 'plur';
import {gulpPlugin} from 'gulp-plugin-extras';
const PLUGIN_NAME = 'gulp-imagemin';
const defaultPlugins = ['gifsicle', 'mozjpeg', 'optipng', 'svgo'];
const loadPlugin = async (pluginName, ...arguments_) => {
try {
const {default: plugin} = await import(`imagemin-${pluginName}`);
return plugin(...arguments_);
} catch (error) {
console.log('er', error);
console.log(`${PLUGIN_NAME}: Could not load default plugin \`${pluginName}\``);
}
};
const exposePlugin = async plugin => (...arguments_) => loadPlugin(plugin, ...arguments_);
const getDefaultPlugins = async () => Promise.all(defaultPlugins.flatMap(plugin => loadPlugin(plugin)));
const validExtensions = new Set(['.jpg', '.jpeg', '.png', '.gif', '.svg']);
export default function gulpImagemin(plugins, options) {
if (typeof plugins === 'object' && !Array.isArray(plugins)) {
options = plugins;
plugins = undefined;
}
options = {
// TODO: Remove this when Gulp gets a real logger with levels
silent: process.argv.includes('--silent'),
verbose: process.argv.includes('--verbose'),
...options,
};
let totalBytes = 0;
let totalSavedBytes = 0;
let totalFiles = 0;
return gulpPlugin('gulp-imagemin', async file => {
if (!validExtensions.has(path.extname(file.path).toLowerCase())) {
if (options.verbose) {
console.log(`${PLUGIN_NAME}: Skipping unsupported image ${chalk.blue(file.relative)}`);
}
return file;
}
if (Array.isArray(plugins)) {
plugins = await Promise.all(plugins);
}
const localPlugins = plugins ?? await getDefaultPlugins();
const data = await imagemin.buffer(file.contents, {plugins: localPlugins});
const originalSize = file.contents.length;
const optimizedSize = data.length;
const saved = originalSize - optimizedSize;
const percent = originalSize > 0 ? (saved / originalSize) * 100 : 0;
const savedMessage = `saved ${prettyBytes(saved)} - ${percent.toFixed(1).replace(/\.0$/, '')}%`;
const message = saved > 0 ? savedMessage : 'already optimized';
if (saved > 0) {
totalBytes += originalSize;
totalSavedBytes += saved;
totalFiles++;
}
if (options.verbose) {
console.log(`${PLUGIN_NAME}:`, chalk.green('✔ ') + file.relative + chalk.gray(` (${message})`));
}
file.contents = Buffer.from(data);
return file;
}, {
async * onFinish() { // eslint-disable-line require-yield
if (!options.silent) {
const percent = totalBytes > 0 ? (totalSavedBytes / totalBytes) * 100 : 0;
let message = `Minified ${totalFiles} ${plur('image', totalFiles)}`;
if (totalFiles > 0) {
message += chalk.gray(` (saved ${prettyBytes(totalSavedBytes)} - ${percent.toFixed(1).replace(/\.0$/, '')}%)`);
}
console.log(`${PLUGIN_NAME}:`, message);
}
},
});
}
export const gifsicle = await exposePlugin('gifsicle');
export const mozjpeg = await exposePlugin('mozjpeg');
export const optipng = await exposePlugin('optipng');
export const svgo = await exposePlugin('svgo');