@@ -48,6 +48,8 @@ const experimentalImportMetaResolve =
4848const translators = new SafeMap ( ) ;
4949exports . translators = translators ;
5050
51+ const asyncESM = require ( 'internal/process/esm_loader' ) ;
52+
5153let DECODER = null ;
5254function assertBufferSource ( body , allowString , hookName ) {
5355 if ( allowString && typeof body === 'string' ) {
@@ -80,21 +82,14 @@ function errPath(url) {
8082 return url ;
8183}
8284
83- let esmLoader ;
8485async function importModuleDynamically ( specifier , { url } ) {
85- if ( ! esmLoader ) {
86- esmLoader = require ( 'internal/process/esm_loader' ) . ESMLoader ;
87- }
88- return esmLoader . import ( specifier , url ) ;
86+ return asyncESM . ESMLoader . import ( specifier , url ) ;
8987}
9088
9189function createImportMetaResolve ( defaultParentUrl ) {
9290 return async function resolve ( specifier , parentUrl = defaultParentUrl ) {
93- if ( ! esmLoader ) {
94- esmLoader = require ( 'internal/process/esm_loader' ) . ESMLoader ;
95- }
9691 return PromisePrototypeCatch (
97- esmLoader . resolve ( specifier , parentUrl ) ,
92+ asyncESM . ESMLoader . resolve ( specifier , parentUrl ) ,
9893 ( error ) => (
9994 error . code === 'ERR_UNSUPPORTED_DIR_IMPORT' ?
10095 error . url : PromiseReject ( error ) )
@@ -132,27 +127,18 @@ const isWindows = process.platform === 'win32';
132127const winSepRegEx = / \/ / g;
133128translators . set ( 'commonjs' , function commonjsStrategy ( url , isMain ) {
134129 debug ( `Translating CJSModule ${ url } ` ) ;
135- const pathname = internalURLModule . fileURLToPath ( new URL ( url ) ) ;
136- const cached = this . cjsCache . get ( url ) ;
137- if ( cached ) {
138- this . cjsCache . delete ( url ) ;
139- return cached ;
140- }
141- const module = CJSModule . _cache [
142- isWindows ? StringPrototypeReplace ( pathname , winSepRegEx , '\\' ) : pathname
143- ] ;
144- if ( module && module . loaded ) {
145- const exports = module . exports ;
146- return new ModuleWrap ( url , undefined , [ 'default' ] , function ( ) {
147- this . setExport ( 'default' , exports ) ;
148- } ) ;
149- }
150130 return new ModuleWrap ( url , undefined , [ 'default' ] , function ( ) {
151131 debug ( `Loading CJSModule ${ url } ` ) ;
152- // We don't care about the return val of _load here because Module#load
153- // will handle it for us by checking the loader registry and filling the
154- // exports like above
155- CJSModule . _load ( pathname , undefined , isMain ) ;
132+ const pathname = internalURLModule . fileURLToPath ( new URL ( url ) ) ;
133+ let exports ;
134+ const cachedModule = CJSModule . _cache [ pathname ] ;
135+ if ( cachedModule && asyncESM . ESMLoader . cjsCache . has ( cachedModule ) ) {
136+ exports = asyncESM . ESMLoader . cjsCache . get ( cachedModule ) ;
137+ asyncESM . ESMLoader . cjsCache . delete ( cachedModule ) ;
138+ } else {
139+ exports = CJSModule . _load ( pathname , undefined , isMain ) ;
140+ }
141+ this . setExport ( 'default' , exports ) ;
156142 } ) ;
157143} ) ;
158144
0 commit comments