unibest/vite-plugins/imagemin.ts
2024-04-11 17:00:49 +08:00

44 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// TIPS: 很多用户无法安装这个插件所以先注释掉了如果您可以安装成功那就可以放开这个注释以及下面的viteImagemin配置
// 注意小程序有主包2M的限制所以一般图片会放到图片服务器不放本地那就不需要这个插件
// 如果是开发h5或者app的可以自行安装
import viteImagemin from 'vite-plugin-imagemin'
export default (enabled: boolean) => {
if (!enabled) {
return undefined
}
return viteImagemin({
gifsicle: {
// gif图片压缩
optimizationLevel: 3, // 选择1到3之间的优化级别
interlaced: false, // 隔行扫描gif进行渐进式渲染
// colors: 2 // 将每个输出GIF中不同颜色的数量减少到num或更少。数字必须介于2和256之间。
},
optipng: {
// png
optimizationLevel: 7, // 选择0到7之间的优化级别
},
mozjpeg: {
// jpeg
quality: 20, // 压缩质量范围从0(最差)到100(最佳)。
},
pngquant: {
// png
quality: [0.8, 0.9], // Min和max是介于0(最差)到1(最佳)之间的数字类似于JPEG。达到或超过最高质量所需的最少量的颜色。如果转换导致质量低于最低质量图像将不会被保存。
speed: 4, // 压缩速度1(强力)到11(最快)
},
svgo: {
// svg压缩
plugins: [
{
name: 'removeViewBox',
},
{
name: 'removeEmptyAttrs',
active: false,
},
],
},
})
}