@@ -41,7 +41,7 @@ const { fileURLToPath, pathToFileURL, URL } = require('internal/url');
41
41
let debug = require ( 'internal/util/debuglog' ) . debuglog ( 'esm' , ( fn ) => {
42
42
debug = fn ;
43
43
} ) ;
44
- const { emitExperimentalWarning, kEmptyObject } = require ( 'internal/util' ) ;
44
+ const { emitExperimentalWarning, kEmptyObject, setOwnProperty } = require ( 'internal/util' ) ;
45
45
const {
46
46
ERR_UNKNOWN_BUILTIN_MODULE ,
47
47
ERR_INVALID_RETURN_PROPERTY_VALUE ,
@@ -147,7 +147,7 @@ const cjsCache = new SafeMap();
147
147
function createCJSModuleWrap ( url , source ) {
148
148
debug ( `Translating CJSModule ${ url } ` ) ;
149
149
150
- const filename = fileURLToPath ( new URL ( url ) ) ;
150
+ const filename = StringPrototypeStartsWith ( url , 'file://' ) ? fileURLToPath ( url ) : url ;
151
151
152
152
const { exportNames, module } = cjsPreparseModuleExports ( filename , source ) ;
153
153
const namesWithDefault = exportNames . has ( 'default' ) ?
@@ -173,6 +173,16 @@ function createCJSModuleWrap(url, source) {
173
173
job . runSync ( ) ;
174
174
return cjsCache . get ( job . url ) . exports ;
175
175
} ;
176
+ setOwnProperty ( require , 'resolve' , function resolve ( spec ) {
177
+ // TODO: add support for absolute paths
178
+ if ( spec [ 0 ] === '.' ) {
179
+ spec = pathToFileURL ( join ( __dirname , spec ) ) ;
180
+ }
181
+ const { url : resolvedURL } = asyncESM . esmLoader . resolve ( spec , url ) ;
182
+ return StringPrototypeStartsWith ( resolvedURL , 'file://' ) ? fileURLToPath ( resolvedURL ) : resolvedURL ;
183
+ } ) ;
184
+ setOwnProperty ( require , 'cache' , CJSModule . _cache ) ;
185
+ setOwnProperty ( require , 'main' , process . mainModule ) ;
176
186
177
187
cjsCache . set ( url , module ) ;
178
188
ReflectApply ( compiledWrapper , module . exports ,
@@ -211,6 +221,7 @@ translators.set('commonjs', async function commonjsStrategy(url, source,
211
221
} ) ;
212
222
213
223
function cjsPreparseModuleExports ( filename , source ) {
224
+ // TODO: Do we want to keep hitting the user mutable CJS loader here?
214
225
let module = CJSModule . _cache [ filename ] ;
215
226
if ( module ) {
216
227
const cached = cjsParseCache . get ( module ) ;
0 commit comments