diff --git a/src/plugins/alias-plugin.ts b/src/plugins/alias-plugin.ts index e77cb90d..ba24349b 100644 --- a/src/plugins/alias-plugin.ts +++ b/src/plugins/alias-plugin.ts @@ -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) { diff --git a/test/integration/exports-order/index.test.ts b/test/integration/exports-order/index.test.ts new file mode 100644 index 00000000..38b6bee9 --- /dev/null +++ b/test/integration/exports-order/index.test.ts @@ -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';`, + }) + }) +}) diff --git a/test/integration/exports-order/package.json b/test/integration/exports-order/package.json new file mode 100644 index 00000000..1d0333f3 --- /dev/null +++ b/test/integration/exports-order/package.json @@ -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" + } + } + } +} diff --git a/test/integration/exports-order/src/a.ts b/test/integration/exports-order/src/a.ts new file mode 100644 index 00000000..f370286b --- /dev/null +++ b/test/integration/exports-order/src/a.ts @@ -0,0 +1,2 @@ +import './utils.js' +export const foo = 'foo' diff --git a/test/integration/exports-order/src/index.ts b/test/integration/exports-order/src/index.ts new file mode 100644 index 00000000..ae91476e --- /dev/null +++ b/test/integration/exports-order/src/index.ts @@ -0,0 +1 @@ +export * from './a.js' diff --git a/test/integration/exports-order/src/utils.ts b/test/integration/exports-order/src/utils.ts new file mode 100644 index 00000000..3286c412 --- /dev/null +++ b/test/integration/exports-order/src/utils.ts @@ -0,0 +1 @@ +export const goo = 'goo'