From 3b3270346926922f68dc975c109e2d381c586980 Mon Sep 17 00:00:00 2001 From: Mark Dalgleish Date: Thu, 20 Mar 2025 09:04:46 +1100 Subject: [PATCH] Fix virtual module typegen with `moduleDetection: "force"` --- .changeset/honest-moles-brush.md | 5 +++++ integration/typegen-test.ts | 24 ++++++++++++++++++++++ packages/react-router-dev/typegen/index.ts | 2 +- 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 .changeset/honest-moles-brush.md diff --git a/.changeset/honest-moles-brush.md b/.changeset/honest-moles-brush.md new file mode 100644 index 0000000000..7d62c02898 --- /dev/null +++ b/.changeset/honest-moles-brush.md @@ -0,0 +1,5 @@ +--- +"@react-router/dev": patch +--- + +Fix typegen for virtual modules when `moduleDetection` is set to `force` diff --git a/integration/typegen-test.ts b/integration/typegen-test.ts index 7b0ff85cee..1460c528ab 100644 --- a/integration/typegen-test.ts +++ b/integration/typegen-test.ts @@ -466,6 +466,30 @@ test.describe("typegen", () => { expect(proc.status).toBe(0); }); + test("works with tsconfig 'moduleDetection' set to 'force'", async () => { + const cwd = await createProject({ + "vite.config.ts": viteConfig, + "app/routes.ts": tsx` + import { type RouteConfig } from "@react-router/dev/routes"; + export default [] satisfies RouteConfig; + `, + "app/handler.ts": tsx` + import { createRequestHandler } from "react-router"; + import * as serverBuild from "virtual:react-router/server-build"; + export default createRequestHandler(serverBuild); + `, + }); + + const tsconfig = await fse.readJson(path.join(cwd, "tsconfig.json")); + tsconfig.compilerOptions.moduleDetection = "force"; + await fse.writeJson(path.join(cwd, "tsconfig.json"), tsconfig); + + const proc = typecheck(cwd); + expect(proc.stdout.toString()).toBe(""); + expect(proc.stderr.toString()).toBe(""); + expect(proc.status).toBe(0); + }); + test("dynamic import matches 'createRequestHandler' function argument type", async () => { const cwd = await createProject({ "vite.config.ts": viteConfig, diff --git a/packages/react-router-dev/typegen/index.ts b/packages/react-router-dev/typegen/index.ts index 393a0025d1..342f27f808 100644 --- a/packages/react-router-dev/typegen/index.ts +++ b/packages/react-router-dev/typegen/index.ts @@ -89,7 +89,7 @@ async function writeAll(ctx: Context): Promise { const registerPath = Path.join(typegenDir, "+register.ts"); fs.writeFileSync(registerPath, register(ctx)); - const virtualPath = Path.join(typegenDir, "+virtual.ts"); + const virtualPath = Path.join(typegenDir, "+virtual.d.ts"); fs.writeFileSync(virtualPath, virtual); }