@@ -84,6 +84,7 @@ const manifest = getOptionValue('--experimental-policy') ?
8484 require ( 'internal/process/policy' ) . manifest :
8585 null ;
8686const { compileFunction } = internalBinding ( 'contextify' ) ;
87+ const userConditions = getOptionValue ( '--conditions' ) ;
8788
8889// Whether any user-provided CJS modules had been loaded (executed).
8990// Used for internal assertions.
@@ -477,8 +478,12 @@ function applyExports(basePath, expansion) {
477478 if ( typeof pkgExports === 'object' ) {
478479 if ( ObjectPrototypeHasOwnProperty ( pkgExports , mappingKey ) ) {
479480 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 ;
482487 }
483488
484489 let dirMatch = '' ;
@@ -495,6 +500,9 @@ function applyExports(basePath, expansion) {
495500 const subpath = StringPrototypeSlice ( mappingKey , dirMatch . length ) ;
496501 const resolved = resolveExportsTarget ( pathToFileURL ( basePath + '/' ) ,
497502 mapping , subpath , mappingKey ) ;
503+ if ( resolved === null || resolved === undefined )
504+ throw new ERR_PACKAGE_PATH_NOT_EXPORTED (
505+ basePath , mappingKey + subpath ) ;
498506 // Extension searching for folder exports only
499507 const rc = stat ( resolved ) ;
500508 if ( rc === 0 ) return resolved ;
@@ -582,21 +590,29 @@ function resolveExportsTarget(baseUrl, target, subpath, mappingKey) {
582590 throw new ERR_INVALID_MODULE_SPECIFIER ( mappingKey + subpath , reason ) ;
583591 } else if ( ArrayIsArray ( target ) ) {
584592 if ( target . length === 0 )
585- throw new ERR_PACKAGE_PATH_NOT_EXPORTED (
586- baseUrl . pathname , mappingKey + subpath ) ;
593+ return null ;
587594 let lastException ;
588595 for ( const targetValue of target ) {
596+ let resolved ;
589597 try {
590- return resolveExportsTarget ( baseUrl , targetValue , subpath , mappingKey ) ;
598+ resolved = resolveExportsTarget ( baseUrl , targetValue , subpath ,
599+ mappingKey ) ;
591600 } catch ( e ) {
592601 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' )
595603 throw e ;
596604 }
605+ if ( resolved === undefined )
606+ continue ;
607+ if ( resolved === null ) {
608+ lastException = null ;
609+ continue ;
610+ }
611+ return resolved ;
597612 }
598613 // Throw last fallback error
599- assert ( lastException !== undefined ) ;
614+ if ( lastException === undefined || lastException === null )
615+ return lastException ;
600616 throw lastException ;
601617 } else if ( typeof target === 'object' && target !== null ) {
602618 const keys = ObjectKeys ( target ) ;
@@ -605,30 +621,17 @@ function resolveExportsTarget(baseUrl, target, subpath, mappingKey) {
605621 'contain numeric property keys.' ) ;
606622 }
607623 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 ;
625630 }
626631 }
627- throw new ERR_PACKAGE_PATH_NOT_EXPORTED (
628- baseUrl . pathname , mappingKey + subpath ) ;
632+ return undefined ;
629633 } else if ( target === null ) {
630- throw new ERR_PACKAGE_PATH_NOT_EXPORTED (
631- baseUrl . pathname , mappingKey + subpath ) ;
634+ return null ;
632635 }
633636 throw new ERR_INVALID_PACKAGE_TARGET ( baseUrl . pathname , mappingKey , target ) ;
634637}
@@ -985,8 +988,7 @@ Module._load = function(request, parent, isMain) {
985988 return module . exports ;
986989} ;
987990
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 ] ) ;
990992Module . _resolveFilename = function ( request , parent , isMain , options ) {
991993 if ( NativeModule . canBeRequiredByUsers ( request ) ) {
992994 return request ;
0 commit comments