webpack image-webpack-loader

使用image-webpack-loader压缩图片

安装

1
npm install image-webpack-loader --save-dev

配置

1
2
3
4
5
6
7
8
9
10
11
12
loaders: [{
test: /\.(gif|png|jpe?g|svg)$/i,
use: [
'file-loader',
{
loader: 'image-webpack-loader',
options: {
bypassOnDebug: true,
},
},
],
}]

可选配置

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
loaders: [{
test: /\.(gif|png|jpe?g|svg)$/i,
use: [
'file-loader',
{
loader: 'image-webpack-loader',
options: {
mozjpeg: {
progressive: true,
quality: 65
},
// optipng.enabled: false will disable optipng
optipng: {
enabled: false,
},
pngquant: {
quality: '65-90',
speed: 4
},
gifsicle: {
interlaced: false,
},
// the webp option will enable WEBP
webp: {
quality: 75
}
}
},
],
}]

使用imagemin-webpack-plugin压缩没有被file-loader处理的图片

安装

1
npm install imagemin-webpack-plugin --save-dev

配置

1
2
3
4
5
6
7
8
9
10
11
import ImageminPlugin from 'imagemin-webpack-plugin'

module.exports = {
plugins: [
// Copy the images folder and optimize all the images
new CopyWebpackPlugin([{
from: 'images/'
}]),
new ImageminPlugin({ test: /\.(jpe?g|png|gif|svg)$/i })
]
}
您的支持将鼓励我继续创作!