diff --git a/shared/utils/package-analysis.ts b/shared/utils/package-analysis.ts index ca05328a3..f5ce07972 100644 --- a/shared/utils/package-analysis.ts +++ b/shared/utils/package-analysis.ts @@ -82,8 +82,10 @@ export function detectModuleFormat(pkg: ExtendedPackageJson): ModuleFormat { // Legacy detection without exports field if (hasModule && hasMain) { - // Has both module (ESM) and main (CJS) fields - return 'dual' + // Check for dual packages (has module field and main points to cjs) + const mainIsCJS = pkg.main?.endsWith('.cjs') || (pkg.main?.endsWith('.js') && !isTypeModule) + + return mainIsCJS ? 'dual' : 'esm' } if (hasModule || isTypeModule) { diff --git a/test/unit/shared/utils/package-analysis.spec.ts b/test/unit/shared/utils/package-analysis.spec.ts index f217a7263..af2cae040 100644 --- a/test/unit/shared/utils/package-analysis.spec.ts +++ b/test/unit/shared/utils/package-analysis.spec.ts @@ -26,6 +26,16 @@ describe('detectModuleFormat', () => { expect(detectModuleFormat({ module: 'index.mjs', main: 'index.js' })).toBe('dual') }) + it('detects dual from type + module + main fields', () => { + expect(detectModuleFormat({ type: 'module', module: 'index.js', main: 'index.cjs' })).toBe( + 'dual', + ) + }) + + it('detects esm from type + module + main fields', () => { + expect(detectModuleFormat({ type: 'module', module: 'index.js', main: 'index.js' })).toBe('esm') + }) + it('detects ESM from module field without main', () => { expect(detectModuleFormat({ module: 'index.mjs' })).toBe('esm') })