@@ -107,6 +107,7 @@ export interface InternalResolveOptions extends Required<ResolveOptions> {
107
107
shouldExternalize ?: ( id : string ) => boolean | undefined
108
108
// Check this resolve is called from `hookNodeResolve` in SSR
109
109
isHookNodeResolve ?: boolean
110
+ overrideConditions ?: string [ ]
110
111
}
111
112
112
113
export function resolvePlugin ( resolveOptions : InternalResolveOptions ) : Plugin {
@@ -579,19 +580,12 @@ function tryResolveFile(
579
580
}
580
581
}
581
582
582
- export interface InternalNodeResolveOptions extends InternalResolveOptions {
583
- /**
584
- * When defined, only conditions defined in this array will be used.
585
- */
586
- overrideConditions ?: string [ ]
587
- }
588
-
589
583
export const idToPkgMap = new Map < string , PackageData > ( )
590
584
591
585
export function tryNodeResolve (
592
586
id : string ,
593
587
importer : string | null | undefined ,
594
- options : InternalNodeResolveOptions ,
588
+ options : InternalResolveOptions ,
595
589
targetWeb : boolean ,
596
590
depsOptimizer ?: DepsOptimizer ,
597
591
ssr ?: boolean ,
@@ -930,7 +924,8 @@ export function resolvePackageEntry(
930
924
data ,
931
925
'.' ,
932
926
options ,
933
- getInlineConditions ( options . conditions , targetWeb )
927
+ getInlineConditions ( options , targetWeb ) ,
928
+ options . overrideConditions
934
929
)
935
930
if ( ! entryPoints . length ) {
936
931
packageEntryFailure ( id )
@@ -1037,13 +1032,32 @@ function packageEntryFailure(id: string, details?: string) {
1037
1032
)
1038
1033
}
1039
1034
1040
- function getInlineConditions ( conditions : string [ ] , targetWeb : boolean ) {
1041
- const inlineConditions =
1042
- targetWeb && ! conditions . includes ( 'node' ) ? [ 'browser' ] : [ 'node' ]
1035
+ /**
1036
+ * This generates conditions that aren't inferred by `resolveExports`
1037
+ * from the `options` object.
1038
+ */
1039
+ function getInlineConditions (
1040
+ options : InternalResolveOptions ,
1041
+ targetWeb : boolean
1042
+ ) {
1043
+ const inlineConditions : string [ ] = [ ]
1044
+
1045
+ const conditions : readonly string [ ] =
1046
+ options . overrideConditions || options . conditions
1047
+
1048
+ if ( targetWeb ) {
1049
+ if ( ! conditions . includes ( 'node' ) ) {
1050
+ inlineConditions . push ( 'browser' )
1051
+ }
1052
+ } else if ( ! conditions . includes ( 'browser' ) ) {
1053
+ inlineConditions . push ( 'node' )
1054
+ }
1043
1055
1044
1056
// The "module" condition is no longer recommended, but some older
1045
1057
// packages may still use it.
1046
- inlineConditions . push ( 'module' )
1058
+ if ( ! options . isRequire && ! conditions . includes ( 'require' ) ) {
1059
+ inlineConditions . push ( 'module' )
1060
+ }
1047
1061
1048
1062
return inlineConditions
1049
1063
}
@@ -1058,7 +1072,7 @@ function resolveDeepImport(
1058
1072
data
1059
1073
} : PackageData ,
1060
1074
targetWeb : boolean ,
1061
- options : InternalNodeResolveOptions
1075
+ options : InternalResolveOptions
1062
1076
) : string | undefined {
1063
1077
const cache = getResolvedCache ( id , targetWeb )
1064
1078
if ( cache ) {
@@ -1075,7 +1089,7 @@ function resolveDeepImport(
1075
1089
data ,
1076
1090
file ,
1077
1091
options ,
1078
- getInlineConditions ( options . conditions , targetWeb ) ,
1092
+ getInlineConditions ( options , targetWeb ) ,
1079
1093
options . overrideConditions
1080
1094
)
1081
1095
if ( ! possibleFiles . length ) {
0 commit comments