Loading...
Loading...
TanStack Router bundler plugin for route generation and automatic code splitting. Supports Vite, Webpack, Rspack, and esbuild. Configures autoCodeSplitting, routesDirectory, target framework, and code split groupings.
npx skill4agent add tanstack/router router-plugin@tanstack/router-pluginCRITICAL: The router plugin MUST come before the framework plugin (React, Solid, Vue) in the Vite config. Wrong order causes route generation and code splitting to fail silently.
npm install -D @tanstack/router-plugin// vite.config.ts
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import { tanstackRouter } from '@tanstack/router-plugin/vite'
export default defineConfig({
plugins: [
// MUST come before react()
tanstackRouter({
target: 'react',
autoCodeSplitting: true,
}),
react(),
],
})// webpack.config.js
const { tanstackRouter } = require('@tanstack/router-plugin/webpack')
module.exports = {
plugins: [
tanstackRouter({
target: 'react',
autoCodeSplitting: true,
}),
],
}// rspack.config.js
const { tanstackRouter } = require('@tanstack/router-plugin/rspack')
module.exports = {
plugins: [
tanstackRouter({
target: 'react',
autoCodeSplitting: true,
}),
],
}import { tanstackRouter } from '@tanstack/router-plugin/esbuild'
import esbuild from 'esbuild'
esbuild.build({
plugins: [
tanstackRouter({
target: 'react',
autoCodeSplitting: true,
}),
],
})| Option | Type | Default | Description |
|---|---|---|---|
| | | Target framework |
| | | Directory containing route files |
| | | Path for generated route tree |
| | | Enable automatic code splitting |
| | | Set to |
| Option | Type | Default | Description |
|---|---|---|---|
| | | Prefix filter for route files |
| | | Prefix to exclude files from routing |
| | | Pattern to exclude from routing |
| | | Token identifying index routes |
| | | Token identifying route config files |
tanstackRouter({
target: 'react',
autoCodeSplitting: true,
codeSplittingOptions: {
// Default groupings for all routes
defaultBehavior: [['component'], ['errorComponent'], ['notFoundComponent']],
// Per-route custom splitting
splitBehavior: ({ routeId }) => {
if (routeId === '/dashboard') {
// Keep loader and component together for dashboard
return [['loader', 'component'], ['errorComponent']]
}
// Return undefined to use defaultBehavior
},
},
})| Option | Type | Default | Description |
|---|---|---|---|
| | | Quote style in generated code |
| | | Use semicolons in generated code |
| | | Disable TypeScript types |
| | | Suppress plugin logs |
| | | Add file extensions to imports |
| | | Format generated route tree |
| | | When |
import { routes } from './routes'
tanstackRouter({
target: 'react',
virtualRouteConfig: routes, // or './routes.ts'
})routeTree.gen.tsautoCodeSplitting: trueverboseFileRoutes: falsecreateFileRouteimport {
tanstackRouter, // Composed (default)
tanstackRouterGenerator, // Generator only
tanStackRouterCodeSplitter, // Code splitter only
tanstackRouterAutoImport, // Auto-import only
} from '@tanstack/router-plugin/vite'// WRONG — react() before tanstackRouter()
plugins: [react(), tanstackRouter({ target: 'react' })]
// CORRECT — tanstackRouter() first
plugins: [tanstackRouter({ target: 'react' }), react()]target'react'// WRONG for Solid — generates React imports
tanstackRouter({ autoCodeSplitting: true })
// CORRECT for Solid
tanstackRouter({ target: 'solid', autoCodeSplitting: true })autoCodeSplittingcreateLazyRoutelazyRouteComponent// WRONG — manual lazy loading with autoCodeSplitting enabled
const LazyAbout = lazyRouteComponent(() => import('./about'))
// CORRECT — just write normal route files, plugin handles splitting
// src/routes/about.tsx
export const Route = createFileRoute('/about')({
component: AboutPage,
})
function AboutPage() {
return <h1>About</h1>
}