-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
generateRouteRecords.ts
61 lines (56 loc) · 1.52 KB
/
generateRouteRecords.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
56
57
58
59
60
61
import type { TreeLeaf } from '../core/tree'
export function generateRouteRecord(
node: TreeLeaf,
indent = 0,
parent: TreeLeaf | null = null
): string {
// root
if (node.value.path === '/' && indent === 0) {
return `[
${node
.getSortedChildren()
.map((child) => generateRouteRecord(child, indent + 1))
.join(',\n')}
]`
}
const startIndent = ' '.repeat(indent * 2)
const indentStr = ' '.repeat((indent + 1) * 2)
// TODO: should meta be defined a different way to allow preserving imports?
// const meta = node.value.overrides.meta
return `${startIndent}{
${indentStr}path: '${node.path}',
${indentStr}${
node.value.filePaths.size ? `name: '${node.name}',` : '/* no name */'
}
${indentStr}${
node.value.filePaths.size
? generateRouteRecordComponent(node, indentStr)
: '/* no component */'
}
${indentStr}${
node.children.size > 0
? `children: [
${node
.getSortedChildren()
.map((child) => generateRouteRecord(child, indent + 2, node))
.join(',\n')}
${indentStr}],`
: '/* no children */'
}
${startIndent}}`
}
function generateRouteRecordComponent(
node: TreeLeaf,
indentStr: string
): string {
const files = Array.from(node.value.filePaths)
const isDefaultExport = files.length === 1 && files[0][0] === 'default'
return isDefaultExport
? `component: () => import('${files[0][1]}'),`
: // files has at least one entry
`components: {
${files
.map(([key, path]) => `${indentStr + ' '}'${key}': () => import('${path}')`)
.join(',\n')}
${indentStr}},`
}