Skip to content

Skip typegen for routes outside the app directory #12996

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/funny-paws-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@react-router/dev": patch
---

Prevent typegen with route files are outside the app directory
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@
- vonagam
- WalkAlone0325
- whxhlgy
- wilcoxmd
- willemarcel
- williamsdyyz
- willsawyerrrr
Expand Down
121 changes: 64 additions & 57 deletions packages/react-router-dev/typegen/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,82 +170,89 @@ export function generateRoutes(ctx: Context): VirtualFile {
export function generateRouteModuleAnnotations(
ctx: Context
): Array<VirtualFile> {
return Object.values(ctx.config.routes).map((route) => {
const filename = getRouteModuleAnnotationsFilepath(ctx, route);
return Object.values(ctx.config.routes)
.filter((route) => isRouteInAppDirectory(ctx, route))
.map((route) => {
const filename = getRouteModuleAnnotationsFilepath(ctx, route);

const parents = getParents(ctx, route);
const parents = getParents(ctx, route);

const content = ts`
// Generated by React Router
const content = ts`
// Generated by React Router

import type {
Params,
RouteModuleAnnotations,
CreateLoaderData,
CreateActionData,
} from "react-router/internal";
import type {
Params,
RouteModuleAnnotations,
CreateLoaderData,
CreateActionData,
} from "react-router/internal";

${parents.map((parent) => parent.import).join("\n" + " ".repeat(3))}
type Parents = [${parents.map((parent) => parent.name).join(", ")}]
${parents.map((parent) => parent.import).join("\n" + " ".repeat(3))}
type Parents = [${parents.map((parent) => parent.name).join(", ")}]

type Id = "${route.id}"
type Module = typeof import("../${Pathe.filename(route.file)}.js")
type Id = "${route.id}"
type Module = typeof import("../${Pathe.filename(route.file)}.js")

export type unstable_Props = {
params: Params[Id]
loaderData: CreateLoaderData<Module>
actionData: CreateActionData<Module>
}
export type unstable_Props = {
params: Params[Id]
loaderData: CreateLoaderData<Module>
actionData: CreateActionData<Module>
}

type Annotations = RouteModuleAnnotations<unstable_Props & {
parents: Parents,
module: Module,
}>;
type Annotations = RouteModuleAnnotations<unstable_Props & {
parents: Parents,
module: Module,
}>;

export namespace Route {
// links
export type LinkDescriptors = Annotations["LinkDescriptors"];
export type LinksFunction = Annotations["LinksFunction"];
export namespace Route {
// links
export type LinkDescriptors = Annotations["LinkDescriptors"];
export type LinksFunction = Annotations["LinksFunction"];

// meta
export type MetaArgs = Annotations["MetaArgs"];
export type MetaDescriptors = Annotations["MetaDescriptors"];
export type MetaFunction = Annotations["MetaFunction"];
// meta
export type MetaArgs = Annotations["MetaArgs"];
export type MetaDescriptors = Annotations["MetaDescriptors"];
export type MetaFunction = Annotations["MetaFunction"];

// headers
export type HeadersArgs = Annotations["HeadersArgs"];
export type HeadersFunction = Annotations["HeadersFunction"];
// headers
export type HeadersArgs = Annotations["HeadersArgs"];
export type HeadersFunction = Annotations["HeadersFunction"];

// unstable_middleware
export type unstable_MiddlewareFunction = Annotations["unstable_MiddlewareFunction"];
// unstable_middleware
export type unstable_MiddlewareFunction = Annotations["unstable_MiddlewareFunction"];

// unstable_clientMiddleware
export type unstable_ClientMiddlewareFunction = Annotations["unstable_ClientMiddlewareFunction"];
// unstable_clientMiddleware
export type unstable_ClientMiddlewareFunction = Annotations["unstable_ClientMiddlewareFunction"];

// loader
export type LoaderArgs = Annotations["LoaderArgs"];
// loader
export type LoaderArgs = Annotations["LoaderArgs"];

// clientLoader
export type ClientLoaderArgs = Annotations["ClientLoaderArgs"];
// clientLoader
export type ClientLoaderArgs = Annotations["ClientLoaderArgs"];

// action
export type ActionArgs = Annotations["ActionArgs"];
// action
export type ActionArgs = Annotations["ActionArgs"];

// clientAction
export type ClientActionArgs = Annotations["ClientActionArgs"];
// clientAction
export type ClientActionArgs = Annotations["ClientActionArgs"];

// HydrateFallback
export type HydrateFallbackProps = Annotations["HydrateFallbackProps"];
// HydrateFallback
export type HydrateFallbackProps = Annotations["HydrateFallbackProps"];

// Component
export type ComponentProps = Annotations["ComponentProps"];
// Component
export type ComponentProps = Annotations["ComponentProps"];

// ErrorBoundary
export type ErrorBoundaryProps = Annotations["ErrorBoundaryProps"];
}
`;
return { filename, content };
});
// ErrorBoundary
export type ErrorBoundaryProps = Annotations["ErrorBoundaryProps"];
}
`;
return { filename, content };
});
}

function isRouteInAppDirectory(ctx: Context, route: RouteManifestEntry) {
const absoluteRoutePath = Path.resolve(ctx.config.appDirectory, route.file);
return absoluteRoutePath.startsWith(ctx.config.appDirectory);
}

function getRouteModuleAnnotationsFilepath(
Expand Down