From ad21ec35de8b6745e30f6982a0e29a1cf97d55c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Sat, 22 Apr 2023 03:45:32 +0900 Subject: [PATCH] refactor: resolveExports (#10917) --- packages/vite/src/node/plugins/resolve.ts | 57 +++++++++-------------- 1 file changed, 21 insertions(+), 36 deletions(-) diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 34bdcf4a699a55..0d5f4100877dbe 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -670,7 +670,6 @@ function tryResolveRealFileWithExtensions( export type InternalResolveOptionsWithOverrideConditions = InternalResolveOptions & { /** - * @deprecated In future, `conditions` will work like this. * @internal */ overrideConditions?: string[] @@ -1072,8 +1071,6 @@ function packageEntryFailure(id: string, details?: string) { ) } -const conditionalConditions = new Set(['production', 'development', 'module']) - function resolveExportsOrImports( pkg: PackageData['data'], key: string, @@ -1081,43 +1078,31 @@ function resolveExportsOrImports( targetWeb: boolean, type: 'imports' | 'exports', ) { - const overrideConditions = options.overrideConditions - ? new Set(options.overrideConditions) - : undefined + const additionalConditions = new Set( + options.overrideConditions || [ + 'production', + 'development', + 'module', + ...options.conditions, + ], + ) - const conditions = [] - if ( - (!overrideConditions || overrideConditions.has('production')) && - options.isProduction - ) { - conditions.push('production') - } - if ( - (!overrideConditions || overrideConditions.has('development')) && - !options.isProduction - ) { - conditions.push('development') - } - if ( - (!overrideConditions || overrideConditions.has('module')) && - !options.isRequire - ) { - conditions.push('module') - } - if (options.overrideConditions) { - conditions.push( - ...options.overrideConditions.filter((condition) => - conditionalConditions.has(condition), - ), - ) - } else if (options.conditions.length > 0) { - conditions.push(...options.conditions) - } + const conditions = [...additionalConditions].filter((condition) => { + switch (condition) { + case 'production': + return options.isProduction + case 'development': + return !options.isProduction + case 'module': + return !options.isRequire + } + return true + }) const fn = type === 'imports' ? imports : exports const result = fn(pkg, key, { - browser: targetWeb && !conditions.includes('node'), - require: options.isRequire && !conditions.includes('import'), + browser: targetWeb && !additionalConditions.has('node'), + require: options.isRequire && !additionalConditions.has('import'), conditions, })