Skip to content

Commit

Permalink
chore: refactor isDepsOptimizerEnabled
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed May 23, 2024
1 parent 006cfb7 commit 5f36aa6
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 40 deletions.
14 changes: 0 additions & 14 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
29 changes: 14 additions & 15 deletions packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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 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 (
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
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions packages/vite/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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),
Expand Down
9 changes: 3 additions & 6 deletions packages/vite/src/node/server/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5f36aa6

Please sign in to comment.