Skip to content

Commit 57177a9

Browse files
committed
feat(projects): 添加新的优雅路由文件
1 parent a8be849 commit 57177a9

File tree

3 files changed

+298
-0
lines changed

3 files changed

+298
-0
lines changed

src/router/elegant/imports.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
12+
export const layouts = {
13+
"(base)": () => import("@/pages/(base)/layout.tsx"),
14+
"(blank)": () => import("@/pages/(blank)/layout.tsx"),
15+
"(blank)_login": () => import("@/pages/(blank)/login/layout.tsx"),
16+
"root": () => import("@/pages/layout.tsx"),
17+
};
18+
19+
export const pages = {
20+
"(base)_home": () => import("@/pages/(base)/home/index.tsx"),
21+
"(blank)_login_code-login": () => import("@/pages/(blank)/login/code-login/index.tsx"),
22+
"(blank)_login": () => import("@/pages/(blank)/login/index.tsx"),
23+
"(blank)_login_register": () => import("@/pages/(blank)/login/register/index.tsx"),
24+
"(blank)_login_reset-pwd": () => import("@/pages/(blank)/login/reset-pwd/index.tsx"),
25+
"403": () => import("@/pages/403.tsx"),
26+
"404": () => import("@/pages/404.tsx"),
27+
"500": () => import("@/pages/500.tsx"),
28+
"root": () => import("@/pages/index.tsx"),
29+
};
30+
31+
export const errors = {
32+
"root": () => import("@/pages/error.tsx"),
33+
};
34+
35+
export const configs = {
36+
"(base)_home": () => import("@/pages/(base)/home/config.ts"),
37+
"(blank)_login_code-login": () => import("@/pages/(blank)/login/code-login/config.ts"),
38+
"(blank)_login": () => import("@/pages/(blank)/login/config.ts"),
39+
"(blank)_login_register": () => import("@/pages/(blank)/login/register/config.ts"),
40+
"(blank)_login_reset-pwd": () => import("@/pages/(blank)/login/reset-pwd/config.ts"),
41+
"root": () => import("@/pages/config.ts"),
42+
};
43+
44+
export const loadings = {
45+
"root": "/src/pages/loading.tsx",
46+
};

src/router/elegant/routes.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* eslint-disable */
2+
// Generated by elegant-router
3+
// Read more: https://github.com/mufeng889/elegant-router
4+
// Vue auto route: https://github.com/soybeanjs/elegant-router
5+
// 请不要手动修改此文件,否则会导致优雅路由无法正常工作
6+
// 如果需要修改,请在优雅路由配置文件中进行修改
7+
// 这是自动生成的文件,请不要手动修改
8+
9+
export const generatedRoutes = [
10+
{
11+
matchedFiles: ['root', 'root', '/root', 'root'],
12+
name: 'root',
13+
path: '/',
14+
children: [
15+
{
16+
matchedFiles: ['(base)', null, null, null],
17+
name: '(base)',
18+
path: null,
19+
children: [{ matchedFiles: [null, '(base)_home', null, null], name: '(base)_home', path: '/home' }]
20+
},
21+
{
22+
matchedFiles: ['(blank)', null, null, null],
23+
name: '(blank)',
24+
path: null,
25+
children: [
26+
{
27+
matchedFiles: ['(blank)_login', '(blank)_login', null, null],
28+
name: '(blank)_login',
29+
path: '/login',
30+
children: [
31+
{
32+
matchedFiles: [null, '(blank)_login_code-login', null, null],
33+
name: '(blank)_login_code-login',
34+
path: '/login/code-login'
35+
},
36+
{
37+
matchedFiles: [null, '(blank)_login_register', null, null],
38+
name: '(blank)_login_register',
39+
path: '/login/register'
40+
},
41+
{
42+
matchedFiles: [null, '(blank)_login_reset-pwd', null, null],
43+
name: '(blank)_login_reset-pwd',
44+
path: '/login/reset-pwd'
45+
}
46+
]
47+
}
48+
]
49+
},
50+
{ matchedFiles: [null, '403', null, null], name: '403', path: '/403' },
51+
{ matchedFiles: [null, '404', null, null], name: '404', path: '/404' },
52+
{ matchedFiles: [null, '500', null, null], name: '500', path: '/500' },
53+
{ matchedFiles: [null, '404', null, null], name: 'notFound', path: '*' }
54+
]
55+
}
56+
];

src/router/elegant/transform.ts

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
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

Comments
 (0)