-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
fix(vite): unset optimizeDeps.include for server environment
#33550
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
fix(vite): unset optimizeDeps.include for server environment
#33550
Conversation
|
|
WalkthroughThe Vite environments plugin now captures the Vite inline config via a local Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
packages/vite/src/plugins/environments.ts(3 hunks)packages/vite/src/shared/client.ts(1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{ts,tsx}
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Follow standard TypeScript conventions and best practices
Files:
packages/vite/src/shared/client.tspackages/vite/src/plugins/environments.ts
🧬 Code graph analysis (1)
packages/vite/src/plugins/environments.ts (2)
packages/vite/src/plugins/dev-server.ts (1)
config(15-57)packages/vite/src/shared/client.ts (1)
clientEnvironment(4-87)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: build
🔇 Additional comments (2)
packages/vite/src/shared/client.ts (1)
50-61: Virtual module exclusions look comprehensive and well-documented.The updated comment clearly explains why these modules are excluded, and the list covers the key Nuxt virtual modules that should not undergo Vite's dependency optimisation. This directly addresses the PR objective of preventing the "entry point cannot be marked as external" error.
Note: Some patterns may be redundant (e.g.,
'#build'when'#build/*'is present, and'virtual:nuxt:'when'virtual:nuxt:*'is present), but this doesn't affect functionality and may provide explicit documentation of exact module names.packages/vite/src/plugins/environments.ts (1)
71-78: Documentation improvements enhance code clarity.The added comments clearly explain why manual chunks are disabled for the SSR environment and the conditional disabling of advanced chunks when using Rolldown. These clarifications improve code maintainability.
| // Extract entry point for clientEnvironment configuration | ||
| const input = config.build?.rollupOptions?.input | ||
| const entry = typeof input === 'object' && input !== null && !Array.isArray(input) | ||
| ? (input as Record<string, string>).entry || '' | ||
| : typeof input === 'string' ? input : '' | ||
|
|
||
| // Get client environment configuration including optimizeDeps | ||
| const clientEnv = clientEnvironment(nuxt, entry) | ||
|
|
||
| // Explicitly set optimizeDeps from client environment configuration | ||
| // This is necessary because optimizeDeps is not inherited by environments | ||
| // See: https://vite.dev/guide/api-environment | ||
| config.optimizeDeps = clientEnv.optimizeDeps |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
Core logic correctly applies optimizeDeps from clientEnvironment.
This implementation directly addresses the PR objective by explicitly setting config.optimizeDeps from the client environment configuration, resolving the issue where optimizeDeps was not inherited by environments.
However, verify that the empty string fallback on line 39 doesn't cause issues when no valid entry point is found. When entry is '', the clientEnvironment function will receive it and set entries: [''], which may result in ineffective dependency optimisation configuration.
🏁 Script executed:
#!/bin/bash
# Verify how Vite handles empty string in optimizeDeps.entries
# Search for other usages of Rollup input extraction in Nuxt codebase
rg -n -C3 --type=ts "rollupOptions.*input" -g '!node_modules' -g '!dist' -g '!.nuxt'Length of output: 2225
Use utility function to ensure entry point validation.
The code at lines 35-47 correctly applies optimizeDeps from client environment configuration. However, the entry point extraction (lines 36-39) allows an empty string fallback without validation, which is inconsistent with the error handling in resolveClientEntry and resolveServerEntry utility functions in packages/vite/src/utils/config.ts. Those functions throw an error when no entry is found, establishing a validation pattern that environments.ts should follow.
Replace the manual extraction logic with resolveClientEntry to ensure proper validation:
const entry = resolveClientEntry(config)
const clientEnv = clientEnvironment(nuxt, entry)This ensures the entry point is validated before being passed to clientEnvironment, preventing silent fallback to empty string and maintaining consistency with existing error-handling patterns in the codebase.
🤖 Prompt for AI Agents
In packages/vite/src/plugins/environments.ts around lines 35 to 47, the entry
extraction silently falls back to an empty string instead of validating the
client entry; replace the manual extraction with a call to the existing
resolveClientEntry(config) utility so the entry is validated (and will throw if
missing) before passing it to clientEnvironment, remove the current manual
input/type checks, and add the necessary import for resolveClientEntry from
packages/vite/src/utils/config.ts.
@nuxt/kit
nuxt
@nuxt/rspack-builder
@nuxt/schema
@nuxt/vite-builder
@nuxt/webpack-builder
commit: |
CodSpeed Performance ReportMerging #33550 will improve performances by 12.17%Comparing Summary
Benchmarks breakdown
Footnotes |
| '#imports', | ||
| '#app', | ||
| '#build', | ||
| '#build/*', | ||
| '#components', | ||
| '#head', | ||
| 'virtual:nuxt:', | ||
| 'virtual:nuxt:*', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Flo0806 was there a particular reason you added these?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, without these many tests was failing, because the client part was searching for these imports - and they doesn't exists. (pnpm test:runtime)
optimizeDeps.include for server environment
🔗 Linked issue
Fixes: #33538
Summary
Fixes dependency optimization errors in Vite by excluding Nuxt virtual modules from
optimizeDeps.Summary
Fixes Vite dependency optimization errors by properly configuring
optimizeDepsfor the client environment.Problem
When using Vite's environment API, users encountered errors like:
✘ [ERROR] The entry point "ufo" cannot be marked as external
Error during dependency optimization
Root Cause:
In Vite,
optimizeDepsconfiguration is not inherited by environments (see Vitedocs). The
clientEnvironment()function inclient.tsdefines the properoptimizeDepsconfiguration, but it wasn't being applied to the client environment inconfigEnvironment().Solution
1.
/packages/vite/src/plugins/environments.ts:Explicitly set
optimizeDepsfromclientEnvironment()configuration:2. /packages/vite/src/shared/client.ts:
Extended the optimizeDeps.exclude list to include all Nuxt virtual modules (#imports, #app, #build/*, etc.) that are
resolved by Nuxt's own module resolution and should not be optimized by Vite.
Test Plan
References