Support nested roots (#18)

* feat: remove isLoader route property

* fix: has-parent-route fs-router util

* feat: handle routeGenerator nested __root

* feat: enable multiple roots with nested dynamic paths

* feat: enable nested routes

* fix: prevent route path creation for __root

* feat: prevent wrong path on subfolder index routes

* chore: cleaned tutorial example folder

* feat: update version to v0.8.0
This commit is contained in:
Valerio Ageno
2024-07-23 19:13:18 +02:00
committed by GitHub
parent 444d1479b0
commit 1726312d34
26 changed files with 556 additions and 106 deletions
+14 -76
View File
@@ -1,15 +1,17 @@
import * as fsp from 'fs/promises'
import path from 'path'
import { buildRouteConfig } from './build-route-config'
import { hasParentRoute } from './has-parent-route'
import {
cleanPath,
determineNodePath,
multiSortBy,
spaces,
replaceBackslash,
removeExt,
routePathToVariable,
removeGroups,
removeLastSlash,
removeUnderscores,
removeLayoutSegments,
} from './utils'
@@ -18,6 +20,7 @@ import type { Config, RouteNode } from './types'
import { ROUTES_FOLDER, ROOT_PATH_ID, GENERATED_ROUTE_TREE } from './constants'
import { format } from 'prettier'
import { sortRouteNodes } from './sort-route-nodes'
let latestTask = 0
@@ -56,9 +59,6 @@ async function getRouteNodes(
// Remove the index from the route path and
// if the route path is empty, use `/'
const isLoader = routePath.includes('-loading')
if (routePath === 'index') {
routePath = '/'
}
@@ -69,7 +69,6 @@ async function getRouteNodes(
filePath,
fullPath,
routePath,
isLoader,
variableName,
})
} else if (fullPath.match(/\.(rs)$/)) {
@@ -95,38 +94,6 @@ async function getRouteNodes(
return { routeNodes, rustHandlersNodes }
}
export function hasParentRoute(
routes: RouteNode[],
node: RouteNode,
routePathToCheck: string | undefined,
): RouteNode | null {
if (!routePathToCheck || routePathToCheck === '/') {
return null
}
const sortedNodes = multiSortBy(routes, [
(d): number => d.routePath.length * -1,
(d): string | undefined => d.variableName,
]).filter((d) => d.routePath !== `/${ROOT_PATH_ID}`)
for (const route of sortedNodes) {
if (route.routePath === '/') continue
if (
routePathToCheck.startsWith(`${route.routePath}/`) &&
route.routePath !== routePathToCheck
) {
return route
}
}
const segments = routePathToCheck.split('/')
segments.pop() // Remove the last segment
const parentRoutePath = segments.join('/')
return hasParentRoute(routes, node, parentRoutePath)
}
export async function routeGenerator(config = defaultConfig): Promise<void> {
if (!isFirst) {
isFirst = true
@@ -149,25 +116,12 @@ export async function routeGenerator(config = defaultConfig): Promise<void> {
const { routeNodes: beforeRouteNodes, rustHandlersNodes } =
await getRouteNodes(config)
const preRouteNodes = multiSortBy(beforeRouteNodes, [
(d): number => (d.routePath === '/' ? -1 : 1),
(d): number => d.routePath.split('/').length,
(d): number => (d.filePath.match(/[./]index[.]/) ? 1 : -1),
(d): number =>
d.filePath.match(
/[./](component|errorComponent|pendingComponent|loader|lazy)[.]/,
)
? 1
: -1,
(d): number => (d.filePath.match(/[./]route[.]/) ? -1 : 1),
(d): number => (d.routePath.endsWith('/') ? -1 : 1),
(d): string => d.routePath,
]).filter((d) => ![`/${ROOT_PATH_ID}`].includes(d.routePath || ''))
const preRouteNodes = sortRouteNodes(beforeRouteNodes)
const routeNodes: RouteNode[] = []
// Loop over the flat list of routeNodes and
// build up a tree based on the routeNodes' routePath
const routeNodes: RouteNode[] = []
const handleNode = async (node: RouteNode): Promise<void> => {
const parentRoute = hasParentRoute(routeNodes, node, node.routePath)
@@ -175,8 +129,8 @@ export async function routeGenerator(config = defaultConfig): Promise<void> {
node.path = determineNodePath(node)
node.cleanedPath = removeGroups(
removeUnderscores(removeLayoutSegments(node.path)) ?? '',
node.cleanedPath = removeLastSlash(
removeGroups(removeUnderscores(removeLayoutSegments(node.path)) ?? ''),
)
if (node.parent) {
@@ -190,25 +144,6 @@ export async function routeGenerator(config = defaultConfig): Promise<void> {
await handleNode(node)
}
function buildRouteConfig(nodes: RouteNode[], depth = 1): string {
const children = nodes.map((node) => {
if (node.isLayout) {
return
}
const route = `${node.variableName}Route`
if (node.children?.length) {
const childConfigs = buildRouteConfig(node.children, depth + 1)
return `${route}.addChildren([${spaces(depth * 4)}${childConfigs}])`
}
return route
})
return children.filter(Boolean).join(`,`)
}
const routeConfigChildrenText = buildRouteConfig(routeNodes)
const sortedRouteNodes = multiSortBy(routeNodes, [
@@ -237,7 +172,10 @@ export async function routeGenerator(config = defaultConfig): Promise<void> {
const createRoutes = [
...sortedRouteNodes.map((node) => {
return `const ${node.variableName} = createRoute({ component: ${node.variableName}Import })`
const isRoot = node.routePath.endsWith(ROOT_PATH_ID)
const rootDeclaration = isRoot ? ', isRoot: true' : ''
return `const ${node.variableName} = createRoute({ component: ${node.variableName}Import${rootDeclaration} })`
}),
].join('\n')
@@ -247,7 +185,7 @@ export async function routeGenerator(config = defaultConfig): Promise<void> {
return [
`const ${node.variableName}Route = ${node.variableName}.update({
${[
`path: '${node.cleanedPath}'`,
!node.path?.endsWith(ROOT_PATH_ID) && `path: '${node.cleanedPath}'`,
`getParentRoute: () => ${node.parent?.variableName ?? 'root'}Route`,
rustHandlersNodes.includes(node.path || '')
? 'hasHandler: true'