@tanstack/router-generator 1.99.9 → 1.99.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,111 @@
1
+ import type { Config } from './config'
2
+
3
+ type TemplateTag = 'tsrImports' | 'tsrPath' | 'tsrExportStart' | 'tsrExportEnd'
4
+
5
+ export function fillTemplate(
6
+ template: string,
7
+ values: Record<TemplateTag, string>,
8
+ ) {
9
+ return template.replace(
10
+ /%%(\w+)%%/g,
11
+ (_, key) => values[key as TemplateTag] || '',
12
+ )
13
+ }
14
+
15
+ type TargetTemplate = {
16
+ fullPkg: string
17
+ subPkg: string
18
+ rootRoute: {
19
+ template: () => string
20
+ imports: {
21
+ tsrImports: () => string
22
+ tsrExportStart: () => string
23
+ tsrExportEnd: () => string
24
+ }
25
+ }
26
+ route: {
27
+ template: () => string
28
+ imports: {
29
+ tsrImports: () => string
30
+ tsrExportStart: (routePath: string) => string
31
+ tsrExportEnd: () => string
32
+ }
33
+ }
34
+ lazyRoute: {
35
+ template: () => string
36
+ imports: {
37
+ tsrImports: () => string
38
+ tsrExportStart: (routePath: string) => string
39
+ tsrExportEnd: () => string
40
+ }
41
+ }
42
+ }
43
+
44
+ export function getTargetTemplate(target: Config['target']): TargetTemplate {
45
+ switch (target) {
46
+ // TODO: Remove this disabled eslint rule when more target types are added.
47
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
48
+ case 'react':
49
+ return {
50
+ fullPkg: '@tanstack/react-router',
51
+ subPkg: 'react-router',
52
+ rootRoute: {
53
+ template: () =>
54
+ [
55
+ 'import * as React from "react"\n',
56
+ '%%tsrImports%%',
57
+ '\n\n',
58
+ '%%tsrExportStart%%{\n component: RootComponent\n }%%tsrExportEnd%%\n\n',
59
+ 'function RootComponent() { return (<React.Fragment><div>Hello "%%tsrPath%%"!</div><Outlet /></React.Fragment>) };\n',
60
+ ].join(''),
61
+ imports: {
62
+ tsrImports: () =>
63
+ "import { Outlet, createRootRoute } from '@tanstack/react-router';",
64
+ tsrExportStart: () => 'export const Route = createRootRoute(',
65
+ tsrExportEnd: () => ');',
66
+ },
67
+ },
68
+ route: {
69
+ template: () =>
70
+ [
71
+ '%%tsrImports%%',
72
+ '\n\n',
73
+ '%%tsrExportStart%%{\n component: RouteComponent\n }%%tsrExportEnd%%\n\n',
74
+ 'function RouteComponent() { return <div>Hello "%%tsrPath%%"!</div> };\n',
75
+ ].join(''),
76
+ imports: {
77
+ tsrImports: () =>
78
+ "import { createFileRoute } from '@tanstack/react-router';",
79
+ tsrExportStart: (routePath) =>
80
+ `export const Route = createFileRoute('${routePath}')(`,
81
+ tsrExportEnd: () => ');',
82
+ },
83
+ },
84
+ lazyRoute: {
85
+ template: () =>
86
+ [
87
+ '%%tsrImports%%',
88
+ '\n\n',
89
+ '%%tsrExportStart%%{\n component: RouteComponent\n }%%tsrExportEnd%%\n\n',
90
+ 'function RouteComponent() { return <div>Hello "%%tsrPath%%"!</div> };\n',
91
+ ].join(''),
92
+ imports: {
93
+ tsrImports: () =>
94
+ "import { createLazyFileRoute } from '@tanstack/react-router';",
95
+ tsrExportStart: (routePath) =>
96
+ `export const Route = createLazyFileRoute('${routePath}')(`,
97
+ tsrExportEnd: () => ');',
98
+ },
99
+ },
100
+ }
101
+ default:
102
+ throw new Error(`router-generator: Unknown target type: ${target}`)
103
+ }
104
+ }
105
+
106
+ export const defaultAPIRouteTemplate = [
107
+ 'import { json } from "@tanstack/start";\n',
108
+ '%%tsrImports%%',
109
+ '\n\n',
110
+ '%%tsrExportStart%%{ GET: ({ request, params }) => { return json({ message:\'Hello "%%tsrPath%%"!\' }) }}%%tsrExportEnd%%\n',
111
+ ].join('')