feat(router): add simplified route generation option #67
+567
−25
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
更加简单的路由
要使一个组件有路由功能,需要导出一个
createFileRoute()
,因为目前选择的是 Tanstack Router 的 File-Based Routing. 虽然这一部分可以通过generate生成,但是如果碰到嵌套路由,使用这个命令就会有点反直觉,因此我的预期目标为:尽量不让这部分代码出现在组件文件内,示例代码如下:要达到这一目标,可以使用 Code-Based Routing ,在 TanStack Router 的官网里有写到:
因此,完全可以使用 Code-Based Routing 来替代 File-Based Routing 实现 TNF 中的路由功能,并简化路由。
只需按照 TanStack Router 的 File-Based Routing 路由规范( Code-Based Routing 和 File-Based Routing 的路由规范有差别,这里以 File-Based Routing 为准),实现路由路线的解析,生成一个符合 File-Based Routing 规范的路由路线,就可以平替原来的 generator 以自动生成
routeTree.gen.ts
。本PR实现了路由生成的功能(如果这个方案ok的话,后面补充example和测试用例),但是缺少路由配置的功能,原因有两点:
RouteConfig
,暂时没有确定的方案,可能要用 babel 或者 typescript 解析文件,不知道该用哪个方案比较好路由配置的 feature 预期如下:
下面举一个例子,例子里尽可能覆盖了可能情况:
生成出来的代码:
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm ci
Changesets
pnpm changeset
and following the prompts. Changesets that add features and fix bugs should all bepatch
before we release0.1.0
. Please prefix changeset messages withfeat:
,fix:
, orchore:
.Edits