Skip to content

Commit

Permalink
fix: route gen ignores non js/ts(x) files
Browse files Browse the repository at this point in the history
  • Loading branch information
tannerlinsley committed Jan 14, 2024
1 parent 6d7a5b3 commit b3409d4
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 51 deletions.
5 changes: 4 additions & 1 deletion packages/react-router/src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,10 @@ export function matchByPath(
if (routeSegment) {
if (routeSegment.type === 'wildcard') {
if (baseSegment?.value) {
params['*'] = joinPaths(baseSegments.slice(i).map((d) => d.value))
const _splat = joinPaths(baseSegments.slice(i).map((d) => d.value))
// TODO: Deprecate *
params['*'] = _splat
params['_splat'] = _splat
return true
}
return false
Expand Down
23 changes: 12 additions & 11 deletions packages/router-cli/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import path from 'path'
import { readFileSync } from 'fs'
import { readFileSync, existsSync } from 'fs'
import { z } from 'zod'

export const configSchema = z.object({
routeFilePrefix: z.string().optional(),
routeFileIgnorePrefix: z.string().optional().default('-'),
routesDirectory: z.string(),
generatedRouteTree: z.string(),
routesDirectory: z.string().optional().default('./src/routes'),
generatedRouteTree: z.string().optional().default('./src/routeTree.gen.ts'),
quoteStyle: z.enum(['single', 'double']).optional().default('single'),
future: z
.object({
unstable_codeSplitting: z.boolean().optional(),
})
.optional(),
})

export type Config = z.infer<typeof configSchema>

const configFilePathJson = path.resolve(process.cwd(), 'tsr.config.json')

export async function getConfig(): Promise<Config> {
return configSchema.parse(
JSON.parse(readFileSync(configFilePathJson, 'utf-8')),
)
const exists = existsSync(configFilePathJson)

if (exists) {
return configSchema.parse(
JSON.parse(readFileSync(configFilePathJson, 'utf-8')),
)
}

return configSchema.parse({})
}
79 changes: 40 additions & 39 deletions packages/router-cli/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as fs from 'fs/promises'
import * as prettier from 'prettier'
import { Config } from './config'
import { cleanPath, trimPathLeft } from '@tanstack/react-router'
import { existsSync } from 'fs'

let latestTask = 0
export const rootPathId = '__root'
Expand Down Expand Up @@ -60,7 +61,7 @@ async function getRouteNodes(config: Config) {

if (stat.isDirectory()) {
await recurse(relativePath)
} else {
} else if (fullPath.match(/\.(tsx|ts|jsx|js)$/)) {
const filePath = replaceBackslash(path.join(dir, fileName))
const filePathNoExt = removeExt(filePath)
let routePath =
Expand Down Expand Up @@ -160,7 +161,9 @@ export async function generator(config: Config) {
(d) => (d.filePath?.match(/[./]route[.]/) ? -1 : 1),
(d) => (d.routePath?.endsWith('/') ? -1 : 1),
(d) => d.routePath,
]).filter((d) => d.routePath !== `/${routePathIdPrefix + rootPathId}`)
]).filter(
(d) => ![`/${routePathIdPrefix + rootPathId}`].includes(d.routePath || ''),
)

const routeTree: RouteNode[] = []
const routePiecesByPath: Record<string, RouteSubNode> = {}
Expand All @@ -187,43 +190,39 @@ export async function generator(config: Config) {

node.cleanedPath = removeUnderscores(node.path) ?? ''

if (config.future?.unstable_codeSplitting) {
if (
!node.isVirtual &&
(node.isLoader ||
node.isComponent ||
node.isErrorComponent ||
node.isPendingComponent)
) {
routePiecesByPath[node.routePath!] =
routePiecesByPath[node.routePath!] || {}

routePiecesByPath[node.routePath!]![
node.isLoader
? 'loader'
: node.isErrorComponent
? 'errorComponent'
: node.isPendingComponent
? 'pendingComponent'
: 'component'
] = node

const anchorRoute = routeNodes.find(
(d) => d.routePath === node.routePath,
)

if (!anchorRoute) {
handleNode({
...node,
isVirtual: true,
isLoader: false,
isComponent: false,
isErrorComponent: false,
isPendingComponent: false,
})
}
return
if (
!node.isVirtual &&
(node.isLoader ||
node.isComponent ||
node.isErrorComponent ||
node.isPendingComponent)
) {
routePiecesByPath[node.routePath!] =
routePiecesByPath[node.routePath!] || {}

routePiecesByPath[node.routePath!]![
node.isLoader
? 'loader'
: node.isErrorComponent
? 'errorComponent'
: node.isPendingComponent
? 'pendingComponent'
: 'component'
] = node

const anchorRoute = routeNodes.find((d) => d.routePath === node.routePath)

if (!anchorRoute) {
handleNode({
...node,
isVirtual: true,
isLoader: false,
isComponent: false,
isErrorComponent: false,
isPendingComponent: false,
})
}
return
}

if (node.parent) {
Expand Down Expand Up @@ -417,7 +416,9 @@ export async function generator(config: Config) {
}
}`,
`export const routeTree = rootRoute.addChildren([${routeConfigChildrenText}])`,
].join('\n')
]
.filter(Boolean)
.join('\n')

const routeConfigFileContent = await prettier.format(routeImports, {
semi: false,
Expand Down

0 comments on commit b3409d4

Please sign in to comment.