Skip to content

Commit

Permalink
fix(router-generator): rootRouteChildren is correctly generated whe…
Browse files Browse the repository at this point in the history
…n using `disableTypes` (#2313)

* test(router-generator): js output is correct

* fix(router-generator): rootRoute with children is correctly being generated when `disableTypes` is set

* refactor(router-generator): make the last line ternery a single line
  • Loading branch information
SeanCassiere committed Sep 10, 2024
1 parent e6616c8 commit f462448
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 10 deletions.
12 changes: 5 additions & 7 deletions packages/router-generator/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -623,15 +623,13 @@ export const Route = createAPIFileRoute('${escapedRoutePath}')({
fileRoutesById: FileRoutesById
}`,
`export interface RootRouteChildren {
${routeTree.map((child) => `${child.variableName}Route: typeof ${getResolvedRouteNodeVariableName(child)}`).join(',')}
}`,
`const rootRouteChildren: RootRouteChildren = {
${routeTree.map((child) => `${child.variableName}Route: ${getResolvedRouteNodeVariableName(child)}`).join(',')}
${routeTree.map((child) => `${child.variableName}Route: typeof ${getResolvedRouteNodeVariableName(child)}`).join(',')}
}`,
]),
TYPES_DISABLED
? `export const routeTree = rootRoute._addFileChildren(rootRouteChildren)`
: `export const routeTree = rootRoute._addFileChildren(rootRouteChildren)._addFileTypes<FileRouteTypes>()`,
`const rootRouteChildren${TYPES_DISABLED ? '' : ': RootRouteChildren'} = {
${routeTree.map((child) => `${child.variableName}Route: ${getResolvedRouteNodeVariableName(child)}`).join(',')}
}`,
`export const routeTree = rootRoute._addFileChildren(rootRouteChildren)${TYPES_DISABLED ? '' : '._addFileTypes<FileRouteTypes>()'}`,
...config.routeTreeFileFooter,
]
.filter(Boolean)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,47 @@
// Import Routes

import { Route as rootRoute } from './routes/__root'
import { Route as PostsImport } from './routes/posts'
import { Route as IndexImport } from './routes/index'
import { Route as UsersUserIdImport } from './routes/users.$userId'
import { Route as PostsPostIdImport } from './routes/posts/$postId'

// Create/Update Routes

const PostsRoute = PostsImport.update({
path: '/posts',
getParentRoute: () => rootRoute,
})

const IndexRoute = IndexImport.update({
path: '/',
getParentRoute: () => rootRoute,
})

const UsersUserIdRoute = UsersUserIdImport.update({
path: '/users/$userId',
getParentRoute: () => rootRoute,
})

const PostsPostIdRoute = PostsPostIdImport.update({
path: '/$postId',
getParentRoute: () => PostsRoute,
})

// Create and export the route tree

const PostsRouteChildren = {
PostsPostIdRoute: PostsPostIdRoute,
}

const PostsRouteWithChildren = PostsRoute._addFileChildren(PostsRouteChildren)

const rootRouteChildren = {
IndexRoute: IndexRoute,
PostsRoute: PostsRouteWithChildren,
UsersUserIdRoute: UsersUserIdRoute,
}

export const routeTree = rootRoute._addFileChildren(rootRouteChildren)

/* prettier-ignore-end */
Expand All @@ -32,11 +62,26 @@ export const routeTree = rootRoute._addFileChildren(rootRouteChildren)
"__root__": {
"filePath": "__root.tsx",
"children": [
"/"
"/",
"/posts",
"/users/$userId"
]
},
"/": {
"filePath": "index.tsx"
},
"/posts": {
"filePath": "posts.tsx",
"children": [
"/posts/$postId"
]
},
"/posts/$postId": {
"filePath": "posts/$postId.tsx",
"parent": "/posts"
},
"/users/$userId": {
"filePath": "users.$userId.tsx"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,47 @@
// Import Routes

import { Route as rootRoute } from './routes/__root'
import { Route as PostsImport } from './routes/posts'
import { Route as IndexImport } from './routes/index'
import { Route as UsersUserIdImport } from './routes/users.$userId'
import { Route as PostsPostIdImport } from './routes/posts/$postId'

// Create/Update Routes

const PostsRoute = PostsImport.update({
path: '/posts',
getParentRoute: () => rootRoute,
})

const IndexRoute = IndexImport.update({
path: '/',
getParentRoute: () => rootRoute,
})

const UsersUserIdRoute = UsersUserIdImport.update({
path: '/users/$userId',
getParentRoute: () => rootRoute,
})

const PostsPostIdRoute = PostsPostIdImport.update({
path: '/$postId',
getParentRoute: () => PostsRoute,
})

// Create and export the route tree

const PostsRouteChildren = {
PostsPostIdRoute: PostsPostIdRoute,
}

const PostsRouteWithChildren = PostsRoute._addFileChildren(PostsRouteChildren)

const rootRouteChildren = {
IndexRoute: IndexRoute,
PostsRoute: PostsRouteWithChildren,
UsersUserIdRoute: UsersUserIdRoute,
}

export const routeTree = rootRoute._addFileChildren(rootRouteChildren)

/* prettier-ignore-end */
Expand All @@ -32,11 +62,26 @@ export const routeTree = rootRoute._addFileChildren(rootRouteChildren)
"__root__": {
"filePath": "__root.tsx",
"children": [
"/"
"/",
"/posts",
"/users/$userId"
]
},
"/": {
"filePath": "index.tsx"
},
"/posts": {
"filePath": "posts.tsx",
"children": [
"/posts/$postId"
]
},
"/posts/$postId": {
"filePath": "posts/$postId.tsx",
"parent": "/posts"
},
"/users/$userId": {
"filePath": "users.$userId.tsx"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import * as React from 'react'
import { createFileRoute } from '@tanstack/react-router'

export const Route = createFileRoute('/')({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as React from 'react'
import { createFileRoute } from '@tanstack/react-router'

export const Route = createFileRoute('/posts')({
component: () => <div>Hello /posts!</div>,
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as React from 'react'
import { createFileRoute } from '@tanstack/react-router'

export const Route = createFileRoute('/posts/$postId')({
component: () => <div>Hello /posts/$postId!</div>,
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as React from 'react'
import { createFileRoute } from '@tanstack/react-router'

export const Route = createFileRoute('/users/$userId')({
component: () => <div>Hello /users/$userId!</div>,
})

0 comments on commit f462448

Please sign in to comment.