Skip to content

Commit

Permalink
fix: noBaseUrl scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
kingston committed Oct 12, 2024
1 parent 6bd0af9 commit 27fd7ff
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
22 changes: 21 additions & 1 deletion src/transformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('fixImportPath', () => {
compilerOptions: {
baseUrl: './src',
paths: {
'@utils/*': ['utils/*'],
'@utils/*': ['./utils/*'],
},
},
},
Expand Down Expand Up @@ -117,4 +117,24 @@ describe('fixImportPath', () => {
);
expect(result).toBeUndefined();
});

it('should not throw an error for module paths with tsConfig without base URL', () => {
fsExistsSyncMock.mockReturnValue(false);
const result = fixImportPath(
'./root/src/file.ts',
'vitest',
resolverCache,
{
...tsConfig,
config: {
...tsConfig.config,
compilerOptions: {
...tsConfig.config.compilerOptions,
baseUrl: undefined,
},
},
},
);
expect(result).toBeUndefined();
});
});
8 changes: 6 additions & 2 deletions src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,15 @@ export function fixImportPath(
.map((p) => findSpecifierSuffix(p))
.find((s) => s !== undefined);

const hasNonBaseUrlMatch =
matchingPaths.length === 1 &&
!matchingPaths[0]?.endsWith(`/${specifier}`);

if (
!suffix &&
matchingPaths.length > 0 &&
// catch scenario where we default to baseUrl which could be a normal module
(matchingPaths.length !== 1 ||
!matchingPaths[0].endsWith(`/${specifier}`))
hasNonBaseUrlMatch
) {
throw new Error(`Could not find valid extension for ${specifier}`);
}
Expand Down

0 comments on commit 27fd7ff

Please sign in to comment.