Installation
Install the Vite plugin as a development dependency:The
@tanstack/router-plugin package provides plugins for multiple bundlers. For Vite-specific functionality, import from @tanstack/router-plugin/vite.Basic Setup
Add the plugin to yourvite.config.ts:
Configuration
Configure the plugin with an options object:Configuration Options
'react' | 'solid' | 'vue'
default:"react"
The framework you’re using with TanStack Router
string
default:"./src/routes"
The directory containing your route files
string
default:"./src/routeTree.gen.ts"
Where to output the generated route tree file
string
Optional prefix for route files (e.g., “route” matches “route.home.tsx”)
string
default:"-"
Files starting with this prefix will be ignored
string
A regex pattern for files to ignore
'single' | 'double'
default:"single"
Quote style for generated code
boolean
default:false
Whether to include semicolons in generated code
boolean
Automatically enable code splitting for your routes
boolean
default:false
Disable TypeScript type generation
boolean
default:false
Disable plugin logging output
boolean | string
default:false
Add file extensions to imports. Can be
true for .js or a custom extensionCodeSplittingOptions
Advanced code splitting configuration (see below)
Code Splitting
The plugin supports automatic code splitting to optimize your bundle size:Code Splitting Options
CodeSplitGroupings
Default grouping strategy for route components:
(params: { routeId: string }) => CodeSplitGroupings | undefined
Function to control splitting behavior per route. Return
undefined to use defaultBehaviorArray<string>
Route properties to remove during code splitting (e.g.,
['loader', 'action'])boolean
default:true
Enable hot module replacement for code-split route components
Framework-Specific Configuration
React
Solid
Vue
Specialized Plugins
The package exports specialized plugins for specific use cases:Route Generator Only
Only generate routes without code splitting:Code Splitter Only
Only handle code splitting (requires pre-generated routes):Route Auto-Import
Automatically import route files:Environment-Specific Configuration
The plugin supports Vite’s environment API:Hot Module Replacement (HMR)
The plugin automatically configures HMR for your routes:- Route components are hot-reloaded without full page refresh
- Route configuration changes trigger route tree regeneration
- Maintains router state during HMR updates
- Works with code-split routes
Using with TypeScript
The plugin generates TypeScript types automatically. Ensure the generated route tree is included in yourtsconfig.json:
Alternative: Standalone Vite Plugin Package
You can also use the standalone Vite plugin package:This package is a thin wrapper around
@tanstack/router-plugin/vite. Both approaches are equivalent.Migration from CLI
If you’re currently using the CLI (tsr watch), migrate to the Vite plugin:
Before:
vite.config.ts:
Troubleshooting
Plugin order matters
Always place the router plugin before your framework plugin:Routes not generating
- Verify
routesDirectorypath is correct - Check that route files match the expected naming convention
- Ensure Vite dev server is running
- Check the console for plugin errors
HMR not working
- Verify the plugin is before your framework plugin
- Check that
addHmris not set tofalsein code splitting options - Restart the Vite dev server
Type errors
- Ensure the generated route tree is included in
tsconfig.json - Restart your TypeScript server
- Run
tsc --noEmitto check for errors
Code splitting issues
- Verify
autoCodeSplittingis set totrue - Check your
splitBehaviorfunction returns valid groupings - Ensure route components are exported correctly