File tree Expand file tree Collapse file tree 3 files changed +64
-2
lines changed Expand file tree Collapse file tree 3 files changed +64
-2
lines changed Original file line number Diff line number Diff line change @@ -151,9 +151,9 @@ export abstract class AbstractRouteGenerator<Config extends ExtendedRoutesConfig
151
151
152
152
protected getRelativeImportPath ( fileLocation : string ) {
153
153
const currentExt = path . extname ( fileLocation ) ;
154
- let newExtension = '' ;
154
+ let newExtension = this . options . rewriteRelativeImportExtensions ? currentExt : '' ;
155
155
156
- if ( this . options . esm ) {
156
+ if ( this . options . esm && ! this . options . rewriteRelativeImportExtensions ) {
157
157
switch ( currentExt ) {
158
158
case '.ts' :
159
159
default :
Original file line number Diff line number Diff line change @@ -276,4 +276,10 @@ export interface RoutesConfig {
276
276
* @default true
277
277
*/
278
278
bodyCoercion ?: boolean ;
279
+
280
+ /**
281
+ * When enabled, the imports in the routes files will keep having a `.ts` extention to support the TypeScript 5.7 feature rewriteRelativeImportExtensions.
282
+ * @default false
283
+ */
284
+ rewriteRelativeImportExtensions ?: boolean ;
279
285
}
Original file line number Diff line number Diff line change @@ -179,5 +179,61 @@ describe('RouteGenerator', () => {
179
179
180
180
expect ( models ) . to . equal ( './controller.cjs' ) ;
181
181
} ) ;
182
+
183
+ it ( 'uses ts for routes if esm is true and rewriteRelativeImportExtensions is true' , ( ) => {
184
+ const generator = new DefaultRouteGenerator (
185
+ {
186
+ controllers : [
187
+ {
188
+ location : 'controller.ts' ,
189
+ methods : [ ] ,
190
+ name : '' ,
191
+ path : '' ,
192
+ } ,
193
+ ] ,
194
+ referenceTypeMap : { } ,
195
+ } ,
196
+ {
197
+ bodyCoercion : true ,
198
+ entryFile : 'mockEntryFile' ,
199
+ routesDir : '.' ,
200
+ noImplicitAdditionalProperties : 'silently-remove-extras' ,
201
+ esm : true ,
202
+ rewriteRelativeImportExtensions : true ,
203
+ } ,
204
+ ) ;
205
+
206
+ const models = generator . buildContent ( '{{#each controllers}}{{modulePath}}{{/each}}' ) ;
207
+
208
+ expect ( models ) . to . equal ( './controller.ts' ) ;
209
+ } ) ;
210
+
211
+ it ( 'uses mts for routes if rewriteRelativeImportExtensions and esm is true and source is mts' , ( ) => {
212
+ const generator = new DefaultRouteGenerator (
213
+ {
214
+ controllers : [
215
+ {
216
+ location : 'controller.mts' ,
217
+ methods : [ ] ,
218
+ name : '' ,
219
+ path : '' ,
220
+ } ,
221
+ ] ,
222
+ referenceTypeMap : { } ,
223
+ } ,
224
+ {
225
+ bodyCoercion : true ,
226
+ entryFile : 'mockEntryFile' ,
227
+ routesDir : '.' ,
228
+ noImplicitAdditionalProperties : 'silently-remove-extras' ,
229
+ esm : true ,
230
+ rewriteRelativeImportExtensions : true ,
231
+ } ,
232
+ ) ;
233
+
234
+ const models = generator . buildContent ( '{{#each controllers}}{{modulePath}}{{/each}}' ) ;
235
+
236
+ expect ( models ) . to . equal ( './controller.mts' ) ;
237
+ } ) ;
182
238
} ) ;
183
239
} ) ;
You can’t perform that action at this time.
0 commit comments