forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanifest.ts
53 lines (46 loc) · 1.24 KB
/
manifest.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
import type { RouteConfigEntry } from "@react-router/dev/routes";
export interface RouteManifestEntry {
path?: string;
index?: boolean;
caseSensitive?: boolean;
id: string;
parentId?: string;
file: string;
}
export interface RouteManifest {
[routeId: string]: RouteManifestEntry;
}
export function routeManifestToRouteConfig(
routeManifest: RouteManifest,
rootId = "root"
): RouteConfigEntry[] {
let routeConfigById: {
[id: string]: Omit<RouteConfigEntry, "id"> &
Required<Pick<RouteConfigEntry, "id">>;
} = {};
for (let id in routeManifest) {
let route = routeManifest[id];
routeConfigById[id] = {
id: route.id,
file: route.file,
path: route.path,
index: route.index,
caseSensitive: route.caseSensitive,
};
}
let routeConfig: RouteConfigEntry[] = [];
for (let id in routeConfigById) {
let route = routeConfigById[id];
let parentId = routeManifest[route.id].parentId;
if (parentId === rootId) {
routeConfig.push(route);
} else {
let parentRoute = parentId && routeConfigById[parentId];
if (parentRoute) {
parentRoute.children = parentRoute.children || [];
parentRoute.children.push(route);
}
}
}
return routeConfig;
}