Documentation Index
Fetch the complete documentation index at: https://mintlify.com/tanstack/router/llms.txt
Use this file to discover all available pages before exploring further.
Vite Plugin
The TanStack Router Vite plugin provides seamless integration with Vite, handling route generation, code splitting, and hot module replacement automatically.Installation
Basic Usage
Add the plugin to yourvite.config.ts:
Configuration
The plugin accepts a configuration object with the following options:Configuration Options
Route Generation
routesDirectory
- Type:
string - Default:
'./src/routes' - The directory where your route files are located.
generatedRouteTree
- Type:
string - Default:
'./src/routeTree.gen.ts' - The file where the generated route tree will be written.
routeFilePrefix
- Type:
string - Optional
- A prefix to require for all route files.
routeFileIgnorePrefix
- Type:
string - Default:
'-' - Files starting with this prefix will be ignored during route generation.
routeFileIgnorePattern
- Type:
string - Optional
- A regex pattern to ignore specific route files.
Code Splitting
codeSplittingOptions
- Type:
CodeSplittingOptions - Optional
- Controls how routes are code-split.
TypeScript Options
disableTypes
- Type:
boolean - Default:
false - Disable TypeScript type generation.
quoteStyle
- Type:
'single' | 'double' - Default:
'single' - Quote style for generated code.
semicolons
- Type:
boolean - Default:
false - Whether to include semicolons in generated code.
Advanced Options
autoCodeSplitting
- Type:
boolean - Optional
- Enable automatic code splitting for all routes.
enableRouteTreeFormatting
- Type:
boolean - Default:
true - Format the generated route tree file with Prettier.
routeTreeFileHeader
- Type:
string[] - Default:
['/* eslint-disable */', '// @ts-nocheck', '// noinspection JSUnusedGlobalSymbols'] - Lines to add at the top of the generated route tree file.
plugin.vite.environmentName
- Type:
string - Optional
- Specify a Vite environment name for the plugin.
Separate Plugins
For more granular control, you can use individual plugins:Route Generator Only
Code Splitter Only
Auto Import Plugin
How It Works
The Vite plugin combines multiple sub-plugins:- Route Generator: Scans your routes directory and generates the route tree
- Code Splitter: Transforms routes for automatic code splitting
- HMR Handler: Enables hot module replacement for route updates
- Auto Import: Handles automatic imports for route components
Examples
File-Based Routing with Code Splitting
Custom Route File Patterns
Multiple Frameworks
Troubleshooting
Routes Not Generated
Ensure yourroutesDirectory path is correct and relative to your project root.
TypeScript Errors
Make sure the generated route tree file is included in yourtsconfig.json:
HMR Not Working
Check thatcodeSplittingOptions.addHmr is enabled (itβs true by default).