@@ -85,6 +85,7 @@ const manifest = getOptionValue('--experimental-policy') ?
8585 require ( 'internal/process/policy' ) . manifest :
8686 null ;
8787const { compileFunction } = internalBinding ( 'contextify' ) ;
88+ const userConditions = getOptionValue ( '--conditions' ) ;
8889
8990// Whether any user-provided CJS modules had been loaded (executed).
9091// Used for internal assertions.
@@ -491,8 +492,12 @@ function applyExports(basePath, expansion) {
491492 if ( typeof pkgExports === 'object' ) {
492493 if ( ObjectPrototypeHasOwnProperty ( pkgExports , mappingKey ) ) {
493494 const mapping = pkgExports [ mappingKey ] ;
494- return resolveExportsTarget ( pathToFileURL ( basePath + '/' ) , mapping , '' ,
495- mappingKey ) ;
495+ const resolved = resolveExportsTarget (
496+ pathToFileURL ( basePath + '/' ) , mapping , '' , mappingKey ) ;
497+ if ( resolved === null || resolved === undefined )
498+ throw new ERR_PACKAGE_PATH_NOT_EXPORTED (
499+ basePath , mappingKey ) ;
500+ return resolved ;
496501 }
497502
498503 let dirMatch = '' ;
@@ -509,6 +514,9 @@ function applyExports(basePath, expansion) {
509514 const subpath = StringPrototypeSlice ( mappingKey , dirMatch . length ) ;
510515 const resolved = resolveExportsTarget ( pathToFileURL ( basePath + '/' ) ,
511516 mapping , subpath , mappingKey ) ;
517+ if ( resolved === null || resolved === undefined )
518+ throw new ERR_PACKAGE_PATH_NOT_EXPORTED (
519+ basePath , mappingKey + subpath ) ;
512520 // Extension searching for folder exports only
513521 const rc = stat ( resolved ) ;
514522 if ( rc === 0 ) return resolved ;
@@ -596,21 +604,29 @@ function resolveExportsTarget(baseUrl, target, subpath, mappingKey) {
596604 throw new ERR_INVALID_MODULE_SPECIFIER ( mappingKey + subpath , reason ) ;
597605 } else if ( ArrayIsArray ( target ) ) {
598606 if ( target . length === 0 )
599- throw new ERR_PACKAGE_PATH_NOT_EXPORTED (
600- baseUrl . pathname , mappingKey + subpath ) ;
607+ return null ;
601608 let lastException ;
602609 for ( const targetValue of target ) {
610+ let resolved ;
603611 try {
604- return resolveExportsTarget ( baseUrl , targetValue , subpath , mappingKey ) ;
612+ resolved = resolveExportsTarget ( baseUrl , targetValue , subpath ,
613+ mappingKey ) ;
605614 } catch ( e ) {
606615 lastException = e ;
607- if ( e . code !== 'ERR_PACKAGE_PATH_NOT_EXPORTED' &&
608- e . code !== 'ERR_INVALID_PACKAGE_TARGET' )
616+ if ( e . code !== 'ERR_INVALID_PACKAGE_TARGET' )
609617 throw e ;
610618 }
619+ if ( resolved === undefined )
620+ continue ;
621+ if ( resolved === null ) {
622+ lastException = null ;
623+ continue ;
624+ }
625+ return resolved ;
611626 }
612627 // Throw last fallback error
613- assert ( lastException !== undefined ) ;
628+ if ( lastException === undefined || lastException === null )
629+ return lastException ;
614630 throw lastException ;
615631 } else if ( typeof target === 'object' && target !== null ) {
616632 const keys = ObjectKeys ( target ) ;
@@ -619,30 +635,17 @@ function resolveExportsTarget(baseUrl, target, subpath, mappingKey) {
619635 'contain numeric property keys.' ) ;
620636 }
621637 for ( const p of keys ) {
622- switch ( p ) {
623- case 'node' :
624- case 'require' :
625- try {
626- return resolveExportsTarget ( baseUrl , target [ p ] , subpath ,
627- mappingKey ) ;
628- } catch ( e ) {
629- if ( e . code !== 'ERR_PACKAGE_PATH_NOT_EXPORTED' ) throw e ;
630- }
631- break ;
632- case 'default' :
633- try {
634- return resolveExportsTarget ( baseUrl , target . default , subpath ,
635- mappingKey ) ;
636- } catch ( e ) {
637- if ( e . code !== 'ERR_PACKAGE_PATH_NOT_EXPORTED' ) throw e ;
638- }
638+ if ( cjsConditions . has ( p ) || p === 'default' ) {
639+ const resolved = resolveExportsTarget ( baseUrl , target [ p ] , subpath ,
640+ mappingKey ) ;
641+ if ( resolved === undefined )
642+ continue ;
643+ return resolved ;
639644 }
640645 }
641- throw new ERR_PACKAGE_PATH_NOT_EXPORTED (
642- baseUrl . pathname , mappingKey + subpath ) ;
646+ return undefined ;
643647 } else if ( target === null ) {
644- throw new ERR_PACKAGE_PATH_NOT_EXPORTED (
645- baseUrl . pathname , mappingKey + subpath ) ;
648+ return null ;
646649 }
647650 throw new ERR_INVALID_PACKAGE_TARGET ( baseUrl . pathname , mappingKey , target ) ;
648651}
@@ -999,8 +1002,7 @@ Module._load = function(request, parent, isMain) {
9991002 return module . exports ;
10001003} ;
10011004
1002- // TODO: Use this set when resolving pkg#exports conditions.
1003- const cjsConditions = new SafeSet ( [ 'require' , 'node' ] ) ;
1005+ const cjsConditions = new SafeSet ( [ 'require' , 'node' , ...userConditions ] ) ;
10041006Module . _resolveFilename = function ( request , parent , isMain , options ) {
10051007 if ( NativeModule . canBeRequiredByUsers ( request ) ) {
10061008 return request ;
0 commit comments