Skip to content

Commit 2ae3702

Browse files
fix(router-plugin): do not overwrite resolved generator config with unprocessed inline config (#5963)
* do not overwrite resolved config with unprocessed inline input * ci: apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent d2f7ff4 commit 2ae3702

File tree

3 files changed

+119
-1
lines changed

3 files changed

+119
-1
lines changed

packages/router-plugin/src/core/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export const configSchema = generatorConfigSchema.extend({
105105
export const getConfig = (inlineConfig: Partial<Config>, root: string) => {
106106
const config = getGeneratorConfig(inlineConfig, root)
107107

108-
return configSchema.parse({ ...config, ...inlineConfig })
108+
return configSchema.parse({ ...inlineConfig, ...config })
109109
}
110110

111111
export type Config = z.infer<typeof configSchema>
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import path from 'node:path'
2+
import { readFileSync } from 'node:fs'
3+
import { describe, expect, it } from 'vitest'
4+
import { getConfig } from '../src'
5+
import type { Config } from '../src'
6+
7+
describe('getConfig', () => {
8+
const testCases = [
9+
{
10+
name: 'inline config',
11+
inlineConfig: {
12+
target: 'solid',
13+
autoCodeSplitting: false,
14+
enableRouteGeneration: true,
15+
} as Partial<Config>,
16+
withJson: false,
17+
},
18+
{
19+
name: `inline config and relative paths`,
20+
inlineConfig: {
21+
target: 'solid',
22+
autoCodeSplitting: false,
23+
routesDirectory: 'src/paths',
24+
generatedRouteTree: 'src/tree/routeTree.gen.ts',
25+
enableRouteGeneration: true,
26+
} as Partial<Config>,
27+
withJson: false,
28+
},
29+
{
30+
name: `inline config and "./" form relative paths`,
31+
inlineConfig: {
32+
target: 'solid',
33+
autoCodeSplitting: true,
34+
routesDirectory: './src/paths',
35+
generatedRouteTree: './src/tree/routeTree.gen.ts',
36+
enableRouteGeneration: true,
37+
} as Partial<Config>,
38+
withJson: false,
39+
},
40+
{
41+
name: 'inline config and absolute paths',
42+
inlineConfig: {
43+
target: 'solid',
44+
autoCodeSplitting: false,
45+
routesDirectory: '/src/paths',
46+
generatedRouteTree: '/src/tree/routeTree.gen.ts',
47+
enableRouteGeneration: true,
48+
} as Partial<Config>,
49+
withJson: false,
50+
},
51+
{
52+
name: 'json config',
53+
inlineConfig: {} as Partial<Config>,
54+
withJson: true,
55+
},
56+
{
57+
name: 'combination of json and inline config',
58+
inlineConfig: {
59+
target: 'react',
60+
autoCodeSplitting: true,
61+
routesDirectory: './src/paths',
62+
generatedRouteTree: './src/tree/routeTree.gen.ts',
63+
enableRouteGeneration: true,
64+
} as Partial<Config>,
65+
withJson: true,
66+
},
67+
]
68+
69+
it.each(testCases)('must resolve $name', ({ inlineConfig, withJson }) => {
70+
const rootPath = withJson ? 'withJson' : 'withoutJson'
71+
const root = path.resolve(import.meta.dirname, 'config', rootPath)
72+
73+
const jsonConfig = withJson
74+
? JSON.parse(readFileSync(path.resolve(root, 'tsr.config.json'), 'utf-8'))
75+
: undefined
76+
77+
const routesPath =
78+
inlineConfig.routesDirectory ??
79+
jsonConfig?.routesDirectory ??
80+
'src/routes'
81+
82+
const routeTreePath =
83+
inlineConfig.generatedRouteTree ??
84+
jsonConfig?.generatedRouteTree ??
85+
'src/routeTree.gen.ts'
86+
87+
const routesDirectory = path.resolve(
88+
import.meta.dirname,
89+
'config',
90+
rootPath,
91+
routesPath,
92+
)
93+
94+
const generatedRouteTree = path.resolve(
95+
import.meta.dirname,
96+
'config',
97+
rootPath,
98+
routeTreePath,
99+
)
100+
101+
const resolvedConfig = getConfig(inlineConfig, root)
102+
103+
expect(resolvedConfig).toEqual(
104+
expect.objectContaining({
105+
...jsonConfig,
106+
...inlineConfig,
107+
routesDirectory,
108+
generatedRouteTree,
109+
}),
110+
)
111+
})
112+
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"routesDirectory": "./src/routes",
3+
"generatedRouteTree": "./src/routeTree.gen.ts",
4+
"target": "solid",
5+
"autoCodeSplitting": false
6+
}

0 commit comments

Comments
 (0)