Skip to content

Commit 56120c3

Browse files
committed
fix(webpack): register WebP imagemin in plugins for dev, avoid duplicate instance
Move ImageMinimizerPlugin to plugins (generator + optional prod minifier) so ?as=webp works when minimization is off. Remove it from optimization.minimizer to keep a single instance and avoid asset name conflicts. Minor shorthand for mode in dev/prod config.
1 parent 50732ce commit 56120c3

4 files changed

Lines changed: 35 additions & 29 deletions

File tree

config/plugins.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
const path = require('path')
2+
const ImageMinimizerPlugin = require('image-minimizer-webpack-plugin')
3+
const svgoconfig = require('./svgo.config')
24
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
35
const { WebpackManifestPlugin } = require('webpack-manifest-plugin')
46
const ESLintPlugin = require('eslint-webpack-plugin')
@@ -15,7 +17,38 @@ const SpriteHashPlugin = require('./webpack-sprite-hash-plugin')
1517

1618
module.exports = {
1719
get: function (mode) {
20+
const isProduction = mode === 'production'
21+
// A single instance: `optimization.minimizer` is only `apply()`'d when `minimize: true`
22+
// (see webpack `WebpackOptionsApply.js`), so WebP `?as=webp` must live on the main
23+
// `plugins` list to work with `yarn start` / dev. Image minify runs on `processAssets`
24+
// from this same plugin in production only (keeps dev watch fast).
25+
const imageMinimizerOptions = {
26+
loader: true,
27+
generator: [
28+
{
29+
preset: 'webp',
30+
implementation: ImageMinimizerPlugin.imageminGenerate,
31+
options: {
32+
plugins: ['imagemin-webp'],
33+
},
34+
},
35+
],
36+
}
37+
if (isProduction) {
38+
imageMinimizerOptions.minimizer = {
39+
implementation: ImageMinimizerPlugin.imageminMinify,
40+
options: {
41+
plugins: [
42+
['gifsicle', { interlaced: true }],
43+
['jpegtran', { progressive: true }],
44+
['optipng', { optimizationLevel: 5 }],
45+
['svgo', { svgoconfig }],
46+
],
47+
},
48+
}
49+
}
1850
const plugins = [
51+
new ImageMinimizerPlugin(imageMinimizerOptions),
1952
new WebpackThemeJsonPlugin({
2053
watch: mode !== 'production',
2154
}),

config/webpack.common.js

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
const path = require('path')
22
const entries = require('./entries')
3-
const ImageMinimizerPlugin = require('image-minimizer-webpack-plugin')
43
const TerserPlugin = require('terser-webpack-plugin')
5-
const svgoconfig = require('./svgo.config')
64

75
module.exports = {
86
entry: entries,
@@ -14,31 +12,6 @@ module.exports = {
1412
},
1513
optimization: {
1614
minimizer: [
17-
new ImageMinimizerPlugin({
18-
minimizer: {
19-
implementation: ImageMinimizerPlugin.imageminMinify,
20-
options: {
21-
// Lossless optimization with custom option
22-
// Feel free to experiment with options for better result for you
23-
plugins: [
24-
['gifsicle', { interlaced: true }],
25-
['jpegtran', { progressive: true }],
26-
['optipng', { optimizationLevel: 5 }],
27-
// Svgo configuration here https://github.com/svg/svgo#configuratio
28-
['svgo', { svgoconfig }],
29-
],
30-
},
31-
},
32-
generator: [
33-
{
34-
preset: 'webp',
35-
implementation: ImageMinimizerPlugin.imageminGenerate,
36-
options: {
37-
plugins: ['imagemin-webp'],
38-
},
39-
},
40-
],
41-
}),
4215
new TerserPlugin({
4316
parallel: true,
4417
terserOptions: {

config/webpack.dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const loaders = require('./loaders')
66
const mode = 'development'
77

88
module.exports = merge(common, {
9-
mode: mode,
9+
mode,
1010
stats: 'errors-only',
1111
devtool: 'inline-source-map',
1212
devServer: {

config/webpack.prod.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const loaders = require('./loaders')
55
const mode = 'production'
66

77
module.exports = merge(common, {
8-
mode: mode,
8+
mode,
99
stats: 'minimal',
1010
output: {
1111
filename: '[name]-min.js',

0 commit comments

Comments
 (0)