Skip to content

Commit d2b46f8

Browse files
nowellsNowell Stritepcattori
authored
fix(dev/typegen): fix non-JS/TS files (#12453)
* #12440 introduced a fix for tsconfig, however, it broke non-JS routes (like .mdx) When we have a file `./app/routes/test/index.mdx` which is a route we get the following error: ``` .react-router/types/app/routes/test/+types/index.ts:10:29 - error TS2307: Cannot find module ../index.js or its corresponding type declarations. 10 type Module = typeof import("../index.js") ~~~~~~~~~~~~~ ``` * Create two-needles-sell.md --------- Co-authored-by: Nowell Strite <[email protected]> Co-authored-by: Pedro Cattori <[email protected]>
1 parent a7ab88a commit d2b46f8

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

.changeset/two-needles-sell.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+
Fix typegen for non-{.js,.jsx,.ts,.tsx} routes like .mdx

contributors.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@
259259
- Nismit
260260
- nnhjs
261261
- noisypigeon
262+
- nowells
262263
- Nurai1
263264
- Obi-Dann
264265
- OlegDev1

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,11 +322,20 @@ function getRouteAnnotations({
322322

323323
function relativeImportSource(from: string, to: string) {
324324
let path = Path.relative(Path.dirname(from), to);
325+
326+
let extension = Path.extname(path);
327+
325328
// no extension
326329
path = Path.join(Path.dirname(path), Pathe.filename(path));
327330
if (!path.startsWith("../")) path = "./" + path;
328331

329-
return path + ".js";
332+
// In typescript, we want to support "moduleResolution": "nodenext" as well as not having "allowImportingTsExtensions": true,
333+
// so we normalize all JS like files to `.js`, but allow other extensions such as `.mdx` and others that might be used as routes.
334+
if (!extension || /\.(js|ts)x?$/.test(extension)) {
335+
extension = ".js";
336+
}
337+
338+
return path + extension;
330339
}
331340

332341
function rootDirsPath(ctx: Context, typesPath: string): string {

0 commit comments

Comments
 (0)