Vite 中文文档 Vite 中文文档
指南
GitHub (opens new window)
指南
GitHub (opens new window)
  • vite

    • 指引
    • 为什么选 Vite
    • 开始
    • 功能
    • 命令行界面
    • 使用插件
    • 依赖预构建
    • 静态资源处理
    • 构建生产版本
    • 部署静态站点
    • 环境变量和模式
    • 服务端渲染
    • 后端集成
    • 与其他工具比较
    • 故障排除
    • 从 v3 迁移
  • API

  • 配置参考

  • vite

  • API

  • 配置参考

vite-plugin-imagemin


English| 中文

A vite plugin for compressing image assets

Install (yarn or npm)


node version:>=12.0.0

vite version:>=2.0.0

  1. ``` sh
  2. yarn add vite-plugin-imagemin -D

  3. ```

or

  1. ``` sh
  2. npm i vite-plugin-imagemin -D

  3. ```

China installation note


Because imagemin is not easy to install in China. Several solutions are now available

Use yarn to configure in package.json (recommended)

  1. ``` json
  2. "resolutions": {
  3.     "bin-wrapper": "npm:bin-wrapper-china"
  4.   },

  5. ```

Use npm, add the following configuration to the computer host file

  1. ``` shell
  2. 199.232.4.133 raw.githubusercontent.com
  3. ```

Install with cnpm (not recommended)

Usage


Configuration plugin in vite.config.ts

  1. ``` ts
  2. import viteImagemin from 'vite-plugin-imagemin'

  3. export default () => {
  4.   return {
  5.     plugins: [
  6.       viteImagemin({
  7.         gifsicle: {
  8.           optimizationLevel: 7,
  9.           interlaced: false,
  10.         },
  11.         optipng: {
  12.           optimizationLevel: 7,
  13.         },
  14.         mozjpeg: {
  15.           quality: 20,
  16.         },
  17.         pngquant: {
  18.           quality: [0.8, 0.9],
  19.           speed: 4,
  20.         },
  21.         svgo: {
  22.           plugins: [
  23.             {
  24.               name: 'removeViewBox',
  25.             },
  26.             {
  27.               name: 'removeEmptyAttrs',
  28.               active: false,
  29.             },
  30.           ],
  31.         },
  32.       }),
  33.     ],
  34.   }
  35. }
  36. ```

Options


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

Example


Run Example

  1. ``` shell
  2. npm run dev:play
  3. npm run dev:build

  4. ```

Sample project


Vben Admin

License


MIT

Inspiration


vite-plugin-compress
Last Updated: 2023-05-23 11:11:51