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 4d3c6ac
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
8 changes: 2 additions & 6 deletions packages/vite/src/node/config.ts
Expand Up @@ -48,11 +48,7 @@ import {
DEFAULT_MAIN_FIELDS,
ENV_ENTRY
} from './constants'
import type {
InternalResolveOptions,
InternalResolveOptionsWithOverrideConditions,
ResolveOptions
} from './plugins/resolve'
import type { InternalResolveOptions, ResolveOptions } from './plugins/resolve'
import { resolvePlugin, tryNodeResolve } from './plugins/resolve'
import type { LogLevel, Logger } from './logger'
import { createLogger } from './logger'
Expand Down Expand Up @@ -958,7 +954,7 @@ async function bundleConfigFile(
{
name: 'externalize-deps',
setup(build) {
const options: InternalResolveOptionsWithOverrideConditions = {
const options: InternalResolveOptions = {
root: path.dirname(fileName),
isBuild: true,
isProduction: true,
Expand Down
51 changes: 36 additions & 15 deletions packages/vite/src/node/plugins/resolve.ts
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 4d3c6ac

Please sign in to comment.