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

config.ts

142 lines4.1 KB

1import { z } from 'zod'
2import {
3 configSchema as generatorConfigSchema,
4 getConfig as getGeneratorConfig,
5} from '@tanstack/router-generator'
6import type {
7 CreateFileRoute,
8 RegisteredRouter,
9 RouteIds,
10} from '@tanstack/router-core'
11import type { CodeSplitGroupings } from './constants'
12import type { CodeSplitCompilerPlugin } from './code-splitter/plugins'
13​
14export const splitGroupingsSchema = z
15 .array(
16 z.array(
17 z.union([
18 z.literal('loader'),
19 z.literal('component'),
20 z.literal('pendingComponent'),
21 z.literal('errorComponent'),
22 z.literal('notFoundComponent'),
23 ]),
24 ),
25 {
26 message:
27 " Must be an Array of Arrays containing the split groupings. i.e. [['component'], ['pendingComponent'], ['errorComponent', 'notFoundComponent']]",
28 },
29 )
30 .superRefine((val, ctx) => {
31 const flattened = val.flat()
32 const unique = [...new Set(flattened)]
33​
34 // Elements must be unique,
35 // ie. this shouldn't be allows [['component'], ['component', 'loader']]
36 if (unique.length !== flattened.length) {
37 ctx.addIssue({
38 code: 'custom',
39 message:
40 " Split groupings must be unique and not repeated. i.e. i.e. [['component'], ['pendingComponent'], ['errorComponent', 'notFoundComponent']]." +
41 `\n You input was: ${JSON.stringify(val)}.`,
42 })
43 }
44 })
45​
46export type CodeSplittingOptions = {
47 /**
48 * Use this function to programmatically control the code splitting behavior
49 * based on the `routeId` for each route.
50 *
51 * If you just need to change the default behavior, you can use the `defaultBehavior` option.
52 * @param params
53 */
54 splitBehavior?: (params: {
55 routeId: RouteIds<RegisteredRouter['routeTree']>
56 }) => CodeSplitGroupings | undefined | void
57​
58 /**
59 * The default/global configuration to control your code splitting behavior per route.
60 * @default [['component'],['pendingComponent'],['errorComponent'],['notFoundComponent']]
61 */
62 defaultBehavior?: CodeSplitGroupings
63​
64 /**
65 * The nodes that shall be deleted from the route.
66 * @default undefined
67 */
68 deleteNodes?: Array<DeletableNodes>
69​
70 /**
71 * @default true
72 */
73 addHmr?: boolean
74​
75 /**
76 * Internal compiler plugins used by framework integrations.
77 * @internal
78 */
79 compilerPlugins?: Array<CodeSplitCompilerPlugin>
81​
82export type HmrStyle = 'vite' | 'webpack'
83​
84export type HmrOptions = {
85 /**
86 * Selects the HMR runtime style to emit code for.
87 * - `'vite'` (default): ESM `import.meta.hot` with Vite accept-callback semantics.
88 * - `'webpack'`: `import.meta.webpackHot` with webpack / Rspack `module.hot` re-execution semantics.
89 *
90 * Bundler-specific plugin entries (e.g. `rspack.ts`, `webpack.ts`) set this explicitly.
91 */
92 style?: HmrStyle
94​
95const codeSplittingOptionsSchema = z.object({
96 splitBehavior: z
97 .custom<
98 CodeSplittingOptions['splitBehavior']
99 >((value) => typeof value === 'function')
100 .optional(),
101 defaultBehavior: splitGroupingsSchema.optional(),
102 deleteNodes: z.array(z.string()).optional(),
103 addHmr: z.boolean().optional().default(true),
104})
105​
106type FileRouteKeys = keyof (Parameters<
107 CreateFileRoute<any, any, any, any, any>
108>[0] & {})
109export type DeletableNodes = FileRouteKeys | (string & {})
110​
111export const configSchema = generatorConfigSchema.extend({
112 enableRouteGeneration: z.boolean().optional(),
113 codeSplittingOptions: z
114 .custom<CodeSplittingOptions>((v) => {
115 return codeSplittingOptionsSchema.parse(v)
116 })
117 .optional(),
118 plugin: z
119 .object({
120 hmr: z
121 .object({
122 style: z.enum(['vite', 'webpack']).optional(),
123 })
124 .optional(),
125 vite: z
126 .object({
127 environmentName: z.string().optional(),
128 })
129 .optional(),
130 })
131 .optional(),
132})
133​
134export const getConfig = (inlineConfig: Partial<Config>, root: string) => {
135 const config = getGeneratorConfig(inlineConfig, root)
136​
137 return configSchema.parse({ ...inlineConfig, ...config })
139​
140export type Config = z.infer<typeof configSchema>
141export type ConfigInput = z.input<typeof configSchema>
142export type ConfigOutput = z.output<typeof configSchema>