Which project does this relate to?
Router
Describe the bug
When using @tanstack/router-plugin/vite together with unplugin-auto-import/vite, auto-imported functions (like React hooks) throw runtime ReferenceError, even though TypeScript compilation succeeds without errors.
Your Example Website or App
https://stackblitz.com/edit/github-5wvqnagp?file=vite.config.js
Steps to Reproduce the Bug or Issue
- Install both plugins:
pnpm add -D @tanstack/router-plugin unplugin-auto-import
- Configure
vite.config.ts:
import { tanstackRouter } from '@tanstack/router-plugin/vite';
import AutoImport from 'unplugin-auto-import/vite';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [
AutoImport({
imports: ['react'],
dts: './src/auto-imports.d.ts',
}),
tanstackRouter({
target: 'react',
autoCodeSplitting: true,
routesDirectory: './src/pages',
}),
react(),
],
});
- Create a route file at
src/pages/login.tsx:
import { createFileRoute } from '@tanstack/react-router';
export const Route = createFileRoute('/login')({
component: RouteComponent,
});
function RouteComponent() {
const [username, setUsername] = useState(''); // No explicit import
return <div>{username}</div>;
}
- Run the dev server and navigate to
/login
⚠️ Expected Behavior
useState should be auto-imported at runtime by unplugin-auto-import
- The component should render without errors
❌ Actual Behavior
Runtime error in browser:
ReferenceError: useState is not defined
at RouteComponent (login.tsx:8:35)
Important observations:
- ✅ TypeScript does NOT show any errors (types are correctly generated in
auto-imports.d.ts)
- ✅ Without
@tanstack/router-plugin/vite, auto-import works perfectly
- ❌ With
@tanstack/router-plugin/vite, auto-imported functions are undefined at runtime
Expected behavior
After enabling auto-complete, it can be recognized normally.
Screenshots or Videos
Platform
- Router / Start Version: ^1.133.27
- OS: macOS
- Browser: Chrome
- Browser Version: [e.g. 91.1]
- Bundler: vite
- Bundler Version: npm:rolldown-vite@7.1.14
Additional context
No response
Which project does this relate to?
Router
Describe the bug
When using @tanstack/router-plugin/vite together with unplugin-auto-import/vite, auto-imported functions (like React hooks) throw runtime ReferenceError, even though TypeScript compilation succeeds without errors.
Your Example Website or App
https://stackblitz.com/edit/github-5wvqnagp?file=vite.config.js
Steps to Reproduce the Bug or Issue
vite.config.ts:src/pages/login.tsx:/loginuseStateshould be auto-imported at runtime byunplugin-auto-import❌ Actual Behavior
Runtime error in browser:
Important observations:
auto-imports.d.ts)@tanstack/router-plugin/vite, auto-import works perfectly@tanstack/router-plugin/vite, auto-imported functions are undefined at runtimeExpected behavior
After enabling auto-complete, it can be recognized normally.
Screenshots or Videos
Platform
Additional context
No response