Loading...
Loading...
Compare original and translation side by side
@tanstack/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.
重要提示:在Vite配置中,router插件必须放在框架插件(React、Solid、Vue)之前,顺序错误会导致路由生成和代码分割静默失败。
npm install -D @tanstack/router-pluginnpm 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(),
],
})// vite.config.ts
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import { tanstackRouter } from '@tanstack/router-plugin/vite'
export default defineConfig({
plugins: [
// 必须放在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,
}),
],
}// 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,
}),
],
}// 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,
}),
],
})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
},
},
})tanstackRouter({
target: 'react',
autoCodeSplitting: true,
codeSplittingOptions: {
// 所有路由的默认分组规则
defaultBehavior: [['component'], ['errorComponent'], ['notFoundComponent']],
// 单路由自定义分割规则
splitBehavior: ({ routeId }) => {
if (routeId === '/dashboard') {
// 仪表板页面的loader和component打包在同一分组
return [['loader', 'component'], ['errorComponent']]
}
// 返回undefined则使用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 |
| 选项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| | | 生成代码的引号风格 |
| | | 生成代码是否使用分号 |
| | | 关闭TypeScript类型声明 |
| | | 关闭插件日志输出 |
| | | 为导入语句添加文件后缀 |
| | | 格式化生成的路由树代码 |
| | | 设置为 |
import { routes } from './routes'
tanstackRouter({
target: 'react',
virtualRouteConfig: routes, // or './routes.ts'
})import { routes } from './routes'
tanstackRouter({
target: 'react',
virtualRouteConfig: routes, // 也可以填文件路径 './routes.ts'
})routeTree.gen.tsautoCodeSplitting: trueverboseFileRoutes: falsecreateFileRouterouteTree.gen.tsautoCodeSplitting: trueverboseFileRoutes: falsecreateFileRouteimport {
tanstackRouter, // Composed (default)
tanstackRouterGenerator, // Generator only
tanStackRouterCodeSplitter, // Code splitter only
tanstackRouterAutoImport, // Auto-import only
} from '@tanstack/router-plugin/vite'import {
tanstackRouter, // 组合版(默认)
tanstackRouterGenerator, // 仅路由生成器
tanStackRouterCodeSplitter, // 仅代码分割器
tanstackRouterAutoImport, // 仅自动导入插件
} from '@tanstack/router-plugin/vite'// WRONG — react() before tanstackRouter()
plugins: [react(), tanstackRouter({ target: 'react' })]
// CORRECT — tanstackRouter() first
plugins: [tanstackRouter({ target: 'react' }), react()]// 错误 —— react()放在tanstackRouter()之前
plugins: [react(), tanstackRouter({ target: 'react' })]
// 正确 —— tanstackRouter()放在最前
plugins: [tanstackRouter({ target: 'react' }), react()]target'react'// WRONG for Solid — generates React imports
tanstackRouter({ autoCodeSplitting: true })
// CORRECT for Solid
tanstackRouter({ target: 'solid', autoCodeSplitting: true })target'react'// Solid框架错误写法 —— 会生成React相关导入语句
tanstackRouter({ autoCodeSplitting: true })
// 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>
}autoCodeSplittingcreateLazyRoutelazyRouteComponent// 错误 —— 开启autoCodeSplitting后仍手动懒加载
const LazyAbout = lazyRouteComponent(() => import('./about'))
// 正确 —— 正常编写路由文件即可,插件会处理分割
// src/routes/about.tsx
export const Route = createFileRoute('/about')({
component: AboutPage,
})
function AboutPage() {
return <h1>关于我们</h1>
}