Skip to content

Commit d7de1e7

Browse files
authored
fix: detect esm only pkg w/ main and module fields (#1494)
1 parent 5d63406 commit d7de1e7

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

shared/utils/package-analysis.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,10 @@ export function detectModuleFormat(pkg: ExtendedPackageJson): ModuleFormat {
8282

8383
// Legacy detection without exports field
8484
if (hasModule && hasMain) {
85-
// Has both module (ESM) and main (CJS) fields
86-
return 'dual'
85+
// Check for dual packages (has module field and main points to cjs)
86+
const mainIsCJS = pkg.main?.endsWith('.cjs') || (pkg.main?.endsWith('.js') && !isTypeModule)
87+
88+
return mainIsCJS ? 'dual' : 'esm'
8789
}
8890

8991
if (hasModule || isTypeModule) {

test/unit/shared/utils/package-analysis.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ describe('detectModuleFormat', () => {
2626
expect(detectModuleFormat({ module: 'index.mjs', main: 'index.js' })).toBe('dual')
2727
})
2828

29+
it('detects dual from type + module + main fields', () => {
30+
expect(detectModuleFormat({ type: 'module', module: 'index.js', main: 'index.cjs' })).toBe(
31+
'dual',
32+
)
33+
})
34+
35+
it('detects esm from type + module + main fields', () => {
36+
expect(detectModuleFormat({ type: 'module', module: 'index.js', main: 'index.js' })).toBe('esm')
37+
})
38+
2939
it('detects ESM from module field without main', () => {
3040
expect(detectModuleFormat({ module: 'index.mjs' })).toBe('esm')
3141
})

0 commit comments

Comments
 (0)