Skip to content

Commit f21c90c

Browse files
author
Nowell Strite
committed
Support moduleResolution Node16 and NodeNext for non-JS files.
#12440 introduced a fix for tsconfig, however, it broke non-JS routes (like .mdx)
1 parent f0145cc commit f21c90c

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

.changeset/moody-eagles-deny.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+
Support `moduleResolution` `Node16` and `NodeNext` for non-JS files.

contributors.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@
211211
- Nismit
212212
- nnhjs
213213
- noisypigeon
214+
- nowells
214215
- Nurai1
215216
- Obi-Dann
216217
- OlegDev1

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import ts from "dedent";
22
import * as Path from "pathe";
3-
import * as Pathe from "pathe/utils";
43

54
import { type RouteManifest, type RouteManifestEntry } from "../config/routes";
65
import { type Context } from "./context";
76
import { getTypesPath } from "./paths";
87

8+
function getTypescriptSafePath(path: string) {
9+
// In typescript, we want to support "moduleResolution": "nodenext" as well as not having "allowImportingTsExtensions": true,
10+
// so we normalize all JS like files to `.js`, but allow other extensions such as `.mdx` and others that might be used as routes.
11+
return path.replace(/\.(js|ts)x?$/, ".js");
12+
}
13+
914
export function generate(ctx: Context, route: RouteManifestEntry): string {
1015
const lineage = getRouteLineage(ctx.config.routes, route);
1116
const urlpath = lineage.map((route) => route.path).join("/");
@@ -20,9 +25,9 @@ export function generate(ctx: Context, route: RouteManifestEntry): string {
2025
);
2126

2227
const indent = i === 0 ? "" : " ".repeat(2);
23-
let source = noExtension(rel);
28+
let source = getTypescriptSafePath(rel);
2429
if (!source.startsWith("../")) source = "./" + source;
25-
return `${indent}import type { Info as Parent${i} } from "${source}.js"`;
30+
return `${indent}import type { Info as Parent${i} } from "${source}"`;
2631
})
2732
.join("\n");
2833

@@ -34,7 +39,7 @@ export function generate(ctx: Context, route: RouteManifestEntry): string {
3439
3540
${parentTypeImports}
3641
37-
type Module = typeof import("../${Pathe.filename(route.file)}.js")
42+
type Module = typeof import("../${getTypescriptSafePath(Path.basename(route.file))}")
3843
3944
export type Info = {
4045
parents: [${parents.map((_, i) => `Parent${i}`).join(", ")}],
@@ -72,9 +77,6 @@ export function generate(ctx: Context, route: RouteManifestEntry): string {
7277
`;
7378
}
7479

75-
const noExtension = (path: string) =>
76-
Path.join(Path.dirname(path), Pathe.filename(path));
77-
7880
function getRouteLineage(routes: RouteManifest, route: RouteManifestEntry) {
7981
const result: RouteManifestEntry[] = [];
8082
while (route) {

0 commit comments

Comments
 (0)