This repository was archived by the owner on Jan 10, 2025. It is now read-only.
forked from iampava/imagemin-webp-webpack-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.js
More file actions
148 lines (129 loc) · 5.44 KB
/
Copy pathplugin.js
File metadata and controls
148 lines (129 loc) · 5.44 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const imagemin = require('imagemin');
const webp = require('imagemin-webp');
const gif2webp = require('imagemin-gif2webp');
const GREEN = '\x1b[32m%s\x1b[0m';
const RED = '\x1b[31m%s\x1b[0m';
class ImageminWebpWebpackPlugin {
constructor({
config = [
{
test: /\.(jpe?g|png)/,
options: {
quality: 75
}
}
],
overrideExtension = true,
detailedLogs = false,
strict = true,
silent = false
} = {}) {
this.config = config;
this.detailedLogs = detailedLogs;
this.strict = strict;
this.overrideExtension = overrideExtension;
this.silent = silent;
}
apply(compiler) {
const onEmit = (compilation, cb) => {
let assetNames = Object.keys(compilation.assets);
let nrOfImagesFailed = 0;
if (this.silent && this.detailedLogs) {
compilation.warnings.push(new Error(`ImageminWebpWebpackPlugin: both the 'silent' and 'detailedLogs' options are true. Overriding 'detailedLogs' and disabling all console output.`));
}
Promise.all(
assetNames.map(name => {
for (let i = 0; i < this.config.length; i++) {
if (this.config[i].test.test(name)) {
let outputName = name;
if (this.overrideExtension) {
outputName = outputName
.split('.')
.slice(0, -1)
.join('.');
}
outputName = `${outputName}.webp`;
let currentAsset = compilation.assets[name];
return imagemin
.buffer(currentAsset.source(), {
plugins: [
webp(this.config[i].options),
gif2webp(this.config[i].quality),
]
})
.then(buffer => {
let savedKB = (currentAsset.size() - buffer.length) / 1000;
if (this.detailedLogs && !this.silent) {
console.log(GREEN, `${savedKB.toFixed(1)} KB saved from '${name}'`);
}
emitAsset(outputName, buffer, compilation);
return savedKB;
})
.catch(err => {
let customErr = new Error(`ImageminWebpWebpackPlugin: "${name}" wasn't converted!`);
nrOfImagesFailed++;
if (this.strict) {
compilation.errors.push(err, customErr);
} else if (this.detailedLogs) {
compilation.warnings.push(err, customErr);
}
return 0;
});
}
}
return Promise.resolve(0);
})
).then(savedKBArr => {
if (!this.silent) {
let totalKBSaved = savedKBArr.reduce((acc, cur) => acc + cur, 0);
if (totalKBSaved < 100) {
console.log(GREEN, `imagemin-webp-webpack-plugin: ${Math.floor(totalKBSaved)} KB saved`);
} else {
console.log(GREEN, `imagemin-webp-webpack-plugin: ${Math.floor(totalKBSaved / 100) / 10} MB saved`);
}
if (nrOfImagesFailed > 0) {
console.log(RED, `imagemin-webp-webpack-plugin: ${nrOfImagesFailed} images failed to convert to webp`);
}
}
cb();
});
};
hookPlugin(compiler, onEmit);
}
}
function hookPlugin(compiler, onEmit) {
if (compiler.hooks) {
// wait for compilation to start before detecting asset support
compiler.hooks.thisCompilation.tap('ImageminWebpWebpackPlugin', compilation => {
if (compilation.hooks.processAssets)
// webpack 5.x
compilation.hooks.processAssets.tapAsync({
name: 'ImageminWebpWebpackPlugin',
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_DERIVED
}, (assets, cb) => onEmit(compilation, cb));
else {
// webpack 4.x
compiler.hooks.emit.tapAsync('ImageminWebpWebpackPlugin', onEmit);
}
})
} else {
// older versions
compiler.plugin('emit', onEmit);
}
}
function emitAsset(name, buffer, compilation) {
if (compilation.emitAsset) {
// webpack 5.x
compilation.emitAsset(name, {
source: () => buffer,
size: () => buffer.length
})
} else {
// webpack 4.x & 3.x
compilation.assets[outputName] = {
source: () => buffer,
size: () => buffer.length
};
}
}
module.exports = ImageminWebpWebpackPlugin;