@@ -84,6 +84,7 @@ const manifest = getOptionValue('--experimental-policy') ?
84
84
require ( 'internal/process/policy' ) . manifest :
85
85
null ;
86
86
const { compileFunction } = internalBinding ( 'contextify' ) ;
87
+ const userConditions = getOptionValue ( '--conditions' ) ;
87
88
88
89
// Whether any user-provided CJS modules had been loaded (executed).
89
90
// Used for internal assertions.
@@ -477,8 +478,12 @@ function applyExports(basePath, expansion) {
477
478
if ( typeof pkgExports === 'object' ) {
478
479
if ( ObjectPrototypeHasOwnProperty ( pkgExports , mappingKey ) ) {
479
480
const mapping = pkgExports [ mappingKey ] ;
480
- return resolveExportsTarget ( pathToFileURL ( basePath + '/' ) , mapping , '' ,
481
- mappingKey ) ;
481
+ const resolved = resolveExportsTarget (
482
+ pathToFileURL ( basePath + '/' ) , mapping , '' , mappingKey ) ;
483
+ if ( resolved === null || resolved === undefined )
484
+ throw new ERR_PACKAGE_PATH_NOT_EXPORTED (
485
+ basePath , mappingKey ) ;
486
+ return resolved ;
482
487
}
483
488
484
489
let dirMatch = '' ;
@@ -495,6 +500,9 @@ function applyExports(basePath, expansion) {
495
500
const subpath = StringPrototypeSlice ( mappingKey , dirMatch . length ) ;
496
501
const resolved = resolveExportsTarget ( pathToFileURL ( basePath + '/' ) ,
497
502
mapping , subpath , mappingKey ) ;
503
+ if ( resolved === null || resolved === undefined )
504
+ throw new ERR_PACKAGE_PATH_NOT_EXPORTED (
505
+ basePath , mappingKey + subpath ) ;
498
506
// Extension searching for folder exports only
499
507
const rc = stat ( resolved ) ;
500
508
if ( rc === 0 ) return resolved ;
@@ -582,21 +590,29 @@ function resolveExportsTarget(baseUrl, target, subpath, mappingKey) {
582
590
throw new ERR_INVALID_MODULE_SPECIFIER ( mappingKey + subpath , reason ) ;
583
591
} else if ( ArrayIsArray ( target ) ) {
584
592
if ( target . length === 0 )
585
- throw new ERR_PACKAGE_PATH_NOT_EXPORTED (
586
- baseUrl . pathname , mappingKey + subpath ) ;
593
+ return null ;
587
594
let lastException ;
588
595
for ( const targetValue of target ) {
596
+ let resolved ;
589
597
try {
590
- return resolveExportsTarget ( baseUrl , targetValue , subpath , mappingKey ) ;
598
+ resolved = resolveExportsTarget ( baseUrl , targetValue , subpath ,
599
+ mappingKey ) ;
591
600
} catch ( e ) {
592
601
lastException = e ;
593
- if ( e . code !== 'ERR_PACKAGE_PATH_NOT_EXPORTED' &&
594
- e . code !== 'ERR_INVALID_PACKAGE_TARGET' )
602
+ if ( e . code !== 'ERR_INVALID_PACKAGE_TARGET' )
595
603
throw e ;
596
604
}
605
+ if ( resolved === undefined )
606
+ continue ;
607
+ if ( resolved === null ) {
608
+ lastException = null ;
609
+ continue ;
610
+ }
611
+ return resolved ;
597
612
}
598
613
// Throw last fallback error
599
- assert ( lastException !== undefined ) ;
614
+ if ( lastException === undefined || lastException === null )
615
+ return lastException ;
600
616
throw lastException ;
601
617
} else if ( typeof target === 'object' && target !== null ) {
602
618
const keys = ObjectKeys ( target ) ;
@@ -605,30 +621,17 @@ function resolveExportsTarget(baseUrl, target, subpath, mappingKey) {
605
621
'contain numeric property keys.' ) ;
606
622
}
607
623
for ( const p of keys ) {
608
- switch ( p ) {
609
- case 'node' :
610
- case 'require' :
611
- try {
612
- return resolveExportsTarget ( baseUrl , target [ p ] , subpath ,
613
- mappingKey ) ;
614
- } catch ( e ) {
615
- if ( e . code !== 'ERR_PACKAGE_PATH_NOT_EXPORTED' ) throw e ;
616
- }
617
- break ;
618
- case 'default' :
619
- try {
620
- return resolveExportsTarget ( baseUrl , target . default , subpath ,
621
- mappingKey ) ;
622
- } catch ( e ) {
623
- if ( e . code !== 'ERR_PACKAGE_PATH_NOT_EXPORTED' ) throw e ;
624
- }
624
+ if ( cjsConditions . has ( p ) || p === 'default' ) {
625
+ const resolved = resolveExportsTarget ( baseUrl , target [ p ] , subpath ,
626
+ mappingKey ) ;
627
+ if ( resolved === undefined )
628
+ continue ;
629
+ return resolved ;
625
630
}
626
631
}
627
- throw new ERR_PACKAGE_PATH_NOT_EXPORTED (
628
- baseUrl . pathname , mappingKey + subpath ) ;
632
+ return undefined ;
629
633
} else if ( target === null ) {
630
- throw new ERR_PACKAGE_PATH_NOT_EXPORTED (
631
- baseUrl . pathname , mappingKey + subpath ) ;
634
+ return null ;
632
635
}
633
636
throw new ERR_INVALID_PACKAGE_TARGET ( baseUrl . pathname , mappingKey , target ) ;
634
637
}
@@ -985,8 +988,7 @@ Module._load = function(request, parent, isMain) {
985
988
return module . exports ;
986
989
} ;
987
990
988
- // TODO: Use this set when resolving pkg#exports conditions.
989
- const cjsConditions = new SafeSet ( [ 'require' , 'node' ] ) ;
991
+ const cjsConditions = new SafeSet ( [ 'require' , 'node' , ...userConditions ] ) ;
990
992
Module . _resolveFilename = function ( request , parent , isMain , options ) {
991
993
if ( NativeModule . canBeRequiredByUsers ( request ) ) {
992
994
return request ;
0 commit comments