Skip to content

Commit 2860031

Browse files
committed
Check module ID against resolved root route path
1 parent 6643a1c commit 2860031

File tree

4 files changed

+31
-23
lines changed

4 files changed

+31
-23
lines changed

integration/vite-presets-test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ test.describe("Vite / presets", async () => {
226226
"serverBundles",
227227
"serverModuleFormat",
228228
"ssr",
229+
"unstable_rootRouteFile",
229230
"unstable_routeConfig",
230231
]);
231232

packages/react-router-dev/config/config.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,10 @@ export type ResolvedReactRouterConfig = Readonly<{
264264
* SPA without server-rendering. Default's to `true`.
265265
*/
266266
ssr: boolean;
267+
/**
268+
* The absolute path to the root route file.
269+
*/
270+
unstable_rootRouteFile: string;
267271
/**
268272
* The resolved array of route config entries exported from `routes.ts`
269273
*/
@@ -345,6 +349,8 @@ type Result<T> =
345349
error: string;
346350
};
347351

352+
type ConfigResult = Result<ResolvedReactRouterConfig>;
353+
348354
function ok<T>(value: T): Result<T> {
349355
return { ok: true, value };
350356
}
@@ -365,7 +371,7 @@ async function resolveConfig({
365371
reactRouterConfigFile?: string;
366372
skipRoutes?: boolean;
367373
validateConfig?: ValidateConfigFunction;
368-
}): Promise<Result<ResolvedReactRouterConfig>> {
374+
}): Promise<ConfigResult> {
369375
let reactRouterUserConfig: ReactRouterConfig = {};
370376

371377
if (reactRouterConfigFile) {
@@ -506,7 +512,7 @@ async function resolveConfig({
506512
let appDirectory = Path.resolve(root, userAppDirectory || "app");
507513
let buildDirectory = Path.resolve(root, userBuildDirectory);
508514

509-
let rootRouteFile = findEntry(appDirectory, "root");
515+
let rootRouteFile = findEntry(appDirectory, "root", { absolute: true });
510516
if (!rootRouteFile) {
511517
let rootRouteDisplayPath = Path.relative(
512518
root,
@@ -556,7 +562,7 @@ async function resolveConfig({
556562
{
557563
id: "root",
558564
path: "",
559-
file: rootRouteFile,
565+
file: Path.relative(appDirectory, rootRouteFile),
560566
children: result.routeConfig,
561567
},
562568
];
@@ -609,6 +615,7 @@ async function resolveConfig({
609615
serverBundles,
610616
serverModuleFormat,
611617
ssr,
618+
unstable_rootRouteFile: rootRouteFile,
612619
unstable_routeConfig: routeConfig,
613620
} satisfies ResolvedReactRouterConfig);
614621

@@ -622,7 +629,7 @@ async function resolveConfig({
622629
type ChokidarEventName = ChokidarEmitArgs[0];
623630

624631
type ChangeHandler = (args: {
625-
result: Result<ResolvedReactRouterConfig>;
632+
result: ConfigResult;
626633
configCodeChanged: boolean;
627634
routeConfigCodeChanged: boolean;
628635
configChanged: boolean;
@@ -632,7 +639,7 @@ type ChangeHandler = (args: {
632639
}) => void;
633640

634641
export type ConfigLoader = {
635-
getConfig: () => Promise<Result<ResolvedReactRouterConfig>>;
642+
getConfig: () => Promise<ConfigResult>;
636643
onChange: (handler: ChangeHandler) => () => void;
637644
close: () => Promise<void>;
638645
};

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,12 +309,11 @@ export function reactRouterRSCVitePlugin(): Vite.PluginOption[] {
309309
{
310310
name: "react-router/rsc/virtual-route-modules",
311311
transform(code, id) {
312-
if (!routeIdByFile) return;
313312
return transformVirtualRouteModules({
314313
code,
315314
id,
316315
viteCommand,
317-
routeIdByFile,
316+
rootRouteFile: config.unstable_rootRouteFile,
318317
viteEnvironment: this.environment,
319318
});
320319
},

packages/react-router-dev/vite/rsc/virtual-route-modules.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,20 @@ export function transformVirtualRouteModules({
9696
id,
9797
code,
9898
viteCommand,
99-
routeIdByFile,
99+
rootRouteFile,
100100
viteEnvironment,
101101
}: {
102102
id: string;
103103
code: string;
104104
viteCommand: ViteCommand;
105-
routeIdByFile: Map<string, string>;
105+
rootRouteFile: string;
106106
viteEnvironment: Vite.Environment;
107107
}) {
108-
if (isVirtualRouteModuleId(id) || routeIdByFile.has(id)) {
108+
if (isVirtualRouteModuleId(id)) {
109109
return createVirtualRouteModuleCode({
110110
id,
111111
code,
112-
routeIdByFile,
112+
rootRouteFile,
113113
viteCommand,
114114
viteEnvironment,
115115
});
@@ -127,7 +127,7 @@ export function transformVirtualRouteModules({
127127
return createVirtualClientRouteModuleCode({
128128
id,
129129
code,
130-
routeIdByFile,
130+
rootRouteFile,
131131
viteCommand,
132132
});
133133
}
@@ -136,13 +136,13 @@ export function transformVirtualRouteModules({
136136
async function createVirtualRouteModuleCode({
137137
id,
138138
code: routeSource,
139-
routeIdByFile,
139+
rootRouteFile,
140140
viteCommand,
141141
viteEnvironment,
142142
}: {
143143
id: string;
144144
code: string;
145-
routeIdByFile: Map<string, string>;
145+
rootRouteFile: string;
146146
viteCommand: ViteCommand;
147147
viteEnvironment: Vite.Environment;
148148
}) {
@@ -196,7 +196,7 @@ async function createVirtualRouteModuleCode({
196196
}
197197

198198
if (
199-
isRootRouteId({ id, routeIdByFile }) &&
199+
isRootRouteFile({ id, rootRouteFile }) &&
200200
!staticExports.includes("ErrorBoundary")
201201
) {
202202
code += `export { ErrorBoundary } from "${clientModuleId}";\n`;
@@ -251,12 +251,12 @@ function createVirtualServerRouteModuleCode({
251251
function createVirtualClientRouteModuleCode({
252252
id,
253253
code: routeSource,
254-
routeIdByFile,
254+
rootRouteFile,
255255
viteCommand,
256256
}: {
257257
id: string;
258258
code: string;
259-
routeIdByFile: Map<string, string>;
259+
rootRouteFile: string;
260260
viteCommand: ViteCommand;
261261
}) {
262262
const { staticExports, isServerFirstRoute, hasClientExports } =
@@ -274,7 +274,7 @@ function createVirtualClientRouteModuleCode({
274274
generatorResult.code = '"use client";' + generatorResult.code;
275275

276276
if (
277-
isRootRouteId({ id, routeIdByFile }) &&
277+
isRootRouteFile({ id, rootRouteFile }) &&
278278
!staticExports.includes("ErrorBoundary")
279279
) {
280280
const hasRootLayout = staticExports.includes("Layout");
@@ -327,12 +327,13 @@ function isVirtualServerRouteModuleId(id: string): boolean {
327327
return /(\?|&)server-route-module(&|$)/.test(id);
328328
}
329329

330-
function isRootRouteId({
330+
function isRootRouteFile({
331331
id,
332-
routeIdByFile,
332+
rootRouteFile,
333333
}: {
334334
id: string;
335-
routeIdByFile: Map<string, string>;
336-
}) {
337-
return routeIdByFile.has(id.split("?")[0]);
335+
rootRouteFile: string;
336+
}): boolean {
337+
const filePath = id.split("?")[0];
338+
return filePath === rootRouteFile;
338339
}

0 commit comments

Comments
 (0)