feat(packages/tuono-(router|fs-router-vite-plugin)): replace __route with __layout (#283)

This commit is contained in:
Marco Pasqualetti
2025-01-03 17:08:18 +01:00
committed by GitHub
parent cf56b3a53e
commit 0ef1252e1f
24 changed files with 82 additions and 78 deletions
@@ -10,12 +10,12 @@ const routes = [
routePath: '/posts/my-post',
variableName: 'PostsMyPost',
parent: {
filePath: 'posts/__root.tsx',
filePath: 'posts/__layout.tsx',
fullPath:
'/tuono/packages/tuono-fs-router-vite-plugin/tests/generator/multi-level-root-dynamic/routes/posts/__root.tsx',
routePath: '/posts/__root',
'/tuono/packages/tuono-fs-router-vite-plugin/tests/generator/multi-level-root-dynamic/routes/posts/__layout.tsx',
routePath: '/posts/__layout',
variableName: 'Postsroot',
path: '/posts/__root',
path: '/posts/__layout',
cleanedPath: '/posts',
children: undefined,
},
@@ -29,12 +29,12 @@ const routes = [
routePath: '/posts/',
variableName: 'PostsIndex',
parent: {
filePath: 'posts/__root.tsx',
filePath: 'posts/__layout.tsx',
fullPath:
'/home/valerio/Documents/tuono/packages/tuono-fs-router-vite-plugin/tests/generator/multi-level-root-dynamic/routes/posts/__root.tsx',
routePath: '/posts/__root',
'/home/valerio/Documents/tuono/packages/tuono-fs-router-vite-plugin/tests/generator/multi-level-root-dynamic/routes/posts/__layout.tsx',
routePath: '/posts/__layout',
variableName: 'Postsroot',
path: '/posts/__root',
path: '/posts/__layout',
cleanedPath: '/posts',
children: undefined,
},
@@ -48,12 +48,12 @@ const routes = [
routePath: '/posts/',
variableName: 'PostspostIndex',
parent: {
filePath: 'posts/__root.tsx',
filePath: 'posts/__layout.tsx',
fullPath:
'/tuono/packages/tuono-fs-router-vite-plugin/tests/generator/multi-level-root-dynamic/routes/posts/__root.tsx',
routePath: '/posts/__root',
'/tuono/packages/tuono-fs-router-vite-plugin/tests/generator/multi-level-root-dynamic/routes/posts/__layout.tsx',
routePath: '/posts/__layout',
variableName: 'Postsroot',
path: '/posts/__root',
path: '/posts/__layout',
cleanedPath: '/posts',
children: undefined,
},
@@ -1,3 +1,3 @@
export const ROUTES_FOLDER = './src/routes/'
export const ROOT_PATH_ID = '__root'
export const LAYOUT_PATH_ID = '__layout'
export const GENERATED_ROUTE_TREE = './.tuono/routeTree.gen.ts'
@@ -18,7 +18,11 @@ import {
} from './utils'
import type { Config, RouteNode } from './types'
import { ROUTES_FOLDER, ROOT_PATH_ID, GENERATED_ROUTE_TREE } from './constants'
import {
ROUTES_FOLDER,
LAYOUT_PATH_ID,
GENERATED_ROUTE_TREE,
} from './constants'
import { sortRouteNodes } from './sort-route-nodes'
import isDefaultExported from './utils/is-default-exported'
@@ -154,7 +158,7 @@ export async function routeGenerator(config = defaultConfig): Promise<void> {
const routeConfigChildrenText = buildRouteConfig(routeNodes)
const sortedRouteNodes = multiSortBy(routeNodes, [
(d): number => (d.routePath.includes(`/${ROOT_PATH_ID}`) ? -1 : 1),
(d): number => (d.routePath.includes(`/${LAYOUT_PATH_ID}`) ? -1 : 1),
(d): number => d.routePath.split('/').length,
(d): number => (d.routePath.endsWith("index'") ? -1 : 1),
(d): RouteNode => d,
@@ -179,7 +183,7 @@ export async function routeGenerator(config = defaultConfig): Promise<void> {
const createRoutes = [
...sortedRouteNodes.map((node) => {
const isRoot = node.routePath.endsWith(ROOT_PATH_ID)
const isRoot = node.routePath.endsWith(LAYOUT_PATH_ID)
const rootDeclaration = isRoot ? ', isRoot: true' : ''
const variableName = node.variableName as string
@@ -195,7 +199,7 @@ export async function routeGenerator(config = defaultConfig): Promise<void> {
return [
`const ${variableName}Route = ${variableName}.update({
${[
!node.path?.endsWith(ROOT_PATH_ID) && `path: '${cleanedPath}'`,
!node.path?.endsWith(LAYOUT_PATH_ID) && `path: '${cleanedPath}'`,
`getParentRoute: () => ${node.parent?.variableName ?? 'root'}Route`,
rustHandlersNodes.includes(node.path || '')
? 'hasHandler: true'
@@ -213,15 +217,15 @@ export async function routeGenerator(config = defaultConfig): Promise<void> {
'// This file is auto-generated by Tuono',
"import { createRoute, dynamic } from 'tuono'",
[
`import RootImport from './${replaceBackslash(
`import RootLayoutImport from './${replaceBackslash(
path.relative(
path.dirname(config.generatedRouteTree),
path.resolve(config.folderName, ROOT_PATH_ID),
path.resolve(config.folderName, LAYOUT_PATH_ID),
),
)}'`,
].join('\n'),
imports,
'const rootRoute = createRoute({ isRoot: true, component: RootImport });',
'const rootRoute = createRoute({ isRoot: true, component: RootLayoutImport });',
createRoutes,
'// Create/Update Routes',
createRouteUpdates,
@@ -13,12 +13,12 @@ const routes = [
cleanedPath: '/posts/[post]',
},
{
filePath: 'posts/__root.tsx',
filePath: 'posts/__layout.tsx',
fullPath:
'/tuono/packages/tuono-fs-router-vite-plugin/tests/generator/multi-level-root/routes/posts/__root.tsx',
routePath: '/posts/__root',
'/tuono/packages/tuono-fs-router-vite-plugin/tests/generator/multi-level-root/routes/posts/__layout.tsx',
routePath: '/posts/__layout',
variableName: 'Postsroot',
path: '/posts/__root',
path: '/posts/__layout',
cleanedPath: '/posts',
},
{
@@ -42,12 +42,12 @@ const routes = [
]
const parent = {
filePath: 'posts/__root.tsx',
filePath: 'posts/__layout.tsx',
fullPath:
'/tuono/packages/tuono-fs-router-vite-plugin/tests/generator/multi-level-root/routes/posts/__root.tsx',
routePath: '/posts/__root',
'/tuono/packages/tuono-fs-router-vite-plugin/tests/generator/multi-level-root/routes/posts/__layout.tsx',
routePath: '/posts/__layout',
variableName: 'Postsroot',
path: '/posts/__root',
path: '/posts/__layout',
cleanedPath: '/posts',
}
@@ -1,4 +1,4 @@
import { ROOT_PATH_ID } from './constants'
import { LAYOUT_PATH_ID } from './constants'
import { multiSortBy } from './utils'
import type { RouteNode } from './types'
@@ -19,15 +19,15 @@ export function hasParentRoute(
(d): number => d.routePath.length * -1,
(d): string | undefined => d.variableName,
])
// Exclude base __root file
.filter((d) => d.routePath !== `/${ROOT_PATH_ID}`)
// Exclude base __layout file
.filter((d) => d.routePath !== `/${LAYOUT_PATH_ID}`)
for (const route of sortedNodes) {
if (route.routePath === '/') continue
if (
route.routePath.startsWith(parentRoutePath) &&
route.routePath.endsWith(ROOT_PATH_ID)
route.routePath.endsWith(LAYOUT_PATH_ID)
) {
return route
}
@@ -22,10 +22,10 @@ const routes = [
cleanedPath: '/about',
},
{
filePath: '__root.tsx',
filePath: '__layout.tsx',
fullPath:
'/tuono/packages/tuono-fs-router-vite-plugin/tests/generator/multi-level-root-dynamic/routes/__root.tsx',
routePath: '/__root',
'/tuono/packages/tuono-fs-router-vite-plugin/tests/generator/multi-level-root-dynamic/routes/__layout.tsx',
routePath: '/__layout',
variableName: 'root',
},
{
@@ -50,10 +50,10 @@ const routes = [
variableName: 'PostsIndex',
},
{
filePath: 'posts/__root.tsx',
filePath: 'posts/__layout.tsx',
fullPath:
'/tuono/packages/tuono-fs-router-vite-plugin/tests/generator/multi-level-root-dynamic/routes/posts/__root.tsx',
routePath: '/posts/__root',
'/tuono/packages/tuono-fs-router-vite-plugin/tests/generator/multi-level-root-dynamic/routes/posts/__layout.tsx',
routePath: '/posts/__layout',
variableName: 'Postsroot',
},
]
@@ -78,10 +78,10 @@ const expectedSorting = [
cleanedPath: '/about',
},
{
filePath: 'posts/__root.tsx',
filePath: 'posts/__layout.tsx',
fullPath:
'/tuono/packages/tuono-fs-router-vite-plugin/tests/generator/multi-level-root-dynamic/routes/posts/__root.tsx',
routePath: '/posts/__root',
'/tuono/packages/tuono-fs-router-vite-plugin/tests/generator/multi-level-root-dynamic/routes/posts/__layout.tsx',
routePath: '/posts/__layout',
variableName: 'Postsroot',
},
{
@@ -1,6 +1,6 @@
import type { RouteNode } from './types'
import { multiSortBy } from './utils'
import { ROOT_PATH_ID } from './constants'
import { LAYOUT_PATH_ID } from './constants'
// Routes need to be sorted in order to iterate over the handleNode fn
// with first the items that might be parent routes
@@ -14,4 +14,4 @@ export const sortRouteNodes = (routes: Array<RouteNode>): Array<RouteNode> =>
(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 || ''))
]).filter((d) => ![`/${LAYOUT_PATH_ID}`].includes(d.routePath || ''))