@buzola/codegen 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/__tests__/codegen.test.d.ts +2 -0
- package/dist/__tests__/codegen.test.d.ts.map +1 -0
- package/dist/__tests__/codegen.test.js +208 -0
- package/dist/__tests__/codegen.test.js.map +1 -0
- package/dist/__tests__/conventions.test.d.ts +2 -0
- package/dist/__tests__/conventions.test.d.ts.map +1 -0
- package/dist/__tests__/conventions.test.js +80 -0
- package/dist/__tests__/conventions.test.js.map +1 -0
- package/dist/__tests__/page-extractor.test.d.ts +2 -0
- package/dist/__tests__/page-extractor.test.d.ts.map +1 -0
- package/dist/__tests__/page-extractor.test.js +82 -0
- package/dist/__tests__/page-extractor.test.js.map +1 -0
- package/dist/__tests__/resolve.test.d.ts +2 -0
- package/dist/__tests__/resolve.test.d.ts.map +1 -0
- package/dist/__tests__/resolve.test.js +110 -0
- package/dist/__tests__/resolve.test.js.map +1 -0
- package/dist/__tests__/tree-builder.test.d.ts +2 -0
- package/dist/__tests__/tree-builder.test.d.ts.map +1 -0
- package/dist/__tests__/tree-builder.test.js +474 -0
- package/dist/__tests__/tree-builder.test.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +40 -0
- package/dist/cli.js.map +1 -0
- package/dist/config.d.ts +20 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +20 -0
- package/dist/config.js.map +1 -0
- package/dist/conventions.d.ts +53 -0
- package/dist/conventions.d.ts.map +1 -0
- package/dist/conventions.js +82 -0
- package/dist/conventions.js.map +1 -0
- package/dist/generate.d.ts +28 -0
- package/dist/generate.d.ts.map +1 -0
- package/dist/generate.js +46 -0
- package/dist/generate.js.map +1 -0
- package/dist/generator/codegen.d.ts +19 -0
- package/dist/generator/codegen.d.ts.map +1 -0
- package/dist/generator/codegen.js +162 -0
- package/dist/generator/codegen.js.map +1 -0
- package/dist/generator/page-extractor.d.ts +21 -0
- package/dist/generator/page-extractor.d.ts.map +1 -0
- package/dist/generator/page-extractor.js +23 -0
- package/dist/generator/page-extractor.js.map +1 -0
- package/dist/generator/scanner.d.ts +11 -0
- package/dist/generator/scanner.d.ts.map +1 -0
- package/dist/generator/scanner.js +34 -0
- package/dist/generator/scanner.js.map +1 -0
- package/dist/generator/tree-builder.d.ts +54 -0
- package/dist/generator/tree-builder.d.ts.map +1 -0
- package/dist/generator/tree-builder.js +355 -0
- package/dist/generator/tree-builder.js.map +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/resolve.d.ts +49 -0
- package/dist/resolve.d.ts.map +1 -0
- package/dist/resolve.js +41 -0
- package/dist/resolve.js.map +1 -0
- package/package.json +34 -0
- package/src/__tests__/codegen.test.ts +229 -0
- package/src/__tests__/conventions.test.ts +93 -0
- package/src/__tests__/page-extractor.test.ts +95 -0
- package/src/__tests__/resolve.test.ts +120 -0
- package/src/__tests__/tree-builder.test.ts +554 -0
- package/src/cli.ts +43 -0
- package/src/config.ts +35 -0
- package/src/conventions.ts +116 -0
- package/src/generate.ts +67 -0
- package/src/generator/codegen.ts +213 -0
- package/src/generator/page-extractor.ts +48 -0
- package/src/generator/scanner.ts +45 -0
- package/src/generator/tree-builder.ts +449 -0
- package/src/index.ts +23 -0
- package/src/resolve.ts +75 -0
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import * as path from 'node:path'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* File naming conventions for page-centric routing.
|
|
5
|
+
*
|
|
6
|
+
* _layout.tsx → Layout wrapper (renders children via <Outlet />)
|
|
7
|
+
* _404.tsx → Not-found catch-all (auto-assigned catch-all pattern within parent scope)
|
|
8
|
+
* index.tsx → Index route (renders at parent's exact path)
|
|
9
|
+
* about.tsx → Named page file (page ID derived from file name)
|
|
10
|
+
* (group)/ → Pathless group (no URL segment)
|
|
11
|
+
*
|
|
12
|
+
* Files prefixed with `_` are special convention files, not regular pages.
|
|
13
|
+
* Route patterns (URL paths) come exclusively from `.route()` on `createPage()`.
|
|
14
|
+
* File names only determine page IDs and layout nesting.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
export const ROUTE_FILE_EXTENSIONS = ['.tsx', '.jsx', '.ts', '.js']
|
|
18
|
+
|
|
19
|
+
export interface ParsedRouteFile {
|
|
20
|
+
/** The route segment this file contributes. */
|
|
21
|
+
segment: string
|
|
22
|
+
/** Whether this is a layout file. */
|
|
23
|
+
isLayout: boolean
|
|
24
|
+
/** Whether this is an index file. */
|
|
25
|
+
isIndex: boolean
|
|
26
|
+
/** Whether this is a not-found catch-all file. */
|
|
27
|
+
isNotFound: boolean
|
|
28
|
+
/** Whether this directory is a pathless group. */
|
|
29
|
+
isPathlessGroup: boolean
|
|
30
|
+
/** Original file path relative to routes dir. */
|
|
31
|
+
relativePath: string
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Check if a file is a route file based on extension.
|
|
36
|
+
*/
|
|
37
|
+
export function isRouteFile(filePath: string): boolean {
|
|
38
|
+
return ROUTE_FILE_EXTENSIONS.some(ext => filePath.endsWith(ext))
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Parse a directory name for routing conventions.
|
|
43
|
+
*/
|
|
44
|
+
export function parseDirName(name: string): { segment: string; isPathlessGroup: boolean } {
|
|
45
|
+
// Pathless group: (auth)
|
|
46
|
+
if (name.startsWith('(') && name.endsWith(')')) {
|
|
47
|
+
return { segment: '', isPathlessGroup: true }
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return { segment: name, isPathlessGroup: false }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Parse a route file name (without extension) into routing info.
|
|
55
|
+
*/
|
|
56
|
+
export function parseFileName(nameWithoutExt: string): {
|
|
57
|
+
segment: string
|
|
58
|
+
isLayout: boolean
|
|
59
|
+
isIndex: boolean
|
|
60
|
+
isNotFound: boolean
|
|
61
|
+
} {
|
|
62
|
+
if (nameWithoutExt === '_layout') {
|
|
63
|
+
return { segment: '', isLayout: true, isIndex: false, isNotFound: false }
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (nameWithoutExt === '_404') {
|
|
67
|
+
return { segment: '', isLayout: false, isIndex: false, isNotFound: true }
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (nameWithoutExt === 'index') {
|
|
71
|
+
return { segment: '', isLayout: false, isIndex: true, isNotFound: false }
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return { segment: nameWithoutExt, isLayout: false, isIndex: false, isNotFound: false }
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Parse a route file path relative to the routes directory.
|
|
79
|
+
*/
|
|
80
|
+
export function parseRouteFile(relativePath: string): ParsedRouteFile {
|
|
81
|
+
const ext = path.extname(relativePath)
|
|
82
|
+
const withoutExt = relativePath.slice(0, -ext.length)
|
|
83
|
+
const parts = withoutExt.split(path.sep)
|
|
84
|
+
const fileName = parts.pop()!
|
|
85
|
+
const dirs = parts
|
|
86
|
+
|
|
87
|
+
const fileInfo = parseFileName(fileName)
|
|
88
|
+
|
|
89
|
+
// Build segment from directory path + file
|
|
90
|
+
const segments: string[] = []
|
|
91
|
+
let isPathlessGroup = false
|
|
92
|
+
|
|
93
|
+
for (const dir of dirs) {
|
|
94
|
+
const dirInfo = parseDirName(dir)
|
|
95
|
+
if (dirInfo.isPathlessGroup) {
|
|
96
|
+
isPathlessGroup = true
|
|
97
|
+
} else {
|
|
98
|
+
segments.push(dirInfo.segment)
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (fileInfo.segment) {
|
|
103
|
+
segments.push(fileInfo.segment)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const segment = segments.length > 0 ? segments.join('/') : ''
|
|
107
|
+
|
|
108
|
+
return {
|
|
109
|
+
segment,
|
|
110
|
+
isLayout: fileInfo.isLayout,
|
|
111
|
+
isIndex: fileInfo.isIndex,
|
|
112
|
+
isNotFound: fileInfo.isNotFound,
|
|
113
|
+
isPathlessGroup,
|
|
114
|
+
relativePath,
|
|
115
|
+
}
|
|
116
|
+
}
|
package/src/generate.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import * as fs from 'node:fs'
|
|
2
|
+
import { generateRouteModule } from './generator/codegen.js'
|
|
3
|
+
import type { ModuleLoader } from './generator/page-extractor.js'
|
|
4
|
+
import { scanRouteFiles } from './generator/scanner.js'
|
|
5
|
+
import { buildFileRouteTree } from './generator/tree-builder.js'
|
|
6
|
+
|
|
7
|
+
export interface GenerateOptions {
|
|
8
|
+
/** Absolute path to the routes directory. */
|
|
9
|
+
routesDir: string
|
|
10
|
+
/** Absolute path to the output file. */
|
|
11
|
+
outputPath: string
|
|
12
|
+
/** Parameter names that should be persistent across navigations. */
|
|
13
|
+
persistentParams?: string[]
|
|
14
|
+
/** Custom module loader. Defaults to native `import()`. */
|
|
15
|
+
moduleLoader?: ModuleLoader
|
|
16
|
+
/** When true, return `false` instead of throwing if `routesDir` is missing. */
|
|
17
|
+
optional?: boolean
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Generate the `buzola.gen.ts` route module from a routes directory.
|
|
22
|
+
*
|
|
23
|
+
* Works standalone (no Vite required) — uses native `import()` as the
|
|
24
|
+
* default module loader. Pass a custom `moduleLoader` to integrate with
|
|
25
|
+
* Vite's `ssrLoadModule` or other bundlers.
|
|
26
|
+
*
|
|
27
|
+
* Note: The default module loader uses native `import()`, which can load
|
|
28
|
+
* `.ts`/`.tsx` files with Bun but not with Node.js. For Node.js, provide
|
|
29
|
+
* a custom `moduleLoader` (e.g. via Vite's `ssrLoadModule`).
|
|
30
|
+
*
|
|
31
|
+
* @returns `true` if the output file was written (content changed), `false` otherwise.
|
|
32
|
+
*/
|
|
33
|
+
export async function generate(options: GenerateOptions): Promise<boolean> {
|
|
34
|
+
const { routesDir, outputPath, persistentParams, moduleLoader = defaultModuleLoader, optional = false } = options
|
|
35
|
+
|
|
36
|
+
if (!fs.existsSync(routesDir)) {
|
|
37
|
+
if (optional) return false
|
|
38
|
+
throw new Error(`Routes directory does not exist: ${routesDir}`)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const files = scanRouteFiles(routesDir)
|
|
42
|
+
const tree = await buildFileRouteTree(files, moduleLoader)
|
|
43
|
+
const code = generateRouteModule({ tree, routesDir, outputPath, persistentParams })
|
|
44
|
+
|
|
45
|
+
return writeIfChanged(outputPath, code)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async function defaultModuleLoader(absolutePath: string): Promise<Record<string, unknown>> {
|
|
49
|
+
try {
|
|
50
|
+
return await import(absolutePath)
|
|
51
|
+
} catch (error) {
|
|
52
|
+
throw new Error(
|
|
53
|
+
`Failed to load module "${absolutePath}". The default module loader uses native import() which `
|
|
54
|
+
+ `supports .ts/.tsx files with Bun but not Node.js. For Node.js, provide a custom moduleLoader option.`,
|
|
55
|
+
{ cause: error },
|
|
56
|
+
)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function writeIfChanged(filePath: string, content: string): boolean {
|
|
61
|
+
if (fs.existsSync(filePath) && fs.readFileSync(filePath, 'utf-8') === content) {
|
|
62
|
+
return false
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
fs.writeFileSync(filePath, content, 'utf-8')
|
|
66
|
+
return true
|
|
67
|
+
}
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import * as path from 'node:path'
|
|
2
|
+
import type { FileRouteNode } from './tree-builder.js'
|
|
3
|
+
|
|
4
|
+
export interface CodegenOptions {
|
|
5
|
+
/** The route tree from file scanning. */
|
|
6
|
+
tree: FileRouteNode[]
|
|
7
|
+
/** Absolute path to the routes directory. */
|
|
8
|
+
routesDir: string
|
|
9
|
+
/** Absolute path to the output file. */
|
|
10
|
+
outputPath: string
|
|
11
|
+
/** Parameter names that should be persistent across navigations. */
|
|
12
|
+
persistentParams?: string[]
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Quote a property key when it isn't a valid JS identifier (e.g. hyphenated).
|
|
17
|
+
*/
|
|
18
|
+
function formatPropertyKey(name: string): string {
|
|
19
|
+
return /^[A-Za-z_$][\w$]*$/.test(name) ? name : `'${name}'`
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Generate the buzola.gen.ts file with:
|
|
24
|
+
* 1. Module augmentation for BuzolaPageMap (type safety)
|
|
25
|
+
* 2. Page registry (page ID → route pattern)
|
|
26
|
+
* 3. Route tree construction code
|
|
27
|
+
*/
|
|
28
|
+
export function generateRouteModule(options: CodegenOptions): string {
|
|
29
|
+
const { tree, outputPath } = options
|
|
30
|
+
const outputDir = path.dirname(outputPath)
|
|
31
|
+
let importCounter = 0
|
|
32
|
+
|
|
33
|
+
const lines: string[] = []
|
|
34
|
+
|
|
35
|
+
lines.push('// This file is auto-generated by @buzola/codegen. Do not edit.')
|
|
36
|
+
lines.push('/* eslint-disable */')
|
|
37
|
+
lines.push('')
|
|
38
|
+
|
|
39
|
+
lines.push(`import { lazy, type ComponentType } from 'react';`)
|
|
40
|
+
lines.push(`import { buildRouteTree } from '@buzola/router';`)
|
|
41
|
+
lines.push(`import type { RouteConfig } from '@buzola/router';`)
|
|
42
|
+
lines.push('')
|
|
43
|
+
|
|
44
|
+
const imports: { varName: string; filePath: string; isLazy: boolean; exportName: string }[] = []
|
|
45
|
+
const importsByKey = new Map<string, { varName: string; isLazy: boolean }>()
|
|
46
|
+
collectImports(tree, outputDir, imports, importsByKey, () => importCounter++)
|
|
47
|
+
|
|
48
|
+
for (const imp of imports) {
|
|
49
|
+
if (imp.isLazy) {
|
|
50
|
+
lines.push(
|
|
51
|
+
`const ${imp.varName} = () => import('${imp.filePath}').then(m => ({ default: m.${imp.exportName}.component })) as Promise<{ default: ComponentType }>;`,
|
|
52
|
+
)
|
|
53
|
+
} else {
|
|
54
|
+
if (imp.exportName !== 'default') {
|
|
55
|
+
lines.push(`import { ${imp.exportName} as ${imp.varName} } from '${imp.filePath}';`)
|
|
56
|
+
} else {
|
|
57
|
+
lines.push(`import ${imp.varName} from '${imp.filePath}';`)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
lines.push('')
|
|
63
|
+
|
|
64
|
+
const pages = collectPages(tree)
|
|
65
|
+
|
|
66
|
+
lines.push('// Module augmentation for type-safe page-centric routing')
|
|
67
|
+
lines.push(`declare module '@buzola/router' {`)
|
|
68
|
+
lines.push(' interface BuzolaPageMap {')
|
|
69
|
+
for (const page of pages) {
|
|
70
|
+
if (page.params.length === 0) {
|
|
71
|
+
lines.push(` '${page.pageId}': {};`)
|
|
72
|
+
} else {
|
|
73
|
+
const paramEntries = page.params.map(p => {
|
|
74
|
+
const type = p.array ? 'string[]' : 'string'
|
|
75
|
+
const key = formatPropertyKey(p.name)
|
|
76
|
+
return p.optional ? `${key}?: ${type}` : `${key}: ${type}`
|
|
77
|
+
}).join('; ')
|
|
78
|
+
lines.push(` '${page.pageId}': { ${paramEntries} };`)
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
lines.push(' }')
|
|
82
|
+
if (options.persistentParams && options.persistentParams.length > 0) {
|
|
83
|
+
lines.push(' interface BuzolaPersistentParams {')
|
|
84
|
+
for (const param of options.persistentParams) {
|
|
85
|
+
lines.push(` ${formatPropertyKey(param)}: true;`)
|
|
86
|
+
}
|
|
87
|
+
lines.push(' }')
|
|
88
|
+
}
|
|
89
|
+
lines.push('}')
|
|
90
|
+
lines.push('')
|
|
91
|
+
|
|
92
|
+
lines.push('// Page registry — page ID → route pattern')
|
|
93
|
+
lines.push('export const pageRegistry: Record<string, string> = {')
|
|
94
|
+
for (const page of pages) {
|
|
95
|
+
lines.push(` '${page.pageId}': '${page.routePattern}',`)
|
|
96
|
+
}
|
|
97
|
+
lines.push('};')
|
|
98
|
+
lines.push('')
|
|
99
|
+
|
|
100
|
+
lines.push('const routeConfigs: RouteConfig[] = [')
|
|
101
|
+
generateTreeConfig(tree, lines, importsByKey, 1)
|
|
102
|
+
lines.push('];')
|
|
103
|
+
lines.push('')
|
|
104
|
+
lines.push('export const routes = buildRouteTree(routeConfigs);')
|
|
105
|
+
lines.push('')
|
|
106
|
+
|
|
107
|
+
return lines.join('\n')
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
interface PageInfo {
|
|
111
|
+
pageId: string
|
|
112
|
+
routePattern: string
|
|
113
|
+
params: { name: string; optional: boolean; array: boolean }[]
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Collect page info from the file route tree for codegen.
|
|
118
|
+
*/
|
|
119
|
+
function collectPages(nodes: FileRouteNode[]): PageInfo[] {
|
|
120
|
+
const result: PageInfo[] = []
|
|
121
|
+
|
|
122
|
+
for (const node of nodes) {
|
|
123
|
+
if (node.filePath && !node.isLayout && !node.isNotFound) {
|
|
124
|
+
if (node.pageExports) {
|
|
125
|
+
for (const exp of node.pageExports) {
|
|
126
|
+
result.push({
|
|
127
|
+
pageId: exp.pageId,
|
|
128
|
+
routePattern: exp.routePattern,
|
|
129
|
+
params: exp.params,
|
|
130
|
+
})
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (node.children.length > 0) {
|
|
136
|
+
result.push(...collectPages(node.children))
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return result
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function importKey(filePath: string, exportName?: string): string {
|
|
144
|
+
return `${filePath}::${exportName ?? 'default'}`
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function collectImports(
|
|
148
|
+
nodes: FileRouteNode[],
|
|
149
|
+
outputDir: string,
|
|
150
|
+
imports: { varName: string; filePath: string; isLazy: boolean; exportName: string }[],
|
|
151
|
+
importsByKey: Map<string, { varName: string; isLazy: boolean }>,
|
|
152
|
+
nextId: () => number,
|
|
153
|
+
): void {
|
|
154
|
+
for (const node of nodes) {
|
|
155
|
+
if (node.filePath) {
|
|
156
|
+
const key = importKey(node.filePath, node.exportName)
|
|
157
|
+
if (!importsByKey.has(key)) {
|
|
158
|
+
const relativePath = path.relative(outputDir, node.filePath).replaceAll('\\', '/')
|
|
159
|
+
const importPath = relativePath.startsWith('.') ? relativePath : `./${relativePath}`
|
|
160
|
+
const cleanPath = importPath.replace(/\.(tsx?|jsx?)$/, '')
|
|
161
|
+
const varName = `Route${nextId()}`
|
|
162
|
+
const isLazy = !node.isLayout
|
|
163
|
+
const exportName = node.exportName ?? 'default'
|
|
164
|
+
imports.push({ varName, filePath: cleanPath, isLazy, exportName })
|
|
165
|
+
importsByKey.set(key, { varName, isLazy })
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
collectImports(node.children, outputDir, imports, importsByKey, nextId)
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function generateTreeConfig(
|
|
174
|
+
nodes: FileRouteNode[],
|
|
175
|
+
lines: string[],
|
|
176
|
+
importsByKey: Map<string, { varName: string; isLazy: boolean }>,
|
|
177
|
+
indent: number,
|
|
178
|
+
): void {
|
|
179
|
+
const pad = ' '.repeat(indent)
|
|
180
|
+
|
|
181
|
+
for (const node of nodes) {
|
|
182
|
+
const imp = node.filePath ? importsByKey.get(importKey(node.filePath, node.exportName)) : undefined
|
|
183
|
+
|
|
184
|
+
lines.push(`${pad}{`)
|
|
185
|
+
lines.push(`${pad} path: '${node.segment ? `/${node.segment}` : '/'}',`)
|
|
186
|
+
|
|
187
|
+
// Emit matchPath when page has a custom route pattern that differs from file-hierarchy position
|
|
188
|
+
if (node.pageExports && node.pageExports.length > 0 && node.pageExports[0].routePattern !== node.fullPath) {
|
|
189
|
+
lines.push(`${pad} matchPath: '${node.pageExports[0].routePattern}',`)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (imp) {
|
|
193
|
+
if (imp.isLazy) {
|
|
194
|
+
lines.push(`${pad} component: lazy(${imp.varName}),`)
|
|
195
|
+
lines.push(`${pad} preload: ${imp.varName},`)
|
|
196
|
+
} else {
|
|
197
|
+
lines.push(`${pad} component: ${imp.varName},`)
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (node.isLayout) lines.push(`${pad} isLayout: true,`)
|
|
202
|
+
if (node.isIndex) lines.push(`${pad} isIndex: true,`)
|
|
203
|
+
if (node.isPathlessGroup) lines.push(`${pad} isPathlessGroup: true,`)
|
|
204
|
+
|
|
205
|
+
if (node.children.length > 0) {
|
|
206
|
+
lines.push(`${pad} children: [`)
|
|
207
|
+
generateTreeConfig(node.children, lines, importsByKey, indent + 2)
|
|
208
|
+
lines.push(`${pad} ],`)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
lines.push(`${pad}},`)
|
|
212
|
+
}
|
|
213
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
export interface ExtractedPage {
|
|
2
|
+
/** Export name (e.g., "detail", "list"). "default" for default exports. */
|
|
3
|
+
exportName: string
|
|
4
|
+
/** Explicit route pattern from .route('...'), if present. */
|
|
5
|
+
route?: string
|
|
6
|
+
/** Whether this is a default export. */
|
|
7
|
+
isDefault: boolean
|
|
8
|
+
/** Extracted param info from .params(z.object({...})). */
|
|
9
|
+
params: ExtractedParam[]
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface ExtractedParam {
|
|
13
|
+
name: string
|
|
14
|
+
optional: boolean
|
|
15
|
+
array: boolean
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export type ModuleLoader = (absolutePath: string) => Promise<Record<string, unknown>>
|
|
19
|
+
|
|
20
|
+
function isPageDefinition(value: unknown): value is {
|
|
21
|
+
__buzolaPage: true
|
|
22
|
+
route?: string
|
|
23
|
+
paramsMeta: { name: string; optional: boolean; array: boolean }[]
|
|
24
|
+
} {
|
|
25
|
+
return value != null && typeof value === 'object' && '__buzolaPage' in value && (value as any).__buzolaPage === true
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Extract createPage() metadata by loading the module and inspecting exports.
|
|
30
|
+
*/
|
|
31
|
+
export async function extractPages(absolutePath: string, loadModule: ModuleLoader): Promise<ExtractedPage[]> {
|
|
32
|
+
const mod = await loadModule(absolutePath)
|
|
33
|
+
const pages: ExtractedPage[] = []
|
|
34
|
+
|
|
35
|
+
for (const [key, value] of Object.entries(mod)) {
|
|
36
|
+
if (!isPageDefinition(value)) continue
|
|
37
|
+
|
|
38
|
+
const exportName = key === 'default' ? 'default' : key
|
|
39
|
+
pages.push({
|
|
40
|
+
exportName,
|
|
41
|
+
isDefault: key === 'default',
|
|
42
|
+
route: value.route,
|
|
43
|
+
params: value.paramsMeta ?? [],
|
|
44
|
+
})
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return pages
|
|
48
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import * as fs from 'node:fs'
|
|
2
|
+
import * as path from 'node:path'
|
|
3
|
+
import { isRouteFile } from '../conventions.js'
|
|
4
|
+
|
|
5
|
+
export interface ScannedFile {
|
|
6
|
+
/** Absolute path to the file. */
|
|
7
|
+
absolutePath: string
|
|
8
|
+
/** Path relative to the routes directory. */
|
|
9
|
+
relativePath: string
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Scan a directory recursively for route files.
|
|
14
|
+
*/
|
|
15
|
+
export function scanRouteFiles(routesDir: string): ScannedFile[] {
|
|
16
|
+
const files: ScannedFile[] = []
|
|
17
|
+
|
|
18
|
+
function scan(dir: string): void {
|
|
19
|
+
if (!fs.existsSync(dir)) return
|
|
20
|
+
|
|
21
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true })
|
|
22
|
+
|
|
23
|
+
for (const entry of entries) {
|
|
24
|
+
const fullPath = path.join(dir, entry.name)
|
|
25
|
+
|
|
26
|
+
if (entry.isDirectory()) {
|
|
27
|
+
// Skip node_modules and hidden directories
|
|
28
|
+
if (entry.name.startsWith('.') || entry.name === 'node_modules') continue
|
|
29
|
+
scan(fullPath)
|
|
30
|
+
} else if (entry.isFile() && isRouteFile(entry.name)) {
|
|
31
|
+
files.push({
|
|
32
|
+
absolutePath: fullPath,
|
|
33
|
+
relativePath: path.relative(routesDir, fullPath),
|
|
34
|
+
})
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
scan(routesDir)
|
|
40
|
+
|
|
41
|
+
// Sort for deterministic output
|
|
42
|
+
files.sort((a, b) => a.relativePath.localeCompare(b.relativePath))
|
|
43
|
+
|
|
44
|
+
return files
|
|
45
|
+
}
|