@@ -19,6 +19,9 @@ const {
19
19
StringPrototypeStartsWith,
20
20
globalThis,
21
21
} = primordials ;
22
+ let debug = require ( 'internal/util/debuglog' ) . debuglog ( 'esm' , ( fn ) => {
23
+ debug = fn ;
24
+ } ) ;
22
25
23
26
const { ModuleWrap, kEvaluated } = internalBinding ( 'module_wrap' ) ;
24
27
const {
@@ -53,8 +56,7 @@ const isCommonJSGlobalLikeNotDefinedError = (errorMessage) =>
53
56
) ;
54
57
55
58
class ModuleJobBase {
56
- constructor ( loader , url , importAttributes , moduleWrapMaybePromise , isMain , inspectBrk ) {
57
- this . loader = loader ;
59
+ constructor ( url , importAttributes , moduleWrapMaybePromise , isMain , inspectBrk ) {
58
60
this . importAttributes = importAttributes ;
59
61
this . isMain = isMain ;
60
62
this . inspectBrk = inspectBrk ;
@@ -67,11 +69,13 @@ class ModuleJobBase {
67
69
/* A ModuleJob tracks the loading of a single Module, and the ModuleJobs of
68
70
* its dependencies, over time. */
69
71
class ModuleJob extends ModuleJobBase {
72
+ #loader = null ;
70
73
// `loader` is the Loader instance used for loading dependencies.
71
74
constructor ( loader , url , importAttributes = { __proto__ : null } ,
72
75
moduleProvider , isMain , inspectBrk , sync = false ) {
73
76
const modulePromise = ReflectApply ( moduleProvider , loader , [ url , isMain ] ) ;
74
- super ( loader , url , importAttributes , modulePromise , isMain , inspectBrk ) ;
77
+ super ( url , importAttributes , modulePromise , isMain , inspectBrk ) ;
78
+ this . #loader = loader ;
75
79
// Expose the promise to the ModuleWrap directly for linking below.
76
80
// `this.module` is also filled in below.
77
81
this . modulePromise = modulePromise ;
@@ -94,7 +98,8 @@ class ModuleJob extends ModuleJobBase {
94
98
// these `link` callbacks depending on each other.
95
99
const dependencyJobs = [ ] ;
96
100
const promises = this . module . link ( async ( specifier , attributes ) => {
97
- const job = await this . loader . getModuleJob ( specifier , url , attributes ) ;
101
+ const job = await this . #loader. getModuleJob ( specifier , url , attributes ) ;
102
+ debug ( `async link() ${ this . url } -> ${ specifier } ` , job ) ;
98
103
ArrayPrototypePush ( dependencyJobs , job ) ;
99
104
return job . modulePromise ;
100
105
} ) ;
@@ -126,6 +131,8 @@ class ModuleJob extends ModuleJobBase {
126
131
async _instantiate ( ) {
127
132
const jobsInGraph = new SafeSet ( ) ;
128
133
const addJobsToDependencyGraph = async ( moduleJob ) => {
134
+ debug ( `async addJobsToDependencyGraph() ${ this . url } ` , moduleJob ) ;
135
+
129
136
if ( jobsInGraph . has ( moduleJob ) ) {
130
137
return ;
131
138
}
@@ -161,7 +168,7 @@ class ModuleJob extends ModuleJobBase {
161
168
const { 1 : childSpecifier , 2 : name } = RegExpPrototypeExec (
162
169
/ m o d u l e ' ( .* ) ' d o e s n o t p r o v i d e a n e x p o r t n a m e d ' ( .+ ) ' / ,
163
170
e . message ) ;
164
- const { url : childFileURL } = await this . loader . resolve (
171
+ const { url : childFileURL } = await this . # loader. resolve (
165
172
childSpecifier ,
166
173
parentFileUrl ,
167
174
kEmptyObject ,
@@ -172,7 +179,7 @@ class ModuleJob extends ModuleJobBase {
172
179
// in the import attributes and some formats require them; but we only
173
180
// care about CommonJS for the purposes of this error message.
174
181
( { format } =
175
- await this . loader . load ( childFileURL ) ) ;
182
+ await this . # loader. load ( childFileURL ) ) ;
176
183
} catch {
177
184
// Continue regardless of error.
178
185
}
@@ -265,18 +272,27 @@ class ModuleJob extends ModuleJobBase {
265
272
// All the steps are ensured to be synchronous and it throws on instantiating
266
273
// an asynchronous graph.
267
274
class ModuleJobSync extends ModuleJobBase {
275
+ #loader = null ;
268
276
constructor ( loader , url , importAttributes , moduleWrap , isMain , inspectBrk ) {
269
- super ( loader , url , importAttributes , moduleWrap , isMain , inspectBrk , true ) ;
277
+ super ( url , importAttributes , moduleWrap , isMain , inspectBrk , true ) ;
270
278
assert ( this . module instanceof ModuleWrap ) ;
279
+ this . #loader = loader ;
271
280
const moduleRequests = this . module . getModuleRequestsSync ( ) ;
281
+ const linked = [ ] ;
272
282
for ( let i = 0 ; i < moduleRequests . length ; ++ i ) {
273
283
const { 0 : specifier , 1 : attributes } = moduleRequests [ i ] ;
274
- const wrap = this . loader . getModuleWrapForRequire ( specifier , url , attributes ) ;
284
+ const job = this . # loader. getModuleWrapForRequire ( specifier , url , attributes ) ;
275
285
const isLast = ( i === moduleRequests . length - 1 ) ;
276
286
// TODO(joyeecheung): make the resolution callback deal with both promisified
277
287
// an raw module wraps, then we don't need to wrap it with a promise here.
278
- this . module . cacheResolvedWrapsSync ( specifier , PromiseResolve ( wrap ) , isLast ) ;
288
+ this . module . cacheResolvedWrapsSync ( specifier , PromiseResolve ( job . module ) , isLast ) ;
289
+ ArrayPrototypePush ( linked , job ) ;
279
290
}
291
+ this . linked = linked ;
292
+ }
293
+
294
+ get modulePromise ( ) {
295
+ return PromiseResolve ( this . module ) ;
280
296
}
281
297
282
298
async run ( ) {
0 commit comments