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
10 changes: 10 additions & 0 deletions packages/node-modules-tools/src/analyze-esm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,14 @@ describe('analyzePackageModuleType', () => {
exports: nestExports(11, './index.mjs'),
})).toBe('unknown')
})

it('handles main: false without crashing', () => {
// Some packages (e.g. math-intrinsics, dunder-proto) declare "main": false
// to indicate exports-only packages. Should not crash.
expect(analyze({
name: 'math-intrinsics',
main: false as any,
exports: { './abs': './abs.js' },
})).toBe('unknown')
})
})
13 changes: 8 additions & 5 deletions packages/node-modules-tools/src/analyze-esm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,16 @@ export function analyzePackageModuleType(pkgJson: PackageJson): PackageModuleTyp
if (pkgJson.name?.startsWith('@types/'))
return 'dts'

// Ensure main is a string (can be false in some packages)
const main = typeof pkgJson.main === 'string' ? pkgJson.main : undefined

// Native binary packages (e.g. @oxc-parser/binding-linux-x64-gnu)
if (pkgJson.main?.endsWith('.node'))
if (main?.endsWith('.node'))
return 'bin'

const hasExports = pkgJson.exports != null
const hasModule = !!pkgJson.module
const hasMain = !!pkgJson.main
const hasMain = !!main
const isTypeModule = pkgJson.type === 'module'

// Check exports field for module format indicators
Expand Down Expand Up @@ -103,15 +106,15 @@ export function analyzePackageModuleType(pkgJson: PackageJson): PackageModuleTyp

// Legacy detection (no exports or exports without clear conditions)
if (hasModule && hasMain) {
const mainIsCJS = pkgJson.main?.endsWith('.cjs')
|| (pkgJson.main?.endsWith('.js') && !isTypeModule)
const mainIsCJS = main?.endsWith('.cjs')
|| (main?.endsWith('.js') && !isTypeModule)
return mainIsCJS ? 'faux' : 'esm'
}

if (hasModule)
return 'faux'

if (isTypeModule || (hasMain && pkgJson.main?.endsWith('.mjs')))
if (isTypeModule || (hasMain && main?.endsWith('.mjs')))
return 'esm'

if (hasMain)
Expand Down
Loading