11import ts from "dedent" ;
22import * as Path from "pathe" ;
3- import * as Pathe from "pathe/utils" ;
43
54import { type RouteManifest , type RouteManifestEntry } from "../config/routes" ;
65import { type Context } from "./context" ;
76import { 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 ( / \. ( j s | t s ) x ? $ / , ".js" ) ;
12+ }
13+
914export 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-
7880function getRouteLineage ( routes : RouteManifest , route : RouteManifestEntry ) {
7981 const result : RouteManifestEntry [ ] = [ ] ;
8082 while ( route ) {
0 commit comments