Skip to content

Commit b7b1876

Browse files
authored
Skip typegen for routes outside the app directory (#12996)
* Only generate types for route modules in a projects app directory * Add changeset * Add username to contributors.yml
1 parent 0423079 commit b7b1876

File tree

3 files changed

+70
-57
lines changed

3 files changed

+70
-57
lines changed

.changeset/funny-paws-divide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@react-router/dev": patch
3+
---
4+
5+
Prevent typegen with route files are outside the app directory

contributors.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@
371371
- vonagam
372372
- WalkAlone0325
373373
- whxhlgy
374+
- wilcoxmd
374375
- willemarcel
375376
- williamsdyyz
376377
- willsawyerrrr

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

Lines changed: 64 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -170,82 +170,89 @@ export function generateRoutes(ctx: Context): VirtualFile {
170170
export function generateRouteModuleAnnotations(
171171
ctx: Context
172172
): Array<VirtualFile> {
173-
return Object.values(ctx.config.routes).map((route) => {
174-
const filename = getRouteModuleAnnotationsFilepath(ctx, route);
173+
return Object.values(ctx.config.routes)
174+
.filter((route) => isRouteInAppDirectory(ctx, route))
175+
.map((route) => {
176+
const filename = getRouteModuleAnnotationsFilepath(ctx, route);
175177

176-
const parents = getParents(ctx, route);
178+
const parents = getParents(ctx, route);
177179

178-
const content = ts`
179-
// Generated by React Router
180+
const content = ts`
181+
// Generated by React Router
180182
181-
import type {
182-
Params,
183-
RouteModuleAnnotations,
184-
CreateLoaderData,
185-
CreateActionData,
186-
} from "react-router/internal";
183+
import type {
184+
Params,
185+
RouteModuleAnnotations,
186+
CreateLoaderData,
187+
CreateActionData,
188+
} from "react-router/internal";
187189
188-
${parents.map((parent) => parent.import).join("\n" + " ".repeat(3))}
189-
type Parents = [${parents.map((parent) => parent.name).join(", ")}]
190+
${parents.map((parent) => parent.import).join("\n" + " ".repeat(3))}
191+
type Parents = [${parents.map((parent) => parent.name).join(", ")}]
190192
191-
type Id = "${route.id}"
192-
type Module = typeof import("../${Pathe.filename(route.file)}.js")
193+
type Id = "${route.id}"
194+
type Module = typeof import("../${Pathe.filename(route.file)}.js")
193195
194-
export type unstable_Props = {
195-
params: Params[Id]
196-
loaderData: CreateLoaderData<Module>
197-
actionData: CreateActionData<Module>
198-
}
196+
export type unstable_Props = {
197+
params: Params[Id]
198+
loaderData: CreateLoaderData<Module>
199+
actionData: CreateActionData<Module>
200+
}
199201
200-
type Annotations = RouteModuleAnnotations<unstable_Props & {
201-
parents: Parents,
202-
module: Module,
203-
}>;
202+
type Annotations = RouteModuleAnnotations<unstable_Props & {
203+
parents: Parents,
204+
module: Module,
205+
}>;
204206
205-
export namespace Route {
206-
// links
207-
export type LinkDescriptors = Annotations["LinkDescriptors"];
208-
export type LinksFunction = Annotations["LinksFunction"];
207+
export namespace Route {
208+
// links
209+
export type LinkDescriptors = Annotations["LinkDescriptors"];
210+
export type LinksFunction = Annotations["LinksFunction"];
209211
210-
// meta
211-
export type MetaArgs = Annotations["MetaArgs"];
212-
export type MetaDescriptors = Annotations["MetaDescriptors"];
213-
export type MetaFunction = Annotations["MetaFunction"];
212+
// meta
213+
export type MetaArgs = Annotations["MetaArgs"];
214+
export type MetaDescriptors = Annotations["MetaDescriptors"];
215+
export type MetaFunction = Annotations["MetaFunction"];
214216
215-
// headers
216-
export type HeadersArgs = Annotations["HeadersArgs"];
217-
export type HeadersFunction = Annotations["HeadersFunction"];
217+
// headers
218+
export type HeadersArgs = Annotations["HeadersArgs"];
219+
export type HeadersFunction = Annotations["HeadersFunction"];
218220
219-
// unstable_middleware
220-
export type unstable_MiddlewareFunction = Annotations["unstable_MiddlewareFunction"];
221+
// unstable_middleware
222+
export type unstable_MiddlewareFunction = Annotations["unstable_MiddlewareFunction"];
221223
222-
// unstable_clientMiddleware
223-
export type unstable_ClientMiddlewareFunction = Annotations["unstable_ClientMiddlewareFunction"];
224+
// unstable_clientMiddleware
225+
export type unstable_ClientMiddlewareFunction = Annotations["unstable_ClientMiddlewareFunction"];
224226
225-
// loader
226-
export type LoaderArgs = Annotations["LoaderArgs"];
227+
// loader
228+
export type LoaderArgs = Annotations["LoaderArgs"];
227229
228-
// clientLoader
229-
export type ClientLoaderArgs = Annotations["ClientLoaderArgs"];
230+
// clientLoader
231+
export type ClientLoaderArgs = Annotations["ClientLoaderArgs"];
230232
231-
// action
232-
export type ActionArgs = Annotations["ActionArgs"];
233+
// action
234+
export type ActionArgs = Annotations["ActionArgs"];
233235
234-
// clientAction
235-
export type ClientActionArgs = Annotations["ClientActionArgs"];
236+
// clientAction
237+
export type ClientActionArgs = Annotations["ClientActionArgs"];
236238
237-
// HydrateFallback
238-
export type HydrateFallbackProps = Annotations["HydrateFallbackProps"];
239+
// HydrateFallback
240+
export type HydrateFallbackProps = Annotations["HydrateFallbackProps"];
239241
240-
// Component
241-
export type ComponentProps = Annotations["ComponentProps"];
242+
// Component
243+
export type ComponentProps = Annotations["ComponentProps"];
242244
243-
// ErrorBoundary
244-
export type ErrorBoundaryProps = Annotations["ErrorBoundaryProps"];
245-
}
246-
`;
247-
return { filename, content };
248-
});
245+
// ErrorBoundary
246+
export type ErrorBoundaryProps = Annotations["ErrorBoundaryProps"];
247+
}
248+
`;
249+
return { filename, content };
250+
});
251+
}
252+
253+
function isRouteInAppDirectory(ctx: Context, route: RouteManifestEntry) {
254+
const absoluteRoutePath = Path.resolve(ctx.config.appDirectory, route.file);
255+
return absoluteRoutePath.startsWith(ctx.config.appDirectory);
249256
}
250257

251258
function getRouteModuleAnnotationsFilepath(

0 commit comments

Comments
 (0)