22
33const fs = require ( 'node:fs' )
44const Module = require ( 'node:module' )
5+ const path = require ( 'node:path' )
56const vm = require ( 'node:vm' )
67
78const normalizePackageData = require ( 'normalize-package-data' )
89const validatePackageName = require ( 'validate-npm-package-name' )
910
10- const { normalizePath } = require ( './path' )
11+ const { normalizePath, isRelative } = require ( './path' )
12+ const { findUpSync } = require ( './fs' )
1113
1214const { createRequire, isBuiltin } = Module
1315
16+ const PACKAGE_JSON = 'package.json'
17+
1418const cjsPluginPrefixRegExp = / ^ \x00 /
1519const cjsPluginSuffixRegExp =
1620 / \? c o m m o n j s - (?: e n t r y | e s - i m p o r t | e x p o r t s | e x t e r n a l | m o d u l e | p r o x y | w r a p p e d ) $ /
@@ -38,12 +42,12 @@ function resolveId(id_, req = require) {
3842 }
3943 if ( req !== require ) {
4044 try {
41- resolvedId = req . resolve ( id )
45+ resolvedId = normalizePath ( req . resolve ( id ) )
4246 } catch { }
4347 }
4448 if ( resolvedId === undefined ) {
4549 try {
46- resolvedId = require . resolve ( id )
50+ resolvedId = normalizePath ( require . resolve ( id ) )
4751 } catch { }
4852 }
4953 if ( resolvedId === undefined ) {
@@ -56,27 +60,16 @@ function resolveId(id_, req = require) {
5660 return fs . existsSync ( tsId ) ? tsId : resolvedId
5761}
5862
59- const memoizeIsPackageName = new Map ( )
60-
6163function isPackageName ( id ) {
62- const memResult = memoizeIsPackageName . get ( id )
63- if ( memResult !== undefined ) return memResult
64- const result = validatePackageName ( id ) . validForOldPackages
65- memoizeIsPackageName . set ( id , result )
66- return result
64+ return validatePackageName ( id ) . validForOldPackages
6765}
6866
69- const memoizeIsEsmId = new Map ( )
70-
7167function isEsmId ( id_ , parentId_ ) {
7268 if ( isBuiltin ( id_ ) ) {
7369 return false
7470 }
7571 const parentId = parentId_ ? resolveId ( parentId_ ) : undefined
7672 const resolvedId = resolveId ( id_ , parentId )
77- const memKey = `${ resolvedId } |${ parentId } `
78- const memResult = memoizeIsEsmId . get ( memKey )
79- if ( memResult !== undefined ) return memResult
8073 let result = false
8174 if ( resolvedId . endsWith ( '.mjs' ) ) {
8275 result = true
@@ -85,15 +78,28 @@ function isEsmId(id_, parentId_) {
8578 ! resolvedId . endsWith ( '.json' ) &&
8679 ! resolvedId . endsWith ( '.ts' )
8780 ) {
88- try {
89- new vm . Script ( fs . readFileSync ( resolvedId , 'utf8' ) )
90- } catch ( e ) {
91- if ( e instanceof SyntaxError ) {
92- result = true
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' ) {
92+ return true
93+ }
94+ try {
95+ new vm . Script ( fs . readFileSync ( resolvedId , 'utf8' ) )
96+ } catch ( e ) {
97+ if ( e instanceof SyntaxError ) {
98+ result = true
99+ }
93100 }
94101 }
95102 }
96- memoizeIsEsmId . set ( memKey , result )
97103 return result
98104}
99105
@@ -108,6 +114,13 @@ function normalizePackageJson(pkgJson) {
108114 return pkgJson
109115}
110116
117+ function readPackageJsonSync ( filepath_ ) {
118+ const filepath = filepath_ . endsWith ( PACKAGE_JSON )
119+ ? filepath_
120+ : path . join ( filepath_ , PACKAGE_JSON )
121+ return normalizePackageJson ( JSON . parse ( fs . readFileSync ( filepath , 'utf8' ) ) )
122+ }
123+
111124module . exports = {
112125 isBuiltin,
113126 isEsmId,
@@ -116,5 +129,6 @@ module.exports = {
116129 getPackageNameEnd,
117130 normalizeId,
118131 normalizePackageJson,
132+ readPackageJsonSync,
119133 resolveId
120134}
0 commit comments