@@ -38,7 +38,7 @@ const {
38
38
forceDefaultLoader,
39
39
} = require ( 'internal/modules/esm/utils' ) ;
40
40
const { kImplicitTypeAttribute } = require ( 'internal/modules/esm/assert' ) ;
41
- const { ModuleWrap, kEvaluating, kEvaluated } = internalBinding ( 'module_wrap' ) ;
41
+ const { ModuleWrap, kEvaluating, kEvaluated, kEvaluationPhase , kSourcePhase } = internalBinding ( 'module_wrap' ) ;
42
42
const {
43
43
urlToFilename,
44
44
} = require ( 'internal/modules/helpers' ) ;
@@ -236,8 +236,7 @@ class ModuleLoader {
236
236
async executeModuleJob ( url , wrap , isEntryPoint = false ) {
237
237
const { ModuleJob } = require ( 'internal/modules/esm/module_job' ) ;
238
238
const module = await onImport . tracePromise ( async ( ) => {
239
- const job = new ModuleJob (
240
- this , url , undefined , wrap , false , false ) ;
239
+ const job = new ModuleJob ( this , url , undefined , wrap , kEvaluationPhase , false , false ) ;
241
240
this . loadCache . set ( url , undefined , job ) ;
242
241
const { module } = await job . run ( isEntryPoint ) ;
243
242
return module ;
@@ -273,11 +272,12 @@ class ModuleLoader {
273
272
* @param {string } [parentURL] The URL of the module where the module request is initiated.
274
273
* It's undefined if it's from the root module.
275
274
* @param {ImportAttributes } importAttributes Attributes from the import statement or expression.
275
+ * @param {number } phase Import phase.
276
276
* @returns {Promise<ModuleJobBase> }
277
277
*/
278
- async getModuleJobForImport ( specifier , parentURL , importAttributes ) {
278
+ async getModuleJobForImport ( specifier , parentURL , importAttributes , phase ) {
279
279
const resolveResult = await this . resolve ( specifier , parentURL , importAttributes ) ;
280
- return this . #getJobFromResolveResult( resolveResult , parentURL , importAttributes , false ) ;
280
+ return this . #getJobFromResolveResult( resolveResult , parentURL , importAttributes , phase , false ) ;
281
281
}
282
282
283
283
/**
@@ -287,11 +287,12 @@ class ModuleLoader {
287
287
* @param {string } specifier See {@link getModuleJobForImport}
288
288
* @param {string } [parentURL] See {@link getModuleJobForImport}
289
289
* @param {ImportAttributes } importAttributes See {@link getModuleJobForImport}
290
+ * @param {number } phase Import phase.
290
291
* @returns {Promise<ModuleJobBase> }
291
292
*/
292
- getModuleJobForRequireInImportedCJS ( specifier , parentURL , importAttributes ) {
293
+ getModuleJobForRequireInImportedCJS ( specifier , parentURL , importAttributes , phase ) {
293
294
const resolveResult = this . resolveSync ( specifier , parentURL , importAttributes ) ;
294
- return this . #getJobFromResolveResult( resolveResult , parentURL , importAttributes , true ) ;
295
+ return this . #getJobFromResolveResult( resolveResult , parentURL , importAttributes , phase , true ) ;
295
296
}
296
297
297
298
/**
@@ -300,16 +301,21 @@ class ModuleLoader {
300
301
* @param {{ format: string, url: string } } resolveResult Resolved module request.
301
302
* @param {string } [parentURL] See {@link getModuleJobForImport}
302
303
* @param {ImportAttributes } importAttributes See {@link getModuleJobForImport}
304
+ * @param {number } phase Import phase.
303
305
* @param {boolean } isForRequireInImportedCJS Whether this is done for require() in imported CJS.
304
306
* @returns {ModuleJobBase }
305
307
*/
306
- #getJobFromResolveResult( resolveResult , parentURL , importAttributes , isForRequireInImportedCJS = false ) {
308
+ #getJobFromResolveResult( resolveResult , parentURL , importAttributes , phase ,
309
+ isForRequireInImportedCJS = false ) {
307
310
const { url, format } = resolveResult ;
308
311
const resolvedImportAttributes = resolveResult . importAttributes ?? importAttributes ;
309
312
let job = this . loadCache . get ( url , resolvedImportAttributes . type ) ;
310
313
311
314
if ( job === undefined ) {
312
- job = this . #createModuleJob( url , resolvedImportAttributes , parentURL , format , isForRequireInImportedCJS ) ;
315
+ job = this . #createModuleJob( url , resolvedImportAttributes , phase , parentURL , format ,
316
+ isForRequireInImportedCJS ) ;
317
+ } else {
318
+ job . ensurePhase ( phase ) ;
313
319
}
314
320
315
321
return job ;
@@ -377,7 +383,7 @@ class ModuleLoader {
377
383
const inspectBrk = ( isMain && getOptionValue ( '--inspect-brk' ) ) ;
378
384
379
385
const { ModuleJobSync } = require ( 'internal/modules/esm/module_job' ) ;
380
- job = new ModuleJobSync ( this , url , kEmptyObject , wrap , isMain , inspectBrk ) ;
386
+ job = new ModuleJobSync ( this , url , kEmptyObject , wrap , kEvaluationPhase , isMain , inspectBrk ) ;
381
387
this . loadCache . set ( url , kImplicitTypeAttribute , job ) ;
382
388
mod [ kRequiredModuleSymbol ] = job . module ;
383
389
return { wrap : job . module , namespace : job . runSync ( parent ) . namespace } ;
@@ -389,9 +395,10 @@ class ModuleLoader {
389
395
* @param {string } specifier Specifier of the the imported module.
390
396
* @param {string } parentURL Where the import comes from.
391
397
* @param {object } importAttributes import attributes from the import statement.
398
+ * @param {number } phase The import phase.
392
399
* @returns {ModuleJobBase }
393
400
*/
394
- getModuleJobForRequire ( specifier , parentURL , importAttributes ) {
401
+ getModuleJobForRequire ( specifier , parentURL , importAttributes , phase ) {
395
402
const parsed = URLParse ( specifier ) ;
396
403
if ( parsed != null ) {
397
404
const protocol = parsed . protocol ;
@@ -422,6 +429,7 @@ class ModuleLoader {
422
429
}
423
430
throw new ERR_REQUIRE_CYCLE_MODULE ( message ) ;
424
431
}
432
+ job . ensurePhase ( phase ) ;
425
433
// Otherwise the module could be imported before but the evaluation may be already
426
434
// completed (e.g. the require call is lazy) so it's okay. We will return the
427
435
// module now and check asynchronicity of the entire graph later, after the
@@ -463,7 +471,7 @@ class ModuleLoader {
463
471
464
472
const inspectBrk = ( isMain && getOptionValue ( '--inspect-brk' ) ) ;
465
473
const { ModuleJobSync } = require ( 'internal/modules/esm/module_job' ) ;
466
- job = new ModuleJobSync ( this , url , importAttributes , wrap , isMain , inspectBrk ) ;
474
+ job = new ModuleJobSync ( this , url , importAttributes , wrap , phase , isMain , inspectBrk ) ;
467
475
468
476
this . loadCache . set ( url , importAttributes . type , job ) ;
469
477
return job ;
@@ -543,13 +551,14 @@ class ModuleLoader {
543
551
* by the time this returns. Otherwise it may still have pending module requests.
544
552
* @param {string } url The URL that was resolved for this module.
545
553
* @param {ImportAttributes } importAttributes See {@link getModuleJobForImport}
554
+ * @param {number } phase Import phase.
546
555
* @param {string } [parentURL] See {@link getModuleJobForImport}
547
556
* @param {string } [format] The format hint possibly returned by the `resolve` hook
548
557
* @param {boolean } isForRequireInImportedCJS Whether this module job is created for require()
549
558
* in imported CJS.
550
559
* @returns {ModuleJobBase } The (possibly pending) module job
551
560
*/
552
- #createModuleJob( url , importAttributes , parentURL , format , isForRequireInImportedCJS ) {
561
+ #createModuleJob( url , importAttributes , phase , parentURL , format , isForRequireInImportedCJS ) {
553
562
const context = { format, importAttributes } ;
554
563
555
564
const isMain = parentURL === undefined ;
@@ -575,6 +584,7 @@ class ModuleLoader {
575
584
url ,
576
585
importAttributes ,
577
586
moduleOrModulePromise ,
587
+ phase ,
578
588
isMain ,
579
589
inspectBrk ,
580
590
isForRequireInImportedCJS ,
@@ -592,11 +602,18 @@ class ModuleLoader {
592
602
* @param {string } parentURL Path of the parent importing the module.
593
603
* @param {Record<string, string> } importAttributes Validations for the
594
604
* module import.
605
+ * @param {number } [phase] The phase of the import.
606
+ * @param {boolean } [isEntryPoint] Whether this is the realm-level entry point.
595
607
* @returns {Promise<ModuleExports> }
596
608
*/
597
- async import ( specifier , parentURL , importAttributes , isEntryPoint = false ) {
609
+ async import ( specifier , parentURL , importAttributes , phase = kEvaluationPhase , isEntryPoint = false ) {
598
610
return onImport . tracePromise ( async ( ) => {
599
- const moduleJob = await this . getModuleJobForImport ( specifier , parentURL , importAttributes ) ;
611
+ const moduleJob = await this . getModuleJobForImport ( specifier , parentURL , importAttributes ,
612
+ phase ) ;
613
+ if ( phase === kSourcePhase ) {
614
+ const module = await moduleJob . modulePromise ;
615
+ return module . getModuleSourceObject ( ) ;
616
+ }
600
617
const { module } = await moduleJob . run ( isEntryPoint ) ;
601
618
return module . getNamespace ( ) ;
602
619
} , {
0 commit comments