diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index 17f7ba115..8bd49eef8 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -1762,20 +1762,6 @@ async function runConfigEnvironmentHook( } } -export function getDepOptimizationOptions( - config: ResolvedConfig, - ssr: boolean, -): DepOptimizationOptions { - return ssr ? config.ssr.optimizeDeps : config.optimizeDeps -} -export function isDepsOptimizerEnabled( - config: ResolvedConfig, - ssr: boolean, -): boolean { - const optimizeDeps = getDepOptimizationOptions(config, ssr) - return !(optimizeDeps.noDiscovery && !optimizeDeps.include?.length) -} - function optimizeDepsDisabledBackwardCompatibility( resolved: ResolvedConfig, optimizeDeps: DepOptimizationOptions, diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index ac9a34fb2..9af9ba724 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -160,6 +160,15 @@ export interface DepOptimizationOptions { force?: boolean } +export function isDepOptimizationEnabled( + optimizeDeps: DepOptimizationOptions, +): boolean { + return ( + !(optimizeDeps.disabled === true || optimizeDeps.disabled === 'dev') && + !(optimizeDeps.noDiscovery && !optimizeDeps.include?.length) + ) +} + // TODO: We first need to define if entries and force should be per-environment // export type ResolvedDepOptimizationOptions = Required diff --git a/packages/vite/src/node/plugins/importAnalysis.ts b/packages/vite/src/node/plugins/importAnalysis.ts index e897c7195..19ee2d352 100644 --- a/packages/vite/src/node/plugins/importAnalysis.ts +++ b/packages/vite/src/node/plugins/importAnalysis.ts @@ -51,7 +51,6 @@ import { } from '../utils' import { getFsUtils } from '../fsUtils' import { checkPublicFile } from '../publicDir' -import { getDepOptimizationOptions } from '../config' import type { ResolvedConfig } from '../config' import type { Plugin } from '../plugin' import type { DevEnvironment } from '../server/environment' @@ -277,20 +276,20 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin { let importerFile = importer - const optimizeDeps = getDepOptimizationOptions(config, ssr) - if (moduleListContains(optimizeDeps?.exclude, url)) { - if (depsOptimizer) { - await depsOptimizer.scanProcessing + if ( + depsOptimizer && + moduleListContains(depsOptimizer.options.exclude, url) + ) { + await depsOptimizer.scanProcessing - // if the dependency encountered in the optimized file was excluded from the optimization - // the dependency needs to be resolved starting from the original source location of the optimized file - // because starting from node_modules/.vite will not find the dependency if it was not hoisted - // (that is, if it is under node_modules directory in the package source of the optimized file) - for (const optimizedModule of depsOptimizer.metadata.depInfoList) { - if (!optimizedModule.src) continue // Ignore chunks - if (optimizedModule.file === importerModule.file) { - importerFile = optimizedModule.src - } + // if the dependency encountered in the optimized file was excluded from the optimization + // the dependency needs to be resolved starting from the original source location of the optimized file + // because starting from node_modules/.vite will not find the dependency if it was not hoisted + // (that is, if it is under node_modules directory in the package source of the optimized file) + for (const optimizedModule of depsOptimizer.metadata.depInfoList) { + if (!optimizedModule.src) continue // Ignore chunks + if (optimizedModule.file === importerModule.file) { + importerFile = optimizedModule.src } } } diff --git a/packages/vite/src/node/plugins/index.ts b/packages/vite/src/node/plugins/index.ts index 6e3251181..1bd609d0a 100644 --- a/packages/vite/src/node/plugins/index.ts +++ b/packages/vite/src/node/plugins/index.ts @@ -1,7 +1,7 @@ import aliasPlugin, { type ResolverFunction } from '@rollup/plugin-alias' import type { ObjectHook } from 'rollup' import type { PluginHookUtils, ResolvedConfig } from '../config' -import { isDepsOptimizerEnabled } from '../config' +import { isDepOptimizationEnabled } from '../optimizer' import type { HookHandler, Plugin, PluginWithRequiredHook } from '../plugin' import { watchPackageDataPlugin } from '../packages' import { getFsUtils } from '../fsUtils' @@ -38,13 +38,14 @@ export async function resolvePlugins( ? await (await import('../build')).resolveBuildPlugins(config) : { pre: [], post: [] } const { modulePreload } = config.build - const depsOptimizerEnabled = + const depOptimizationEnabled = !isBuild && - (isDepsOptimizerEnabled(config, false) || - isDepsOptimizerEnabled(config, true)) + Object.values(config.environments).some((environment) => + isDepOptimizationEnabled(environment.dev.optimizeDeps), + ) return [ - depsOptimizerEnabled ? optimizedDepsPlugin(config) : null, + depOptimizationEnabled ? optimizedDepsPlugin(config) : null, isBuild ? metadataPlugin() : null, !isWorker ? watchPackageDataPlugin(config.packageCache) : null, preAliasPlugin(config), diff --git a/packages/vite/src/node/server/environment.ts b/packages/vite/src/node/server/environment.ts index e853644bd..3aa773e8d 100644 --- a/packages/vite/src/node/server/environment.ts +++ b/packages/vite/src/node/server/environment.ts @@ -12,12 +12,13 @@ import { getDefaultResolvedEnvironmentOptions } from '../config' import { mergeConfig, promiseWithResolvers } from '../utils' import type { FetchModuleOptions } from '../ssr/fetchModule' import { fetchModule } from '../ssr/fetchModule' +import type { DepsOptimizer } from '../optimizer' +import { isDepOptimizationEnabled } from '../optimizer' import { createDepsOptimizer, createExplicitDepsOptimizer, } from '../optimizer/optimizer' import { resolveEnvironmentPlugins } from '../plugin' -import type { DepsOptimizer } from '../optimizer' import { EnvironmentModuleGraph } from './moduleGraph' import type { HMRChannel } from './hmr' import { createNoopHMRChannel, getShortName, updateModules } from './hmr' @@ -139,11 +140,7 @@ export class DevEnvironment extends BaseEnvironment { const { optimizeDeps } = this.options.dev if (setup?.depsOptimizer) { this.depsOptimizer = setup?.depsOptimizer - } else if ( - optimizeDeps?.disabled === true || - optimizeDeps?.disabled === 'build' || - (optimizeDeps?.noDiscovery && optimizeDeps?.include?.length === 0) - ) { + } else if (!isDepOptimizationEnabled(optimizeDeps)) { this.depsOptimizer = undefined } else { // We only support auto-discovery for the client environment, for all other