Skip to content

Commit 3c492bb

Browse files
committed
fix: respect overrideConditions and isRequire
…in the `getInlineConditions` function.
1 parent 296fe46 commit 3c492bb

File tree

1 file changed

+36
-15
lines changed

1 file changed

+36
-15
lines changed

packages/vite/src/node/plugins/resolve.ts

+36-15
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ export interface InternalResolveOptions extends Required<ResolveOptions> {
107107
shouldExternalize?: (id: string) => boolean | undefined
108108
// Check this resolve is called from `hookNodeResolve` in SSR
109109
isHookNodeResolve?: boolean
110+
overrideConditions?: string[]
110111
}
111112

112113
export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
@@ -579,19 +580,12 @@ function tryResolveFile(
579580
}
580581
}
581582

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-
589583
export const idToPkgMap = new Map<string, PackageData>()
590584

591585
export function tryNodeResolve(
592586
id: string,
593587
importer: string | null | undefined,
594-
options: InternalNodeResolveOptions,
588+
options: InternalResolveOptions,
595589
targetWeb: boolean,
596590
depsOptimizer?: DepsOptimizer,
597591
ssr?: boolean,
@@ -930,7 +924,8 @@ export function resolvePackageEntry(
930924
data,
931925
'.',
932926
options,
933-
getInlineConditions(options.conditions, targetWeb)
927+
getInlineConditions(options, targetWeb),
928+
options.overrideConditions
934929
)
935930
if (!entryPoints.length) {
936931
packageEntryFailure(id)
@@ -1037,13 +1032,39 @@ function packageEntryFailure(id: string, details?: string) {
10371032
)
10381033
}
10391034

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+
}
10431055

10441056
// The "module" condition is no longer recommended, but some older
10451057
// packages may still use it.
1046-
inlineConditions.push('module')
1058+
if (!options.isRequire && !conditions.includes('require')) {
1059+
inlineConditions.push('module')
1060+
}
1061+
1062+
// The "overrideConditions" array can add arbitrary conditions.
1063+
options.overrideConditions?.forEach((condition) => {
1064+
if (!inlineConditions.includes(condition)) {
1065+
inlineConditions.push(condition)
1066+
}
1067+
})
10471068

10481069
return inlineConditions
10491070
}
@@ -1058,7 +1079,7 @@ function resolveDeepImport(
10581079
data
10591080
}: PackageData,
10601081
targetWeb: boolean,
1061-
options: InternalNodeResolveOptions
1082+
options: InternalResolveOptions
10621083
): string | undefined {
10631084
const cache = getResolvedCache(id, targetWeb)
10641085
if (cache) {
@@ -1075,7 +1096,7 @@ function resolveDeepImport(
10751096
data,
10761097
file,
10771098
options,
1078-
getInlineConditions(options.conditions, targetWeb),
1099+
getInlineConditions(options, targetWeb),
10791100
options.overrideConditions
10801101
)
10811102
if (!possibleFiles.length) {

0 commit comments

Comments
 (0)