diff --git a/.changeset/funny-paws-divide.md b/.changeset/funny-paws-divide.md new file mode 100644 index 0000000000..30b632fd6e --- /dev/null +++ b/.changeset/funny-paws-divide.md @@ -0,0 +1,5 @@ +--- +"@react-router/dev": patch +--- + +Prevent typegen with route files are outside the app directory diff --git a/contributors.yml b/contributors.yml index 4fe526a6b8..7d04566b0d 100644 --- a/contributors.yml +++ b/contributors.yml @@ -371,6 +371,7 @@ - vonagam - WalkAlone0325 - whxhlgy +- wilcoxmd - willemarcel - williamsdyyz - willsawyerrrr diff --git a/packages/react-router-dev/typegen/generate.ts b/packages/react-router-dev/typegen/generate.ts index 789129d445..cf89379e33 100644 --- a/packages/react-router-dev/typegen/generate.ts +++ b/packages/react-router-dev/typegen/generate.ts @@ -170,82 +170,89 @@ export function generateRoutes(ctx: Context): VirtualFile { export function generateRouteModuleAnnotations( ctx: Context ): Array { - 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 - actionData: CreateActionData - } + export type unstable_Props = { + params: Params[Id] + loaderData: CreateLoaderData + actionData: CreateActionData + } - type Annotations = RouteModuleAnnotations; + type Annotations = RouteModuleAnnotations; - 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(