ESBuild Plugin
The TanStack Router ESBuild plugin provides integration with ESBuild, enabling route generation and code splitting for your router application.Installation
Basic Usage
Add the plugin to your ESBuild configuration:Configuration
The plugin accepts the same configuration options as other TanStack Router plugins: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.
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.
Separate Plugins
For more granular control, use individual plugins:Route Generator Only
Code Splitter Only
How It Works
The ESBuild plugin is built using unplugin, providing a unified plugin interface. The plugin:- Scans your routes directory during the build
- Generates a typed route tree file
- Transforms route imports for code splitting
- Handles route updates during watch mode
Examples
Basic Build Script
Watch Mode
Production Build
TypeScript Configuration
Build Script with npm
Create a build script in yourpackage.json:
build.js:
TypeScript Configuration
Ensure yourtsconfig.json includes the generated route tree:
Troubleshooting
Routes Not Generated
Ensure your build script runs the plugin before bundling your application. The plugin needs to generate the route tree before ESBuild processes your entry point.Watch Mode Not Working
When using ESBuild’s watch mode or context API, the plugin will automatically regenerate routes when files change. Make sure you’re usingesbuild.context() instead of esbuild.build() for watch mode.
TypeScript Errors
If you see TypeScript errors about the route tree:- Make sure the generated route tree file exists
- Check that it’s included in your
tsconfig.json - Run your build once to generate the initial route tree
Performance
ESBuild is extremely fast, but if you notice slow builds:- Check your
routesDirectoryisn’t too broad - Use
routeFileIgnorePatternto exclude unnecessary files - Consider disabling type generation in production builds with
disableTypes: true