-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
routes.ts
55 lines (46 loc) · 1.45 KB
/
routes.ts
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
55
import type { IApi } from '@/types';
import { getConventionRoutes } from '@umijs/core/dist/route/routesConvention';
import path from 'path';
import type { IRoute } from 'umi';
export default (api: IApi) => {
api.describe({ key: 'dumi:routes' });
// disable umi built-in convention routes by a non-existent path
api.modifyConfig((memo) => {
memo.conventionRoutes = {
base: path.join(__dirname, 'dumi-disable-default-routes'),
};
return memo;
});
// generate dumi routes
api.modifyRoutes(() => {
const routes: Record<string, IRoute> = {
// add context layout
'dumi-context-layout': {
id: 'dumi-context-layout',
path: '/',
file: '@/.umi/dumi/Layout.tsx',
absPath: '/',
isLayout: true,
},
};
const { docDirs } = api.config.resolve;
// generator normal docs routes
docDirs.forEach((dir: string) => {
const base = path.join(api.cwd, dir);
const dirRoutes: Record<string, IRoute> = getConventionRoutes({
base,
exclude: [/.*(?<!md)$/],
});
Object.entries(dirRoutes).forEach(([key, route]) => {
// prefix id with dir
route.id = `${dir}/${key}`;
route.parentId = 'dumi-context-layout';
// use absolute path to avoid umi prefix with conventionRoutes.base
route.file = path.resolve(base, route.file);
// save route
routes[route.id] = route;
});
});
return routes;
});
};