@@ -381,17 +381,7 @@ export function resolvePackageEntry(
381
381
// https://nodejs.org/api/packages.html#packages_package_entry_points
382
382
const { exports : exportsField } = data
383
383
if ( exportsField ) {
384
- if ( typeof exportsField === 'string' ) {
385
- entryPoint = exportsField
386
- } else if ( Array . isArray ( exportsField ) ) {
387
- entryPoint = exportsField [ 0 ]
388
- } else if ( isObject ( exportsField ) ) {
389
- if ( '.' in exportsField ) {
390
- entryPoint = resolveConditionalExports ( exportsField [ '.' ] )
391
- } else {
392
- entryPoint = resolveConditionalExports ( exportsField )
393
- }
394
- }
384
+ entryPoint = resolveConditionalExports ( exportsField , '.' )
395
385
}
396
386
}
397
387
@@ -439,24 +429,13 @@ function resolveDeepImport(
439
429
440
430
// map relative based on exports data
441
431
if ( exportsField ) {
442
- let isExported = false
443
432
if ( isObject ( exportsField ) && ! Array . isArray ( exportsField ) ) {
444
- if ( relativeId in exportsField ) {
445
- relativeId = resolveConditionalExports ( exportsField [ relativeId ] )
446
- isExported = true
447
- } else {
448
- for ( const key in exportsField ) {
449
- if ( key . endsWith ( '/' ) && relativeId . startsWith ( key ) ) {
450
- // directory mapping
451
- const replacement = resolveConditionalExports ( exportsField [ key ] )
452
- relativeId = replacement && relativeId . replace ( key , replacement )
453
- isExported = true
454
- break
455
- }
456
- }
457
- }
433
+ relativeId = resolveConditionalExports ( exportsField , relativeId )
434
+ } else {
435
+ // not exposed
436
+ relativeId = undefined
458
437
}
459
- if ( ! isExported || ! relativeId ) {
438
+ if ( ! relativeId ) {
460
439
throw new Error (
461
440
`Package subpath '${ relativeId } ' is not defined by "exports" in ` +
462
441
`${ path . join ( dir , 'package.json' ) } .`
@@ -481,20 +460,51 @@ function resolveDeepImport(
481
460
}
482
461
}
483
462
484
- function resolveConditionalExports ( exp : any ) : string | undefined {
463
+ const ENV_KEYS = [
464
+ 'esmodules' ,
465
+ 'import' ,
466
+ 'module' ,
467
+ 'require' ,
468
+ 'browser' ,
469
+ 'node' ,
470
+ 'default'
471
+ ]
472
+
473
+ function resolveConditionalExports ( exp : any , id : string ) : string | undefined {
485
474
if ( typeof exp === 'string' ) {
486
475
return exp
487
476
} else if ( isObject ( exp ) ) {
488
- if ( typeof exp . browser === 'string' ) {
489
- return exp . browser
490
- } else if ( typeof exp . import === 'string' ) {
491
- return exp . import
492
- } else if ( typeof exp . default === 'string' ) {
493
- return exp . default
477
+ let isFileListing : boolean | undefined
478
+ let fallback : string | undefined
479
+ for ( const key in exp ) {
480
+ if ( isFileListing === undefined ) {
481
+ isFileListing = key [ 0 ] === '.'
482
+ }
483
+ if ( isFileListing ) {
484
+ if ( key === id ) {
485
+ return resolveConditionalExports ( exp [ key ] , id )
486
+ } else if ( key . endsWith ( '/' ) && id . startsWith ( key ) ) {
487
+ // mapped directory
488
+ const replacement = resolveConditionalExports ( exp [ key ] , id )
489
+ return replacement && id . replace ( key , replacement )
490
+ }
491
+ } else if ( ENV_KEYS . includes ( key ) ) {
492
+ // https://github.com/vitejs/vite/issues/1418
493
+ // respect env key order
494
+ // but intentionally de-prioritize "require" and "default" keys
495
+ if ( key === 'require' || key === 'default' ) {
496
+ if ( ! fallback ) fallback = key
497
+ } else {
498
+ return resolveConditionalExports ( exp [ key ] , id )
499
+ }
500
+ }
501
+ if ( fallback ) {
502
+ return resolveConditionalExports ( exp [ key ] , id )
503
+ }
494
504
}
495
505
} else if ( Array . isArray ( exp ) ) {
496
506
for ( let i = 0 ; i < exp . length ; i ++ ) {
497
- const res = resolveConditionalExports ( exp [ i ] )
507
+ const res = resolveConditionalExports ( exp [ i ] , id )
498
508
if ( res ) return res
499
509
}
500
510
}
0 commit comments