-
Notifications
You must be signed in to change notification settings - Fork 839
/
index.tsx
54 lines (48 loc) · 1.47 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { Suspense, lazy } from 'react';
import { RouteObject } from 'react-router-dom';
import Layout from '@/pages/Layout';
import baseRoutes, { RouteNode } from './routes';
import RouteGuard from './RouteGuard';
import RouteErrorBoundary from './RouteErrorBoundary';
const routes: RouteNode[] = [];
const routeWrapper = (routeNodes: RouteNode[], root: RouteNode[]) => {
routeNodes.forEach((rn) => {
if (rn.page === 'pages/Layout') {
rn.element = rn.guard ? (
<RouteGuard onEnter={rn.guard} path={rn.path}>
<Layout />
</RouteGuard>
) : (
<Layout />
);
rn.errorElement = <RouteErrorBoundary />;
} else {
/**
* cannot use a fully dynamic import statement
* ref: https://webpack.js.org/api/module-methods/#import-1
*/
const pagePath = rn.page.replace('pages/', '');
const Ctrl = lazy(() => import(`@/pages/${pagePath}`));
rn.element = (
<Suspense>
{rn.guard ? (
<RouteGuard onEnter={rn.guard} path={rn.path}>
<Ctrl />
</RouteGuard>
) : (
<Ctrl />
)}
</Suspense>
);
rn.errorElement = <RouteErrorBoundary />;
}
root.push(rn);
const children = Array.isArray(rn.children) ? rn.children : null;
if (children) {
rn.children = [];
routeWrapper(children, rn.children);
}
});
};
routeWrapper(baseRoutes, routes);
export default routes as RouteObject[];