diff --git a/.changeset/two-needles-sell.md b/.changeset/two-needles-sell.md new file mode 100644 index 0000000000..a804cf4680 --- /dev/null +++ b/.changeset/two-needles-sell.md @@ -0,0 +1,5 @@ +--- +"@react-router/dev": patch +--- + +Fix typegen for non-{.js,.jsx,.ts,.tsx} routes like .mdx diff --git a/contributors.yml b/contributors.yml index 7818781f43..3263f413c9 100644 --- a/contributors.yml +++ b/contributors.yml @@ -258,6 +258,7 @@ - Nismit - nnhjs - noisypigeon +- nowells - Nurai1 - Obi-Dann - OlegDev1 diff --git a/packages/react-router-dev/typegen/generate.ts b/packages/react-router-dev/typegen/generate.ts index aa094a086a..30a638e0f7 100644 --- a/packages/react-router-dev/typegen/generate.ts +++ b/packages/react-router-dev/typegen/generate.ts @@ -322,11 +322,20 @@ function getRouteAnnotations({ function relativeImportSource(from: string, to: string) { let path = Path.relative(Path.dirname(from), to); + + let extension = Path.extname(path); + // no extension path = Path.join(Path.dirname(path), Pathe.filename(path)); if (!path.startsWith("../")) path = "./" + path; - return path + ".js"; + // In typescript, we want to support "moduleResolution": "nodenext" as well as not having "allowImportingTsExtensions": true, + // so we normalize all JS like files to `.js`, but allow other extensions such as `.mdx` and others that might be used as routes. + if (!extension || /\.(js|ts)x?$/.test(extension)) { + extension = ".js"; + } + + return path + extension; } function rootDirsPath(ctx: Context, typesPath: string): string {