diff --git a/docs/.vitepress/config.mts b/docs/.vitepress/config.mts index a269be46b4..68ab18ea37 100644 --- a/docs/.vitepress/config.mts +++ b/docs/.vitepress/config.mts @@ -163,6 +163,7 @@ export default extendConfig( { text: 'Build', link: '/config/build' }, { text: 'Pack', link: '/config/pack' }, { text: 'Staged', link: '/config/staged' }, + { text: 'Troubleshooting', link: '/config/troubleshooting' }, ], }, ], diff --git a/docs/config/index.md b/docs/config/index.md index 78807ce104..810a406189 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -28,4 +28,4 @@ Vite+ extends the basic Vite configuration with these additions: - [`test`](/config/test) for Vitest - [`run`](/config/run) for Vite Task - [`pack`](/config/pack) for tsdown -- [`staged`](/config/staged) for staged-file checks +- [`staged`](/config/staged) for staged-file checks \ No newline at end of file diff --git a/docs/config/troubleshooting.md b/docs/config/troubleshooting.md new file mode 100644 index 0000000000..0abdbbf2e1 --- /dev/null +++ b/docs/config/troubleshooting.md @@ -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. +::: diff --git a/packages/cli/snap-tests/vite-plugins-async/index.html b/packages/cli/snap-tests/vite-plugins-async/index.html new file mode 100644 index 0000000000..7febab8c7e --- /dev/null +++ b/packages/cli/snap-tests/vite-plugins-async/index.html @@ -0,0 +1,8 @@ + + +
+ + + diff --git a/packages/cli/snap-tests/vite-plugins-async/my-plugin.ts b/packages/cli/snap-tests/vite-plugins-async/my-plugin.ts new file mode 100644 index 0000000000..33ba10fcfe --- /dev/null +++ b/packages/cli/snap-tests/vite-plugins-async/my-plugin.ts @@ -0,0 +1,8 @@ +export default function myLazyPlugin() { + return { + name: 'my-lazy-plugin', + transformIndexHtml(html: string) { + return html.replace('