Open History to see the last commit
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)
16}
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 }
45}
46β
47export function getTargetTemplate(config: Config): TargetTemplate {
48 const target = config.target
49 switch (target) {
50 case 'react':
51 return {
52 fullPkg: '@tanstack/react-router',
53 subPkg: 'react-router',
54 rootRoute: {
55 template: () =>
56 [
57 'import * as React from "react"\n',
58 '%%tsrImports%%',
59 '\n\n',
60 '%%tsrExportStart%%{\n component: RootComponent\n }%%tsrExportEnd%%\n\n',
61 'function RootComponent() { return (<React.Fragment><div>Hello "%%tsrPath%%"!</div><Outlet /></React.Fragment>) };\n',
62 ].join(''),
63 imports: {
64 tsrImports: () =>
65 "import { Outlet, createRootRoute } from '@tanstack/react-router';",
66 tsrExportStart: () => 'export const Route = createRootRoute(',
67 tsrExportEnd: () => ');',
68 },
69 },
70 route: {
71 template: () =>
72 [
73 '%%tsrImports%%',
74 '\n\n',
75 '%%tsrExportStart%%{\n component: RouteComponent\n }%%tsrExportEnd%%\n\n',
76 'function RouteComponent() { return <div>Hello "%%tsrPath%%"!</div> };\n',
77 ].join(''),
78 imports: {
79 tsrImports: () =>
80 config.verboseFileRoutes === false
81 ? ''
82 : "import { createFileRoute } from '@tanstack/react-router';",
83 tsrExportStart: (routePath) =>
84 config.verboseFileRoutes === false
85 ? 'export const Route = createFileRoute('
86 : `export const Route = createFileRoute('${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 config.verboseFileRoutes === false
101 ? ''
102 : "import { createLazyFileRoute } from '@tanstack/react-router';",
103 tsrExportStart: (routePath) =>
104 config.verboseFileRoutes === false
105 ? 'export const Route = createLazyFileRoute('
106 : `export const Route = createLazyFileRoute('${routePath}')(`,
107 tsrExportEnd: () => ');',
108 },
109 },
110 }
111 case 'solid':
112 return {
113 fullPkg: '@tanstack/solid-router',
114 subPkg: 'solid-router',
115 rootRoute: {
116 template: () =>
117 [
118 'import * as Solid from "solid-js"\n',
119 '%%tsrImports%%',
120 '\n\n',
121 '%%tsrExportStart%%{\n component: RootComponent\n }%%tsrExportEnd%%\n\n',
122 'function RootComponent() { return (<><div>Hello "%%tsrPath%%"!</div><Outlet /></>) };\n',
123 ].join(''),
124 imports: {
125 tsrImports: () =>
126 "import { Outlet, createRootRoute } from '@tanstack/solid-router';",
127 tsrExportStart: () => 'export const Route = createRootRoute(',
128 tsrExportEnd: () => ');',
129 },
130 },
131 route: {
132 template: () =>
133 [
134 '%%tsrImports%%',
135 '\n\n',
136 '%%tsrExportStart%%{\n component: RouteComponent\n }%%tsrExportEnd%%\n\n',
137 'function RouteComponent() { return <div>Hello "%%tsrPath%%"!</div> };\n',
138 ].join(''),
139 imports: {
140 tsrImports: () =>
141 config.verboseFileRoutes === false
142 ? ''
143 : "import { createFileRoute } from '@tanstack/solid-router';",
144 tsrExportStart: (routePath) =>
145 config.verboseFileRoutes === false
146 ? 'export const Route = createFileRoute('
147 : `export const Route = createFileRoute('${routePath}')(`,
148 tsrExportEnd: () => ');',
149 },
150 },
151 lazyRoute: {
152 template: () =>
153 [
154 '%%tsrImports%%',
155 '\n\n',
156 '%%tsrExportStart%%{\n component: RouteComponent\n }%%tsrExportEnd%%\n\n',
157 'function RouteComponent() { return <div>Hello "%%tsrPath%%"!</div> };\n',
158 ].join(''),
159 imports: {
160 tsrImports: () =>
161 config.verboseFileRoutes === false
162 ? ''
163 : "import { createLazyFileRoute } from '@tanstack/solid-router';",
164β
165 tsrExportStart: (routePath) =>
166 config.verboseFileRoutes === false
167 ? 'export const Route = createLazyFileRoute('
168 : `export const Route = createLazyFileRoute('${routePath}')(`,
169β
170 tsrExportEnd: () => ');',
171 },
172 },
173 }
174 default:
175 throw new Error(`router-generator: Unknown target type: ${target}`)
176 }
177}