From cf28aa35761fa37de6fda187953efc1b0a8d71a9 Mon Sep 17 00:00:00 2001 From: Gerard Wilkinson Date: Thu, 9 Nov 2023 15:51:59 +0000 Subject: [PATCH] fix(toExports): don't strip extension for package import (#291) Co-authored-by: Anthony Fu --- src/utils.ts | 8 +++++++- test/to-export.test.ts | 12 +++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index ac2a1792..7e9f770b 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -118,7 +118,9 @@ export function toExports (imports: Import[], fileDir?: string, includeType = fa const map = toImportModuleMap(imports, includeType) return Object.entries(map) .flatMap(([name, imports]) => { - name = name.replace(/\.[a-z]+$/, '') + if (isFilePath(name)) { + name = name.replace(/\.[a-zA-Z]+$/, '') + } if (fileDir && isAbsolute(name)) { name = relative(fileDir, name) if (!name.match(/^[.\/]/)) { @@ -311,3 +313,7 @@ export function resolveIdAbsolute (id: string, parentId?: string) { url: parentId }) } + +function isFilePath (path: string) { + return path.startsWith('.') || isAbsolute(path) || path.includes('://') +} diff --git a/test/to-export.test.ts b/test/to-export.test.ts index 52c39ab2..636e76a3 100644 --- a/test/to-export.test.ts +++ b/test/to-export.test.ts @@ -66,13 +66,19 @@ describe('toExports', () => { it('strip extensions', () => { const imports: Import[] = [ { from: 'test1.ts', name: 'foo', as: 'foo' }, - { from: 'test2.mjs', name: 'foobar', as: 'foobar' } + { from: 'test2.mjs', name: 'foobar', as: 'foobar' }, + { from: './test1.ts', name: 'foo', as: 'foo' }, + { from: './test2.mjs', name: 'foobar', as: 'foobar' }, + { from: 'test1.ts/test1.ts', name: 'foo', as: 'foo' } ] expect(toExports(imports)) .toMatchInlineSnapshot(` - "export { foo } from 'test1'; - export { foobar } from 'test2';" + "export { foo } from 'test1.ts'; + export { foobar } from 'test2.mjs'; + export { foo } from './test1'; + export { foobar } from './test2'; + export { foo } from 'test1.ts/test1.ts';" `) })