@@ -48,6 +48,8 @@ const experimentalImportMetaResolve =
48
48
const translators = new SafeMap ( ) ;
49
49
exports . translators = translators ;
50
50
51
+ const asyncESM = require ( 'internal/process/esm_loader' ) ;
52
+
51
53
let DECODER = null ;
52
54
function assertBufferSource ( body , allowString , hookName ) {
53
55
if ( allowString && typeof body === 'string' ) {
@@ -80,21 +82,14 @@ function errPath(url) {
80
82
return url ;
81
83
}
82
84
83
- let esmLoader ;
84
85
async 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 ) ;
89
87
}
90
88
91
89
function createImportMetaResolve ( defaultParentUrl ) {
92
90
return async function resolve ( specifier , parentUrl = defaultParentUrl ) {
93
- if ( ! esmLoader ) {
94
- esmLoader = require ( 'internal/process/esm_loader' ) . ESMLoader ;
95
- }
96
91
return PromisePrototypeCatch (
97
- esmLoader . resolve ( specifier , parentUrl ) ,
92
+ asyncESM . ESMLoader . resolve ( specifier , parentUrl ) ,
98
93
( error ) => (
99
94
error . code === 'ERR_UNSUPPORTED_DIR_IMPORT' ?
100
95
error . url : PromiseReject ( error ) )
@@ -132,27 +127,18 @@ const isWindows = process.platform === 'win32';
132
127
const winSepRegEx = / \/ / g;
133
128
translators . set ( 'commonjs' , function commonjsStrategy ( url , isMain ) {
134
129
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
- }
150
130
return new ModuleWrap ( url , undefined , [ 'default' ] , function ( ) {
151
131
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 ) ;
156
142
} ) ;
157
143
} ) ;
158
144
0 commit comments