Skip to content

Commit c250bf6

Browse files
committed
fix: file extension is not added to imported files w/ NodeNext - CodeRabbit suggestions
1 parent 2f1d068 commit c250bf6

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

packages/core/src/utils/tsconfig.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ export function isSyntheticDefaultImportsAllow(config?: Tsconfig) {
1313

1414
const NODE_NEXT_MODULES = new Set(['nodenext', 'node16']);
1515

16+
const NODE_NEXT_EXTENSION_MAP: ReadonlyArray<readonly [string, string]> = [
17+
['.tsx', '.jsx'],
18+
['.mts', '.mjs'],
19+
['.cts', '.cjs'],
20+
['.ts', '.js'],
21+
];
22+
1623
export function getImportExtension(
1724
fileExtension: string,
1825
tsconfig?: Tsconfig,
@@ -29,9 +36,12 @@ export function getImportExtension(
2936
(module && NODE_NEXT_MODULES.has(module)) ||
3037
(moduleResolution && NODE_NEXT_MODULES.has(moduleResolution))
3138
) {
32-
return fileExtension.endsWith('.ts')
33-
? `${fileExtension.slice(0, -3)}.js`
34-
: fileExtension;
39+
for (const [from, to] of NODE_NEXT_EXTENSION_MAP) {
40+
if (fileExtension.endsWith(from)) {
41+
return `${fileExtension.slice(0, -from.length)}${to}`;
42+
}
43+
}
44+
return fileExtension;
3545
}
3646

3747
return fileExtension.replace(/\.ts$/, '') || '';

packages/core/src/writers/schemas.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ function normalizeCanonicalImportPaths(
222222
schemaPath,
223223
canonical.importPath.replaceAll('\\', '/'),
224224
);
225+
// `relative` derives from canonical.importPath (built via getPath) so it
226+
// normally ends with `fileExtension`; the `.ts$` branch is a defensive
227+
// fallback for legacy/hardcoded `.ts` paths that don't match the
228+
// configured fileExtension (e.g. `.gen.ts`).
225229
const withoutFileExtension = relative.endsWith(fileExtension)
226230
? relative.slice(0, -fileExtension.length)
227231
: relative.replace(/\.ts$/, '');

0 commit comments

Comments
 (0)