Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

template.ts

222 lines7.4 KB

1import { format } from './utils'
2import type { Config } from './config'
3​
4type TemplateTag = 'tsrImports' | 'tsrPath' | 'tsrExportStart' | 'tsrExportEnd'
5​
6export function fillTemplate(
7 config: Config,
8 template: string,
9 values: Record<TemplateTag, string>,
10) {
11 const replaced = template.replace(
12 /%%(\w+)%%/g,
13 (_, key) => values[key as TemplateTag] || '',
14 )
15 return format(replaced, config)
17​
18export type TargetTemplate = {
19 fullPkg: string
20 subPkg: string
21 rootRoute: {
22 template: () => string
23 imports: {
24 tsrImports: () => string
25 tsrExportStart: () => string
26 tsrExportEnd: () => string
27 }
28 }
29 route: {
30 template: () => string
31 imports: {
32 tsrImports: () => string
33 tsrExportStart: (routePath: string) => string
34 tsrExportEnd: () => string
35 }
36 }
37 lazyRoute: {
38 template: () => string
39 imports: {
40 tsrImports: () => string
41 tsrExportStart: (routePath: string) => string
42 tsrExportEnd: () => string
43 }
44 }
46​
47function serializeRoutePath(routePath: string) {
48 return JSON.stringify(routePath)
50​
51export function getTargetTemplate(config: Config): TargetTemplate {
52 const target = config.target
53 switch (target) {
54 case 'react':
55 return {
56 fullPkg: '@tanstack/react-router',
57 subPkg: 'react-router',
58 rootRoute: {
59 template: () =>
60 [
61 'import * as React from "react"\n',
62 '%%tsrImports%%',
63 '\n\n',
64 '%%tsrExportStart%%{\n component: RootComponent\n }%%tsrExportEnd%%\n\n',
65 'function RootComponent() { return (<React.Fragment><div>Hello "%%tsrPath%%"!</div><Outlet /></React.Fragment>) };\n',
66 ].join(''),
67 imports: {
68 tsrImports: () =>
69 "import { Outlet, createRootRoute } from '@tanstack/react-router';",
70 tsrExportStart: () => 'export const Route = createRootRoute(',
71 tsrExportEnd: () => ');',
72 },
73 },
74 route: {
75 template: () =>
76 [
77 '%%tsrImports%%',
78 '\n\n',
79 '%%tsrExportStart%%{\n component: RouteComponent\n }%%tsrExportEnd%%\n\n',
80 'function RouteComponent() { return <div>Hello "%%tsrPath%%"!</div> };\n',
81 ].join(''),
82 imports: {
83 tsrImports: () =>
84 "import { createFileRoute } from '@tanstack/react-router';",
85 tsrExportStart: (routePath) =>
86 `export const Route = createFileRoute(${serializeRoutePath(routePath)})(`,
87 tsrExportEnd: () => ');',
88 },
89 },
90 lazyRoute: {
91 template: () =>
92 [
93 '%%tsrImports%%',
94 '\n\n',
95 '%%tsrExportStart%%{\n component: RouteComponent\n }%%tsrExportEnd%%\n\n',
96 'function RouteComponent() { return <div>Hello "%%tsrPath%%"!</div> };\n',
97 ].join(''),
98 imports: {
99 tsrImports: () =>
100 "import { createLazyFileRoute } from '@tanstack/react-router';",
101 tsrExportStart: (routePath) =>
102 `export const Route = createLazyFileRoute(${serializeRoutePath(routePath)})(`,
103 tsrExportEnd: () => ');',
104 },
105 },
106 }
107 case 'solid':
108 return {
109 fullPkg: '@tanstack/solid-router',
110 subPkg: 'solid-router',
111 rootRoute: {
112 template: () =>
113 [
114 'import * as Solid from "solid-js"\n',
115 '%%tsrImports%%',
116 '\n\n',
117 '%%tsrExportStart%%{\n component: RootComponent\n }%%tsrExportEnd%%\n\n',
118 'function RootComponent() { return (<><div>Hello "%%tsrPath%%"!</div><Outlet /></>) };\n',
119 ].join(''),
120 imports: {
121 tsrImports: () =>
122 "import { Outlet, createRootRoute } from '@tanstack/solid-router';",
123 tsrExportStart: () => 'export const Route = createRootRoute(',
124 tsrExportEnd: () => ');',
125 },
126 },
127 route: {
128 template: () =>
129 [
130 '%%tsrImports%%',
131 '\n\n',
132 '%%tsrExportStart%%{\n component: RouteComponent\n }%%tsrExportEnd%%\n\n',
133 'function RouteComponent() { return <div>Hello "%%tsrPath%%"!</div> };\n',
134 ].join(''),
135 imports: {
136 tsrImports: () =>
137 "import { createFileRoute } from '@tanstack/solid-router';",
138 tsrExportStart: (routePath) =>
139 `export const Route = createFileRoute(${serializeRoutePath(routePath)})(`,
140 tsrExportEnd: () => ');',
141 },
142 },
143 lazyRoute: {
144 template: () =>
145 [
146 '%%tsrImports%%',
147 '\n\n',
148 '%%tsrExportStart%%{\n component: RouteComponent\n }%%tsrExportEnd%%\n\n',
149 'function RouteComponent() { return <div>Hello "%%tsrPath%%"!</div> };\n',
150 ].join(''),
151 imports: {
152 tsrImports: () =>
153 "import { createLazyFileRoute } from '@tanstack/solid-router';",
154​
155 tsrExportStart: (routePath) =>
156 `export const Route = createLazyFileRoute(${serializeRoutePath(routePath)})(`,
157​
158 tsrExportEnd: () => ');',
159 },
160 },
161 }
162 case 'vue':
163 return {
164 fullPkg: '@tanstack/vue-router',
165 subPkg: 'vue-router',
166 rootRoute: {
167 template: () =>
168 [
169 'import { h } from "vue"\n',
170 '%%tsrImports%%',
171 '\n\n',
172 '%%tsrExportStart%%{\n component: RootComponent\n }%%tsrExportEnd%%\n\n',
173 'function RootComponent() { return h("div", {}, ["Hello \\"%%tsrPath%%\\"!", h(Outlet)]) };\n',
174 ].join(''),
175 imports: {
176 tsrImports: () =>
177 "import { Outlet, createRootRoute } from '@tanstack/vue-router';",
178 tsrExportStart: () => 'export const Route = createRootRoute(',
179 tsrExportEnd: () => ');',
180 },
181 },
182 route: {
183 template: () =>
184 [
185 'import { h } from "vue"\n',
186 '%%tsrImports%%',
187 '\n\n',
188 '%%tsrExportStart%%{\n component: RouteComponent\n }%%tsrExportEnd%%\n\n',
189 'function RouteComponent() { return h("div", {}, "Hello \\"%%tsrPath%%\\"!") };\n',
190 ].join(''),
191 imports: {
192 tsrImports: () =>
193 "import { createFileRoute } from '@tanstack/vue-router';",
194 tsrExportStart: (routePath) =>
195 `export const Route = createFileRoute(${serializeRoutePath(routePath)})(`,
196 tsrExportEnd: () => ');',
197 },
198 },
199 lazyRoute: {
200 template: () =>
201 [
202 'import { h } from "vue"\n',
203 '%%tsrImports%%',
204 '\n\n',
205 '%%tsrExportStart%%{\n component: RouteComponent\n }%%tsrExportEnd%%\n\n',
206 'function RouteComponent() { return h("div", {}, "Hello \\"%%tsrPath%%\\"!") };\n',
207 ].join(''),
208 imports: {
209 tsrImports: () =>
210 "import { createLazyFileRoute } from '@tanstack/vue-router';",
211​
212 tsrExportStart: (routePath) =>
213 `export const Route = createLazyFileRoute(${serializeRoutePath(routePath)})(`,
214​
215 tsrExportEnd: () => ');',
216 },
217 },
218 }
219 default:
220 throw new Error(`router-generator: Unknown target type: ${target}`)
221 }