@@ -70,37 +70,46 @@ function isEsmId(id_, parentId_) {
70
70
}
71
71
const parentId = parentId_ ? resolveId ( parentId_ ) : undefined
72
72
const resolvedId = resolveId ( id_ , parentId )
73
- let result = false
74
73
if ( resolvedId . endsWith ( '.mjs' ) ) {
75
- result = true
76
- } else if (
77
- ! resolvedId . endsWith ( '.cjs' ) &&
78
- ! resolvedId . endsWith ( '.json' ) &&
79
- ! resolvedId . endsWith ( '.ts' )
74
+ return true
75
+ }
76
+ if (
77
+ resolvedId . endsWith ( '.cjs' ) ||
78
+ resolvedId . endsWith ( '.json' ) ||
79
+ resolvedId . endsWith ( '.ts' )
80
80
) {
81
- let filepath
82
- if ( path . isAbsolute ( resolvedId ) ) {
83
- filepath = resolvedId
84
- } else if ( parentId && isRelative ( resolvedId ) ) {
85
- filepath = path . join ( path . dirname ( parentId ) , resolvedId )
86
- }
87
- if ( filepath ) {
88
- const pkgJsonPath = findUpSync ( 'package.json' , {
89
- cwd : path . dirname ( resolvedId )
90
- } )
91
- if ( pkgJsonPath && require ( pkgJsonPath ) ?. type === 'module' ) {
81
+ return false
82
+ }
83
+ let filepath
84
+ if ( path . isAbsolute ( resolvedId ) ) {
85
+ filepath = resolvedId
86
+ } else if ( parentId && isRelative ( resolvedId ) ) {
87
+ filepath = path . join ( path . dirname ( parentId ) , resolvedId )
88
+ }
89
+ if ( filepath ) {
90
+ const pkgJsonPath = findUpSync ( 'package.json' , {
91
+ cwd : path . dirname ( resolvedId )
92
+ } )
93
+ if ( pkgJsonPath ) {
94
+ const pkgJson = require ( pkgJsonPath )
95
+ const { exports : entryExports } = pkgJson
96
+ if (
97
+ pkgJson . type === 'module' &&
98
+ ! entryExports ?. require &&
99
+ ! entryExports ?. node ?. default ?. endsWith ( '.cjs' )
100
+ ) {
92
101
return true
93
102
}
94
- try {
95
- new vm . Script ( fs . readFileSync ( resolvedId , 'utf8' ) )
96
- } catch ( e ) {
97
- if ( e instanceof SyntaxError ) {
98
- result = true
99
- }
103
+ }
104
+ try {
105
+ new vm . Script ( fs . readFileSync ( resolvedId , 'utf8' ) )
106
+ } catch ( e ) {
107
+ if ( e instanceof SyntaxError ) {
108
+ return true
100
109
}
101
110
}
102
111
}
103
- return result
112
+ return false
104
113
}
105
114
106
115
function normalizeId ( id ) {
0 commit comments