Skip to content

Commit 7301c3e

Browse files
Fix failing ESM imports with file extension
Closes #7
1 parent ed054c0 commit 7301c3e

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

lib/parse/ClassLoader.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,9 +484,14 @@ export class ClassLoader {
484484
): { packageName: string; fileName: string; fileNameReferenced: string } | undefined {
485485
// Handle import paths within the current package
486486
if (importPath.startsWith('.')) {
487+
const fileName = joinFilePath(filePathDirName(currentFilePath), importPath);
488+
const indexOfExtension = fileName.indexOf('.', fileName.lastIndexOf('/'));
489+
const fileNameWithoutExtension = indexOfExtension === -1 ?
490+
fileName :
491+
fileName.slice(0, indexOfExtension);
487492
return {
488493
packageName: currentPackageName,
489-
fileName: joinFilePath(filePathDirName(currentFilePath), importPath),
494+
fileName: fileNameWithoutExtension,
490495
fileNameReferenced: currentFilePath,
491496
};
492497
}

test/parse/ClassLoader.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2368,6 +2368,15 @@ export = NS`,
23682368
});
23692369
});
23702370

2371+
it('for a local file imported with an extension', () => {
2372+
expect(loader.importTargetToAbsolutePath('package', 'dir/lib/fileA', './subdir/fileB.js'))
2373+
.toEqual({
2374+
packageName: 'package',
2375+
fileName: normalizeFilePath('dir/lib/subdir/fileB'),
2376+
fileNameReferenced: 'dir/lib/fileA',
2377+
});
2378+
});
2379+
23712380
it('for a package', () => {
23722381
resolutionContext.packageNameIndexOverrides['other-package'] = '/some-dir/index.js';
23732382
expect(loader.importTargetToAbsolutePath('package', 'dir/lib/fileA', 'other-package'))

0 commit comments

Comments
 (0)