Skip to content

Commit

Permalink
fix: prefer to use the same exports name if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
himself65 committed Feb 10, 2025
1 parent f00b148 commit 2ef6bd8
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/plugins/alias-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,22 @@ export function aliasEntries({
})?.bundlePath
}
} else {
matchedBundlePath = exportMapEntries.find((item) => {
return findJsBundlePathCallback(item, specialCondition)
})?.bundlePath
matchedBundlePath = exportMapEntries
.sort(
// always put special condition after the general condition (default, cjs, esm)
(a, b) => {
if (a.conditionNames.has(specialCondition)) {
return -1
}
if (b.conditionNames.has(specialCondition)) {
return 1
}
return 0
},
)
.find((item) => {
return findJsBundlePathCallback(item, specialCondition)
})?.bundlePath
}

if (matchedBundlePath) {
Expand Down
42 changes: 42 additions & 0 deletions test/integration/exports-order/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { describe, expect, it } from 'vitest'
import {
createJob,
assertContainFiles,
assertFilesContent,
} from '../../testing-utils'

describe('integration exports-order', () => {
const { distDir } = createJob({ directory: __dirname })

it('should work with exports order', async () => {
const distFiles = [
'a.cjs',
'a.d.cts',
'a.d.ts',
'a.edge-light.d.ts',
'a.edge-light.js',
'a.js',
'index.cjs',
'index.d.cts',
'index.d.ts',
'index.edge-light.d.ts',
'index.edge-light.js',
'index.js',
]
await assertContainFiles(distDir, distFiles)
await assertFilesContent(distDir, {
'a.cjs': `const foo = 'foo';
exports.foo = foo;`,
'a.edge-light.js': `const foo = 'foo';
export { foo };`,
'a.js': `const foo = 'foo';
export { foo };`,
'index.cjs': `var a_cjs = require('./a.cjs');`,
'index.edge-light.js': `export * from './a.edge-light.js';`,
'index.js': `export * from './a.js';`,
})
})
})
34 changes: 34 additions & 0 deletions test/integration/exports-order/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "exports-order",
"type": "module",
"exports": {
".": {
"edge-light": {
"types": "./dist/index.edge-light.d.ts",
"default": "./dist/index.edge-light.js"
},
"require": {
"types": "./dist/index.d.cts",
"default": "./dist/index.cjs"
},
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
},
"./a": {
"edge-light": {
"types": "./dist/a.edge-light.d.ts",
"default": "./dist/a.edge-light.js"
},
"require": {
"types": "./dist/a.d.cts",
"default": "./dist/a.cjs"
},
"import": {
"types": "./dist/a.d.ts",
"default": "./dist/a.js"
}
}
}
}
2 changes: 2 additions & 0 deletions test/integration/exports-order/src/a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import './utils.js'
export const foo = 'foo'
1 change: 1 addition & 0 deletions test/integration/exports-order/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './a.js'
1 change: 1 addition & 0 deletions test/integration/exports-order/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const goo = 'goo'

0 comments on commit 2ef6bd8

Please sign in to comment.