vite项目推荐使用的几个插件——开发神器
创始人
2025-05-29 08:23:44

Vite 可以使用插件进行扩展,这得益于 Rollup 优秀的插件接口设计和一部分 Vite 独有的额外选项。

unplugin-vue-components

Vue 的按需组件自动导入。github地址

自动导入UI库

内置了大多数常见UI库解析器。以 Ant Design Vue 为例。

  • 安装
pnpm add unplugin-vue-components -D
  • Vite配置使用
// vite.config.js
import { defineConfig } from 'vite'
import Components from 'unplugin-vue-components/vite'
import {AntDesignVueResolver
} from 'unplugin-vue-components/resolvers'export default defineConfig({plugins: [Components({resolvers: [AntDesignVueResolver()]})]
})

unplugin-auto-import

自动导入 Composition API。github地址

  • 安装
pnpm add unplugin-auto-import -D
  • Vite配置使用
// vite.config.js
import { defineConfig } from 'vite'
import autoImport from 'unplugin-auto-import/vite'export default defineConfig({plugins: [autoImport({imports: ['vue','vue-router','pinia'],dts: false})]
})
  • 使用插件,代码前后对比

使用前:

import { computed, ref } from 'vue'
const count = ref(0)
const doubled = computed(() => count.value * 2)

使用后:

const count = ref(0)
const doubled = computed(() => count.value * 2)

rollup-plugin-visualizer

打包分析插件。

  • 安装
pnpm add rollup-plugin-visualizer -D
  • vite配置
// vite.config.js
import { defineConfig } from 'vite'
import { visualizer } from 'rollup-plugin-visualizer'export default defineConfig({plugins: [visualizer()]
})

打包后,会在根目录下生成一个 stats.html文件,用浏览器打开后,如下图:

visualizer

vite-plugin-compression

使用 gzipbrotli 压缩资源。github地址

  • 安装
pnpm add vite-plugin-compression -D
  • vite配置使用
// vite.config.js
import { defineConfig } from 'vite'
import compression from 'vite-plugin-compression'export default defineConfig({plugins: [// gz格式compression({threshold: 1024 * 500,   // 体积大于 threshold 才会被压缩,单位 bext: '.gz',   // 压缩文件格式deleteOriginFile: false   // 是否删除源文件})// br格式// compression({//   threshold: 1024 * 500,    // 体积大于 threshold 才会被压缩,单位 b//   ext: '.br',//   algorithm: 'brotliCompress',//   deleteOriginFile: false// })]
})
  • 参数说明
paramstypedefaultdefault
verbosebooleantrueWhether to output the compressed result in the console
filterRegExp or (file: string) => booleanDefaultFilterSpecify which resources are not compressed
disablebooleanfalseWhether to disable
thresholdnumber1025It will be compressed if the volume is larger than threshold, the unit is b
algorithmstringgzipCompression algorithm, optional [‘gzip’,‘brotliCompress’ ,‘deflate’,‘deflateRaw’]
extstring.gzSuffix of the generated compressed package
compressionOptionsobject-The parameters of the corresponding compression algorithm
deleteOriginFileboolean-Whether to delete source files after compression

相关内容

热门资讯

cad打印线条粗细设置 cad... 004-线型(下)打印样式设置和线型文件使用一、线宽设置方法制图规范里边的线宽要求,我们已经定义好,...
荼蘼什么意思 岁月缱绻葳蕤生香... 感谢作者【辰夕】的原创独家授权分享编辑整理:【多肉植物百科】百科君坐标:云南 曲靖春而至,季节流转,...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...
阿西吧是什么意思 阿西吧相当于... 即使你没有受到过任何外语培训,你也懂四国语言。汉语:你好英语:Shit韩语:阿西吧(아,씨발! )日...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...