hookehuyr

refactor(vite): 使用 unplugin-auto-import 和 unplugin-vue-components 自动导入组件和 API

移除手动导入和注册 Vant 组件的代码,简化 main.js 文件。新增自动导入配置,提升开发效率和代码可维护性。
This diff is collapsed. Click to expand it.
...@@ -21,6 +21,8 @@ ...@@ -21,6 +21,8 @@
21 "autoprefixer": "^10.4.19", 21 "autoprefixer": "^10.4.19",
22 "postcss": "^8.4.35", 22 "postcss": "^8.4.35",
23 "tailwindcss": "^3.4.1", 23 "tailwindcss": "^3.4.1",
24 + "unplugin-auto-import": "^19.1.1",
25 + "unplugin-vue-components": "^28.4.1",
24 "vite": "^6.2.0" 26 "vite": "^6.2.0"
25 } 27 }
26 } 28 }
......
1 +/* eslint-disable */
2 +/* prettier-ignore */
3 +// @ts-nocheck
4 +// noinspection JSUnusedGlobalSymbols
5 +// Generated by unplugin-auto-import
6 +// biome-ignore lint: disable
7 +export {}
8 +declare global {
9 + const EffectScope: typeof import('vue')['EffectScope']
10 + const computed: typeof import('vue')['computed']
11 + const createApp: typeof import('vue')['createApp']
12 + const customRef: typeof import('vue')['customRef']
13 + const defineAsyncComponent: typeof import('vue')['defineAsyncComponent']
14 + const defineComponent: typeof import('vue')['defineComponent']
15 + const effectScope: typeof import('vue')['effectScope']
16 + const getCurrentInstance: typeof import('vue')['getCurrentInstance']
17 + const getCurrentScope: typeof import('vue')['getCurrentScope']
18 + const h: typeof import('vue')['h']
19 + const inject: typeof import('vue')['inject']
20 + const isProxy: typeof import('vue')['isProxy']
21 + const isReactive: typeof import('vue')['isReactive']
22 + const isReadonly: typeof import('vue')['isReadonly']
23 + const isRef: typeof import('vue')['isRef']
24 + const markRaw: typeof import('vue')['markRaw']
25 + const nextTick: typeof import('vue')['nextTick']
26 + const onActivated: typeof import('vue')['onActivated']
27 + const onBeforeMount: typeof import('vue')['onBeforeMount']
28 + const onBeforeRouteLeave: typeof import('vue-router')['onBeforeRouteLeave']
29 + const onBeforeRouteUpdate: typeof import('vue-router')['onBeforeRouteUpdate']
30 + const onBeforeUnmount: typeof import('vue')['onBeforeUnmount']
31 + const onBeforeUpdate: typeof import('vue')['onBeforeUpdate']
32 + const onDeactivated: typeof import('vue')['onDeactivated']
33 + const onErrorCaptured: typeof import('vue')['onErrorCaptured']
34 + const onMounted: typeof import('vue')['onMounted']
35 + const onRenderTracked: typeof import('vue')['onRenderTracked']
36 + const onRenderTriggered: typeof import('vue')['onRenderTriggered']
37 + const onScopeDispose: typeof import('vue')['onScopeDispose']
38 + const onServerPrefetch: typeof import('vue')['onServerPrefetch']
39 + const onUnmounted: typeof import('vue')['onUnmounted']
40 + const onUpdated: typeof import('vue')['onUpdated']
41 + const onWatcherCleanup: typeof import('vue')['onWatcherCleanup']
42 + const provide: typeof import('vue')['provide']
43 + const reactive: typeof import('vue')['reactive']
44 + const readonly: typeof import('vue')['readonly']
45 + const ref: typeof import('vue')['ref']
46 + const resolveComponent: typeof import('vue')['resolveComponent']
47 + const shallowReactive: typeof import('vue')['shallowReactive']
48 + const shallowReadonly: typeof import('vue')['shallowReadonly']
49 + const shallowRef: typeof import('vue')['shallowRef']
50 + const toRaw: typeof import('vue')['toRaw']
51 + const toRef: typeof import('vue')['toRef']
52 + const toRefs: typeof import('vue')['toRefs']
53 + const toValue: typeof import('vue')['toValue']
54 + const triggerRef: typeof import('vue')['triggerRef']
55 + const unref: typeof import('vue')['unref']
56 + const useAttrs: typeof import('vue')['useAttrs']
57 + const useCssModule: typeof import('vue')['useCssModule']
58 + const useCssVars: typeof import('vue')['useCssVars']
59 + const useId: typeof import('vue')['useId']
60 + const useLink: typeof import('vue-router')['useLink']
61 + const useModel: typeof import('vue')['useModel']
62 + const useRoute: typeof import('vue-router')['useRoute']
63 + const useRouter: typeof import('vue-router')['useRouter']
64 + const useSlots: typeof import('vue')['useSlots']
65 + const useTemplateRef: typeof import('vue')['useTemplateRef']
66 + const watch: typeof import('vue')['watch']
67 + const watchEffect: typeof import('vue')['watchEffect']
68 + const watchPostEffect: typeof import('vue')['watchPostEffect']
69 + const watchSyncEffect: typeof import('vue')['watchSyncEffect']
70 +}
71 +// for type re-export
72 +declare global {
73 + // @ts-ignore
74 + export type { Component, ComponentPublicInstance, ComputedRef, DirectiveBinding, ExtractDefaultPropTypes, ExtractPropTypes, ExtractPublicPropTypes, InjectionKey, PropType, Ref, MaybeRef, MaybeRefOrGetter, VNode, WritableComputedRef } from 'vue'
75 + import('vue')
76 +}
1 +/* eslint-disable */
2 +// @ts-nocheck
3 +// Generated by unplugin-vue-components
4 +// Read more: https://github.com/vuejs/core/pull/3399
5 +// biome-ignore lint: disable
6 +export {}
7 +
8 +/* prettier-ignore */
9 +declare module 'vue' {
10 + export interface GlobalComponents {
11 + ActivityCard: typeof import('./components/ui/ActivityCard.vue')['default']
12 + AppLayout: typeof import('./components/layout/AppLayout.vue')['default']
13 + BottomNav: typeof import('./components/layout/BottomNav.vue')['default']
14 + ConfirmDialog: typeof import('./components/ui/ConfirmDialog.vue')['default']
15 + CourseCard: typeof import('./components/ui/CourseCard.vue')['default']
16 + FrostedGlass: typeof import('./components/ui/FrostedGlass.vue')['default']
17 + GradientHeader: typeof import('./components/ui/GradientHeader.vue')['default']
18 + LiveStreamCard: typeof import('./components/ui/LiveStreamCard.vue')['default']
19 + MenuItem: typeof import('./components/ui/MenuItem.vue')['default']
20 + RouterLink: typeof import('vue-router')['RouterLink']
21 + RouterView: typeof import('vue-router')['RouterView']
22 + SearchBar: typeof import('./components/ui/SearchBar.vue')['default']
23 + SummerCampCard: typeof import('./components/ui/SummerCampCard.vue')['default']
24 + }
25 +}
...@@ -3,17 +3,8 @@ import './style.css' ...@@ -3,17 +3,8 @@ import './style.css'
3 import App from './App.vue' 3 import App from './App.vue'
4 import router from './router' 4 import router from './router'
5 5
6 -// 导入Vant组件和样式
7 -import { Button, NavBar, Tabbar, TabbarItem } from 'vant'
8 import 'vant/lib/index.css' 6 import 'vant/lib/index.css'
9 7
10 const app = createApp(App) 8 const app = createApp(App)
11 app.use(router) 9 app.use(router)
12 -
13 -// 注册Vant组件
14 -app.use(Button)
15 -app.use(NavBar)
16 -app.use(Tabbar)
17 -app.use(TabbarItem)
18 -
19 app.mount('#app') 10 app.mount('#app')
......
...@@ -8,11 +8,25 @@ ...@@ -8,11 +8,25 @@
8 import { defineConfig } from 'vite' 8 import { defineConfig } from 'vite'
9 import vue from '@vitejs/plugin-vue' 9 import vue from '@vitejs/plugin-vue'
10 import vueJsx from '@vitejs/plugin-vue-jsx' 10 import vueJsx from '@vitejs/plugin-vue-jsx'
11 -import path from "path"; 11 +import path from "path"
12 +import Components from 'unplugin-vue-components/vite'
13 +import AutoImport from 'unplugin-auto-import/vite'
14 +import { VantResolver } from 'unplugin-vue-components/resolvers'
12 15
13 // https://vite.dev/config/ 16 // https://vite.dev/config/
14 export default defineConfig({ 17 export default defineConfig({
15 - plugins: [vue(), vueJsx()], 18 + plugins: [
19 + vue(),
20 + vueJsx(),
21 + AutoImport({
22 + imports: ['vue', 'vue-router'],
23 + dts: 'src/auto-imports.d.ts',
24 + }),
25 + Components({
26 + resolvers: [VantResolver()],
27 + dts: 'src/components.d.ts',
28 + }),
29 + ],
16 resolve: { 30 resolve: {
17 alias: { // 将会被传递到 @rollup/plugin-alias 作为 entries 的选项。也可以是一个对象,或一个 { find, replacement } 的数组. 当使用文件系统路径的别名时,请始终使用绝对路径。相对路径的别名值会被原封不动地使用,因此无法被正常解析。 更高级的自定义解析方法可以通过 插件 实现。 31 alias: { // 将会被传递到 @rollup/plugin-alias 作为 entries 的选项。也可以是一个对象,或一个 { find, replacement } 的数组. 当使用文件系统路径的别名时,请始终使用绝对路径。相对路径的别名值会被原封不动地使用,因此无法被正常解析。 更高级的自定义解析方法可以通过 插件 实现。
18 "@": path.resolve(__dirname, "src"), 32 "@": path.resolve(__dirname, "src"),
......