unplugin-vue-components - 自动导入、按需导入组件 安装
1 pnpm install unplugin-vue-components -D
配置
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 import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import vueSetupExtend from 'vite-plugin-vue-setup-extend' import legacy from '@vitejs/plugin-legacy' import compressPlugin from 'vite-plugin-compression' import viteImagemin from 'vite-plugin-imagemin' import Components from 'unplugin-vue-components/vite' import { ElementPlusResolver } from 'unplugin-vue-components/resolvers' export default defineConfig ({ plugins : [ vue (), ...省略插件配置, Components ({ dirs : ['src/components' ], resolvers : [ElementPlusResolver ()], extensions : ['vue' ], directoryAsNamespace : false , deep : true , }) ] })
现在已经注释掉HelloWorld组件引入,项目仍正常运行
1 2 3 4 5 6 7 8 9 10 11 12 13 <script setup lang="ts" name="App"> // This starter template is using Vue 3 <script setup> SFCs // Check out https://vuejs.org/api/sfc-script-setup.html#script-setup // import HelloWorld from './components/HelloWorld.vue' </script> <template> <img alt="Vue logo" src="./assets/logo.png" /> <HelloWorld msg="Hello Vue 3 + TypeScript + Vite" /> </template> <style> </style>
运行或打包会自动生成components.d.ts文件内容如下:
1 2 3 4 5 6 7 8 9 10 11 import '@vue/runtime-core' declare module '@vue/runtime-core' { export interface GlobalComponents { HelloWorld : typeof import ('./src/components/HelloWorld.vue' )['default' ] } } export {}
安装Vant UI 组件库 安装
vite-plugin-style-import - 按需引入组件库样式 安装
1 pnpm install vite-plugin-style-import consola -D
配置
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import vueSetupExtend from 'vite-plugin-vue-setup-extend' import legacy from '@vitejs/plugin-legacy' import compressPlugin from 'vite-plugin-compression' import viteImagemin from 'vite-plugin-imagemin' import Components from 'unplugin-vue-components/vite' import { VantResolver } from 'unplugin-vue-components/resolvers' import { createStyleImportPlugin, VantResolve } from 'vite-plugin-style-import' export default defineConfig ({ plugins : [ vue (), vueSetupExtend (), legacy ({ targets : ['defaults' , 'not IE 11' ], additionalLegacyPolyfills : ['regenerator-runtime/runtime' ] }), compressPlugin ({ verbose : false , disable : false , threshold : 10240 , algorithm : 'gzip' , ext : '.gz' , }), viteImagemin ({ gifsicle : { optimizationLevel : 7 , interlaced : false , }, optipng : { optimizationLevel : 7 , }, mozjpeg : { quality : 20 , }, pngquant : { quality : [0.8 , 0.9 ], speed : 4 , }, svgo : { plugins : [ { name : 'removeViewBox' , }, { name : 'removeEmptyAttrs' , active : false } ] } }), Components ({ dirs : ['src/components' ], resolvers : [VantResolver ()], extensions : ['vue' ], directoryAsNamespace : false , deep : true , }), createStyleImportPlugin ({ resolves : [VantResolve ()] }), ] })
使用vant组件不用手动引入,unplugin-vue-components 按需自动导入组件
1 <van-button type ="primary" > 主要按钮</van-button >
unplugin-auto-import - API自动导入 setup语法让我们不用再一个一个的把变量和方法都return出去就能在模板上使用,大大的解放了我们的双手。然而对于一些常用的VueAPI,比如ref、computed、watch等,还是每次都需要我们在页面上手动进行import { ref } from ‘vue’。 可以通过unplugin-auto-import实现自动导入,无需import即可在文件里使用Vue的API。
1 pnpm install unplugin-auto-import -D
配置
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import vueSetupExtend from 'vite-plugin-vue-setup-extend' import legacy from '@vitejs/plugin-legacy' import compressPlugin from 'vite-plugin-compression' import viteImagemin from 'vite-plugin-imagemin' import Components from 'unplugin-vue-components/vite' import { VantResolver } from 'unplugin-vue-components/resolvers' import { createStyleImportPlugin, VantResolve } from 'vite-plugin-style-import' import AutoImport from 'unplugin-auto-import/vite' export default defineConfig ({ plugins : [ vue (), vueSetupExtend (), legacy ({ targets : ['defaults' , 'not IE 11' ], additionalLegacyPolyfills : ['regenerator-runtime/runtime' ] }), compressPlugin ({ verbose : false , disable : false , threshold : 10240 , algorithm : 'gzip' , ext : '.gz' , }), viteImagemin ({ gifsicle : { optimizationLevel : 7 , interlaced : false , }, optipng : { optimizationLevel : 7 , }, mozjpeg : { quality : 20 , }, pngquant : { quality : [0.8 , 0.9 ], speed : 4 , }, svgo : { plugins : [ { name : 'removeViewBox' , }, { name : 'removeEmptyAttrs' , active : false } ] } }), Components ({ dirs : ['src/components' ], resolvers : [VantResolver ()], extensions : ['vue' ], directoryAsNamespace : false , deep : true , }), createStyleImportPlugin ({ resolves : [VantResolve ()] }), AutoImport ({ imports : ['vue' ], resolvers : [VantResolver ({ importStyle : false })] }) ] })
使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <script setup lang="ts"> const count = ref(0) const state = reactive({ data: '321' }) const { data } = toRefs(state) </script> <template> {{count}} {{data}} </template> <style scoped> </style>
运行或打包会自动生成auto-imports.d.ts文件内容如下:
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 declare global { const computed : typeof import ('vue' )['computed' ] const createApp : typeof import ('vue' )['createApp' ] const customRef : typeof import ('vue' )['customRef' ] const defineAsyncComponent : typeof import ('vue' )['defineAsyncComponent' ] const defineComponent : typeof import ('vue' )['defineComponent' ] const effectScope : typeof import ('vue' )['effectScope' ] const EffectScope : typeof import ('vue' )['EffectScope' ] const getCurrentInstance : typeof import ('vue' )['getCurrentInstance' ] const getCurrentScope : typeof import ('vue' )['getCurrentScope' ] const h : typeof import ('vue' )['h' ] const inject : typeof import ('vue' )['inject' ] const isReadonly : typeof import ('vue' )['isReadonly' ] const isRef : typeof import ('vue' )['isRef' ] const markRaw : typeof import ('vue' )['markRaw' ] const nextTick : typeof import ('vue' )['nextTick' ] const onActivated : typeof import ('vue' )['onActivated' ] const onBeforeMount : typeof import ('vue' )['onBeforeMount' ] const onBeforeUnmount : typeof import ('vue' )['onBeforeUnmount' ] const onBeforeUpdate : typeof import ('vue' )['onBeforeUpdate' ] const onDeactivated : typeof import ('vue' )['onDeactivated' ] const onErrorCaptured : typeof import ('vue' )['onErrorCaptured' ] const onMounted : typeof import ('vue' )['onMounted' ] const onRenderTracked : typeof import ('vue' )['onRenderTracked' ] const onRenderTriggered : typeof import ('vue' )['onRenderTriggered' ] const onScopeDispose : typeof import ('vue' )['onScopeDispose' ] const onServerPrefetch : typeof import ('vue' )['onServerPrefetch' ] const onUnmounted : typeof import ('vue' )['onUnmounted' ] const onUpdated : typeof import ('vue' )['onUpdated' ] const provide : typeof import ('vue' )['provide' ] const reactive : typeof import ('vue' )['reactive' ] const readonly : typeof import ('vue' )['readonly' ] const ref : typeof import ('vue' )['ref' ] const resolveComponent : typeof import ('vue' )['resolveComponent' ] const shallowReactive : typeof import ('vue' )['shallowReactive' ] const shallowReadonly : typeof import ('vue' )['shallowReadonly' ] const shallowRef : typeof import ('vue' )['shallowRef' ] const toRaw : typeof import ('vue' )['toRaw' ] const toRef : typeof import ('vue' )['toRef' ] const toRefs : typeof import ('vue' )['toRefs' ] const triggerRef : typeof import ('vue' )['triggerRef' ] const unref : typeof import ('vue' )['unref' ] const useAttrs : typeof import ('vue' )['useAttrs' ] const useCssModule : typeof import ('vue' )['useCssModule' ] const useCssVars : typeof import ('vue' )['useCssVars' ] const useSlots : typeof import ('vue' )['useSlots' ] const watch : typeof import ('vue' )['watch' ] const watchEffect : typeof import ('vue' )['watchEffect' ] } export {}