Skip to content

Commit ab98426

Browse files
authored
relative route files in resolved config (#12300)
* ensure resolved config has `route.file` relative to app dir * refactor: remove redundant checks for `route.file` being relative
1 parent abfcaa8 commit ab98426

File tree

3 files changed

+13
-17
lines changed

3 files changed

+13
-17
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ async function resolveConfig({
454454

455455
routes = {
456456
...routes,
457-
...configRoutesToRouteManifest(routeConfig),
457+
...configRoutesToRouteManifest(appDirectory, routeConfig),
458458
};
459459
} catch (error: any) {
460460
return err(

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { resolve, win32 } from "node:path";
1+
import * as Path from "pathe";
22
import * as v from "valibot";
33
import pick from "lodash/pick";
4+
45
import invariant from "../invariant";
56

67
declare global {
@@ -315,7 +316,7 @@ export function relative(directory: string): typeof helpers {
315316
* `relative` call that created this helper.
316317
*/
317318
route: (path, file, ...rest) => {
318-
return route(path, resolve(directory, file), ...(rest as any));
319+
return route(path, Path.resolve(directory, file), ...(rest as any));
319320
},
320321
/**
321322
* Helper function for creating a route config entry for an index route, for
@@ -324,7 +325,7 @@ export function relative(directory: string): typeof helpers {
324325
* `relative` call that created this helper.
325326
*/
326327
index: (file, ...rest) => {
327-
return index(resolve(directory, file), ...(rest as any));
328+
return index(Path.resolve(directory, file), ...(rest as any));
328329
},
329330
/**
330331
* Helper function for creating a route config entry for a layout route, for
@@ -333,7 +334,7 @@ export function relative(directory: string): typeof helpers {
333334
* `relative` call that created this helper.
334335
*/
335336
layout: (file, ...rest) => {
336-
return layout(resolve(directory, file), ...(rest as any));
337+
return layout(Path.resolve(directory, file), ...(rest as any));
337338
},
338339

339340
// Passthrough of helper functions that don't need relative scoping so that
@@ -343,6 +344,7 @@ export function relative(directory: string): typeof helpers {
343344
}
344345

345346
export function configRoutesToRouteManifest(
347+
appDirectory: string,
346348
routes: RouteConfigEntry[],
347349
rootId = "root"
348350
): RouteManifest {
@@ -353,7 +355,9 @@ export function configRoutesToRouteManifest(
353355
let manifestItem: RouteManifestEntry = {
354356
id,
355357
parentId,
356-
file: route.file,
358+
file: Path.isAbsolute(route.file)
359+
? Path.relative(appDirectory, route.file)
360+
: route.file,
357361
path: route.path,
358362
index: route.index,
359363
caseSensitive: route.caseSensitive,
@@ -381,11 +385,7 @@ export function configRoutesToRouteManifest(
381385
}
382386

383387
function createRouteId(file: string) {
384-
return normalizeSlashes(stripFileExtension(file));
385-
}
386-
387-
function normalizeSlashes(file: string) {
388-
return file.split(win32.sep).join("/");
388+
return Path.normalize(stripFileExtension(file));
389389
}
390390

391391
function stripFileExtension(file: string) {

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,10 @@ export function getTypesDir(ctx: Context) {
99
}
1010

1111
export function getTypesPath(ctx: Context, route: RouteManifestEntry) {
12-
const rel = Path.isAbsolute(route.file)
13-
? Path.relative(ctx.config.appDirectory, route.file)
14-
: route.file;
15-
1612
return Path.join(
1713
getTypesDir(ctx),
1814
Path.relative(ctx.rootDirectory, ctx.config.appDirectory),
19-
Path.dirname(rel),
20-
"+types/" + Pathe.filename(rel) + ".ts"
15+
Path.dirname(route.file),
16+
"+types/" + Pathe.filename(route.file) + ".ts"
2117
);
2218
}

0 commit comments

Comments
 (0)