feat: add vitePlugins() helper for conditional plugin loading#1215
feat: add vitePlugins() helper for conditional plugin loading#1215
Conversation
✅ Deploy Preview for viteplus-preview canceled.
|
How to use the Graphite Merge QueueAdd the label auto-merge to this PR to add it to the merge queue. You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. This stack of pull requests is managed by Graphite. Learn more about stacking. |
5adc3fd to
49d17df
Compare
leaysgur
left a comment
There was a problem hiding this comment.
I have no objection to recommending this method as a current workaround. 👌🏻
(However, I cannot judge whether this is the best approach, as I do not know the background behind why this lazy was introduced.)
|
I'm really not a fan of the lazy attribute and I don't think we should recommend it. It would be better to make the plugins field either an array or a function that returns |
49d17df to
b3cb2f5
Compare
This needs to be resolved on the Vite side. I think Vite+ should not change the meaning of fields defined by Vite. @sapphi-red |
b3cb2f5 to
8936d0c
Compare
|
Upstream issue created here: vitejs/vite#22085 |
Document the `lazy` field in config page with usage examples showing how to defer heavy plugin imports using async/await dynamic imports. Add unit tests for async/await lazy loading patterns and a snap test that verifies lazy-loaded plugins are applied during vp build.
Use Vite's native Promise-based plugin support via dynamic import() instead of the custom lazy field. Migrate the documentation from config/index.md to a new config/troubleshooting.md page and update the snap test to use the import().then() pattern.
Vite does not support functions or async factories in the plugins array, so there is no native way to truly lazy-load plugins. Revert the troubleshooting docs and snap test to use the vite-plus lazy field.
…ugin loading Replace the `lazy` field approach in `defineConfig` with a standalone `vitePlugins()` function that checks the `VP_COMMAND` environment variable to conditionally load plugins only for commands that need them (dev, build, test, preview). This is simpler, more explicit, and doesn't require modifying defineConfig internals. The VP_COMMAND env var is set automatically by vp for every command. Refs vitejs/vite#22085
8936d0c to
71d3659
Compare
vitePlugins() now returns [] when skipping, making it directly assignable to plugins. For async callbacks, wrap in an array since Vite's plugins field expects PluginOption[], not a bare Promise.
The heavy-plugin.ts throws on import, proving the dynamic import inside vitePlugins() is never executed during vp lint.
|
|
||
| export default defineConfig({ | ||
| plugins: [ | ||
| vitePlugins(() => [myPlugin()]), |
There was a problem hiding this comment.
The vitePlugins naming is a bit confusing to me. If we can somehow indicate that they are skipped for vp lint/fmt/check in the helper name that'd be nice.
There was a problem hiding this comment.
Closest I come is lazyPlugins but that might be too ambiguous. Another one was viteOnlyPlugins.

Summary
vitePlugins()helper that conditionally loads plugins based on theVP_COMMANDenvironment variabledev,build,test, andpreviewcommands; skipped for everything elseVP_COMMANDautomatically inbin.tsfor everyvpcommanddefineConfigback to a plain passthrough (removelazyfield approach)Refs vitejs/vite#22085
Usage
Test plan
vp test -- packages/cli/src/__tests__/index.spec.ts(15 tests)pnpm -F vite-plus snap-test-local lazy-loadingvp checkpasses (format + lint)🤖 Generated with Claude Code