|
1 | 1 | import fs from "node:fs"; |
2 | 2 |
|
| 3 | +import ts from "dedent"; |
3 | 4 | import * as Path from "pathe"; |
4 | 5 | import pc from "picocolors"; |
5 | 6 | import type vite from "vite"; |
6 | 7 |
|
7 | 8 | import { createConfigLoader } from "../config/config"; |
| 9 | +import * as Babel from "../vite/babel"; |
8 | 10 |
|
9 | 11 | import { generate } from "./generate"; |
10 | 12 | import type { Context } from "./context"; |
11 | 13 | import { getTypesDir, getTypesPath } from "./paths"; |
| 14 | +import type { RouteManifest, RouteManifestEntry } from "../config/routes"; |
12 | 15 |
|
13 | 16 | export async function run(rootDirectory: string) { |
14 | 17 | const ctx = await createContext({ rootDirectory, watch: false }); |
@@ -81,4 +84,91 @@ async function writeAll(ctx: Context): Promise<void> { |
81 | 84 | fs.mkdirSync(Path.dirname(typesPath), { recursive: true }); |
82 | 85 | fs.writeFileSync(typesPath, content); |
83 | 86 | }); |
| 87 | + |
| 88 | + const registerPath = Path.join(typegenDir, "+register.ts"); |
| 89 | + fs.writeFileSync(registerPath, register(ctx)); |
| 90 | +} |
| 91 | + |
| 92 | +function register(ctx: Context) { |
| 93 | + const register = ts` |
| 94 | + import "react-router"; |
| 95 | +
|
| 96 | + declare module "react-router" { |
| 97 | + interface Register { |
| 98 | + params: Params; |
| 99 | + } |
| 100 | + } |
| 101 | + `; |
| 102 | + |
| 103 | + const { t } = Babel; |
| 104 | + |
| 105 | + const typeParams = t.tsTypeAliasDeclaration( |
| 106 | + t.identifier("Params"), |
| 107 | + null, |
| 108 | + t.tsTypeLiteral( |
| 109 | + Object.values(ctx.config.routes) |
| 110 | + .map((route) => { |
| 111 | + // filter out pathless (layout) routes |
| 112 | + if (route.id !== "root" && !route.path) return undefined; |
| 113 | + |
| 114 | + const lineage = getRouteLineage(ctx.config.routes, route); |
| 115 | + const fullpath = |
| 116 | + route.id === "root" |
| 117 | + ? "/" |
| 118 | + : lineage |
| 119 | + .map((route) => route.path) |
| 120 | + .filter((path) => path !== undefined) |
| 121 | + .join("/"); |
| 122 | + const params = parseParams(fullpath); |
| 123 | + return t.tsPropertySignature( |
| 124 | + t.stringLiteral(fullpath), |
| 125 | + t.tsTypeAnnotation( |
| 126 | + t.tsTypeLiteral( |
| 127 | + Object.entries(params).map(([param, isRequired]) => { |
| 128 | + const property = t.tsPropertySignature( |
| 129 | + t.stringLiteral(param), |
| 130 | + t.tsTypeAnnotation(t.tsStringKeyword()) |
| 131 | + ); |
| 132 | + property.optional = !isRequired; |
| 133 | + return property; |
| 134 | + }) |
| 135 | + ) |
| 136 | + ) |
| 137 | + ); |
| 138 | + }) |
| 139 | + .filter((x): x is Babel.Babel.TSPropertySignature => x !== undefined) |
| 140 | + ) |
| 141 | + ); |
| 142 | + |
| 143 | + return [register, Babel.generate(typeParams).code].join("\n\n"); |
| 144 | +} |
| 145 | + |
| 146 | +function parseParams(fullpath: string) { |
| 147 | + const result: Record<string, boolean> = {}; |
| 148 | + |
| 149 | + let segments = fullpath.split("/"); |
| 150 | + segments.forEach((segment) => { |
| 151 | + const match = segment.match(/^:([\w-]+)(\?)?/); |
| 152 | + if (!match) return; |
| 153 | + const param = match[1]; |
| 154 | + const isRequired = match[2] === undefined; |
| 155 | + |
| 156 | + result[param] ||= isRequired; |
| 157 | + return; |
| 158 | + }); |
| 159 | + |
| 160 | + const hasSplat = segments.at(-1) === "*"; |
| 161 | + if (hasSplat) result["*"] = true; |
| 162 | + return result; |
| 163 | +} |
| 164 | + |
| 165 | +function getRouteLineage(routes: RouteManifest, route: RouteManifestEntry) { |
| 166 | + const result: RouteManifestEntry[] = []; |
| 167 | + while (route) { |
| 168 | + result.push(route); |
| 169 | + if (!route.parentId) break; |
| 170 | + route = routes[route.parentId]; |
| 171 | + } |
| 172 | + result.reverse(); |
| 173 | + return result; |
84 | 174 | } |
0 commit comments