From 3c1b1e2336f6c91e7a001c9b440133d7da00ef99 Mon Sep 17 00:00:00 2001 From: chansee97 Date: Fri, 1 Mar 2024 17:37:21 +0800 Subject: [PATCH] refactor: simplified code --- build/config/index.ts | 1 - build/index.ts | 2 - build/plugins.ts | 76 +++++++++++++++++++++ build/plugins/compress.ts | 11 --- build/plugins/index.ts | 19 ------ build/plugins/unplugin.ts | 43 ------------ build/plugins/vue.ts | 7 -- build/{config => }/proxy.ts | 0 src/assets/index.ts | 6 -- src/{directive => directives}/copy.ts | 0 src/{directive => directives}/index.ts | 2 +- src/{directive => directives}/permission.ts | 0 src/layouts/components/sider/Menu.vue | 2 +- src/main.ts | 35 +++++----- src/modules/index.ts | 9 +++ src/router/index.ts | 6 +- src/store/{modules => }/app.ts | 0 src/store/{modules => }/auth.ts | 0 src/store/index.ts | 13 ++-- src/store/modules/index.ts | 4 -- src/store/{modules => }/route.ts | 0 src/store/{modules => }/tab.ts | 0 src/views/about/index.vue | 2 +- tsconfig.json | 16 ++--- 24 files changed, 125 insertions(+), 129 deletions(-) delete mode 100644 build/config/index.ts delete mode 100644 build/index.ts create mode 100644 build/plugins.ts delete mode 100644 build/plugins/compress.ts delete mode 100644 build/plugins/index.ts delete mode 100644 build/plugins/unplugin.ts delete mode 100644 build/plugins/vue.ts rename build/{config => }/proxy.ts (100%) delete mode 100644 src/assets/index.ts rename src/{directive => directives}/copy.ts (100%) rename src/{directive => directives}/index.ts (77%) rename src/{directive => directives}/permission.ts (100%) create mode 100644 src/modules/index.ts rename src/store/{modules => }/app.ts (100%) rename src/store/{modules => }/auth.ts (100%) delete mode 100644 src/store/modules/index.ts rename src/store/{modules => }/route.ts (100%) rename src/store/{modules => }/tab.ts (100%) diff --git a/build/config/index.ts b/build/config/index.ts deleted file mode 100644 index e739ac8..0000000 --- a/build/config/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './proxy' diff --git a/build/index.ts b/build/index.ts deleted file mode 100644 index f9ea6e7..0000000 --- a/build/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './config' -export * from './plugins' diff --git a/build/plugins.ts b/build/plugins.ts new file mode 100644 index 0000000..07e1bfe --- /dev/null +++ b/build/plugins.ts @@ -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 +} diff --git a/build/plugins/compress.ts b/build/plugins/compress.ts deleted file mode 100644 index 3664c5a..0000000 --- a/build/plugins/compress.ts +++ /dev/null @@ -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, // 压缩算法 - }) -} diff --git a/build/plugins/index.ts b/build/plugins/index.ts deleted file mode 100644 index 53d0606..0000000 --- a/build/plugins/index.ts +++ /dev/null @@ -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 -} diff --git a/build/plugins/unplugin.ts b/build/plugins/unplugin.ts deleted file mode 100644 index 6f7af90..0000000 --- a/build/plugins/unplugin.ts +++ /dev/null @@ -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__', - }), -] diff --git a/build/plugins/vue.ts b/build/plugins/vue.ts deleted file mode 100644 index b45cea0..0000000 --- a/build/plugins/vue.ts +++ /dev/null @@ -1,7 +0,0 @@ -import vue from '@vitejs/plugin-vue' - -import vueJsx from '@vitejs/plugin-vue-jsx' - -const plugins = [vue(), vueJsx()] - -export default plugins diff --git a/build/config/proxy.ts b/build/proxy.ts similarity index 100% rename from build/config/proxy.ts rename to build/proxy.ts diff --git a/src/assets/index.ts b/src/assets/index.ts deleted file mode 100644 index aed0e5d..0000000 --- a/src/assets/index.ts +++ /dev/null @@ -1,6 +0,0 @@ -// 全局引入的静态资源 -import 'uno.css' -import '@/styles/css/index.css' -import 'virtual:svg-icons-register' - -export function setupAssets() { } diff --git a/src/directive/copy.ts b/src/directives/copy.ts similarity index 100% rename from src/directive/copy.ts rename to src/directives/copy.ts diff --git a/src/directive/index.ts b/src/directives/index.ts similarity index 77% rename from src/directive/index.ts rename to src/directives/index.ts index 4c2a486..878e4c5 100644 --- a/src/directive/index.ts +++ b/src/directives/index.ts @@ -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) } diff --git a/src/directive/permission.ts b/src/directives/permission.ts similarity index 100% rename from src/directive/permission.ts rename to src/directives/permission.ts diff --git a/src/layouts/components/sider/Menu.vue b/src/layouts/components/sider/Menu.vue index 8764135..54abe03 100644 --- a/src/layouts/components/sider/Menu.vue +++ b/src/layouts/components/sider/Menu.vue @@ -1,6 +1,6 @@