@@ -3,6 +3,7 @@ import path from 'node:path'
3
3
import colors from 'picocolors'
4
4
import type { PartialResolvedId } from 'rollup'
5
5
import { resolve as _resolveExports } from 'resolve.exports'
6
+ import { hasESMSyntax } from 'mlly'
6
7
import type { Plugin } from '../plugin'
7
8
import {
8
9
DEFAULT_EXTENSIONS ,
@@ -850,25 +851,22 @@ export function resolvePackageEntry(
850
851
) {
851
852
// if both are present, we may have a problem: some package points both
852
853
// 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 .
854
855
// 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"
856
857
// instead; Otherwise, assume it's ESM and use it.
857
858
const resolvedBrowserEntry = tryFsResolve (
858
859
path . join ( dir , browserEntry ) ,
859
860
options
860
861
)
861
862
if ( resolvedBrowserEntry ) {
862
863
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
871
866
entryPoint = browserEntry
867
+ } else {
868
+ // non-ESM, UMD or IIFE or CJS(!!! e.g. firebase 7.x), prefer module
869
+ entryPoint = data . module
872
870
}
873
871
}
874
872
} else {
0 commit comments