Skip to content

@tanstack/router-plugin/vite and unplugin-auto-import/vite compatibility issues #5653

@JsonLee12138

Description

@JsonLee12138

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

  1. Install both plugins:
pnpm add -D @tanstack/router-plugin unplugin-auto-import
  1. 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(),
  ],
});
  1. 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>;
}
  1. 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

Image

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions