[READ-ONLY] a fast, modern browser for the npm registry

fix: detect esm only pkg w/ main and module fields (#1494)

authored by

Jon Church and committed by
GitHub
d7de1e77 5d63406f

+14 -2
+4 -2
shared/utils/package-analysis.ts
··· 82 82 83 83 // Legacy detection without exports field 84 84 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' 87 89 } 88 90 89 91 if (hasModule || isTypeModule) {
+10
test/unit/shared/utils/package-analysis.spec.ts
··· 26 26 expect(detectModuleFormat({ module: 'index.mjs', main: 'index.js' })).toBe('dual') 27 27 }) 28 28 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 + 29 39 it('detects ESM from module field without main', () => { 30 40 expect(detectModuleFormat({ module: 'index.mjs' })).toBe('esm') 31 41 })