@tanstack/router-generator
Advanced tools
Comparing version 1.85.3 to 1.86.0
import { z } from 'zod'; | ||
export declare const configSchema: z.ZodObject<{ | ||
virtualRouteConfig: z.ZodOptional<z.ZodType<import('@tanstack/virtual-file-routes').VirtualRootRoute, z.ZodTypeDef, import('@tanstack/virtual-file-routes').VirtualRootRoute>>; | ||
virtualRouteConfig: z.ZodOptional<z.ZodUnion<[z.ZodType<import('@tanstack/virtual-file-routes').VirtualRootRoute, z.ZodTypeDef, import('@tanstack/virtual-file-routes').VirtualRootRoute>, z.ZodString]>>; | ||
routeFilePrefix: z.ZodOptional<z.ZodString>; | ||
@@ -58,3 +58,3 @@ routeFileIgnorePrefix: z.ZodDefault<z.ZodOptional<z.ZodString>>; | ||
}; | ||
virtualRouteConfig?: import('@tanstack/virtual-file-routes').VirtualRootRoute | undefined; | ||
virtualRouteConfig?: string | import('@tanstack/virtual-file-routes').VirtualRootRoute | undefined; | ||
routeFilePrefix?: string | undefined; | ||
@@ -68,3 +68,3 @@ routeFileIgnorePattern?: string | undefined; | ||
}, { | ||
virtualRouteConfig?: import('@tanstack/virtual-file-routes').VirtualRootRoute | undefined; | ||
virtualRouteConfig?: string | import('@tanstack/virtual-file-routes').VirtualRootRoute | undefined; | ||
routeFilePrefix?: string | undefined; | ||
@@ -71,0 +71,0 @@ routeFileIgnorePrefix?: string | undefined; |
@@ -21,3 +21,3 @@ import path from "node:path"; | ||
const configSchema = z.object({ | ||
virtualRouteConfig: virtualRootRouteSchema.optional(), | ||
virtualRouteConfig: virtualRootRouteSchema.or(z.string()).optional(), | ||
routeFilePrefix: z.string().optional(), | ||
@@ -24,0 +24,0 @@ routeFileIgnorePrefix: z.string().optional().default("-"), |
import { GetRouteNodesResult } from '../../types'; | ||
import { Config } from '../../config'; | ||
export declare function getRouteNodes(config: Config): Promise<GetRouteNodesResult>; | ||
export declare function getRouteNodes(config: Config, root: string): Promise<GetRouteNodesResult>; |
@@ -8,3 +8,3 @@ import path from "node:path"; | ||
const disallowedRouteGroupConfiguration = /\(([^)]+)\).(ts|js|tsx|jsx)/; | ||
async function getRouteNodes(config) { | ||
async function getRouteNodes(config, root) { | ||
const { routeFilePrefix, routeFileIgnorePrefix, routeFileIgnorePattern } = config; | ||
@@ -47,7 +47,10 @@ const logger = logging({ disabled: config.disableLogging }); | ||
}; | ||
const { routeNodes: virtualRouteNodes } = await getRouteNodes$1({ | ||
...config, | ||
routesDirectory: fullDir, | ||
virtualRouteConfig: dummyRoot | ||
}); | ||
const { routeNodes: virtualRouteNodes } = await getRouteNodes$1( | ||
{ | ||
...config, | ||
routesDirectory: fullDir, | ||
virtualRouteConfig: dummyRoot | ||
}, | ||
root | ||
); | ||
virtualRouteNodes.forEach((node) => { | ||
@@ -54,0 +57,0 @@ const filePath = replaceBackslash(path.join(dir, node.filePath)); |
import { VirtualRouteNode } from '@tanstack/virtual-file-routes'; | ||
import { GetRouteNodesResult, RouteNode } from '../../types'; | ||
import { Config } from '../../config'; | ||
export declare function getRouteNodes(tsrConfig: Config): Promise<GetRouteNodesResult>; | ||
export declare function getRouteNodesRecursive(tsrConfig: Config, fullDir: string, nodes?: Array<VirtualRouteNode>, parent?: RouteNode): Promise<Array<RouteNode>>; | ||
export declare function getRouteNodes(tsrConfig: Config, root: string): Promise<GetRouteNodesResult>; | ||
export declare function getRouteNodesRecursive(tsrConfig: Config, root: string, fullDir: string, nodes?: Array<VirtualRouteNode>, parent?: RouteNode): Promise<Array<RouteNode>>; |
@@ -0,4 +1,6 @@ | ||
import "tsx/esm"; | ||
import path, { resolve, join } from "node:path"; | ||
import { routePathToVariable, removeExt, removeTrailingSlash, removeLeadingSlash } from "../../utils.js"; | ||
import { getRouteNodes as getRouteNodes$1 } from "../physical/getRouteNodes.js"; | ||
import { virtualRootRouteSchema } from "./config.js"; | ||
function ensureLeadingUnderScore(id) { | ||
@@ -20,3 +22,3 @@ if (id.startsWith("_")) { | ||
} | ||
async function getRouteNodes(tsrConfig) { | ||
async function getRouteNodes(tsrConfig, root) { | ||
const fullDir = resolve(tsrConfig.routesDirectory); | ||
@@ -26,11 +28,22 @@ if (tsrConfig.virtualRouteConfig === void 0) { | ||
} | ||
const children = await getRouteNodesRecursive( | ||
let virtualRouteConfig; | ||
let children = []; | ||
if (typeof tsrConfig.virtualRouteConfig === "string") { | ||
virtualRouteConfig = await getVirtualRouteConfigFromFileExport( | ||
tsrConfig, | ||
root | ||
); | ||
} else { | ||
virtualRouteConfig = tsrConfig.virtualRouteConfig; | ||
} | ||
children = await getRouteNodesRecursive( | ||
tsrConfig, | ||
root, | ||
fullDir, | ||
tsrConfig.virtualRouteConfig.children | ||
virtualRouteConfig.children | ||
); | ||
const allNodes = flattenTree({ | ||
children, | ||
filePath: tsrConfig.virtualRouteConfig.file, | ||
fullPath: join(fullDir, tsrConfig.virtualRouteConfig.file), | ||
filePath: virtualRouteConfig.file, | ||
fullPath: join(fullDir, virtualRouteConfig.file), | ||
variableName: "rootRoute", | ||
@@ -44,3 +57,16 @@ routePath: "/", | ||
} | ||
async function getRouteNodesRecursive(tsrConfig, fullDir, nodes, parent) { | ||
async function getVirtualRouteConfigFromFileExport(tsrConfig, root) { | ||
if (tsrConfig.virtualRouteConfig === void 0 || typeof tsrConfig.virtualRouteConfig !== "string" || tsrConfig.virtualRouteConfig === "") { | ||
throw new Error(`virtualRouteConfig is undefined or empty`); | ||
} | ||
const exports = await import(join(root, tsrConfig.virtualRouteConfig)); | ||
if (!("routes" in exports) && !("default" in exports)) { | ||
throw new Error( | ||
`routes not found in ${tsrConfig.virtualRouteConfig}. The routes export must be named like 'export const routes = ...' or done using 'export default ...'` | ||
); | ||
} | ||
const virtualRouteConfig = "default" in exports ? exports.default : exports.routes; | ||
return virtualRootRouteSchema.parse(virtualRouteConfig); | ||
} | ||
async function getRouteNodesRecursive(tsrConfig, root, fullDir, nodes, parent) { | ||
if (nodes === void 0) { | ||
@@ -52,6 +78,9 @@ return []; | ||
if (node.type === "physical") { | ||
const { routeNodes } = await getRouteNodes$1({ | ||
...tsrConfig, | ||
routesDirectory: resolve(fullDir, node.directory) | ||
}); | ||
const { routeNodes } = await getRouteNodes$1( | ||
{ | ||
...tsrConfig, | ||
routesDirectory: resolve(fullDir, node.directory) | ||
}, | ||
root | ||
); | ||
routeNodes.forEach((subtreeNode) => { | ||
@@ -112,2 +141,3 @@ subtreeNode.variableName = routePathToVariable( | ||
tsrConfig, | ||
root, | ||
fullDir, | ||
@@ -142,2 +172,3 @@ node.children, | ||
tsrConfig, | ||
root, | ||
fullDir, | ||
@@ -144,0 +175,0 @@ node.children, |
@@ -6,3 +6,3 @@ import { RouteNode } from './types.js'; | ||
}; | ||
export declare function generator(config: Config): Promise<void>; | ||
export declare function generator(config: Config, root: string): Promise<void>; | ||
/** | ||
@@ -9,0 +9,0 @@ * Removes the last segment from a given path. Segments are considered to be separated by a '/'. |
@@ -18,3 +18,3 @@ import path from "node:path"; | ||
let skipMessage = false; | ||
async function generator(config) { | ||
async function generator(config, root) { | ||
const logger = logging({ disabled: config.disableLogging }); | ||
@@ -48,5 +48,5 @@ logger.log(""); | ||
if (config.virtualRouteConfig) { | ||
getRouteNodesResult = await getRouteNodes(config); | ||
getRouteNodesResult = await getRouteNodes(config, root); | ||
} else { | ||
getRouteNodesResult = await getRouteNodes$1(config); | ||
getRouteNodesResult = await getRouteNodes$1(config, root); | ||
} | ||
@@ -53,0 +53,0 @@ const { rootRouteNode, routeNodes: beforeRouteNodes } = getRouteNodesResult; |
{ | ||
"name": "@tanstack/router-generator", | ||
"version": "1.85.3", | ||
"version": "1.86.0", | ||
"description": "Modern and scalable routing for React applications", | ||
@@ -5,0 +5,0 @@ "author": "Tanner Linsley", |
@@ -22,3 +22,3 @@ import path from 'node:path' | ||
export const configSchema = z.object({ | ||
virtualRouteConfig: virtualRootRouteSchema.optional(), | ||
virtualRouteConfig: virtualRootRouteSchema.or(z.string()).optional(), | ||
routeFilePrefix: z.string().optional(), | ||
@@ -25,0 +25,0 @@ routeFileIgnorePrefix: z.string().optional().default('-'), |
@@ -25,2 +25,3 @@ import path from 'node:path' | ||
config: Config, | ||
root: string, | ||
): Promise<GetRouteNodesResult> { | ||
@@ -76,7 +77,10 @@ const { routeFilePrefix, routeFileIgnorePrefix, routeFileIgnorePattern } = | ||
} | ||
const { routeNodes: virtualRouteNodes } = await getRouteNodesVirtual({ | ||
...config, | ||
routesDirectory: fullDir, | ||
virtualRouteConfig: dummyRoot, | ||
}) | ||
const { routeNodes: virtualRouteNodes } = await getRouteNodesVirtual( | ||
{ | ||
...config, | ||
routesDirectory: fullDir, | ||
virtualRouteConfig: dummyRoot, | ||
}, | ||
root, | ||
) | ||
virtualRouteNodes.forEach((node) => { | ||
@@ -83,0 +87,0 @@ const filePath = replaceBackslash(path.join(dir, node.filePath)) |
@@ -0,1 +1,2 @@ | ||
import 'tsx/esm' | ||
import path, { join, resolve } from 'node:path' | ||
@@ -9,3 +10,7 @@ import { | ||
import { getRouteNodes as getRouteNodesPhysical } from '../physical/getRouteNodes' | ||
import type { VirtualRouteNode } from '@tanstack/virtual-file-routes' | ||
import { virtualRootRouteSchema } from './config' | ||
import type { | ||
VirtualRootRoute, | ||
VirtualRouteNode, | ||
} from '@tanstack/virtual-file-routes' | ||
import type { GetRouteNodesResult, RouteNode } from '../../types' | ||
@@ -36,2 +41,3 @@ import type { Config } from '../../config' | ||
tsrConfig: Config, | ||
root: string, | ||
): Promise<GetRouteNodesResult> { | ||
@@ -42,11 +48,22 @@ const fullDir = resolve(tsrConfig.routesDirectory) | ||
} | ||
const children = await getRouteNodesRecursive( | ||
let virtualRouteConfig: VirtualRootRoute | ||
let children: Array<RouteNode> = [] | ||
if (typeof tsrConfig.virtualRouteConfig === 'string') { | ||
virtualRouteConfig = await getVirtualRouteConfigFromFileExport( | ||
tsrConfig, | ||
root, | ||
) | ||
} else { | ||
virtualRouteConfig = tsrConfig.virtualRouteConfig | ||
} | ||
children = await getRouteNodesRecursive( | ||
tsrConfig, | ||
root, | ||
fullDir, | ||
tsrConfig.virtualRouteConfig.children, | ||
virtualRouteConfig.children, | ||
) | ||
const allNodes = flattenTree({ | ||
children, | ||
filePath: tsrConfig.virtualRouteConfig.file, | ||
fullPath: join(fullDir, tsrConfig.virtualRouteConfig.file), | ||
filePath: virtualRouteConfig.file, | ||
fullPath: join(fullDir, virtualRouteConfig.file), | ||
variableName: 'rootRoute', | ||
@@ -63,4 +80,44 @@ routePath: '/', | ||
/** | ||
* Get the virtual route config from a file export | ||
* | ||
* @example | ||
* ```ts | ||
* // routes.ts | ||
* import { rootRoute } from '@tanstack/virtual-file-routes' | ||
* | ||
* export const routes = rootRoute({ ... }) | ||
* // or | ||
* export default rootRoute({ ... }) | ||
* ``` | ||
* | ||
*/ | ||
async function getVirtualRouteConfigFromFileExport( | ||
tsrConfig: Config, | ||
root: string, | ||
): Promise<VirtualRootRoute> { | ||
if ( | ||
tsrConfig.virtualRouteConfig === undefined || | ||
typeof tsrConfig.virtualRouteConfig !== 'string' || | ||
tsrConfig.virtualRouteConfig === '' | ||
) { | ||
throw new Error(`virtualRouteConfig is undefined or empty`) | ||
} | ||
const exports = await import(join(root, tsrConfig.virtualRouteConfig)) | ||
if (!('routes' in exports) && !('default' in exports)) { | ||
throw new Error( | ||
`routes not found in ${tsrConfig.virtualRouteConfig}. The routes export must be named like 'export const routes = ...' or done using 'export default ...'`, | ||
) | ||
} | ||
const virtualRouteConfig = | ||
'default' in exports ? exports.default : exports.routes | ||
return virtualRootRouteSchema.parse(virtualRouteConfig) | ||
} | ||
export async function getRouteNodesRecursive( | ||
tsrConfig: Config, | ||
root: string, | ||
fullDir: string, | ||
@@ -76,6 +133,9 @@ nodes?: Array<VirtualRouteNode>, | ||
if (node.type === 'physical') { | ||
const { routeNodes } = await getRouteNodesPhysical({ | ||
...tsrConfig, | ||
routesDirectory: resolve(fullDir, node.directory), | ||
}) | ||
const { routeNodes } = await getRouteNodesPhysical( | ||
{ | ||
...tsrConfig, | ||
routesDirectory: resolve(fullDir, node.directory), | ||
}, | ||
root, | ||
) | ||
routeNodes.forEach((subtreeNode) => { | ||
@@ -140,2 +200,3 @@ subtreeNode.variableName = routePathToVariable( | ||
tsrConfig, | ||
root, | ||
fullDir, | ||
@@ -173,2 +234,3 @@ node.children, | ||
tsrConfig, | ||
root, | ||
fullDir, | ||
@@ -175,0 +237,0 @@ node.children, |
@@ -43,3 +43,3 @@ import path from 'node:path' | ||
export async function generator(config: Config) { | ||
export async function generator(config: Config, root: string) { | ||
const logger = logging({ disabled: config.disableLogging }) | ||
@@ -82,5 +82,5 @@ logger.log('') | ||
if (config.virtualRouteConfig) { | ||
getRouteNodesResult = await virtualGetRouteNodes(config) | ||
getRouteNodesResult = await virtualGetRouteNodes(config, root) | ||
} else { | ||
getRouteNodesResult = await physicalGetRouteNodes(config) | ||
getRouteNodesResult = await physicalGetRouteNodes(config, root) | ||
} | ||
@@ -87,0 +87,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
360151
4665