diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 796880b980c66a..632c24e44a7e26 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -107,6 +107,7 @@ export interface InternalResolveOptions extends Required { shouldExternalize?: (id: string) => boolean | undefined // Check this resolve is called from `hookNodeResolve` in SSR isHookNodeResolve?: boolean + overrideConditions?: string[] } export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin { @@ -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() export function tryNodeResolve( id: string, importer: string | null | undefined, - options: InternalNodeResolveOptions, + options: InternalResolveOptions, targetWeb: boolean, depsOptimizer?: DepsOptimizer, ssr?: boolean, @@ -930,7 +924,8 @@ export function resolvePackageEntry( data, '.', options, - getInlineConditions(options.conditions, targetWeb) + getInlineConditions(options, targetWeb), + options.overrideConditions ) if (!entryPoints.length) { packageEntryFailure(id) @@ -1037,13 +1032,32 @@ 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') + } return inlineConditions } @@ -1058,7 +1072,7 @@ function resolveDeepImport( data }: PackageData, targetWeb: boolean, - options: InternalNodeResolveOptions + options: InternalResolveOptions ): string | undefined { const cache = getResolvedCache(id, targetWeb) if (cache) { @@ -1075,7 +1089,7 @@ function resolveDeepImport( data, file, options, - getInlineConditions(options.conditions, targetWeb), + getInlineConditions(options, targetWeb), options.overrideConditions ) if (!possibleFiles.length) {