diff --git a/packages/router-plugin/src/core/config.ts b/packages/router-plugin/src/core/config.ts index d776c7cd5b..f93166d392 100644 --- a/packages/router-plugin/src/core/config.ts +++ b/packages/router-plugin/src/core/config.ts @@ -105,7 +105,7 @@ export const configSchema = generatorConfigSchema.extend({ export const getConfig = (inlineConfig: Partial, root: string) => { const config = getGeneratorConfig(inlineConfig, root) - return configSchema.parse({ ...config, ...inlineConfig }) + return configSchema.parse({ ...inlineConfig, ...config }) } export type Config = z.infer diff --git a/packages/router-plugin/tests/config.test.ts b/packages/router-plugin/tests/config.test.ts new file mode 100644 index 0000000000..2f3e8cc7db --- /dev/null +++ b/packages/router-plugin/tests/config.test.ts @@ -0,0 +1,112 @@ +import path from 'node:path' +import { readFileSync } from 'node:fs' +import { describe, expect, it } from 'vitest' +import { getConfig } from '../src' +import type { Config } from '../src' + +describe('getConfig', () => { + const testCases = [ + { + name: 'inline config', + inlineConfig: { + target: 'solid', + autoCodeSplitting: false, + enableRouteGeneration: true, + } as Partial, + withJson: false, + }, + { + name: `inline config and relative paths`, + inlineConfig: { + target: 'solid', + autoCodeSplitting: false, + routesDirectory: 'src/paths', + generatedRouteTree: 'src/tree/routeTree.gen.ts', + enableRouteGeneration: true, + } as Partial, + withJson: false, + }, + { + name: `inline config and "./" form relative paths`, + inlineConfig: { + target: 'solid', + autoCodeSplitting: true, + routesDirectory: './src/paths', + generatedRouteTree: './src/tree/routeTree.gen.ts', + enableRouteGeneration: true, + } as Partial, + withJson: false, + }, + { + name: 'inline config and absolute paths', + inlineConfig: { + target: 'solid', + autoCodeSplitting: false, + routesDirectory: '/src/paths', + generatedRouteTree: '/src/tree/routeTree.gen.ts', + enableRouteGeneration: true, + } as Partial, + withJson: false, + }, + { + name: 'json config', + inlineConfig: {} as Partial, + withJson: true, + }, + { + name: 'combination of json and inline config', + inlineConfig: { + target: 'react', + autoCodeSplitting: true, + routesDirectory: './src/paths', + generatedRouteTree: './src/tree/routeTree.gen.ts', + enableRouteGeneration: true, + } as Partial, + withJson: true, + }, + ] + + it.each(testCases)('must resolve $name', ({ inlineConfig, withJson }) => { + const rootPath = withJson ? 'withJson' : 'withoutJson' + const root = path.resolve(import.meta.dirname, 'config', rootPath) + + const jsonConfig = withJson + ? JSON.parse(readFileSync(path.resolve(root, 'tsr.config.json'), 'utf-8')) + : undefined + + const routesPath = + inlineConfig.routesDirectory ?? + jsonConfig?.routesDirectory ?? + 'src/routes' + + const routeTreePath = + inlineConfig.generatedRouteTree ?? + jsonConfig?.generatedRouteTree ?? + 'src/routeTree.gen.ts' + + const routesDirectory = path.resolve( + import.meta.dirname, + 'config', + rootPath, + routesPath, + ) + + const generatedRouteTree = path.resolve( + import.meta.dirname, + 'config', + rootPath, + routeTreePath, + ) + + const resolvedConfig = getConfig(inlineConfig, root) + + expect(resolvedConfig).toEqual( + expect.objectContaining({ + ...jsonConfig, + ...inlineConfig, + routesDirectory, + generatedRouteTree, + }), + ) + }) +}) diff --git a/packages/router-plugin/tests/config/withJson/tsr.config.json b/packages/router-plugin/tests/config/withJson/tsr.config.json new file mode 100644 index 0000000000..367f00064c --- /dev/null +++ b/packages/router-plugin/tests/config/withJson/tsr.config.json @@ -0,0 +1,6 @@ +{ + "routesDirectory": "./src/routes", + "generatedRouteTree": "./src/routeTree.gen.ts", + "target": "solid", + "autoCodeSplitting": false +}