Skip to content

Commit e8319a8

Browse files
committed
typegen: register lookup from route ID to route module
1 parent 368e0cb commit e8319a8

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

packages/react-router-dev/typegen/generate.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,16 @@ export function generateRoutes(ctx: Context): Array<VirtualFile> {
105105
interface Register {
106106
pages: Pages
107107
routeFiles: RouteFiles
108+
routeModules: RouteModules
108109
}
109110
}
110111
` +
111112
"\n\n" +
112113
Babel.generate(pagesType(allPages)).code +
113114
"\n\n" +
114-
Babel.generate(routeFilesType({ fileToRoutes, routeToPages })).code,
115+
Babel.generate(routeFilesType({ fileToRoutes, routeToPages })).code +
116+
"\n\n" +
117+
Babel.generate(routeModulesType(ctx)).code,
115118
};
116119

117120
// **/+types/*.ts
@@ -193,6 +196,29 @@ function routeFilesType({
193196
);
194197
}
195198

199+
function routeModulesType(ctx: Context) {
200+
return t.tsTypeAliasDeclaration(
201+
t.identifier("RouteModules"),
202+
null,
203+
t.tsTypeLiteral(
204+
Object.values(ctx.config.routes).map((route) =>
205+
t.tsPropertySignature(
206+
t.stringLiteral(route.id),
207+
t.tsTypeAnnotation(
208+
t.tsTypeQuery(
209+
t.tsImportType(
210+
t.stringLiteral(
211+
`./${Path.relative(ctx.rootDirectory, ctx.config.appDirectory)}/${route.file}`,
212+
),
213+
),
214+
),
215+
),
216+
),
217+
),
218+
),
219+
);
220+
}
221+
196222
function isInAppDirectory(ctx: Context, routeFile: string): boolean {
197223
const path = Path.resolve(ctx.config.appDirectory, routeFile);
198224
return path.startsWith(ctx.config.appDirectory);

0 commit comments

Comments
 (0)