恒美微站 Logo 恒美微站
  • 首页
  • 关于我们
  • 建站服务
  • 主题模板
  • 案例展示
  • 资讯中心
  • 联系我们

Rspack/Rsbuild 项目如何配置 TanStack Router 插件生成路由树

  • 首页
  • 资讯中心
  • /
  • Rspack/Rsbuild 项目如何配置 TanStack Router 插件生成路由树

相关资讯

Escrcpy 中的 Gnirehtet 命令行全解析:从反向网络共享原理到 API 实战 2026/9/15 14:25:54
AI项目RACI矩阵实战指南:责任划分与动态管理 2026/9/15 14:20:53
EMS-YOLO:深度直接训练脉冲SNN实现目标检测的代码解析 2026/9/15 14:20:53

最新资讯

txtai Embeddings 方法全解析:从索引构建到语义搜索的完整 API 指南
dbt Snowflake 认证中的桩密码(ADBC_STUB_PASSWORD):设计意图、OAuth 例外与维护红线
page-agent MCP Server实战:让Claude Desktop和Copilot远程操控你的浏览器
千笔智能体:专业降AI率工具实测与技巧
使用 Axolotl 全参数微调 IBM Granite 4.0:安装、配置与实战指南
Zvec 线程池与并发工具剖析:ailego/parallel 模块深度解析

今日推荐

GDPR下大数据架构重构与隐私保护实践
多组学数据平台架构设计与优化实践
企业主数据管理系统架构设计与实施全解析

本周热门

AI SDK Harness 依赖更新指南:掌握 harness 包 SDK 依赖的升级、桥接同步与一致性校验
Refine v5 Ant Design NumberField 组件实战:基于 Intl 的本地化数字格式化
Flutter应用改名全指南:从Android到iOS的配置与工具实践

本月精选

自研推理加速器Redwood:两周内实现PyTorch模型高效部署的实战教程
V4L2摄像头采集实战:从camera_client.rar到出图全流程解析
从“谁发明了钢琴键”到知识问答智能体:RAG与记忆工程实践

Rspack/Rsbuild 项目如何配置 TanStack Router 插件生成路由树

发布时间:2026/9/15 14:25:54
Rspack/Rsbuild 项目如何配置 TanStack Router 插件生成路由树 Rspack/Rsbuild 项目如何配置 TanStack Router 插件生成路由树【免费下载链接】router A client-first, server-capable, fully type-safe router and full-stack framework for the web (React and more).项目地址: https://gitcode.com/GitHub_Trending/ro/router如果你在一个使用 Rspack 或 Rsbuild 打包的项目里想用 TanStack Router 的文件式路由file-based routing需要安装tanstack/router-plugin插件并把它挂进 Rsbuild 配置。插件会在构建和开发过程中自动扫描路由目录、生成路由树文件routeTree.gen.ts之后你只需按文件组织路由组件即可。本文以 React 项目为主线给出最短可跑通的路径Solid 项目在文中单独标注。安装插件在项目中安装tanstack/router-plugin文档中的安装说明以包管理器标签形式给出React 与 Solid 使用同一个包名官方示例项目将其放在 devDependencies 中版本为^1.168.37# 以 pnpm 为例官方示例项目即使用 pnpm pnpm add -D tanstack/router-plugin路由运行时依赖按框架选择例如 React 项目需要tanstack/react-router示例项目使用^1.170.35。示例项目的 package.json 可以参照。在 rsbuild.config.ts 中注册插件编辑项目根目录的rsbuild.config.ts从tanstack/router-plugin/rspack导入tanstackRouter并放进tools.rspack.pluginsimport { defineConfig } from rsbuild/core import { pluginReact } from rsbuild/plugin-react import { tanstackRouter } from tanstack/router-plugin/rspack export default defineConfig({ plugins: [pluginReact()], tools: { rspack: { plugins: [ tanstackRouter({ target: react, autoCodeSplitting: true, }), ], }, }, })与 官方示例项目的配置 一致。其中target指定框架reactautoCodeSplitting开启自动代码分割。Solid 项目是可选分支配置区别在框架插件和targetimport { defineConfig } from rsbuild/core import { pluginBabel } from rsbuild/plugin-babel import { pluginSolid } from rsbuild/plugin-solid import { tanstackRouter } from tanstack/router-plugin/rspack export default defineConfig({ plugins: [ pluginBabel({ include: /\.(?:jsx|tsx)$/, }), pluginSolid(), ], tools: { rspack: { plugins: [tanstackRouter({ target: solid, autoCodeSplitting: true })], }, }, })建立路由文件与入口路由文件放在插件默认扫描的目录./src/routes下。以示例项目为例结构是src/ ├── routes/ │ ├── __root.tsx # 根布局路由 │ ├── index.tsx # 索引路由对应 / │ └── about.tsx # 普通路由对应 /about ├── app.tsx └── routeTree.gen.ts # 插件自动生成不要手动修改入口组件从生成的文件导入routeTree并创建路由实例示例 app.tsx 的内容import * as React from react import { RouterProvider, createRouter } from tanstack/react-router import { routeTree } from ./routeTree.gen import ./styles.css const router createRouter({ routeTree, defaultPreload: intent, scrollRestoration: true, }) declare module tanstack/react-router { interface Register { router: typeof router } } const App () { return RouterProvider router{router} / } export default App路由文件本身用createFileRoute定义例如src/routes/index.tsximport { createFileRoute } from tanstack/react-router import * as React from react export const Route createFileRoute(/)({ component: HomeComponent, }) function HomeComponent() { return ( div classNamep-2 h3Welcome Home!/h3 /div ) }文件命名规则routeToken默认为routeindexToken默认为index因此posts.tsx、posts.route.tsx、posts/route.tsx对应同一个运行时路径/posts以-开头的文件和目录如-components默认会被忽略可用来存放非路由的伴生文件。完整命名与配置语义见 File-based Routing API Reference。运行并验证路由树已生成启动开发服务器示例项目的dev脚本为rsbuild dev --port 3000pnpm dev插件在构建流程中生成路由树文件默认路径为./src/routeTree.gen.ts。文件头部会标明它是自动生成的、不应手动修改。示例项目的 routeTree.gen.ts 文档示例内容节选如下可以看到两条路由被登记进FileRoutesByFullPath// This file was automatically generated by TanStack Router. // You should NOT make any changes in this file as it will be overwritten. // 以下为文档示例节选 import { Route as rootRouteImport } from ./routes/__root import { Route as IndexRouteImport } from ./routes/index import { Route as AboutRouteImport } from ./routes/about export interface FileRoutesByFullPath { /: typeof IndexRoute /about: typeof AboutRoute }验证方式新增一个路由文件例如src/routes/about.tsx后重新触发构建检查routeTree.gen.ts中是否出现对应的id/path条目——这是判断插件生效的直接依据。打开浏览器访问http://localhost:3000和http://localhost:3000/about确认两个路由都能渲染。生产构建时示例项目使用rsbuild build tsc --noEmit类型检查通过也说明生成文件与路由文件的类型是自洽的。让 linter 与编辑器忽略生成文件routeTree.gen.ts由 TanStack Router 管理不应被 linter/formatter 改动。建议按官方文档 with-rspack 的处理方式在.prettierignore、ESLint 忽略规则或 Biome 的files.ignore中排除该文件。使用 VSCode 时重命名路由后可能出现路由树文件带着错误被意外打开。建议在项目根目录创建.vscode/settings.json把该文件标记为只读、并排除出文件监视与搜索结果{ files.readonlyInclude: { **/routeTree.gen.ts: true }, files.watcherExclude: { **/routeTree.gen.ts: true }, search.exclude: { **/routeTree.gen.ts: true } }默认配置与常用选项插件带有一组默认值多数项目无需任何额外配置{ routesDirectory: ./src/routes, generatedRouteTree: ./src/routeTree.gen.ts, routeFileIgnorePrefix: -, quoteStyle: single }需要自定义时直接修改传给tanstackRouter的配置对象即可。两个值得注意的选项routesDirectory/generatedRouteTree路由目录与路由树文件路径均相对于当前工作目录不能设为空字符串或undefined。autoCodeSplitting默认false官方说明 TanStack Router v2 会将它默认改为true本文示例显式开启了对应能力。不要将routeFilePrefix、routeFileIgnorePrefix或routeFileIgnorePattern配置成会匹配到路由命名约定中所用 token 的值文档明确警告这会导致意外行为。全部可配置项见 File-based Routing API Reference。进一步参考配置文档Installation with Rspack可运行的完整示例Reactexamples/react/quickstart-rspack-file-basedSolid 示例examples/solid/quickstart-rspack-file-based【免费下载链接】router A client-first, server-capable, fully type-safe router and full-stack framework for the web (React and more).项目地址: https://gitcode.com/GitHub_Trending/ro/router创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

关于恒美微站

恒美微站专注于为个体商户、工作室提供极简自助建站服务,让每个人都能轻松拥有专业网站。

快速链接

  • 关于我们
  • 建站服务
  • 主题模板
  • 案例展示
  • 资讯中心

服务项目

  • 可视化建站
  • 拖拽编辑
  • 主题定制
  • SEO 优化
  • 网站托管

联系方式

  • 📍 地址:北京市朝阳区建国路 88 号
  • 📞 电话:400-888-8888
  • ✉️ 邮箱:info@hmyw.cn
  • 🕐 时间:周一至周日 9:00-18:00

© 2024 恒美微站 hmyw.cn 版权所有 | 京 ICP 备 12345678 号