Skip to content

Commit

Permalink
fix(toExports): don't strip extension for package import (#291)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <[email protected]>
  • Loading branch information
GerryWilko and antfu authored Nov 9, 2023
1 parent a90f45c commit cf28aa3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(/^[.\/]/)) {
Expand Down Expand Up @@ -311,3 +313,7 @@ export function resolveIdAbsolute (id: string, parentId?: string) {
url: parentId
})
}

function isFilePath (path: string) {
return path.startsWith('.') || isAbsolute(path) || path.includes('://')
}
12 changes: 9 additions & 3 deletions test/to-export.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';"
`)
})

Expand Down

0 comments on commit cf28aa3

Please sign in to comment.