diff --git a/packages/router-plugin/src/planners/route.ts b/packages/router-plugin/src/planners/route.ts index 83334948b9..810391652b 100644 --- a/packages/router-plugin/src/planners/route.ts +++ b/packages/router-plugin/src/planners/route.ts @@ -291,13 +291,6 @@ export function recognizeRoute( const exp = analysis.exports.find( (e) => e.kind === 'local' && e.name === 'Route' && !e.typeOnly, ) - if (!exp || exp.kind !== 'local' || exp.local === null) { - return { route: null, warnings } - } - const routeSym = exp.local - const unitIds = t1.declaringUnits.get(routeSym) - const routeUnit = - unitIds && unitIds.length === 1 ? t1.units[unitIds[0]!] : undefined const warn = (message: string, range?: Range, code?: string) => { warnings.push({ severity: 'warning', @@ -308,13 +301,33 @@ export function recognizeRoute( }) } const fileName = identity.path + const notOneVariable = (range: Range) => { + if (importsFactory(analysis)) { + warn( + `[tanstack-router] The \`Route\` export of "${fileName}" is not one variable (a namespace of that name merges with it, even one holding only types), so it is not code-split or hot-updated. Rename what else declares \`Route\`.`, + range, + 'route-unrecognized', + ) + } + return { route: null, warnings } + } + if (!exp || exp.kind !== 'local') { + return { route: null, warnings } + } + if (exp.local === null) { + return notOneVariable(exp.range) + } + const routeSym = exp.local + const unitIds = t1.declaringUnits.get(routeSym) + const routeUnit = + unitIds && unitIds.length === 1 ? t1.units[unitIds[0]!] : undefined if ( !routeUnit || routeUnit.kind !== 'declaration' || routeUnit.node.type !== 'VariableDeclarator' || routeUnit.node.init === null ) { - return { route: null, warnings } + return notOneVariable(exp.range) } const outer = unwrapExpression(routeUnit.node.init) if (outer.type !== 'CallExpression') { diff --git a/packages/router-plugin/tests/integration/route-merged-declarations.test.ts b/packages/router-plugin/tests/integration/route-merged-declarations.test.ts new file mode 100644 index 0000000000..12b1cd4382 --- /dev/null +++ b/packages/router-plugin/tests/integration/route-merged-declarations.test.ts @@ -0,0 +1,67 @@ +/** + * A `Route` export sharing its name with a type declaration. What the compiler + * can't split it must say (R78): the page component lands in the route module + * and the build warns once, naming the file; a route that splits says nothing. + */ +import { describe, expect, test } from 'vitest' +import { findDiagnostics } from '@tanstack/compiler-test-utils/diagnostics' +import { build, head, rootRoute } from '../build-harness' + +const timeout = 60_000 + +const route = `export const Route = createFileRoute('/')({ + component: () =>
MARK_PAGE_END
, +}) +` + +const shapes: Array<[string, string]> = [ + ['a type namespace', `namespace Route { export type T = string }\n`], + [ + 'an exported type namespace', + `export namespace Route { export type T = string }\n`, + ], + ['a dotted type namespace', `namespace Route.A { export type T = string }\n`], + [ + 'an exported dotted type namespace', + `export namespace Route.A { export type T = string }\n`, + ], + ['a declared namespace', `declare namespace Route { type T = string }\n`], + ['a type alias', `type Route = string\n`], + ['an exported type alias', `export type Route = string\n`], + ['an interface', `interface Route { a: string }\n`], +] + +describe.each(['vite', 'rspack', 'webpack'] as const)('on %s', (bundler) => { + test.each(shapes)( + '`Route` merged with %s splits or warns once', + async (_, declaration) => { + const app = await build({ + bundler, + env: 'client', + minify: false, + files: { + 'routes/__root.tsx': rootRoute, + 'routes/index.tsx': `${head}${declaration}${route}`, + 'entry.ts': `export { Route } from './routes/index'\n`, + }, + }) + try { + const reported = findDiagnostics( + [...app.warnings, ...app.consoleMessages], + { file: 'routes/index.tsx', root: app.root }, + ) + expect({ + where: app.where('MARK_PAGE_END'), + warnings: reported.length, + }).toEqual( + app.where('MARK_PAGE_END') === 'lazy' + ? { where: 'lazy', warnings: 0 } + : { where: 'eager', warnings: 1 }, + ) + } finally { + await app.dispose() + } + }, + timeout, + ) +})