refactor: simplified code

This commit is contained in:
chansee97 2024-03-01 17:37:21 +08:00
parent b13b156bb9
commit 3c1b1e2336
24 changed files with 125 additions and 129 deletions

View File

@ -1 +0,0 @@
export * from './proxy'

View File

@ -1,2 +0,0 @@
export * from './config'
export * from './plugins'

76
build/plugins.ts Normal file
View File

@ -0,0 +1,76 @@
import path from 'node:path'
import UnoCSS from '@unocss/vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import type { PluginOption } from 'vite'
import Components from 'unplugin-vue-components/vite'
import AutoImport from 'unplugin-auto-import/vite'
import { NaiveUiResolver } from 'unplugin-vue-components/resolvers'
import viteCompression from 'vite-plugin-compression'
import Icons from 'unplugin-icons/vite'
// https://github.com/antfu/unplugin-icons
import IconsResolver from 'unplugin-icons/resolver'
// https://github.com/vbenjs/vite-plugin-svg-icons/blob/main/README.zh_CN.md
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
/**
* @description: vite插件配置
* @param {*} env -
* @return {*}
*/
export function setVitePlugins(env: ImportMetaEnv) {
const plugins = [
// support vue
vue(),
vueJsx(),
// support unocss
UnoCSS(),
// auto import api of lib
AutoImport({
imports: ['vue', 'vue-router', 'pinia', '@vueuse/core'],
include: [
/\.[tj]sx?$/,
/\.vue$/,
/\.vue\?vue/,
/\.md$/,
],
dts: 'src/typings/auto-imports.d.ts',
}),
// auto import components lib
Components({
dts: 'src/typings/components.d.ts',
resolvers: [IconsResolver(), NaiveUiResolver()],
}),
// auto import iconify's icons
Icons({
defaultStyle: 'display:inline-block',
compiler: 'vue3',
}),
// auto use svg icon
createSvgIconsPlugin({
// 指定需要缓存的图标文件夹
iconDirs: [path.resolve(__dirname, 'src/assets/icons')],
// 指定symbolId格式
symbolId: 'icon-[dir]-[name]',
// inject: 'body-last',
// customDomId: '__svg__icons__dom__',
}),
]
// use compression
if (env.VITE_COMPRESS_OPEN === 'Y') {
const { VITE_COMPRESS_TYPE = 'gzip' } = env
plugins.push(viteCompression({
algorithm: VITE_COMPRESS_TYPE, // 压缩算法
}))
}
return plugins
}

View File

@ -1,11 +0,0 @@
import viteCompression from 'vite-plugin-compression'
// https://github.com/vbenjs/vite-plugin-compression/blob/main/README.zh_CN.md
export default (env: ImportMetaEnv) => {
// 默认使用gzip压缩
const { VITE_COMPRESS_TYPE = 'gzip' } = env
return viteCompression({
algorithm: VITE_COMPRESS_TYPE, // 压缩算法
})
}

View File

@ -1,19 +0,0 @@
import type { PluginOption } from 'vite'
import UnoCSS from '@unocss/vite'
import vue from './vue'
import compress from './compress'
import unplugin from './unplugin'
/**
* @description: vite插件配置
* @param {*} env -
* @return {*}
*/
export function setVitePlugins(env: ImportMetaEnv) {
const plugins: PluginOption[] = [...vue, UnoCSS(), ...unplugin]
// 是否压缩
if (env.VITE_COMPRESS_OPEN === 'Y')
plugins.push(compress(env))
return plugins
}

View File

@ -1,43 +0,0 @@
import path from 'node:path'
import Components from 'unplugin-vue-components/vite'
import AutoImport from 'unplugin-auto-import/vite'
import { NaiveUiResolver } from 'unplugin-vue-components/resolvers'
import Icons from 'unplugin-icons/vite'
// https://github.com/antfu/unplugin-icons
import IconsResolver from 'unplugin-icons/resolver'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
// https://github.com/vbenjs/vite-plugin-svg-icons/blob/main/README.zh_CN.md
export default [
AutoImport({
imports: ['vue', 'vue-router', 'pinia', '@vueuse/core'],
include: [
/\.[tj]sx?$/,
/\.vue$/,
/\.vue\?vue/,
/\.md$/,
],
dts: 'src/typings/auto-imports.d.ts',
}),
Components({
dts: 'src/typings/components.d.ts',
resolvers: [IconsResolver(), NaiveUiResolver()],
}),
Icons({
/* options */
defaultStyle: 'display:inline-block',
compiler: 'vue3',
}),
createSvgIconsPlugin({
// 指定需要缓存的图标文件夹
iconDirs: [path.resolve(__dirname, 'src/assets/icons')],
// 指定symbolId格式
symbolId: 'icon-[dir]-[name]',
// inject: 'body-last',
// customDomId: '__svg__icons__dom__',
}),
]

View File

@ -1,7 +0,0 @@
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
const plugins = [vue(), vueJsx()]
export default plugins

View File

@ -1,6 +0,0 @@
// 全局引入的静态资源
import 'uno.css'
import '@/styles/css/index.css'
import 'virtual:svg-icons-register'
export function setupAssets() { }

View File

@ -2,7 +2,7 @@ import type { App } from 'vue'
import { setupPermission } from './permission'
import { setupCopy } from './copy'
export function setupDirectives(app: App) {
export function installDirectives(app: App) {
setupPermission(app)
setupCopy(app)
}

View File

@ -1,6 +1,6 @@
<script setup lang="ts">
import { useAppStore } from '@/store'
import { useRouteStore } from '~/src/store/modules/route'
import { useRouteStore } from '@/store/route'
const appStore = useAppStore()
const routesStore = useRouteStore()

View File

@ -1,29 +1,30 @@
import App from './App.vue'
import AppLoading from './components/common/appLoading.vue'
import { setupRouter } from './router'
import { setupAssets } from './assets'
import { setupStore } from './store'
import { setupDirectives } from './directive'
import { installDirectives } from './directives'
import { installRouter } from '@/router'
import { installPinia } from '@/store'
// 全局引入的静态资源
import 'uno.css'
import '@/styles/css/index.css'
import 'virtual:svg-icons-register'
async function setupApp() {
// 引入静态资源
setupAssets()
// 创建vue实例
const app = createApp(App)
// 安装pinia全局状态库
setupStore(app)
// 载入全局loading加载状态
const appLoading = createApp(AppLoading)
appLoading.mount('#appLoading')
// 安装自定义指令
setupDirectives(app)
// 创建vue实例
const app = createApp(App)
// 安装router
await setupRouter(app)
// 注册模块 Vue-router
installRouter(app)
// 注册模块Pinia
installPinia(app)
// 安装自定义指令
installDirectives(app)
// 卸载载入动画
appLoading.unmount()

9
src/modules/index.ts Normal file
View File

@ -0,0 +1,9 @@
import type { App } from 'vue'
import piniaPluginPersist from 'pinia-plugin-persist'
// 安装pinia全局状态库
export function install(app: App) {
const pinia = createPinia()
pinia.use(piniaPluginPersist)
app.use(pinia)
}

View File

@ -1,7 +1,7 @@
import type { App } from 'vue'
import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'
import { setupRouterGuard } from './guard'
import { routes } from './routes'
import { setupRouterGuard } from '@/router/guard'
import { routes } from '@/router/routes'
const { VITE_ROUTE_MODE = 'hash', VITE_BASE_URL } = import.meta.env
export const router = createRouter({
@ -9,7 +9,7 @@ export const router = createRouter({
routes,
})
// 安装vue路由
export async function setupRouter(app: App) {
export async function installRouter(app: App) {
// 添加路由守卫
setupRouterGuard(router)
app.use(router)

View File

@ -1,10 +1,15 @@
import piniaPluginPersist from 'pinia-plugin-persist'
import type { App } from 'vue'
import piniaPluginPersist from 'pinia-plugin-persist'
export function setupStore(app: App) {
export * from './app'
export * from './auth'
export * from './route'
export * from './tab'
// 安装pinia全局状态库
export function installPinia(app: App) {
const pinia = createPinia()
pinia.use(piniaPluginPersist)
app.use(pinia)
}
export * from './modules'

View File

@ -1,4 +0,0 @@
export * from './app'
export * from './auth'
export * from './route'
export * from './tab'

View File

@ -1,5 +1,5 @@
<script setup lang="ts">
import lib from '~/package.json'
import lib from 'package.json'
</script>
<template>

View File

@ -3,24 +3,22 @@
"target": "ESNext",
"jsx": "preserve",
"lib": ["ESNext", "DOM"],
"useDefineForClassFields": true,
"baseUrl": ".",
"module": "ESNext",
"moduleResolution": "Node",
"moduleResolution": "node",
"paths": {
"~/*": ["./*"],
"@/*": ["./src/*"]
}, //
"@/*": ["src/*"]
},
"resolveJsonModule": true,
"types": ["node", "vite/client", "naive-ui/volar"],
"allowJs": true,
"strict": true,
// "sourceMap": true,
"removeComments": false,
"allowSyntheticDefaultImports": true,
"strictNullChecks": true,
"noUnusedLocals": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"isolatedModules": true,
"skipLibCheck": true
},
// "include": ["src/**/*.d.ts", "src/**/*.vue", "./*.d.ts"]
"exclude": ["node_modules", "dist"]
}