chore: refactor isDepsOptimizerEnabled

This commit is contained in:
patak-dev 2024-05-24 00:30:45 +02:00
parent 006cfb7362
commit 5f36aa6ebb
5 changed files with 31 additions and 39 deletions

View File

@ -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,

View File

@ -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<DepOptimizationOptions>

View File

@ -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
}
}
}

View File

@ -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),

View File

@ -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