Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d496f08

Browse files
committedNov 14, 2022
fix: respect overrideConditions and isRequire
…in the `getInlineConditions` function.
1 parent 296fe46 commit d496f08

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed
 

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

+29-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,32 @@ 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+
}
10471061

10481062
return inlineConditions
10491063
}
@@ -1058,7 +1072,7 @@ function resolveDeepImport(
10581072
data
10591073
}: PackageData,
10601074
targetWeb: boolean,
1061-
options: InternalNodeResolveOptions
1075+
options: InternalResolveOptions
10621076
): string | undefined {
10631077
const cache = getResolvedCache(id, targetWeb)
10641078
if (cache) {
@@ -1075,7 +1089,7 @@ function resolveDeepImport(
10751089
data,
10761090
file,
10771091
options,
1078-
getInlineConditions(options.conditions, targetWeb),
1092+
getInlineConditions(options, targetWeb),
10791093
options.overrideConditions
10801094
)
10811095
if (!possibleFiles.length) {

0 commit comments

Comments
 (0)
Please sign in to comment.