Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Vite 8 (Rolldown-powered) configuration, plugin API, SSR, library mode, and Environment API reference.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/environment-api.md
1---2name: vite-environment-api3description: Vite 6+ Environment API for multiple runtime environments4---56# Environment API (Vite 6+)78The Environment API formalizes multiple runtime environments beyond the traditional client/SSR split.910## Concept1112Before Vite 6: Two implicit environments (`client` and `ssr`).1314Vite 6+: Configure as many environments as needed (browser, node server, edge server, etc.).1516## Basic Configuration1718For SPA/MPA, nothing changes—options apply to the implicit `client` environment:1920```ts21export default defineConfig({22build: { sourcemap: false },23optimizeDeps: { include: ['lib'] },24})25```2627## Multiple Environments2829```ts30export default defineConfig({31build: { sourcemap: false }, // Inherited by all environments32optimizeDeps: { include: ['lib'] }, // Client only33environments: {34// SSR environment35server: {},36// Edge runtime environment37edge: {38resolve: { noExternal: true },39},40},41})42```4344Environments inherit top-level config. Some options (like `optimizeDeps`) only apply to `client` by default.4546## Environment Options4748```ts49interface EnvironmentOptions {50define?: Record<string, any>51resolve?: EnvironmentResolveOptions52optimizeDeps: DepOptimizationOptions53consumer?: 'client' | 'server'54dev: DevOptions55build: BuildOptions56}57```5859## Custom Environment Instances6061Runtime providers can define custom environments:6263```ts64import { customEnvironment } from 'vite-environment-provider'6566export default defineConfig({67environments: {68ssr: customEnvironment({69build: { outDir: '/dist/ssr' },70}),71},72})73```7475Example: Cloudflare's Vite plugin runs code in `workerd` runtime during development.7677## Backward Compatibility7879- `server.moduleGraph` returns mixed client/SSR view80- `ssrLoadModule` still works81- Existing SSR apps work unchanged8283## When to Use8485- **End users**: Usually don't need to configure—frameworks handle it86- **Plugin authors**: Use for environment-aware transformations87- **Framework authors**: Create custom environments for their runtime needs8889## Plugin Environment Access9091Plugins can access environment in hooks:9293```ts94{95name: 'env-aware',96transform(code, id, options) {97if (options?.ssr) {98// SSR-specific transform99}100},101}102```103104<!--105Source references:106- https://vite.dev/guide/api-environment107- https://vite.dev/blog/announcing-vite6108-->109