-
-
Notifications
You must be signed in to change notification settings - Fork 869
/
Copy pathconfig.ts
158 lines (145 loc) · 4.87 KB
/
config.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import path from 'node:path'
import { existsSync, readFileSync } from 'node:fs'
import { z } from 'zod'
import { virtualRootRouteSchema } from './filesystem/virtual/config'
const defaultTemplate = {
routeTemplate: [
'import * as React from "react";\n',
'%%tsrImports%%',
'\n\n',
'%%tsrExportStart%%{\n component: RouteComponent\n }%%tsrExportEnd%%\n\n',
'function RouteComponent() { return "Hello %%tsrPath%%!" };\n',
].join(''),
apiTemplate: [
'import { json } from "@tanstack/start";\n',
'%%tsrImports%%',
'\n\n',
'%%tsrExportStart%%{ GET: ({ request, params }) => { return json({ message:\'Hello "%%tsrPath%%"!\' }) }}%%tsrExportEnd%%\n',
].join(''),
}
export const configSchema = z.object({
virtualRouteConfig: virtualRootRouteSchema.optional(),
routeFilePrefix: z.string().optional(),
routeFileIgnorePrefix: z.string().optional().default('-'),
routeFileIgnorePattern: z.string().optional(),
routesDirectory: z.string().optional().default('./src/routes'),
generatedRouteTree: z.string().optional().default('./src/routeTree.gen.ts'),
quoteStyle: z.enum(['single', 'double']).optional().default('single'),
semicolons: z.boolean().optional().default(false),
disableTypes: z.boolean().optional().default(false),
addExtensions: z.boolean().optional().default(false),
disableLogging: z.boolean().optional().default(false),
disableManifestGeneration: z.boolean().optional().default(false),
apiBase: z.string().optional().default('/api'),
routeTreeFileHeader: z
.array(z.string())
.optional()
.default([
'/* eslint-disable */',
'// @ts-nocheck',
'// noinspection JSUnusedGlobalSymbols',
]),
routeTreeFileFooter: z.array(z.string()).optional().default([]),
autoCodeSplitting: z.boolean().optional(),
indexToken: z.string().optional().default('index'),
routeToken: z.string().optional().default('route'),
pathParamsAllowedCharacters: z
.array(z.enum([';', ':', '@', '&', '=', '+', '$', ',']))
.optional(),
customScaffolding: z
.object({
routeTemplate: z
.string()
.optional()
.default(defaultTemplate.routeTemplate),
apiTemplate: z.string().optional().default(defaultTemplate.apiTemplate),
})
.optional()
.default(defaultTemplate),
experimental: z
.object({
// TODO: Remove this option in the next major release (v2).
enableCodeSplitting: z.boolean().optional(),
})
.optional(),
})
export type Config = z.infer<typeof configSchema>
export function getConfig(
inlineConfig: Partial<Config> = {},
configDirectory?: string,
): Config {
if (configDirectory === undefined) {
configDirectory = process.cwd()
}
const configFilePathJson = path.resolve(configDirectory, 'tsr.config.json')
const exists = existsSync(configFilePathJson)
let config: Config
if (exists) {
config = configSchema.parse({
...JSON.parse(readFileSync(configFilePathJson, 'utf-8')),
...inlineConfig,
})
} else {
config = configSchema.parse(inlineConfig)
}
// If typescript is disabled, make sure the generated route tree is a .js file
if (config.disableTypes) {
config.generatedRouteTree = config.generatedRouteTree.replace(
/\.(ts|tsx)$/,
'.js',
)
}
// if a configDirectory is used, paths should be relative to that directory
if (configDirectory) {
// if absolute configDirectory is provided, use it as the root
if (path.isAbsolute(configDirectory)) {
config.routesDirectory = path.resolve(
configDirectory,
config.routesDirectory,
)
config.generatedRouteTree = path.resolve(
configDirectory,
config.generatedRouteTree,
)
} else {
config.routesDirectory = path.resolve(
process.cwd(),
configDirectory,
config.routesDirectory,
)
config.generatedRouteTree = path.resolve(
process.cwd(),
configDirectory,
config.generatedRouteTree,
)
}
}
validateConfig(config)
return config
}
function validateConfig(config: Config) {
if (typeof config.experimental?.enableCodeSplitting !== 'undefined') {
const message = `
------
⚠️ ⚠️ ⚠️
ERROR: The "experimental.enableCodeSplitting" flag has been made stable and is now "autoCodeSplitting". Please update your configuration file to use "autoCodeSplitting" instead of "experimental.enableCodeSplitting".
------
`
console.error(message)
throw new Error(message)
}
if (config.indexToken === config.routeToken) {
throw new Error(
`The "indexToken" and "routeToken" options must be different.`,
)
}
if (
config.routeFileIgnorePrefix &&
config.routeFileIgnorePrefix.trim() === '_'
) {
throw new Error(
`The "routeFileIgnorePrefix" cannot be an underscore ("_"). This is a reserved character used to denote a pathless route. Please use a different prefix.`,
)
}
return config
}