Skip to content

Commit

Permalink
Merge pull request #1725 from Azlond/1714-rewriteRelativeImportExtens…
Browse files Browse the repository at this point in the history
…ions-support

feat: #1714 added support for ts 5.7 rewriteRelativeImportExtensions
  • Loading branch information
WoH authored Dec 8, 2024
2 parents ca2068d + 5ff892f commit 9a09986
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/cli/src/routeGeneration/routeGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ export abstract class AbstractRouteGenerator<Config extends ExtendedRoutesConfig

protected getRelativeImportPath(fileLocation: string) {
const currentExt = path.extname(fileLocation);
let newExtension = '';
let newExtension = this.options.rewriteRelativeImportExtensions ? currentExt : '';

if (this.options.esm) {
if (this.options.esm && !this.options.rewriteRelativeImportExtensions) {
switch (currentExt) {
case '.ts':
default:
Expand Down
6 changes: 6 additions & 0 deletions packages/runtime/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,10 @@ export interface RoutesConfig {
* @default true
*/
bodyCoercion?: boolean;

/**
* When enabled, the imports in the routes files will keep having a `.ts` extention to support the TypeScript 5.7 feature rewriteRelativeImportExtensions.
* @default false
*/
rewriteRelativeImportExtensions?: boolean;
}
56 changes: 56 additions & 0 deletions tests/unit/templating/routeGenerator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,5 +179,61 @@ describe('RouteGenerator', () => {

expect(models).to.equal('./controller.cjs');
});

it('uses ts for routes if esm is true and rewriteRelativeImportExtensions is true', () => {
const generator = new DefaultRouteGenerator(
{
controllers: [
{
location: 'controller.ts',
methods: [],
name: '',
path: '',
},
],
referenceTypeMap: {},
},
{
bodyCoercion: true,
entryFile: 'mockEntryFile',
routesDir: '.',
noImplicitAdditionalProperties: 'silently-remove-extras',
esm: true,
rewriteRelativeImportExtensions: true,
},
);

const models = generator.buildContent('{{#each controllers}}{{modulePath}}{{/each}}');

expect(models).to.equal('./controller.ts');
});

it('uses mts for routes if rewriteRelativeImportExtensions and esm is true and source is mts', () => {
const generator = new DefaultRouteGenerator(
{
controllers: [
{
location: 'controller.mts',
methods: [],
name: '',
path: '',
},
],
referenceTypeMap: {},
},
{
bodyCoercion: true,
entryFile: 'mockEntryFile',
routesDir: '.',
noImplicitAdditionalProperties: 'silently-remove-extras',
esm: true,
rewriteRelativeImportExtensions: true,
},
);

const models = generator.buildContent('{{#each controllers}}{{modulePath}}{{/each}}');

expect(models).to.equal('./controller.mts');
});
});
});

0 comments on commit 9a09986

Please sign in to comment.