Skip to content
Open
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/yellow-carpets-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@react-router/dev": patch
---

Fix typegen support for routes outside appDirectory
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- alberto
- AlemTuzlak
- Aleuck
- alex-pex
- alexandernanberg
- alexanderson1993
- alexlbr
Expand Down
52 changes: 52 additions & 0 deletions integration/typegen-test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from "node:fs/promises";

import { expect } from "@playwright/test";
import tsx from "dedent";
import * as Path from "pathe";

Expand Down Expand Up @@ -336,6 +337,57 @@ test.describe("typegen", () => {
await $("pnpm typecheck");
});

test("routes outside app dir", async ({ cwd, edit, $ }) => {
// Create the subdirectories
await fs.mkdir(Path.join(cwd, "app/router"), { recursive: true });
await fs.mkdir(Path.join(cwd, "app/pages"), { recursive: true });

await edit({
"react-router.config.ts": tsx`
export default {
appDirectory: "app/router",
}
`,
"app/router/routes.ts": tsx`
import { type RouteConfig, route } from "@react-router/dev/routes";

export default [
route("products/:id", "../pages/product.tsx")
] satisfies RouteConfig;
`,
"app/router/root.tsx": tsx`
import { Outlet } from "react-router";

export default function Root() {
return <Outlet />;
}
`,
"app/pages/product.tsx": tsx`
import type { Expect, Equal } from "../expect-type"
import type { Route } from "./+types/product"

export function loader({ params }: Route.LoaderArgs) {
type Test = Expect<Equal<typeof params, { id: string }>>
return { planet: "world" }
}

export default function Component({ loaderData }: Route.ComponentProps) {
type Test = Expect<Equal<typeof loaderData.planet, string>>
return <h1>Hello, {loaderData.planet}!</h1>
}
`,
});
await $("pnpm typecheck");

// Verify that the types file was generated in the correct location
const annotationPath = Path.join(
cwd,
".react-router/types/app/pages/+types/product.ts",
);
const annotation = await fs.readFile(annotationPath, "utf8");
expect(annotation).toContain("export namespace Route");
});

test("matches", async ({ edit, $ }) => {
await edit({
"app/routes.ts": tsx`
Expand Down
6 changes: 3 additions & 3 deletions packages/react-router-dev/typegen/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export function generateRoutes(ctx: Context): Array<VirtualFile> {

// **/+types/*.ts
const allAnnotations: Array<VirtualFile> = Array.from(fileToRoutes.entries())
.filter(([file]) => isInAppDirectory(ctx, file))
.filter(([file]) => isInRootDirectory(ctx, file))
.map(([file, routeIds]) =>
getRouteAnnotations({ ctx, file, routeIds, lineages }),
);
Expand Down Expand Up @@ -193,9 +193,9 @@ function routeFilesType({
);
}

function isInAppDirectory(ctx: Context, routeFile: string): boolean {
function isInRootDirectory(ctx: Context, routeFile: string): boolean {
const path = Path.resolve(ctx.config.appDirectory, routeFile);
return path.startsWith(ctx.config.appDirectory);
return path.startsWith(ctx.rootDirectory);
}

function getRouteAnnotations({
Expand Down