Skip to content

Commit

Permalink
fix: respect overrideConditions and isRequire
Browse files Browse the repository at this point in the history
…in the `getInlineConditions` function.
  • Loading branch information
aleclarson committed Nov 14, 2022
1 parent 296fe46 commit 3c492bb
Showing 1 changed file with 36 additions and 15 deletions.
51 changes: 36 additions & 15 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export interface InternalResolveOptions extends Required<ResolveOptions> {
shouldExternalize?: (id: string) => boolean | undefined
// Check this resolve is called from `hookNodeResolve` in SSR
isHookNodeResolve?: boolean
overrideConditions?: string[]
}

export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
Expand Down Expand Up @@ -579,19 +580,12 @@ function tryResolveFile(
}
}

export interface InternalNodeResolveOptions extends InternalResolveOptions {
/**
* When defined, only conditions defined in this array will be used.
*/
overrideConditions?: string[]
}

export const idToPkgMap = new Map<string, PackageData>()

export function tryNodeResolve(
id: string,
importer: string | null | undefined,
options: InternalNodeResolveOptions,
options: InternalResolveOptions,
targetWeb: boolean,
depsOptimizer?: DepsOptimizer,
ssr?: boolean,
Expand Down Expand Up @@ -930,7 +924,8 @@ export function resolvePackageEntry(
data,
'.',
options,
getInlineConditions(options.conditions, targetWeb)
getInlineConditions(options, targetWeb),
options.overrideConditions
)
if (!entryPoints.length) {
packageEntryFailure(id)
Expand Down Expand Up @@ -1037,13 +1032,39 @@ function packageEntryFailure(id: string, details?: string) {
)
}

function getInlineConditions(conditions: string[], targetWeb: boolean) {
const inlineConditions =
targetWeb && !conditions.includes('node') ? ['browser'] : ['node']
/**
* This generates conditions that aren't inferred by `resolveExports`
* from the `options` object.
*/
function getInlineConditions(
options: InternalResolveOptions,
targetWeb: boolean
) {
const inlineConditions: string[] = []

const conditions: readonly string[] =
options.overrideConditions || options.conditions

if (targetWeb) {
if (!conditions.includes('node')) {
inlineConditions.push('browser')
}
} else if (!conditions.includes('browser')) {
inlineConditions.push('node')
}

// The "module" condition is no longer recommended, but some older
// packages may still use it.
inlineConditions.push('module')
if (!options.isRequire && !conditions.includes('require')) {
inlineConditions.push('module')
}

// The "overrideConditions" array can add arbitrary conditions.
options.overrideConditions?.forEach((condition) => {
if (!inlineConditions.includes(condition)) {
inlineConditions.push(condition)
}
})

return inlineConditions
}
Expand All @@ -1058,7 +1079,7 @@ function resolveDeepImport(
data
}: PackageData,
targetWeb: boolean,
options: InternalNodeResolveOptions
options: InternalResolveOptions
): string | undefined {
const cache = getResolvedCache(id, targetWeb)
if (cache) {
Expand All @@ -1075,7 +1096,7 @@ function resolveDeepImport(
data,
file,
options,
getInlineConditions(options.conditions, targetWeb),
getInlineConditions(options, targetWeb),
options.overrideConditions
)
if (!possibleFiles.length) {
Expand Down

0 comments on commit 3c492bb

Please sign in to comment.