@@ -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.
@@ -470,8 +471,12 @@ function applyExports(basePath, expansion) {
470471 if ( typeof pkgExports === 'object' ) {
471472 if ( ObjectPrototypeHasOwnProperty ( pkgExports , mappingKey ) ) {
472473 const mapping = pkgExports [ mappingKey ] ;
473- return resolveExportsTarget ( pathToFileURL ( basePath + '/' ) , mapping , '' ,
474- mappingKey ) ;
474+ const resolved = resolveExportsTarget (
475+ pathToFileURL ( basePath + '/' ) , mapping , '' , mappingKey ) ;
476+ if ( resolved === null || resolved === undefined )
477+ throw new ERR_PACKAGE_PATH_NOT_EXPORTED (
478+ basePath , mappingKey ) ;
479+ return resolved ;
475480 }
476481
477482 let dirMatch = '' ;
@@ -488,6 +493,9 @@ function applyExports(basePath, expansion) {
488493 const subpath = StringPrototypeSlice ( mappingKey , dirMatch . length ) ;
489494 const resolved = resolveExportsTarget ( pathToFileURL ( basePath + '/' ) ,
490495 mapping , subpath , mappingKey ) ;
496+ if ( resolved === null || resolved === undefined )
497+ throw new ERR_PACKAGE_PATH_NOT_EXPORTED (
498+ basePath , mappingKey + subpath ) ;
491499 // Extension searching for folder exports only
492500 const rc = stat ( resolved ) ;
493501 if ( rc === 0 ) return resolved ;
@@ -575,21 +583,29 @@ function resolveExportsTarget(baseUrl, target, subpath, mappingKey) {
575583 throw new ERR_INVALID_MODULE_SPECIFIER ( mappingKey + subpath , reason ) ;
576584 } else if ( ArrayIsArray ( target ) ) {
577585 if ( target . length === 0 )
578- throw new ERR_PACKAGE_PATH_NOT_EXPORTED (
579- baseUrl . pathname , mappingKey + subpath ) ;
586+ return null ;
580587 let lastException ;
581588 for ( const targetValue of target ) {
589+ let resolved ;
582590 try {
583- return resolveExportsTarget ( baseUrl , targetValue , subpath , mappingKey ) ;
591+ resolved = resolveExportsTarget ( baseUrl , targetValue , subpath ,
592+ mappingKey ) ;
584593 } catch ( e ) {
585594 lastException = e ;
586- if ( e . code !== 'ERR_PACKAGE_PATH_NOT_EXPORTED' &&
587- e . code !== 'ERR_INVALID_PACKAGE_TARGET' )
595+ if ( e . code !== 'ERR_INVALID_PACKAGE_TARGET' )
588596 throw e ;
589597 }
598+ if ( resolved === undefined )
599+ continue ;
600+ if ( resolved === null ) {
601+ lastException = null ;
602+ continue ;
603+ }
604+ return resolved ;
590605 }
591606 // Throw last fallback error
592- assert ( lastException !== undefined ) ;
607+ if ( lastException === undefined || lastException === null )
608+ return lastException ;
593609 throw lastException ;
594610 } else if ( typeof target === 'object' && target !== null ) {
595611 const keys = ObjectKeys ( target ) ;
@@ -598,30 +614,17 @@ function resolveExportsTarget(baseUrl, target, subpath, mappingKey) {
598614 'contain numeric property keys.' ) ;
599615 }
600616 for ( const p of keys ) {
601- switch ( p ) {
602- case 'node' :
603- case 'require' :
604- try {
605- return resolveExportsTarget ( baseUrl , target [ p ] , subpath ,
606- mappingKey ) ;
607- } catch ( e ) {
608- if ( e . code !== 'ERR_PACKAGE_PATH_NOT_EXPORTED' ) throw e ;
609- }
610- break ;
611- case 'default' :
612- try {
613- return resolveExportsTarget ( baseUrl , target . default , subpath ,
614- mappingKey ) ;
615- } catch ( e ) {
616- if ( e . code !== 'ERR_PACKAGE_PATH_NOT_EXPORTED' ) throw e ;
617- }
617+ if ( cjsConditions . has ( p ) || p === 'default' ) {
618+ const resolved = resolveExportsTarget ( baseUrl , target [ p ] , subpath ,
619+ mappingKey ) ;
620+ if ( resolved === undefined )
621+ continue ;
622+ return resolved ;
618623 }
619624 }
620- throw new ERR_PACKAGE_PATH_NOT_EXPORTED (
621- baseUrl . pathname , mappingKey + subpath ) ;
625+ return undefined ;
622626 } else if ( target === null ) {
623- throw new ERR_PACKAGE_PATH_NOT_EXPORTED (
624- baseUrl . pathname , mappingKey + subpath ) ;
627+ return null ;
625628 }
626629 throw new ERR_INVALID_PACKAGE_TARGET ( baseUrl . pathname , mappingKey , target ) ;
627630}
@@ -921,8 +924,7 @@ Module._load = function(request, parent, isMain) {
921924 return module . exports ;
922925} ;
923926
924- // TODO: Use this set when resolving pkg#exports conditions.
925- const cjsConditions = new SafeSet ( [ 'require' , 'node' ] ) ;
927+ const cjsConditions = new SafeSet ( [ 'require' , 'node' , ...userConditions ] ) ;
926928Module . _resolveFilename = function ( request , parent , isMain , options ) {
927929 if ( NativeModule . canBeRequiredByUsers ( request ) ) {
928930 return request ;
0 commit comments