Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: optimizeDeps back to top level #18465

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,19 +456,13 @@ export default defineConfig({
},
}),
],
environments: {
client: {
dev: {
optimizeDeps: {
include: [
'@shikijs/vitepress-twoslash/client',
'gsap',
'gsap/dist/ScrollTrigger',
'gsap/dist/MotionPathPlugin',
],
},
},
},
optimizeDeps: {
include: [
'@shikijs/vitepress-twoslash/client',
'gsap',
'gsap/dist/ScrollTrigger',
'gsap/dist/MotionPathPlugin',
],
},
},
buildEnd,
Expand Down
6 changes: 2 additions & 4 deletions docs/guide/api-environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ export default {
},
},
ssr: {
dev: {
optimizeDeps: {}, // configure the SSR environment
},
optimizeDeps: {}, // configure the SSR environment
},
rsc: {
resolve: {
Expand All @@ -61,7 +59,7 @@ export default {
}
```

The `EnvironmentOptions` interface exposes all the per-environment options. There are `SharedEnvironmentOptions` that apply to both `build` and `dev`, like `resolve`. And there are `DevEnvironmentOptions` and `BuildEnvironmentOptions` for dev and build specific options (like `dev.optimizeDeps` or `build.outDir`).
The `EnvironmentOptions` interface exposes all the per-environment options. There are `SharedEnvironmentOptions` that apply to both `build` and `dev`, like `resolve`. And there are `DevEnvironmentOptions` and `BuildEnvironmentOptions` for dev and build specific options (like `optimizeDeps` or `build.outDir`).

```ts
interface EnvironmentOptions extends SharedEnvironmentOptions {
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/node/baseEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export function getDefaultResolvedEnvironmentOptions(
resolve: config.resolve,
consumer: 'server',
webCompatible: false,
optimizeDeps: config.optimizeDeps,
dev: config.dev,
build: config.build,
}
Expand Down
51 changes: 24 additions & 27 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,6 @@ export interface DevEnvironmentOptions {
| false
| ((sourcePath: string, sourcemapPath: string) => boolean)

/**
* Optimize deps config
*/
optimizeDeps?: DepOptimizationOptions

/**
* create the Dev Environment instance
*/
Expand Down Expand Up @@ -251,6 +246,10 @@ export interface SharedEnvironmentOptions {
* Temporal options, we should remove these in favor of fine-grained control
*/
webCompatible?: boolean // was ssr.target === 'webworker'
/**
* Optimize deps config
*/
optimizeDeps?: DepOptimizationOptions
}

export interface EnvironmentOptions extends SharedEnvironmentOptions {
Expand All @@ -271,6 +270,7 @@ export type ResolvedEnvironmentOptions = {
resolve: ResolvedResolveOptions
consumer: 'client' | 'server'
webCompatible: boolean
optimizeDeps: DepOptimizationOptions
dev: ResolvedDevEnvironmentOptions
build: ResolvedBuildEnvironmentOptions
}
Expand Down Expand Up @@ -589,7 +589,6 @@ export type ResolvedConfig = Readonly<

export function resolveDevEnvironmentOptions(
dev: DevEnvironmentOptions | undefined,
preserverSymlinks: boolean,
environmentName: string | undefined,
consumer: 'client' | 'server' | undefined,
// Backward compatibility
Expand All @@ -603,11 +602,6 @@ export function resolveDevEnvironmentOptions(
: dev?.sourcemapIgnoreList || isInNodeModules,
preTransformRequests: dev?.preTransformRequests ?? consumer === 'client',
warmup: dev?.warmup ?? [],
optimizeDeps: resolveDepOptimizationOptions(
dev?.optimizeDeps,
preserverSymlinks,
consumer,
),
createEnvironment:
dev?.createEnvironment ??
(environmentName === 'client'
Expand Down Expand Up @@ -646,9 +640,13 @@ function resolveEnvironmentOptions(
resolve,
consumer,
webCompatible: options.webCompatible ?? consumer === 'client',
optimizeDeps: resolveDepOptimizationOptions(
options.optimizeDeps,
resolve.preserveSymlinks,
consumer,
),
dev: resolveDevEnvironmentOptions(
options.dev,
resolve.preserveSymlinks,
environmentName,
consumer,
skipSsrTransform,
Expand Down Expand Up @@ -911,13 +909,8 @@ export async function resolveConfig(

checkBadCharactersInPath(resolvedRoot, logger)

// Backward compatibility: merge optimizeDeps into environments.client.dev.optimizeDeps as defaults
const configEnvironmentsClient = config.environments!.client!
configEnvironmentsClient.dev ??= {}
configEnvironmentsClient.dev.optimizeDeps = mergeConfig(
config.optimizeDeps ?? {},
configEnvironmentsClient.dev.optimizeDeps ?? {},
)

const deprecatedSsrOptimizeDepsConfig = config.ssr?.optimizeDeps ?? {}
let configEnvironmentsSsr = config.environments!.ssr
Expand All @@ -935,10 +928,9 @@ export async function resolveConfig(

// Backward compatibility: merge ssr into environments.ssr.config as defaults
if (configEnvironmentsSsr) {
configEnvironmentsSsr.dev ??= {}
configEnvironmentsSsr.dev.optimizeDeps = mergeConfig(
configEnvironmentsSsr.optimizeDeps = mergeConfig(
deprecatedSsrOptimizeDepsConfig,
configEnvironmentsSsr.dev.optimizeDeps ?? {},
configEnvironmentsSsr.optimizeDeps ?? {},
)

configEnvironmentsSsr.resolve ??= {}
Expand Down Expand Up @@ -972,9 +964,16 @@ export async function resolveConfig(

// Merge default environment config values
const defaultEnvironmentOptions = getDefaultEnvironmentOptions(config)
// Some top level options only apply to the client environment
const defaultClientEnvironmentOptions = {
...defaultEnvironmentOptions,
optimizeDeps: config.optimizeDeps,
}
for (const name of Object.keys(config.environments)) {
config.environments[name] = mergeConfig(
defaultEnvironmentOptions,
name === 'client'
? defaultClientEnvironmentOptions
: defaultEnvironmentOptions,
config.environments[name],
)
}
Expand All @@ -995,16 +994,15 @@ export async function resolveConfig(
)
}

// Backward compatibility: merge environments.client.dev.optimizeDeps back into optimizeDeps
// Backward compatibility: merge environments.client.optimizeDeps back into optimizeDeps
// The same object is assigned back for backward compatibility. The ecosystem is modifying
// optimizeDeps in the ResolvedConfig hook, so these changes will be reflected on the
// client environment.
const backwardCompatibleOptimizeDeps =
resolvedEnvironments.client.dev.optimizeDeps
resolvedEnvironments.client.optimizeDeps

const resolvedDevEnvironmentOptions = resolveDevEnvironmentOptions(
config.dev,
resolvedDefaultResolve.preserveSymlinks,
// default environment options
undefined,
undefined,
Expand All @@ -1022,7 +1020,7 @@ export async function resolveConfig(
...config.ssr,
external: resolvedEnvironments.ssr?.resolve.external,
noExternal: resolvedEnvironments.ssr?.resolve.noExternal,
optimizeDeps: resolvedEnvironments.ssr?.dev?.optimizeDeps,
optimizeDeps: resolvedEnvironments.ssr?.optimizeDeps,
resolve: {
...config.ssr?.resolve,
conditions: resolvedEnvironments.ssr?.resolve.conditions,
Expand Down Expand Up @@ -1230,10 +1228,9 @@ export async function resolveConfig(
},
future: config.future,

// Backward compatibility, users should use environment.config.dev.optimizeDeps
optimizeDeps: backwardCompatibleOptimizeDeps,
ssr,

optimizeDeps: backwardCompatibleOptimizeDeps,
resolve: resolvedDefaultResolve,
dev: resolvedDevEnvironmentOptions,
build: resolvedBuildOptions,
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/optimizer/esbuildDepPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function esbuildDepPlugin(
external: string[],
): Plugin {
const { isProduction } = environment.config
const { extensions } = environment.config.dev.optimizeDeps
const { extensions } = environment.config.optimizeDeps

// remove optimizable extensions from `externalTypes` list
const allExternalTypes = extensions
Expand Down
12 changes: 6 additions & 6 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ export async function optimizeExplicitEnvironmentDeps(
): Promise<DepOptimizationMetadata> {
const cachedMetadata = await loadCachedDepOptimizationMetadata(
environment,
environment.config.dev.optimizeDeps.force ?? false,
environment.config.optimizeDeps.force ?? false,
false,
)
if (cachedMetadata) {
Expand Down Expand Up @@ -729,7 +729,7 @@ async function prepareEsbuildOptimizerRun(
const flatIdDeps: Record<string, string> = {}
const idToExports: Record<string, ExportsData> = {}

const { optimizeDeps } = environment.config.dev
const { optimizeDeps } = environment.config

const { plugins: pluginsFromConfig = [], ...esbuildOptions } =
optimizeDeps?.esbuildOptions ?? {}
Expand Down Expand Up @@ -812,7 +812,7 @@ export async function addManuallyIncludedOptimizeDeps(
deps: Record<string, string>,
): Promise<void> {
const { logger } = environment
const { optimizeDeps } = environment.config.dev
const { optimizeDeps } = environment.config
const optimizeDepsInclude = optimizeDeps?.include ?? []
if (optimizeDepsInclude.length) {
const unableToOptimize = (id: string, msg: string) => {
Expand Down Expand Up @@ -1059,7 +1059,7 @@ export async function extractExportsData(
): Promise<ExportsData> {
await init

const { optimizeDeps } = environment.config.dev
const { optimizeDeps } = environment.config

const esbuildOptions = optimizeDeps?.esbuildOptions ?? {}
if (optimizeDeps.extensions?.some((ext) => filePath.endsWith(ext))) {
Expand Down Expand Up @@ -1112,7 +1112,7 @@ function needsInterop(
exportsData: ExportsData,
output?: { exports: string[] },
): boolean {
if (environment.config.dev.optimizeDeps?.needsInterop?.includes(id)) {
if (environment.config.optimizeDeps?.needsInterop?.includes(id)) {
return true
}
const { hasModuleSyntax, exports } = exportsData
Expand Down Expand Up @@ -1156,7 +1156,7 @@ function getConfigHash(environment: Environment): string {
// Take config into account
// only a subset of config options that can affect dep optimization
const { config } = environment
const { optimizeDeps } = config.dev
const { optimizeDeps } = config
const content = JSON.stringify(
{
mode: process.env.NODE_ENV || config.mode,
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/optimizer/optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function createDepsOptimizer(

let closed = false

const options = environment.config.dev.optimizeDeps
const options = environment.config.optimizeDeps

const { noDiscovery, holdUntilCrawlEnd } = options

Expand Down Expand Up @@ -748,7 +748,7 @@ export function createExplicitDepsOptimizer(
run: () => {},

close: async () => {},
options: environment.config.dev.optimizeDeps,
options: environment.config.optimizeDeps,
}

let inited = false
Expand Down
12 changes: 6 additions & 6 deletions packages/vite/src/node/optimizer/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export function scanImports(environment: ScanEnvironment): {
entries = computedEntries

if (!entries.length) {
if (!config.optimizeDeps.entries && !config.dev.optimizeDeps.include) {
if (!config.optimizeDeps.entries && !config.optimizeDeps.include) {
environment.logger.warn(
colors.yellow(
'(!) Could not auto-determine entry point from rollupOptions or html files ' +
Expand Down Expand Up @@ -247,7 +247,7 @@ export function scanImports(environment: ScanEnvironment): {
async function computeEntries(environment: ScanEnvironment) {
let entries: string[] = []

const explicitEntryPatterns = environment.config.dev.optimizeDeps.entries
const explicitEntryPatterns = environment.config.optimizeDeps.entries
const buildInput = environment.config.build.rollupOptions?.input

if (explicitEntryPatterns) {
Expand Down Expand Up @@ -283,7 +283,7 @@ async function computeEntries(environment: ScanEnvironment) {
// dependencies.
entries = entries.filter(
(entry) =>
isScannable(entry, environment.config.dev.optimizeDeps.extensions) &&
isScannable(entry, environment.config.optimizeDeps.extensions) &&
fs.existsSync(entry),
)

Expand All @@ -302,7 +302,7 @@ async function prepareEsbuildScanner(
const plugin = esbuildScanPlugin(environment, deps, missing, entries)

const { plugins = [], ...esbuildOptions } =
environment.config.dev.optimizeDeps.esbuildOptions ?? {}
environment.config.optimizeDeps.esbuildOptions ?? {}

// The plugin pipeline automatically loads the closest tsconfig.json.
// But esbuild doesn't support reading tsconfig.json if the plugin has resolved the path (https://github.com/evanw/esbuild/issues/2265).
Expand Down Expand Up @@ -356,7 +356,7 @@ function globEntries(pattern: string | string[], environment: ScanEnvironment) {
'**/node_modules/**',
`**/${environment.config.build.outDir}/**`,
// if there aren't explicit entries, also ignore other common folders
...(environment.config.dev.optimizeDeps.entries
...(environment.config.optimizeDeps.entries
? []
: [`**/__tests__/**`, `**/coverage/**`]),
],
Expand Down Expand Up @@ -409,7 +409,7 @@ function esbuildScanPlugin(
return res
}

const optimizeDepsOptions = environment.config.dev.optimizeDeps
const optimizeDepsOptions = environment.config.optimizeDeps
const include = optimizeDepsOptions.include
const exclude = [
...(optimizeDepsOptions.exclude ?? []),
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export async function resolvePlugins(
const depOptimizationEnabled =
!isBuild &&
Object.values(config.environments).some(
(environment) => !isDepOptimizationDisabled(environment.dev.optimizeDeps),
(environment) => !isDepOptimizationDisabled(environment.optimizeDeps),
)

return [
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ export function resolvePlugin(
scan: resolveOpts?.scan ?? resolveOptions.scan,
}

const depsOptimizerOptions = this.environment.config.dev.optimizeDeps
const depsOptimizerOptions = this.environment.config.optimizeDeps

const resolvedImports = resolveSubpathImports(id, importer, options)
if (resolvedImports) {
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/server/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export class DevEnvironment extends BaseEnvironment {
})
})

const { optimizeDeps } = this.config.dev
const { optimizeDeps } = this.config
if (context.depsOptimizer) {
this.depsOptimizer = context.depsOptimizer
} else if (isDepOptimizationDisabled(optimizeDeps)) {
Expand Down
6 changes: 2 additions & 4 deletions playground/environment-react-ssr/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ export default defineConfig((env) => ({
},
},
ssr: {
dev: {
optimizeDeps: {
noDiscovery: false,
},
optimizeDeps: {
noDiscovery: false,
},
build: {
outDir: 'dist/server',
Expand Down