Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions shared/utils/package-analysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions test/unit/shared/utils/package-analysis.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
Expand Down
Loading