Skip to main content

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:
  1. Scans your routes directory during the build
  2. Generates a typed route tree file
  3. Transforms route imports for code splitting
  4. 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 your package.json:
Then create build.js:

TypeScript Configuration

Ensure your tsconfig.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 using esbuild.context() instead of esbuild.build() for watch mode.

TypeScript Errors

If you see TypeScript errors about the route tree:
  1. Make sure the generated route tree file exists
  2. Check that it’s included in your tsconfig.json
  3. Run your build once to generate the initial route tree

Performance

ESBuild is extremely fast, but if you notice slow builds:
  1. Check your routesDirectory isn’t too broad
  2. Use routeFileIgnorePattern to exclude unnecessary files
  3. Consider disabling type generation in production builds with disableTypes: true