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 2a4223d
Show file tree
Hide file tree
Showing 21 changed files with 53 additions and 57 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
4 changes: 2 additions & 2 deletions playground/hmr-ssr/accept-exports/main-accepted/callback.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export const x = 'X'
export const x = 'Z'

if (import.meta.hot) {
import.meta.hot.acceptExports(['x'], (m) => {
log(`reloaded >>> ${m.x}`)
log(`reloaded (2) >>> ${m.x}`)
})
}
2 changes: 1 addition & 1 deletion playground/hmr-ssr/accept-exports/main-accepted/dep.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default 'dep0'
export default 'dep1'
8 changes: 4 additions & 4 deletions playground/hmr-ssr/accept-exports/main-accepted/target.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import dep from './dep'

export const a = 'A0'
export const a = 'A3'

const bValue = 'B0'
const bValue = 'B3'
export { bValue as b }

const def = 'D0'
const def = 'D3'

export default def

log(`<<<<<< ${a} ${bValue} ${def} ; ${dep}`)

if (import.meta.hot) {
import.meta.hot.acceptExports(['a', 'default'])
import.meta.hot.acceptExports(['b', 'default'])
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const x = 'y'

const def = 'def0'
const def = 'def1'

export default def

Expand Down
2 changes: 1 addition & 1 deletion playground/hmr-ssr/accept-exports/main-non-accepted/dep.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default 'dep0'
export default 'dep1'
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dep from './dep'

export const a = 'A0'
export const a = 'A1'

export const b = 'B0'

Expand Down
2 changes: 1 addition & 1 deletion playground/hmr-ssr/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import './queries'
import logo from './logo.svg'
import { msg as softInvalidationMsg } from './soft-invalidation'

export const foo = 1
export const foo = 3
text('.app', foo)
text('.dep', depFoo)
text('.nested', nestedFoo)
Expand Down
2 changes: 1 addition & 1 deletion playground/hmr-ssr/hmrDep.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const foo = 1
export const foo = 3
export { foo as nestedFoo } from './hmrNestedDep'

if (import.meta.hot) {
Expand Down
2 changes: 1 addition & 1 deletion playground/hmr-ssr/hmrNestedDep.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const foo = 1
export const foo = 3
2 changes: 1 addition & 1 deletion playground/hmr-ssr/importing-updated/a.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const val = 'a0'
const val = 'a1'
globalThis.__HMR__['.importing-reloaded'] ??= ''
globalThis.__HMR__['.importing-reloaded'] += `a.js: ${val}<br>`

Expand Down
2 changes: 1 addition & 1 deletion playground/hmr-ssr/importing-updated/b.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import a from './a.js'

const val = `b0,${a}`
const val = `b1,${a}`

globalThis.__HMR__['.importing-reloaded'] ??= ''
globalThis.__HMR__['.importing-reloaded'] += `b.js: ${val}<br>`
Expand Down
2 changes: 1 addition & 1 deletion playground/hmr-ssr/invalidation/child.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ if (import.meta.hot) {
})
}

export const value = 'child'
export const value = 'child updated'
1 change: 1 addition & 0 deletions playground/hmr-ssr/non-tested/dep.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const test = 'true'

import.meta.hot.accept()
//comment
2 changes: 2 additions & 0 deletions playground/hmr-ssr/non-tested/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ function main() {
main()

import.meta.hot.accept('./dep.js')

export const query5 = 'query5'
1 change: 1 addition & 0 deletions playground/hmr-ssr/queries/multi-query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//comment
2 changes: 1 addition & 1 deletion playground/hmr-ssr/soft-invalidation/child.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const foo = 'bar'
export const foo = 'updated'

0 comments on commit 2a4223d

Please sign in to comment.