|
| 1 | +/* prettier-ignore */ |
| 2 | +/* eslint-disable */ |
| 3 | +// Generated by elegant-router |
| 4 | +// Read more: https://github.com/mufeng889/elegant-router |
| 5 | +// Vue auto route: https://github.com/soybeanjs/elegant-router |
| 6 | +// 请不要手动修改此文件,否则会导致优雅路由无法正常工作 |
| 7 | +// 如果需要修改,请在优雅路由配置文件中进行修改 |
| 8 | +// 这是自动生成的文件,请不要手动修改 |
| 9 | + |
| 10 | + |
| 11 | +import type { RouteKey, RouteMap, RoutePath } from '@elegant-router/types'; |
| 12 | +import type { ElegantConstRoute } from '@soybean-react/vite-plugin-react-router'; |
| 13 | +import type { ReactElement } from 'react'; |
| 14 | +import type { RouteObject } from 'react-router-dom'; |
| 15 | + |
| 16 | +type LazyRouteComponent = Record<string, () => Promise<{ default: () => ReactElement }>>; |
| 17 | + |
| 18 | +type RouteConfig = Pick<RouteObject, 'action' | 'caseSensitive' | 'loader' | 'shouldRevalidate'> & { config?: any }; |
| 19 | + |
| 20 | +type LazyRouteConfig = Record<string, () => Promise<RouteConfig>>; |
| 21 | + |
| 22 | +const loadings = import.meta.glob(`/src/pages/**/loading.tsx`, { eager: true, import: 'default' }); |
| 23 | + |
| 24 | +/** |
| 25 | + * transform elegant const routes to react routes |
| 26 | + * |
| 27 | + * @param routes elegant const routes |
| 28 | + * @param layouts layout components |
| 29 | + * @param views view components |
| 30 | + */ |
| 31 | +export function transformElegantRoutesToReactRoutes( |
| 32 | + routes: ElegantConstRoute[], |
| 33 | + layouts: LazyRouteComponent, |
| 34 | + views: LazyRouteComponent, |
| 35 | + errors: LazyRouteComponent, |
| 36 | + configs: LazyRouteConfig |
| 37 | +) { |
| 38 | + return routes.flatMap(route => transformElegantRouteToReactRoute(route, layouts, views, errors, configs)); |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * transform elegant route to react route |
| 43 | + * |
| 44 | + * @param route elegant const route |
| 45 | + * @param layouts layout components |
| 46 | + * @param views view components |
| 47 | + */ |
| 48 | +export function transformElegantRouteToReactRoute( |
| 49 | + route: ElegantConstRoute, |
| 50 | + layouts: LazyRouteComponent, |
| 51 | + views: LazyRouteComponent, |
| 52 | + errors: LazyRouteComponent, |
| 53 | + configs: LazyRouteConfig |
| 54 | +): RouteObject { |
| 55 | + const ROUTE_DEGREE_SPLITTER = '_'; |
| 56 | + |
| 57 | + function isRouteGroup(name: string) { |
| 58 | + const lastName = name.split(ROUTE_DEGREE_SPLITTER).at(-1); |
| 59 | + return lastName?.startsWith('(') && lastName?.endsWith(')'); |
| 60 | + } |
| 61 | + |
| 62 | + const { children, matchedFiles, name, path } = route; |
| 63 | + |
| 64 | + function getComPonent(routeName: string) { |
| 65 | + if (matchedFiles[0]) { |
| 66 | + return layouts[matchedFiles[0]](); |
| 67 | + } |
| 68 | + |
| 69 | + if (!isRouteGroup(routeName) && matchedFiles[1]) { |
| 70 | + return views[matchedFiles[1]](); |
| 71 | + } |
| 72 | + |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + // Get the error boundary component |
| 77 | + function getErrorComponent() { |
| 78 | + return matchedFiles[3] ? errors[matchedFiles[3]]() : null; |
| 79 | + } |
| 80 | + |
| 81 | + // Convert route config, simplifying the logic for actions, loader, etc. |
| 82 | + function convertConfig(m: RouteConfig) { |
| 83 | + const { action, config, loader, shouldRevalidate, ...rest } = m; |
| 84 | + return { |
| 85 | + ...rest, |
| 86 | + action, // always use action |
| 87 | + handle: config, |
| 88 | + loader, // always use loader |
| 89 | + shouldRevalidate |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + // Get config for the route if available |
| 94 | + async function getConfig(index:boolean=false) { |
| 95 | + if(matchedFiles[0]&&matchedFiles[1]&&!index) return null |
| 96 | + |
| 97 | + if (configs?.[name]) { |
| 98 | + const config = await configs[name](); |
| 99 | + return convertConfig(config); |
| 100 | + } |
| 101 | + |
| 102 | + return null; |
| 103 | + } |
| 104 | + |
| 105 | + const loaderModule = [getComPonent(name), getErrorComponent()]; |
| 106 | + |
| 107 | + const reactRoute = { |
| 108 | + children: [], |
| 109 | + HydrateFallback: matchedFiles[2] ? loadings[`/src/pages${matchedFiles[2]==='/root'?'':matchedFiles[2]}/loading.tsx`] : null, |
| 110 | + id: name, |
| 111 | + lazy: async () => { |
| 112 | + const [Component, ErrorBoundary] = await Promise.all(loaderModule); |
| 113 | + |
| 114 | + return { |
| 115 | + Component: Component?.default, |
| 116 | + ErrorBoundary: ErrorBoundary?.default, |
| 117 | + ...(await getConfig()) |
| 118 | + }; |
| 119 | + }, |
| 120 | + path |
| 121 | + } as RouteObject; |
| 122 | + |
| 123 | + if (children?.length) { |
| 124 | + reactRoute.children = children.flatMap(child => |
| 125 | + transformElegantRouteToReactRoute(child, layouts, views, errors, configs) |
| 126 | + ); |
| 127 | + |
| 128 | + if (matchedFiles[0] && matchedFiles[1] && !isRouteGroup(name)) { |
| 129 | + reactRoute.children.unshift({ |
| 130 | + index: true, |
| 131 | + lazy: async () => { |
| 132 | + const [Component, ErrorBoundary] = await Promise.all([matchedFiles[1]?views[matchedFiles[1]]():null, getErrorComponent()]); |
| 133 | + |
| 134 | + return { |
| 135 | + Component: Component?.default, |
| 136 | + ErrorBoundary: ErrorBoundary?.default, |
| 137 | + ...(await getConfig(true)) |
| 138 | + }; |
| 139 | + } |
| 140 | + }); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return reactRoute; |
| 145 | +} |
| 146 | + |
| 147 | + |
| 148 | +/** |
| 149 | + * map of route name and route path |
| 150 | + */ |
| 151 | +export const routeMap: RouteMap = { |
| 152 | + "not-found": "*", |
| 153 | + "exception": "/exception", |
| 154 | + "exception_403": "/exception/403", |
| 155 | + "exception_404": "/exception/404", |
| 156 | + "exception_500": "/exception/500", |
| 157 | + "document": "/document", |
| 158 | + "document_project": "/document/project", |
| 159 | + "document_project-link": "/document/project-link", |
| 160 | + "document_react": "/document/react", |
| 161 | + "document_vite": "/document/vite", |
| 162 | + "document_unocss": "/document/unocss", |
| 163 | + "document_procomponents": "/document/procomponents", |
| 164 | + "document_antd": "/document/antd", |
| 165 | + "logout": "/logout", |
| 166 | + "(base)_home": "/home", |
| 167 | + "(blank)_login": "/login", |
| 168 | + "(blank)_login_code-login": "/login/code-login", |
| 169 | + "(blank)_login_register": "/login/register", |
| 170 | + "(blank)_login_reset-pwd": "/login/reset-pwd", |
| 171 | + "403": "/403", |
| 172 | + "404": "/404", |
| 173 | + "500": "/500", |
| 174 | + "root": "/" |
| 175 | +}; |
| 176 | + |
| 177 | +/** |
| 178 | + * get route path by route name |
| 179 | + * @param name route name |
| 180 | + */ |
| 181 | +export function getRoutePath<T extends RouteKey>(name: T) { |
| 182 | + return routeMap[name]; |
| 183 | +} |
| 184 | + |
| 185 | +/** |
| 186 | + * get route name by route path |
| 187 | + * @param path route path |
| 188 | + */ |
| 189 | +export function getRouteName(path: RoutePath) { |
| 190 | + const routeEntries = Object.entries(routeMap) as [RouteKey, RoutePath][]; |
| 191 | + |
| 192 | + const routeName: RouteKey | null = routeEntries.find(([, routePath]) => routePath === path)?.[0] || null; |
| 193 | + |
| 194 | + return routeName; |
| 195 | +} |
| 196 | + |
0 commit comments