@@ -13,6 +13,7 @@ import type { Context } from "./context";
13
13
import { getTypesDir , getTypesPath } from "./paths" ;
14
14
import * as Params from "./params" ;
15
15
import * as Route from "./route" ;
16
+ import type { RouteManifestEntry } from "../config/routes" ;
16
17
17
18
export async function run ( rootDirectory : string , { mode } : { mode : string } ) {
18
19
const ctx = await createContext ( { rootDirectory, mode, watch : false } ) ;
@@ -108,44 +109,41 @@ function register(ctx: Context) {
108
109
109
110
const { t } = Babel ;
110
111
111
- const indexPaths = new Set (
112
- Object . values ( ctx . config . routes )
113
- . filter ( ( route ) => route . index )
114
- . map ( ( route ) => route . path )
115
- ) ;
112
+ const fullpaths : Record < string , RouteManifestEntry [ ] > = { } ;
113
+ for ( const route of Object . values ( ctx . config . routes ) ) {
114
+ // skip pathless (layout) routes
115
+ if ( route . id !== "root" && ! route . path ) continue ;
116
+
117
+ const lineage = Route . lineage ( ctx . config . routes , route ) ;
118
+ const fullpath = Route . fullpath ( lineage ) ;
119
+ const existing = fullpaths [ fullpath ] ;
120
+ if ( ! existing || existing . length < lineage . length ) {
121
+ fullpaths [ fullpath ] = lineage ;
122
+ }
123
+ }
116
124
117
125
const typeParams = t . tsTypeAliasDeclaration (
118
126
t . identifier ( "Params" ) ,
119
127
null ,
120
128
t . tsTypeLiteral (
121
- Object . values ( ctx . config . routes )
122
- . map ( ( route ) => {
123
- // filter out pathless (layout) routes
124
- if ( route . id !== "root" && ! route . path ) return undefined ;
125
-
126
- // filter out layout routes that have a corresponding index
127
- if ( ! route . index && indexPaths . has ( route . path ) ) return undefined ;
128
-
129
- const lineage = Route . lineage ( ctx . config . routes , route ) ;
130
- const fullpath = Route . fullpath ( lineage ) ;
131
- const params = Params . parse ( fullpath ) ;
132
- return t . tsPropertySignature (
133
- t . stringLiteral ( fullpath ) ,
134
- t . tsTypeAnnotation (
135
- t . tsTypeLiteral (
136
- Object . entries ( params ) . map ( ( [ param , isRequired ] ) => {
137
- const property = t . tsPropertySignature (
138
- t . stringLiteral ( param ) ,
139
- t . tsTypeAnnotation ( t . tsStringKeyword ( ) )
140
- ) ;
141
- property . optional = ! isRequired ;
142
- return property ;
143
- } )
144
- )
129
+ Object . keys ( fullpaths ) . map ( ( fullpath ) => {
130
+ const params = Params . parse ( fullpath ) ;
131
+ return t . tsPropertySignature (
132
+ t . stringLiteral ( fullpath ) ,
133
+ t . tsTypeAnnotation (
134
+ t . tsTypeLiteral (
135
+ Object . entries ( params ) . map ( ( [ param , isRequired ] ) => {
136
+ const property = t . tsPropertySignature (
137
+ t . stringLiteral ( param ) ,
138
+ t . tsTypeAnnotation ( t . tsStringKeyword ( ) )
139
+ ) ;
140
+ property . optional = ! isRequired ;
141
+ return property ;
142
+ } )
145
143
)
146
- ) ;
147
- } )
148
- . filter ( ( x ) : x is Babel . Babel . TSPropertySignature => x !== undefined )
144
+ )
145
+ ) ;
146
+ } )
149
147
)
150
148
) ;
151
149
0 commit comments