@@ -80,6 +80,7 @@ const manifest = getOptionValue('--experimental-policy') ?
8080 require ( 'internal/process/policy' ) . manifest :
8181 null ;
8282const { compileFunction } = internalBinding ( 'contextify' ) ;
83+ const userConditions = getOptionValue ( '--conditions' ) ;
8384
8485// Whether any user-provided CJS modules had been loaded (executed).
8586// Used for internal assertions.
@@ -472,8 +473,12 @@ function applyExports(basePath, expansion) {
472473 if ( typeof pkgExports === 'object' ) {
473474 if ( ObjectPrototypeHasOwnProperty ( pkgExports , mappingKey ) ) {
474475 const mapping = pkgExports [ mappingKey ] ;
475- return resolveExportsTarget ( pathToFileURL ( basePath + '/' ) , mapping , '' ,
476- mappingKey ) ;
476+ const resolved = resolveExportsTarget (
477+ pathToFileURL ( basePath + '/' ) , mapping , '' , mappingKey ) ;
478+ if ( resolved === null || resolved === undefined )
479+ throw new ERR_PACKAGE_PATH_NOT_EXPORTED (
480+ basePath , mappingKey ) ;
481+ return resolved ;
477482 }
478483
479484 let dirMatch = '' ;
@@ -490,6 +495,9 @@ function applyExports(basePath, expansion) {
490495 const subpath = StringPrototypeSlice ( mappingKey , dirMatch . length ) ;
491496 const resolved = resolveExportsTarget ( pathToFileURL ( basePath + '/' ) ,
492497 mapping , subpath , mappingKey ) ;
498+ if ( resolved === null || resolved === undefined )
499+ throw new ERR_PACKAGE_PATH_NOT_EXPORTED (
500+ basePath , mappingKey + subpath ) ;
493501 // Extension searching for folder exports only
494502 const rc = stat ( resolved ) ;
495503 if ( rc === 0 ) return resolved ;
@@ -577,21 +585,29 @@ function resolveExportsTarget(baseUrl, target, subpath, mappingKey) {
577585 throw new ERR_INVALID_MODULE_SPECIFIER ( mappingKey + subpath , reason ) ;
578586 } else if ( ArrayIsArray ( target ) ) {
579587 if ( target . length === 0 )
580- throw new ERR_PACKAGE_PATH_NOT_EXPORTED (
581- baseUrl . pathname , mappingKey + subpath ) ;
588+ return null ;
582589 let lastException ;
583590 for ( const targetValue of target ) {
591+ let resolved ;
584592 try {
585- return resolveExportsTarget ( baseUrl , targetValue , subpath , mappingKey ) ;
593+ resolved = resolveExportsTarget ( baseUrl , targetValue , subpath ,
594+ mappingKey ) ;
586595 } catch ( e ) {
587596 lastException = e ;
588- if ( e . code !== 'ERR_PACKAGE_PATH_NOT_EXPORTED' &&
589- e . code !== 'ERR_INVALID_PACKAGE_TARGET' )
597+ if ( e . code !== 'ERR_INVALID_PACKAGE_TARGET' )
590598 throw e ;
591599 }
600+ if ( resolved === undefined )
601+ continue ;
602+ if ( resolved === null ) {
603+ lastException = null ;
604+ continue ;
605+ }
606+ return resolved ;
592607 }
593608 // Throw last fallback error
594- assert ( lastException !== undefined ) ;
609+ if ( lastException === undefined || lastException === null )
610+ return lastException ;
595611 throw lastException ;
596612 } else if ( typeof target === 'object' && target !== null ) {
597613 const keys = ObjectKeys ( target ) ;
@@ -600,30 +616,17 @@ function resolveExportsTarget(baseUrl, target, subpath, mappingKey) {
600616 'contain numeric property keys.' ) ;
601617 }
602618 for ( const p of keys ) {
603- switch ( p ) {
604- case 'node' :
605- case 'require' :
606- try {
607- return resolveExportsTarget ( baseUrl , target [ p ] , subpath ,
608- mappingKey ) ;
609- } catch ( e ) {
610- if ( e . code !== 'ERR_PACKAGE_PATH_NOT_EXPORTED' ) throw e ;
611- }
612- break ;
613- case 'default' :
614- try {
615- return resolveExportsTarget ( baseUrl , target . default , subpath ,
616- mappingKey ) ;
617- } catch ( e ) {
618- if ( e . code !== 'ERR_PACKAGE_PATH_NOT_EXPORTED' ) throw e ;
619- }
619+ if ( cjsConditions . has ( p ) || p === 'default' ) {
620+ const resolved = resolveExportsTarget ( baseUrl , target [ p ] , subpath ,
621+ mappingKey ) ;
622+ if ( resolved === undefined )
623+ continue ;
624+ return resolved ;
620625 }
621626 }
622- throw new ERR_PACKAGE_PATH_NOT_EXPORTED (
623- baseUrl . pathname , mappingKey + subpath ) ;
627+ return undefined ;
624628 } else if ( target === null ) {
625- throw new ERR_PACKAGE_PATH_NOT_EXPORTED (
626- baseUrl . pathname , mappingKey + subpath ) ;
629+ return null ;
627630 }
628631 throw new ERR_INVALID_PACKAGE_TARGET ( baseUrl . pathname , mappingKey , target ) ;
629632}
@@ -923,8 +926,7 @@ Module._load = function(request, parent, isMain) {
923926 return module . exports ;
924927} ;
925928
926- // TODO: Use this set when resolving pkg#exports conditions.
927- const cjsConditions = new SafeSet ( [ 'require' , 'node' ] ) ;
929+ const cjsConditions = new SafeSet ( [ 'require' , 'node' , ...userConditions ] ) ;
928930Module . _resolveFilename = function ( request , parent , isMain , options ) {
929931 if ( NativeModule . canBeRequiredByUsers ( request ) ) {
930932 return request ;
0 commit comments