From 4cbdc12f905b61fd28b941b5b12cfb9c556d2be7 Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Fri, 29 Nov 2024 17:18:45 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E8=B7=AF=E7=94=B1=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/router/elegant/imports.ts | 15 ------ src/router/elegant/transform.ts | 91 +++++++++++++++----------------- src/router/routes/builtin.ts | 3 +- src/router/routes/index.ts | 3 +- src/store/modules/route/index.ts | 2 - uno.config.ts | 4 +- 6 files changed, 48 insertions(+), 70 deletions(-) delete mode 100644 src/router/elegant/imports.ts diff --git a/src/router/elegant/imports.ts b/src/router/elegant/imports.ts deleted file mode 100644 index 108a92f..0000000 --- a/src/router/elegant/imports.ts +++ /dev/null @@ -1,15 +0,0 @@ -/* eslint-disable */ -/* prettier-ignore */ -// Generated by elegant-router -// Read more: https://github.com/soybeanjs/elegant-router - -import type { RouteComponent } from "vue-router"; -import type { RouteLayout } from "@elegant-router/types"; - -import BaseLayout from "@/layouts/base-layout/index.vue"; -import BlankLayout from "@/layouts/blank-layout/index.vue"; - -export const layouts: Record Promise)> = { - base: BaseLayout, - blank: BlankLayout, -}; diff --git a/src/router/elegant/transform.ts b/src/router/elegant/transform.ts index 48bbb10..6f4d128 100644 --- a/src/router/elegant/transform.ts +++ b/src/router/elegant/transform.ts @@ -3,30 +3,28 @@ // Generated by elegant-router // Read more: https://github.com/soybeanjs/elegant-router -import type { RouteRecordRaw, RouteComponent } from 'vue-router'; +import type { RouteRecordRaw } from 'vue-router'; import type { ElegantConstRoute } from '@elegant-router/vue'; -import type { RouteMap, RouteKey, RoutePath } from '@elegant-router/types'; +import type { RouteKey, RouteMap, RoutePath } from '@elegant-router/types'; +import BaseLayout from '@/layouts/base-layout/index.vue'; +import BlankLayout from '@/layouts/blank-layout/index.vue'; /** * transform elegant const routes to vue routes * @param routes elegant const routes - * @param layouts layout components */ export function transformElegantRoutesToVueRoutes( - routes: ElegantConstRoute[], - layouts: Record Promise)>, + routes: ElegantConstRoute[] ) { - return routes.flatMap(route => transformElegantRouteToVueRoute(route, layouts )); + return routes.flatMap(route => transformElegantRouteToVueRoute(route)); } /** * transform elegant route to vue route * @param route elegant const route - * @param layouts layout components */ function transformElegantRouteToVueRoute( - route: ElegantConstRoute, - layouts: Record Promise)>, + route: ElegantConstRoute ) { const LAYOUT_PREFIX = 'layout.'; const VIEW_PREFIX = 'view.'; @@ -39,12 +37,13 @@ function transformElegantRouteToVueRoute( function getLayoutName(component: string) { const layout = component.replace(LAYOUT_PREFIX, ''); - - if(!layouts[layout]) { - throw new Error(`Layout component "${layout}" not found`); + if (layout === 'base') { + return BaseLayout; } - - return layouts[layout]; + if (layout === 'blank') { + return BlankLayout; + } + throw new Error(`Layout component "${layout}" not found`); } function isView(component: string) { @@ -53,10 +52,8 @@ function transformElegantRouteToVueRoute( function getViewName(component: string) { const view = component.replace(VIEW_PREFIX, ''); - debugger - const v = findView(view) - console.log(v) - if(!v) { + const v = findView(view); + if (!v) { throw new Error(`View component "${view}" not found`); } @@ -135,9 +132,9 @@ function transformElegantRouteToVueRoute( } if (children?.length) { - const childRoutes = children.flatMap(child => transformElegantRouteToVueRoute(child, layouts)); + const childRoutes = children.flatMap(child => transformElegantRouteToVueRoute(child)); - if(isFirstLevelRoute(route)) { + if (isFirstLevelRoute(route)) { vueRoute.children = childRoutes; } else { vueRoutes.push(...childRoutes); @@ -169,41 +166,41 @@ function findView(dirName: string) { if (component && component.length === 3) { viewDirName = component[1]; } - viewDirName = viewDirName.replaceAll("/", "_").replace("_index", "") - if ( viewDirName === dirName) { + viewDirName = viewDirName.replaceAll('/', '_').replace('_index', ''); + if (viewDirName === dirName) { return () => views[dir](); } } - return () => import("@/views/_builtin/404/index.vue"); + return () => import('@/views/_builtin/404/index.vue'); } /** * map of route name and route path */ const routeMap: RouteMap = { - "root": "/", - "not-found": "/:pathMatch(.*)*", - "exception": "/exception", - "exception_403": "/exception/403", - "exception_404": "/exception/404", - "exception_500": "/exception/500", - "403": "/403", - "404": "/404", - "500": "/500", - "about": "/about", - "function": "/function", - "function_hide-child": "/function/hide-child", - "function_hide-child_one": "/function/hide-child/one", - "function_hide-child_three": "/function/hide-child/three", - "function_hide-child_two": "/function/hide-child/two", - "function_multi-tab": "/function/multi-tab", - "function_request": "/function/request", - "function_super-page": "/function/super-page", - "function_tab": "/function/tab", - "function_toggle-auth": "/function/toggle-auth", - "home": "/home", - "login": "/login/:module(pwd-login|code-login|register|reset-pwd|bind-wechat)?", - "user-center": "/user-center" + 'root': '/', + 'not-found': '/:pathMatch(.*)*', + 'exception': '/exception', + 'exception_403': '/exception/403', + 'exception_404': '/exception/404', + 'exception_500': '/exception/500', + '403': '/403', + '404': '/404', + '500': '/500', + 'about': '/about', + 'function': '/function', + 'function_hide-child': '/function/hide-child', + 'function_hide-child_one': '/function/hide-child/one', + 'function_hide-child_three': '/function/hide-child/three', + 'function_hide-child_two': '/function/hide-child/two', + 'function_multi-tab': '/function/multi-tab', + 'function_request': '/function/request', + 'function_super-page': '/function/super-page', + 'function_tab': '/function/tab', + 'function_toggle-auth': '/function/toggle-auth', + 'home': '/home', + 'login': '/login/:module(pwd-login|code-login|register|reset-pwd|bind-wechat)?', + 'user-center': '/user-center' }; /** @@ -211,7 +208,7 @@ const routeMap: RouteMap = { * @param name route name */ export function getRoutePath(name: T) { - console.log(name) + console.log(name); return routeMap[name]; } diff --git a/src/router/routes/builtin.ts b/src/router/routes/builtin.ts index 46cff1c..229559d 100644 --- a/src/router/routes/builtin.ts +++ b/src/router/routes/builtin.ts @@ -1,5 +1,4 @@ import type { CustomRoute } from '@elegant-router/types'; -import { layouts } from '../elegant/imports'; import { transformElegantRoutesToVueRoutes } from '../elegant/transform'; export const ROOT_ROUTE: CustomRoute = { @@ -27,5 +26,5 @@ const builtinRoutes: CustomRoute[] = [ROOT_ROUTE, NOT_FOUND_ROUTE]; /** create builtin vue routes */ export function createBuiltinVueRoutes() { - return transformElegantRoutesToVueRoutes(builtinRoutes, layouts); + return transformElegantRoutesToVueRoutes(builtinRoutes); } diff --git a/src/router/routes/index.ts b/src/router/routes/index.ts index e62ded3..32f89b7 100644 --- a/src/router/routes/index.ts +++ b/src/router/routes/index.ts @@ -1,6 +1,5 @@ import type { ElegantConstRoute } from '@elegant-router/types'; import { generatedRoutes } from '../elegant/routes'; -import { layouts } from '../elegant/imports'; import { transformElegantRoutesToVueRoutes } from '../elegant/transform'; /** @@ -80,5 +79,5 @@ export function createStaticRoutes() { * @param routes Elegant routes */ export function getAuthVueRoutes(routes: ElegantConstRoute[]) { - return transformElegantRoutesToVueRoutes(routes, layouts); + return transformElegantRoutesToVueRoutes(routes); } diff --git a/src/store/modules/route/index.ts b/src/store/modules/route/index.ts index 8fe8961..c17ece3 100644 --- a/src/store/modules/route/index.ts +++ b/src/store/modules/route/index.ts @@ -173,7 +173,6 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => { /** Init static auth route */ async function initStaticAuthRoute() { const { authRoutes: staticAuthRoutes } = createStaticRoutes(); - debugger if (authStore.isStaticSuper) { addAuthRoutes(staticAuthRoutes); } else { @@ -190,7 +189,6 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => { /** Init dynamic auth route */ async function initDynamicAuthRoute() { const { data: routes, error } = await doGetUserRoutes(); - debugger if (!error) { addAuthRoutes(routes); diff --git a/uno.config.ts b/uno.config.ts index c3ab916..45b1e34 100644 --- a/uno.config.ts +++ b/uno.config.ts @@ -4,7 +4,7 @@ import transformerVariantGroup from '@unocss/transformer-variant-group'; import presetUno from '@unocss/preset-uno'; import type { Theme } from '@unocss/preset-uno'; import { presetSoybeanAdmin } from '@sa/uno-preset'; -import presetAttributify from '@unocss/preset-attributify'; +import presetAttributive from '@unocss/preset-attributify'; import { themeVars } from './src/theme/vars'; export default defineConfig({ @@ -29,5 +29,5 @@ export default defineConfig({ }, transformers: [transformerDirectives(), transformerVariantGroup()], // @ts-expect-error presetUno is not compatible with the new API - presets: [presetUno({ dark: 'class' }), presetSoybeanAdmin(), presetAttributify()] + presets: [presetUno({ dark: 'class' }), presetSoybeanAdmin(), presetAttributive()] });