关于vite自动压缩图片的问题

  在使用 vben 后台系统的时候,发现打包后的登录页大图十分模糊!显然是 vite 打包时进行了压缩操作,进一步发现原来是 imagemin 插件在作祟,前往根目录下build\vite\plugin路径修改配置,实现图片不被压缩!

  在 vben 源码中已给出 imagemin 文档链接:https://github.com/anncwb/vite-plugin-imagemin ,配置如下:

params type default default
verbose boolean true Whether to output the compressed result in the console
filter RegExp or (file: string) => boolean - Specify which resources are not compressed
disable boolean false Whether to disable
svgo object or false - See Options
gifsicle object or false - See Options
mozjpeg object or false - See Options
optipng object or false - See Options
pngquant object or false - See Options
webp object or false - See Options

  我们需要关注的是 filter 属性,它允许我们定义文件是否被压缩。
于是有以下代码:

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
import viteImagemin from "vite-plugin-imagemin";

export function configImageminPlugin() {
const plugin = viteImagemin({
gifsicle: {
optimizationLevel: 7,
interlaced: false,
},
optipng: {
optimizationLevel: 7,
},
mozjpeg: {
quality: 8,
},
pngquant: {
quality: [0.8, 0.9],
speed: 4,
},
svgo: {
plugins: [
{
removeViewBox: false,
},
{
removeEmptyAttrs: false,
},
],
},
// 自定义不被压缩的文件
filter: (file) => {
return ["login_pv.jpg", "login_bg.jpg"].indexOf(file) > -1;
},
});
return plugin;
}

以上!


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!