docs: add config troubleshooting page with lazy plugin loading guide#1215
docs: add config troubleshooting page with lazy plugin loading guide#1215
Conversation
✅ Deploy Preview for viteplus-preview ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
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
docs/config/troubleshooting.md
Outdated
|
|
||
| export default defineConfig({ | ||
| plugins: [ | ||
| import('vite-plugin-heavy').then((m) => m.default()), |
There was a problem hiding this comment.
@leaysgur @cpojer @branchseer I referenced @sapphi-red's suggestion and changed to using Vite's standard solution to resolve the slow configuration loading issue caused by plugins. The slow loading problem of other configuration items is temporarily not what this PR is meant to discuss and solve.
There was a problem hiding this comment.
Won't this still cause the plugin to be executed eagerly? I think this should be:
| import('vite-plugin-heavy').then((m) => m.default()), | |
| () => import('vite-plugin-heavy').then((m) => m.default()), |
There was a problem hiding this comment.
You are right that bare import().then() is evaluated eagerly.
However, wrapping it in () => import(...) does not work because Vite treats functions in the plugins array as plugin objects, not as factories to be called. It fails with:
Cannot assign to read only property 'name' of object '[object Object]'
Same issue with async () => { ... } (fails with [object AsyncFunction]).
Vite's PluginOption type is Plugin | false | null | undefined | PluginOption[] | Promise<PluginOption>, which does not include functions. So there is no native way to truly lazy-load plugins in Vite today. The lazy field in vite-plus was created specifically to fill this gap.
There was a problem hiding this comment.
@sapphi-red 😢 let me revert to lazy field for now
This needs to be resolved on the Vite side. I think Vite+ should not change the meaning of fields defined by Vite. @sapphi-red |
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.
b3cb2f5 to
8936d0c
Compare
|
Upstream issue created here: vitejs/vite#22085 |

Summary
docs/config/troubleshooting.mdwith guidance on using thelazyfield to defer heavy plugin importsdefineConfigvp buildVite does not support functions or async factories in the
pluginsarray (PluginOptiononly acceptsPlugin | Promise<Plugin>), so there is no native way to truly lazy-load plugins. Thelazyfield in vite-plus fills this gap.Test plan
vp test run packages/cli/src/__tests__/index.spec.ts(19 tests)pnpm -F vite-plus snap-test-local lazy-loading