-
Notifications
You must be signed in to change notification settings - Fork 153
feat: add vitePlugins() helper for conditional plugin loading #1215
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
Open
fengmk2
wants to merge
14
commits into
main
Choose a base branch
from
how-to-use-lazy-field
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
96dfe1a
docs: add lazy loading plugins documentation and tests
fengmk2 5e8b649
docs: move lazy loading docs to config troubleshooting page
fengmk2 cc8352b
docs: revert to lazy field approach for deferred plugin loading
fengmk2 db99bd6
feat: replace lazy field with vitePlugins() helper for conditional pl…
fengmk2 71d3659
fix: add curly braces to satisfy eslint curly rule
fengmk2 4a6bc99
fix: return empty array instead of undefined and wrap async in array
fengmk2 df26b27
fix: revert vitePlugins return type to T | undefined
fengmk2 df1e199
test: add snap test for sync vitePlugins() usage
fengmk2 c6a609e
refactor: rename lazy-loading-plugins snap test to vite-plugins-async
fengmk2 31f920c
docs: use consistent array-wrapped syntax for both sync and async vit…
fengmk2 52fd15b
test: add snap test to verify vp lint skips plugin loading
fengmk2 45c4827
test: use async import pattern in skip-on-lint snap test
fengmk2 93586a8
fix: move snap test comment inline after command
fengmk2 dfaae49
fix: move snap test comments inline after commands
fengmk2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| # Configuration Troubleshooting | ||
|
|
||
| Use this page when your Vite+ configuration is not behaving the way you expect. | ||
|
|
||
| ## Slow config loading caused by heavy plugins | ||
|
|
||
| When `vite.config.ts` imports heavy plugins at the top level, every `import` is evaluated eagerly, even for commands like `vp lint` or `vp fmt` that don't need those plugins. This can make config loading noticeably slow. | ||
|
|
||
| Use the `vitePlugins()` helper to conditionally load plugins. It checks which `vp` command is running and skips plugin loading for commands that don't need them (like `lint`, `fmt`, `check`): | ||
|
|
||
| ```ts | ||
| import { defineConfig, vitePlugins } from 'vite-plus'; | ||
|
|
||
| import myPlugin from 'vite-plugin-foo'; | ||
|
|
||
| export default defineConfig({ | ||
| plugins: [ | ||
| vitePlugins(() => [myPlugin()]), | ||
| ], | ||
| }); | ||
| ``` | ||
|
|
||
| For heavy plugins that should be lazily imported, combine with dynamic `import()`: | ||
|
|
||
| ```ts | ||
| import { defineConfig, vitePlugins } from 'vite-plus'; | ||
|
|
||
| export default defineConfig({ | ||
| plugins: [ | ||
| vitePlugins(async () => { | ||
| const { default: heavyPlugin } = await import('vite-plugin-heavy'); | ||
| return [heavyPlugin()]; | ||
| }), | ||
| ], | ||
| }); | ||
| ``` | ||
|
|
||
| Plugins load for `dev`, `build`, `test`, and `preview`. They are skipped for `lint`, `fmt`, `check`, and other commands that don't need them. | ||
|
|
||
| ::: info | ||
| `vitePlugins()` works by checking the `VP_COMMAND` environment variable, which is automatically set by `vp` for every command. | ||
| ::: | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <!doctype html> | ||
| <html> | ||
| <body> | ||
| <script type="module"> | ||
| console.log('hello'); | ||
| </script> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| export default function myLazyPlugin() { | ||
| return { | ||
| name: 'my-lazy-plugin', | ||
| transformIndexHtml(html: string) { | ||
| return html.replace('</body>', '<!-- lazy-plugin-injected --></body>'); | ||
| }, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "name": "lazy-loading-plugins-test", | ||
| "private": true | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| > vp build | ||
| > cat dist/index.html | grep 'lazy-plugin-injected' # async vitePlugins() should apply plugins during build | ||
| <!-- lazy-plugin-injected --></body> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "commands": [ | ||
| { | ||
| "command": "vp build", | ||
| "ignoreOutput": true | ||
| }, | ||
| "cat dist/index.html | grep 'lazy-plugin-injected' # async vitePlugins() should apply plugins during build" | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { defineConfig, vitePlugins } from 'vite-plus'; | ||
|
|
||
| export default defineConfig({ | ||
| plugins: [ | ||
| vitePlugins(async () => { | ||
| const { default: myLazyPlugin } = await import('./my-plugin'); | ||
| return [myLazyPlugin()]; | ||
| }), | ||
| ], | ||
| }); |
5 changes: 5 additions & 0 deletions
5
packages/cli/snap-tests/vite-plugins-skip-on-lint/heavy-plugin.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| throw new Error('Plugins should not be loaded during lint'); | ||
|
|
||
| export default function heavyPlugin() { | ||
| return { name: 'heavy-plugin' }; | ||
| } |
4 changes: 4 additions & 0 deletions
4
packages/cli/snap-tests/vite-plugins-skip-on-lint/package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "name": "vite-plugins-skip-on-lint-test", | ||
| "private": true | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| > vp lint src/ # vp lint should not load plugins (heavy-plugin.ts throws if imported) | ||
| Found 0 warnings and 0 errors. | ||
| Finished in <variable>ms on 1 file with <variable> rules using <variable> threads. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export const foo = 'bar'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "commands": [ | ||
| "vp lint src/ # vp lint should not load plugins (heavy-plugin.ts throws if imported)" | ||
| ] | ||
| } |
10 changes: 10 additions & 0 deletions
10
packages/cli/snap-tests/vite-plugins-skip-on-lint/vite.config.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { defineConfig, vitePlugins } from 'vite-plus'; | ||
|
|
||
| export default defineConfig({ | ||
| plugins: [ | ||
| vitePlugins(async () => { | ||
| const { default: heavyPlugin } = await import('./heavy-plugin'); | ||
| return [heavyPlugin()]; | ||
| }), | ||
| ], | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <!doctype html> | ||
| <html> | ||
| <body> | ||
| <script type="module"> | ||
| console.log('hello'); | ||
| </script> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| export default function mySyncPlugin() { | ||
| return { | ||
| name: 'my-sync-plugin', | ||
| transformIndexHtml(html: string) { | ||
| return html.replace('</body>', '<!-- sync-plugin-injected --></body>'); | ||
| }, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "name": "vite-plugins-sync-test", | ||
| "private": true | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| > vp build | ||
| > cat dist/index.html | grep 'sync-plugin-injected' # sync vitePlugins() should apply plugins during build | ||
| <!-- sync-plugin-injected --></body> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "commands": [ | ||
| { | ||
| "command": "vp build", | ||
| "ignoreOutput": true | ||
| }, | ||
| "cat dist/index.html | grep 'sync-plugin-injected' # sync vitePlugins() should apply plugins during build" | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import { defineConfig, vitePlugins } from 'vite-plus'; | ||
|
|
||
| import mySyncPlugin from './my-plugin'; | ||
|
|
||
| export default defineConfig({ | ||
| plugins: [vitePlugins(() => [mySyncPlugin()])], | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The
vitePluginsnaming 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Closest I come is
lazyPluginsbut that might be too ambiguous. Another one wasviteOnlyPlugins.