Skip to content

Commit 4a37a95

Browse files
fix: use only in directory routes on +routes RouteModules (#14439)
* Typegen: only register route module types for routes inside the app directory --------- Co-authored-by: Pedro Cattori <pcattori@gmail.com>
1 parent 1abe213 commit 4a37a95

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

.changeset/shy-eels-drum.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+
Typegen: only register route module types for routes within the app directory

contributors.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
- ericschn
118118
- esadek
119119
- faergeek
120+
- fernandojbf
120121
- FilipJirsak
121122
- focusotter
122123
- foxscotch

integration/typegen-test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,31 @@ test.describe("typegen", () => {
456456
await $("pnpm typecheck");
457457
});
458458

459+
test("route files with paths outside of app directory", async ({
460+
cwd,
461+
edit,
462+
$,
463+
}) => {
464+
await fs.mkdir(Path.join(cwd, "node_modules/external_dependency"), {
465+
recursive: true,
466+
});
467+
await edit({
468+
"app/routes.ts": tsx`
469+
import { type RouteConfig, route } from "@react-router/dev/routes";
470+
471+
export default [
472+
route("outside/:id", "../node_modules/external_dependency/index.js"),
473+
] satisfies RouteConfig;
474+
`,
475+
"node_modules/external_dependency/index.js": tsx`
476+
export default function Component() {
477+
return <div>External Dependency</div>
478+
}
479+
`,
480+
});
481+
await $("pnpm typecheck");
482+
});
483+
459484
test("href", async ({ edit, $ }) => {
460485
await edit({
461486
"app/routes.ts": tsx`

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,15 @@ function routeModulesType(ctx: Context) {
205205
t.tsPropertySignature(
206206
t.stringLiteral(route.id),
207207
t.tsTypeAnnotation(
208-
t.tsTypeQuery(
209-
t.tsImportType(
210-
t.stringLiteral(
211-
`./${Path.relative(ctx.rootDirectory, ctx.config.appDirectory)}/${route.file}`,
212-
),
213-
),
214-
),
208+
isInAppDirectory(ctx, route.file)
209+
? t.tsTypeQuery(
210+
t.tsImportType(
211+
t.stringLiteral(
212+
`./${Path.relative(ctx.rootDirectory, ctx.config.appDirectory)}/${route.file}`,
213+
),
214+
),
215+
)
216+
: t.tsUnknownKeyword(),
215217
),
216218
),
217219
),

0 commit comments

Comments
 (0)