mirror of
https://github.com/tuono-labs/tuono
synced 2026-07-29 22:02:46 -07:00
feat: create basic router
This commit is contained in:
@@ -239,29 +239,40 @@ export async function routeGenerator(config = defaultConfig): Promise<void> {
|
||||
(d): any => d,
|
||||
])
|
||||
|
||||
const imports = [
|
||||
...sortedRouteNodes.map((node) => {
|
||||
return `import ${node.variableName}Import from './${replaceBackslash(
|
||||
removeExt(
|
||||
path.relative(
|
||||
path.dirname(config.generatedRouteTree),
|
||||
path.resolve(config.folderName, node.filePath),
|
||||
),
|
||||
false,
|
||||
),
|
||||
)}'`
|
||||
}),
|
||||
].join('\n')
|
||||
|
||||
const createRoutes = [
|
||||
...sortedRouteNodes.map((node) => {
|
||||
return `const ${node.variableName} = createRoute({ component: ${node.variableName}Import })`
|
||||
}),
|
||||
].join('\n')
|
||||
|
||||
const routeImports = [
|
||||
'// This file is auto-generated by Tuono',
|
||||
"import { createRoute } from 'tuono'",
|
||||
[
|
||||
`import { Route as rootRoute } from './${replaceBackslash(
|
||||
`import RootImport from './${replaceBackslash(
|
||||
path.relative(
|
||||
path.dirname(config.generatedRouteTree),
|
||||
path.resolve(config.folderName, ROOT_PATH_ID),
|
||||
),
|
||||
)}'`,
|
||||
...sortedRouteNodes.map((node) => {
|
||||
return `import { Route as ${
|
||||
node.variableName
|
||||
}Import } from './${replaceBackslash(
|
||||
removeExt(
|
||||
path.relative(
|
||||
path.dirname(config.generatedRouteTree),
|
||||
path.resolve(config.folderName, node.filePath),
|
||||
),
|
||||
false,
|
||||
),
|
||||
)}'`
|
||||
}),
|
||||
].join('\n'),
|
||||
imports,
|
||||
'const rootRoute = createRoute({ isRoot: true, component: RootImport });',
|
||||
createRoutes,
|
||||
'// Create/Update Routes',
|
||||
sortedRouteNodes
|
||||
.map((node) => {
|
||||
@@ -269,7 +280,7 @@ export async function routeGenerator(config = defaultConfig): Promise<void> {
|
||||
const lazyComponentNode = routePiecesByPath[node.routePath]?.lazy
|
||||
|
||||
return [
|
||||
`const ${node.variableName}Route = ${node.variableName}Import.update({
|
||||
`const ${node.variableName}Route = ${node.variableName}.update({
|
||||
${[
|
||||
`path: '${node.cleanedPath}'`,
|
||||
`getParentRoute: () => ${node.parent?.variableName ?? 'root'}Route`,
|
||||
@@ -302,25 +313,6 @@ export async function routeGenerator(config = defaultConfig): Promise<void> {
|
||||
].join('')
|
||||
})
|
||||
.join('\n\n'),
|
||||
...[
|
||||
'// Populate the FileRoutesByPath interface',
|
||||
`declare module '@tanstack/react-router' {
|
||||
interface FileRoutesByPath {
|
||||
${routeNodes
|
||||
.map((routeNode) => {
|
||||
return `'${removeTrailingUnderscores(routeNode.routePath)}': {
|
||||
preLoaderRoute: typeof ${routeNode.variableName}Import
|
||||
parentRoute: typeof ${
|
||||
routeNode.parent?.variableName
|
||||
? `${routeNode.parent.variableName}Import`
|
||||
: 'rootRoute'
|
||||
}
|
||||
}`
|
||||
})
|
||||
.join('\n')}
|
||||
}
|
||||
}`,
|
||||
],
|
||||
'// Create and export the route tree',
|
||||
`export const routeTree = rootRoute.addChildren([${routeConfigChildrenText}])`,
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user