@@ -3,6 +3,7 @@ import path from 'node:path'
33import colors from 'picocolors'
44import type { PartialResolvedId } from 'rollup'
55import { resolve as _resolveExports } from 'resolve.exports'
6+ import { hasESMSyntax } from 'mlly'
67import type { Plugin } from '../plugin'
78import {
89 DEFAULT_EXTENSIONS ,
@@ -850,25 +851,22 @@ export function resolvePackageEntry(
850851 ) {
851852 // if both are present, we may have a problem: some package points both
852853 // to ESM, with "module" targeting Node.js, while some packages points
853- // "module" to browser ESM and "browser" to UMD.
854+ // "module" to browser ESM and "browser" to UMD/IIFE .
854855 // the heuristics here is to actually read the browser entry when
855- // possible and check for hints of UMD . If it is UMD , prefer "module"
856+ // possible and check for hints of ESM . If it is not ESM , prefer "module"
856857 // instead; Otherwise, assume it's ESM and use it.
857858 const resolvedBrowserEntry = tryFsResolve (
858859 path . join ( dir , browserEntry ) ,
859860 options
860861 )
861862 if ( resolvedBrowserEntry ) {
862863 const content = fs . readFileSync ( resolvedBrowserEntry , 'utf-8' )
863- if (
864- ( / t y p e o f e x p o r t s \s * = = / . test ( content ) &&
865- / t y p e o f m o d u l e \s * = = / . test ( content ) ) ||
866- / m o d u l e \. e x p o r t s \s * = / . test ( content )
867- ) {
868- // likely UMD or CJS(!!! e.g. firebase 7.x), prefer module
869- entryPoint = data . module
870- } else {
864+ if ( hasESMSyntax ( content ) ) {
865+ // likely ESM, prefer browser
871866 entryPoint = browserEntry
867+ } else {
868+ // non-ESM, UMD or IIFE or CJS(!!! e.g. firebase 7.x), prefer module
869+ entryPoint = data . module
872870 }
873871 }
874872 } else {
0 commit comments