Skip to content

Commit f77ca63

Browse files
Generate types for virtual:react-router/server-build (#13152)
1 parent 5f6b341 commit f77ca63

File tree

8 files changed

+72
-6
lines changed

8 files changed

+72
-6
lines changed

.changeset/silver-peas-shop.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@react-router/dev": minor
3+
---
4+
5+
Generate types for `virtual:react-router/server-build` module

integration/helpers/vite-plugin-cloudflare-template/workers/app.ts

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ declare module "react-router" {
1414
}
1515

1616
const requestHandler = createRequestHandler(
17-
// @ts-expect-error
1817
() => import("virtual:react-router/server-build"),
1918
import.meta.env.MODE
2019
);

integration/typegen-test.ts

+43
Original file line numberDiff line numberDiff line change
@@ -444,4 +444,47 @@ test.describe("typegen", () => {
444444
expect(proc.stderr.toString()).toBe("");
445445
expect(proc.status).toBe(0);
446446
});
447+
448+
test.describe("virtual:react-router/server-build", async () => {
449+
test("static import matches 'createRequestHandler' argument type", async () => {
450+
const cwd = await createProject({
451+
"vite.config.ts": viteConfig,
452+
"app/routes.ts": tsx`
453+
import { type RouteConfig } from "@react-router/dev/routes";
454+
export default [] satisfies RouteConfig;
455+
`,
456+
"app/handler.ts": tsx`
457+
import { createRequestHandler } from "react-router";
458+
import * as serverBuild from "virtual:react-router/server-build";
459+
export default createRequestHandler(serverBuild);
460+
`,
461+
});
462+
463+
const proc = typecheck(cwd);
464+
expect(proc.stdout.toString()).toBe("");
465+
expect(proc.stderr.toString()).toBe("");
466+
expect(proc.status).toBe(0);
467+
});
468+
469+
test("dynamic import matches 'createRequestHandler' function argument type", async () => {
470+
const cwd = await createProject({
471+
"vite.config.ts": viteConfig,
472+
"app/routes.ts": tsx`
473+
import { type RouteConfig } from "@react-router/dev/routes";
474+
export default [] satisfies RouteConfig;
475+
`,
476+
"app/handler.ts": tsx`
477+
import { createRequestHandler } from "react-router";
478+
export default createRequestHandler(
479+
() => import("virtual:react-router/server-build")
480+
);
481+
`,
482+
});
483+
484+
const proc = typecheck(cwd);
485+
expect(proc.stdout.toString()).toBe("");
486+
expect(proc.stderr.toString()).toBe("");
487+
expect(proc.status).toBe(0);
488+
});
489+
});
447490
});

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

+20
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ async function writeAll(ctx: Context): Promise<void> {
8888

8989
const registerPath = Path.join(typegenDir, "+register.ts");
9090
fs.writeFileSync(registerPath, register(ctx));
91+
92+
const virtualPath = Path.join(typegenDir, "+virtual.ts");
93+
fs.writeFileSync(virtualPath, virtual);
9194
}
9295

9396
function register(ctx: Context) {
@@ -146,3 +149,20 @@ function register(ctx: Context) {
146149

147150
return [register, Babel.generate(typeParams).code].join("\n\n");
148151
}
152+
153+
const virtual = ts`
154+
declare module "virtual:react-router/server-build" {
155+
import { ServerBuild } from "react-router";
156+
export const assets: ServerBuild["assets"];
157+
export const assetsBuildDirectory: ServerBuild["assetsBuildDirectory"];
158+
export const basename: ServerBuild["basename"];
159+
export const entry: ServerBuild["entry"];
160+
export const future: ServerBuild["future"];
161+
export const isSpaMode: ServerBuild["isSpaMode"];
162+
export const prerender: ServerBuild["prerender"];
163+
export const publicPath: ServerBuild["publicPath"];
164+
export const routes: ServerBuild["routes"];
165+
export const ssr: ServerBuild["ssr"];
166+
export const unstable_getCriticalCss: ServerBuild["unstable_getCriticalCss"];
167+
}
168+
`;

packages/react-router-dev/vite/plugin.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
771771
ctx.reactRouterConfig.future.unstable_viteEnvironmentApi &&
772772
viteCommand === "serve"
773773
? `
774-
export const getCriticalCss = ({ pathname }) => {
774+
export const unstable_getCriticalCss = ({ pathname }) => {
775775
return {
776776
rel: "stylesheet",
777777
href: "${

packages/react-router/lib/server-runtime/build.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export interface ServerBuild {
2929
assetsBuildDirectory: string;
3030
future: FutureConfig;
3131
ssr: boolean;
32-
getCriticalCss?: (args: {
32+
unstable_getCriticalCss?: (args: {
3333
pathname: string;
3434
}) => OptionalCriticalCss | Promise<OptionalCriticalCss>;
3535
/**

packages/react-router/lib/server-runtime/server.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,8 @@ export const createRequestHandler: CreateRequestHandlerFunction = (
269269
let { pathname } = url;
270270

271271
let criticalCss: CriticalCss | undefined = undefined;
272-
if (_build.getCriticalCss) {
273-
criticalCss = await _build.getCriticalCss({ pathname });
272+
if (_build.unstable_getCriticalCss) {
273+
criticalCss = await _build.unstable_getCriticalCss({ pathname });
274274
} else if (
275275
mode === ServerMode.Development &&
276276
getDevServerHooks()?.getCriticalCss

playground/vite-plugin-cloudflare/workers/app.ts

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ declare module "react-router" {
1414
}
1515

1616
const requestHandler = createRequestHandler(
17-
// @ts-expect-error
1817
() => import("virtual:react-router/server-build"),
1918
import.meta.env.MODE
2019
);

0 commit comments

Comments
 (0)