1
1
import ts from "dedent" ;
2
2
import * as Path from "pathe" ;
3
- import * as Pathe from "pathe/utils" ;
4
3
5
4
import { type RouteManifest , type RouteManifestEntry } from "../config/routes" ;
6
5
import { type Context } from "./context" ;
7
6
import { getTypesPath } from "./paths" ;
8
7
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
+
9
14
export function generate ( ctx : Context , route : RouteManifestEntry ) : string {
10
15
const lineage = getRouteLineage ( ctx . config . routes , route ) ;
11
16
const urlpath = lineage . map ( ( route ) => route . path ) . join ( "/" ) ;
@@ -20,9 +25,9 @@ export function generate(ctx: Context, route: RouteManifestEntry): string {
20
25
) ;
21
26
22
27
const indent = i === 0 ? "" : " " . repeat ( 2 ) ;
23
- let source = noExtension ( rel ) ;
28
+ let source = getTypescriptSafePath ( rel ) ;
24
29
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 } "` ;
26
31
} )
27
32
. join ( "\n" ) ;
28
33
@@ -34,7 +39,7 @@ export function generate(ctx: Context, route: RouteManifestEntry): string {
34
39
35
40
${ parentTypeImports }
36
41
37
- type Module = typeof import("../${ Pathe . filename ( route . file ) } .js ")
42
+ type Module = typeof import("../${ getTypescriptSafePath ( Path . basename ( route . file ) ) } ")
38
43
39
44
export type Info = {
40
45
parents: [${ parents . map ( ( _ , i ) => `Parent${ i } ` ) . join ( ", " ) } ],
@@ -72,9 +77,6 @@ export function generate(ctx: Context, route: RouteManifestEntry): string {
72
77
` ;
73
78
}
74
79
75
- const noExtension = ( path : string ) =>
76
- Path . join ( Path . dirname ( path ) , Pathe . filename ( path ) ) ;
77
-
78
80
function getRouteLineage ( routes : RouteManifest , route : RouteManifestEntry ) {
79
81
const result : RouteManifestEntry [ ] = [ ] ;
80
82
while ( route ) {
0 commit comments