--- url: /changes/this-environment-in-hooks.md --- # `this.environment` in Hooks ::: tip Feedback Give us feedback at [Environment API feedback discussion](https://github.com/vitejs/vite/discussions/16358) ::: Before Vite 6, only two environments were available: `client` and `ssr`. A single `options.ssr` plugin hook argument in `resolveId`, `load` and `transform` allowed plugin authors to differentiate between these two environments when processing modules in plugin hooks. In Vite 6, a Vite application can define any number of named environments as needed. We're introducing `this.environment` in the plugin context to interact with the environment of the current module in hooks. Affect scope: `Vite Plugin Authors` ::: warning Future Deprecation `this.environment` was introduced in `v6.0`. The deprecation of `options.ssr` is planned for `v7.0`. At that point we'll start recommending migrating your plugins to use the new API. To identify your usage, set `future.removePluginHookSsrArgument` to `"warn"` in your vite config. ::: ## Motivation `this.environment` not only allow the plugin hook implementation to know the current environment name, it also gives access to the environment config options, module graph information, and transform pipeline (`environment.config`, `environment.moduleGraph`, `environment.transformRequest()`). Having the environment instance available in the context allows plugin authors to avoid the dependency of the whole dev server (typically cached at startup through the `configureServer` hook). ## Migration Guide For the existing plugin to do a quick migration, replace the `options.ssr` argument with `this.environment.name !== 'client'` in the `resolveId`, `load` and `transform` hooks: ```ts import { Plugin } from 'vite' export function myPlugin(): Plugin { return { name: 'my-plugin', resolveId(id, importer, options) { const isSSR = options.ssr // [!code --] const isSSR = this.environment.name !== 'client' // [!code ++] if (isSSR) { // SSR specific logic } else { // Client specific logic } }, } } ``` For a more robust long term implementation, the plugin hook should handle for [multiple environments](/guide/api-environment.html#accessing-the-current-environment-in-hooks) using fine-grained environment options instead of relying on the environment name. --- --- url: /guide/backend-integration.md --- # Backend Integration :::tip Note If you want to serve the HTML using a traditional backend (e.g. Rails, Laravel) but use Vite for serving assets, check for existing integrations listed in [Awesome Vite](https://github.com/vitejs/awesome-vite#integrations-with-backends). If you need a custom integration, you can follow the steps in this guide to configure it manually ::: 1. In your Vite config, configure the entry and enable build manifest: ```js twoslash [vite.config.js] import { defineConfig } from 'vite' // ---cut--- export default defineConfig({ server: { cors: { // the origin you will be accessing via browser origin: 'http://my-backend.example.com', }, }, build: { // generate .vite/manifest.json in outDir manifest: true, rollupOptions: { // overwrite default .html entry input: '/path/to/main.js', }, }, }) ``` If you haven't disabled the [module preload polyfill](/config/build-options.md#build-polyfillmodulepreload), you also need to import the polyfill in your entry ```js // add the beginning of your app entry import 'vite/modulepreload-polyfill' ``` 2. For development, inject the following in your server's HTML template (substitute `http://localhost:5173` with the local URL Vite is running at): ```html ``` In order to properly serve assets, you have two options: * Make sure the server is configured to proxy static assets requests to the Vite server * Set [`server.origin`](/config/server-options.md#server-origin) so that generated asset URLs will be resolved using the back-end server URL instead of a relative path This is needed for assets such as images to load properly. Note if you are using React with `@vitejs/plugin-react`, you'll also need to add this before the above scripts, since the plugin is not able to modify the HTML you are serving (substitute `http://localhost:5173` with the local URL Vite is running at): ```html ``` 3. For production: after running `vite build`, a `.vite/manifest.json` file will be generated alongside other asset files. An example manifest file looks like this: ```json [.vite/manifest.json] { "_shared-B7PI925R.js": { "file": "assets/shared-B7PI925R.js", "name": "shared", "css": ["assets/shared-ChJ_j-JJ.css"] }, "_shared-ChJ_j-JJ.css": { "file": "assets/shared-ChJ_j-JJ.css", "src": "_shared-ChJ_j-JJ.css" }, "baz.js": { "file": "assets/baz-B2H3sXNv.js", "name": "baz", "src": "baz.js", "isDynamicEntry": true }, "views/bar.js": { "file": "assets/bar-gkvgaI9m.js", "name": "bar", "src": "views/bar.js", "isEntry": true, "imports": ["_shared-B7PI925R.js"], "dynamicImports": ["baz.js"] }, "views/foo.js": { "file": "assets/foo-BRBmoGS9.js", "name": "foo", "src": "views/foo.js", "isEntry": true, "imports": ["_shared-B7PI925R.js"], "css": ["assets/foo-5UjPuW-k.css"] } } ``` * The manifest has a `Record` structure * For entry or dynamic entry chunks, the key is the relative src path from project root. * For non entry chunks, the key is the base name of the generated file prefixed with `_`. * For the CSS file generated when [`build.cssCodeSplit`](/config/build-options.md#build-csscodesplit) is `false`, the key is `style.css`. * Chunks will contain information on its static and dynamic imports (both are keys that map to the corresponding chunk in the manifest), and also its corresponding CSS and asset files (if any). 4. You can use this file to render links or preload directives with hashed filenames. Here is an example HTML template to render the proper links. The syntax here is for explanation only, substitute with your server templating language. The `importedChunks` function is for illustration and isn't provided by Vite. ```html ``` Specifically, a backend generating HTML should include the following tags given a manifest file and an entry point: * A `` tag for each file in the entry point chunk's `css` list * Recursively follow all chunks in the entry point's `imports` list and include a `` tag for each CSS file of each imported chunk. * A tag for the `file` key of the entry point chunk (` ``` While the following should be included for the entry point `views/bar.js`: ```html ``` ::: details Pseudo implementation of `importedChunks` An example pseudo implementation of `importedChunks` in TypeScript (This will need to be adapted for your programming language and templating language): ```ts import type { Manifest, ManifestChunk } from 'vite' export default function importedChunks( manifest: Manifest, name: string, ): ManifestChunk[] { const seen = new Set() function getImportedChunks(chunk: ManifestChunk): ManifestChunk[] { const chunks: ManifestChunk[] = [] for (const file of chunk.imports ?? []) { const importee = manifest[file] if (seen.has(file)) { continue } seen.add(file) chunks.push(...getImportedChunks(importee)) chunks.push(importee) } return chunks } return getImportedChunks(manifest[name]) } ``` ::: --- --- url: /changes.md --- # Breaking Changes List of breaking changes in Vite including API deprecations, removals, and changes. Most of the changes below can be opt-in using the [`future` option](/config/shared-options.html#future) in your Vite config. ## Planned These changes are planned for the next major version of Vite. The deprecation or usage warnings will guide you where possible, and we're reaching out to framework, plugin authors, and users to apply these changes. * *No planned changes yet* ## Considering These changes are being considered and are often experimental APIs that intend to improve upon current usage patterns. As not all changes are listed here, please check out the [Experimental Label in Vite GitHub Discussions](https://github.com/vitejs/vite/discussions/categories/feedback?discussions_q=label%3Aexperimental+category%3AFeedback) for the full list. We don't recommend switching to these APIs yet. They are included in Vite to help us gather feedback. Please check these proposals and let us know how they work in your use case in each's linked GitHub Discussions. * [`this.environment` in Hooks](/changes/this-environment-in-hooks) * [HMR `hotUpdate` Plugin Hook](/changes/hotupdate-hook) * [Move to per-environment APIs](/changes/per-environment-apis) * [SSR using `ModuleRunner` API](/changes/ssr-using-modulerunner) * [Shared plugins during build](/changes/shared-plugins-during-build) ## Past The changes below has been done or reverted. They are no longer relevant in the current major version. * *No past changes yet* --- --- url: /config/build-options.md --- # Build Options Unless noted, the options in this section are only applied to build. ## build.target * **Type:** `string | string[]` * **Default:** `'modules'` * **Related:** [Browser Compatibility](/guide/build#browser-compatibility) Browser compatibility target for the final bundle. The default value is a Vite special value, `'modules'`, which targets browsers with [native ES Modules](https://caniuse.com/es6-module), [native ESM dynamic import](https://caniuse.com/es6-module-dynamic-import), and [`import.meta`](https://caniuse.com/mdn-javascript_operators_import_meta) support. Vite will replace `'modules'` to `['es2020', 'edge88', 'firefox78', 'chrome87', 'safari14']` Another special value is `'esnext'` - which assumes native dynamic imports support and will only perform minimal transpiling. The transform is performed with esbuild and the value should be a valid [esbuild target option](https://esbuild.github.io/api/#target). Custom targets can either be an ES version (e.g. `es2015`), a browser with version (e.g. `chrome58`), or an array of multiple target strings. Note the build will fail if the code contains features that cannot be safely transpiled by esbuild. See [esbuild docs](https://esbuild.github.io/content-types/#javascript) for more details. ## build.modulePreload * **Type:** `boolean | { polyfill?: boolean, resolveDependencies?: ResolveModulePreloadDependenciesFn }` * **Default:** `{ polyfill: true }` By default, a [module preload polyfill](https://guybedford.com/es-module-preloading-integrity#modulepreload-polyfill) is automatically injected. The polyfill is auto injected into the proxy module of each `index.html` entry. If the build is configured to use a non-HTML custom entry via `build.rollupOptions.input`, then it is necessary to manually import the polyfill in your custom entry: ```js import 'vite/modulepreload-polyfill' ``` Note: the polyfill does **not** apply to [Library Mode](/guide/build#library-mode). If you need to support browsers without native dynamic import, you should probably avoid using it in your library. The polyfill can be disabled using `{ polyfill: false }`. The list of chunks to preload for each dynamic import is computed by Vite. By default, an absolute path including the `base` will be used when loading these dependencies. If the `base` is relative (`''` or `'./'`), `import.meta.url` is used at runtime to avoid absolute paths that depend on the final deployed base. There is experimental support for fine grained control over the dependencies list and their paths using the `resolveDependencies` function. [Give Feedback](https://github.com/vitejs/vite/discussions/13841). It expects a function of type `ResolveModulePreloadDependenciesFn`: ```ts type ResolveModulePreloadDependenciesFn = ( url: string, deps: string[], context: { hostId: string hostType: 'html' | 'js' }, ) => string[] ``` The `resolveDependencies` function will be called for each dynamic import with a list of the chunks it depends on, and it will also be called for each chunk imported in entry HTML files. A new dependencies array can be returned with these filtered or more dependencies injected, and their paths modified. The `deps` paths are relative to the `build.outDir`. The return value should be a relative path to the `build.outDir`. ```js twoslash /** @type {import('vite').UserConfig} */ const config = { // prettier-ignore build: { // ---cut-before--- modulePreload: { resolveDependencies: (filename, deps, { hostId, hostType }) => { return deps.filter(condition) }, }, // ---cut-after--- }, } ``` The resolved dependency paths can be further modified using [`experimental.renderBuiltUrl`](../guide/build.md#advanced-base-options). ## build.polyfillModulePreload * **Type:** `boolean` * **Default:** `true` * **Deprecated** use `build.modulePreload.polyfill` instead Whether to automatically inject a [module preload polyfill](https://guybedford.com/es-module-preloading-integrity#modulepreload-polyfill). ## build.outDir * **Type:** `string` * **Default:** `dist` Specify the output directory (relative to [project root](/guide/#index-html-and-project-root)). ## build.assetsDir * **Type:** `string` * **Default:** `assets` Specify the directory to nest generated assets under (relative to `build.outDir`. This is not used in [Library Mode](/guide/build#library-mode)). ## build.assetsInlineLimit * **Type:** `number` | `((filePath: string, content: Buffer) => boolean | undefined)` * **Default:** `4096` (4 KiB) Imported or referenced assets that are smaller than this threshold will be inlined as base64 URLs to avoid extra http requests. Set to `0` to disable inlining altogether. If a callback is passed, a boolean can be returned to opt-in or opt-out. If nothing is returned the default logic applies. Git LFS placeholders are automatically excluded from inlining because they do not contain the content of the file they represent. ::: tip Note If you specify `build.lib`, `build.assetsInlineLimit` will be ignored and assets will always be inlined, regardless of file size or being a Git LFS placeholder. ::: ## build.cssCodeSplit * **Type:** `boolean` * **Default:** `true` Enable/disable CSS code splitting. When enabled, CSS imported in async JS chunks will be preserved as chunks and fetched together when the chunk is fetched. If disabled, all CSS in the entire project will be extracted into a single CSS file. ::: tip Note If you specify `build.lib`, `build.cssCodeSplit` will be `false` as default. ::: ## build.cssTarget * **Type:** `string | string[]` * **Default:** the same as [`build.target`](#build-target) This option allows users to set a different browser target for CSS minification from the one used for JavaScript transpilation. It should only be used when you are targeting a non-mainstream browser. One example is Android WeChat WebView, which supports most modern JavaScript features but not the [`#RGBA` hexadecimal color notation in CSS](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value#rgb_colors). In this case, you need to set `build.cssTarget` to `chrome61` to prevent vite from transform `rgba()` colors into `#RGBA` hexadecimal notations. ## build.cssMinify * **Type:** `boolean | 'esbuild' | 'lightningcss'` * **Default:** the same as [`build.minify`](#build-minify) for client, `'esbuild'` for SSR This option allows users to override CSS minification specifically instead of defaulting to `build.minify`, so you can configure minification for JS and CSS separately. Vite uses `esbuild` by default to minify CSS. Set the option to `'lightningcss'` to use [Lightning CSS](https://lightningcss.dev/minification.html) instead. If selected, it can be configured using [`css.lightningcss`](./shared-options.md#css-lightningcss). ## build.sourcemap * **Type:** `boolean | 'inline' | 'hidden'` * **Default:** `false` Generate production source maps. If `true`, a separate sourcemap file will be created. If `'inline'`, the sourcemap will be appended to the resulting output file as a data URI. `'hidden'` works like `true` except that the corresponding sourcemap comments in the bundled files are suppressed. ## build.rollupOptions * **Type:** [`RollupOptions`](https://rollupjs.org/configuration-options/) Directly customize the underlying Rollup bundle. This is the same as options that can be exported from a Rollup config file and will be merged with Vite's internal Rollup options. See [Rollup options docs](https://rollupjs.org/configuration-options/) for more details. ## build.commonjsOptions * **Type:** [`RollupCommonJSOptions`](https://github.com/rollup/plugins/tree/master/packages/commonjs#options) Options to pass on to [@rollup/plugin-commonjs](https://github.com/rollup/plugins/tree/master/packages/commonjs). ## build.dynamicImportVarsOptions * **Type:** [`RollupDynamicImportVarsOptions`](https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#options) * **Related:** [Dynamic Import](/guide/features#dynamic-import) Options to pass on to [@rollup/plugin-dynamic-import-vars](https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars). ## build.lib * **Type:** `{ entry: string | string[] | { [entryAlias: string]: string }, name?: string, formats?: ('es' | 'cjs' | 'umd' | 'iife')[], fileName?: string | ((format: ModuleFormat, entryName: string) => string), cssFileName?: string }` * **Related:** [Library Mode](/guide/build#library-mode) Build as a library. `entry` is required since the library cannot use HTML as entry. `name` is the exposed global variable and is required when `formats` includes `'umd'` or `'iife'`. Default `formats` are `['es', 'umd']`, or `['es', 'cjs']`, if multiple entries are used. `fileName` is the name of the package file output, which defaults to the `"name"` in `package.json`. It can also be defined as a function taking the `format` and `entryName` as arguments, and returning the file name. If your package imports CSS, `cssFileName` can be used to specify the name of the CSS file output. It defaults to the same value as `fileName` if it's set a string, otherwise it also falls back to the `"name"` in `package.json`. ```js twoslash [vite.config.js] import { defineConfig } from 'vite' export default defineConfig({ build: { lib: { entry: ['src/main.js'], fileName: (format, entryName) => `my-lib-${entryName}.${format}.js`, cssFileName: 'my-lib-style', }, }, }) ``` ## build.manifest * **Type:** `boolean | string` * **Default:** `false` * **Related:** [Backend Integration](/guide/backend-integration) Whether to generate a manifest file that contains a mapping of non-hashed asset filenames to their hashed versions, which can then be used by a server framework to render the correct asset links. When the value is a string, it will be used as the manifest file path relative to `build.outDir`. When set to `true`, the path would be `.vite/manifest.json`. ## build.ssrManifest * **Type:** `boolean | string` * **Default:** `false` * **Related:** [Server-Side Rendering](/guide/ssr) Whether to generate a SSR manifest file for determining style links and asset preload directives in production. When the value is a string, it will be used as the manifest file path relative to `build.outDir`. When set to `true`, the path would be `.vite/ssr-manifest.json`. ## build.ssr * **Type:** `boolean | string` * **Default:** `false` * **Related:** [Server-Side Rendering](/guide/ssr) Produce SSR-oriented build. The value can be a string to directly specify the SSR entry, or `true`, which requires specifying the SSR entry via `rollupOptions.input`. ## build.emitAssets * **Type:** `boolean` * **Default:** `false` During non-client builds, static assets aren't emitted as it is assumed they would be emitted as part of the client build. This option allows frameworks to force emitting them in other environments build. It is responsibility of the framework to merge the assets with a post build step. ## build.ssrEmitAssets * **Type:** `boolean` * **Default:** `false` During the SSR build, static assets aren't emitted as it is assumed they would be emitted as part of the client build. This option allows frameworks to force emitting them in both the client and SSR build. It is responsibility of the framework to merge the assets with a post build step. This option will be replaced by `build.emitAssets` once Environment API is stable. ## build.minify * **Type:** `boolean | 'terser' | 'esbuild'` * **Default:** `'esbuild'` for client build, `false` for SSR build Set to `false` to disable minification, or specify the minifier to use. The default is [esbuild](https://github.com/evanw/esbuild) which is 20 ~ 40x faster than terser and only 1 ~ 2% worse compression. [Benchmarks](https://github.com/privatenumber/minification-benchmarks) Note the `build.minify` option does not minify whitespaces when using the `'es'` format in lib mode, as it removes pure annotations and breaks tree-shaking. Terser must be installed when it is set to `'terser'`. ```sh npm add -D terser ``` ## build.terserOptions * **Type:** `TerserOptions` Additional [minify options](https://terser.org/docs/api-reference#minify-options) to pass on to Terser. In addition, you can also pass a `maxWorkers: number` option to specify the max number of workers to spawn. Defaults to the number of CPUs minus 1. ## build.write * **Type:** `boolean` * **Default:** `true` Set to `false` to disable writing the bundle to disk. This is mostly used in [programmatic `build()` calls](/guide/api-javascript#build) where further post processing of the bundle is needed before writing to disk. ## build.emptyOutDir * **Type:** `boolean` * **Default:** `true` if `outDir` is inside `root` By default, Vite will empty the `outDir` on build if it is inside project root. It will emit a warning if `outDir` is outside of root to avoid accidentally removing important files. You can explicitly set this option to suppress the warning. This is also available via command line as `--emptyOutDir`. ## build.copyPublicDir * **Type:** `boolean` * **Default:** `true` By default, Vite will copy files from the `publicDir` into the `outDir` on build. Set to `false` to disable this. ## build.reportCompressedSize * **Type:** `boolean` * **Default:** `true` Enable/disable gzip-compressed size reporting. Compressing large output files can be slow, so disabling this may increase build performance for large projects. ## build.chunkSizeWarningLimit * **Type:** `number` * **Default:** `500` Limit for chunk size warnings (in kB). It is compared against the uncompressed chunk size as the [JavaScript size itself is related to the execution time](https://v8.dev/blog/cost-of-javascript-2019). ## build.watch * **Type:** [`WatcherOptions`](https://rollupjs.org/configuration-options/#watch)`| null` * **Default:** `null` Set to `{}` to enable rollup watcher. This is mostly used in cases that involve build-only plugins or integrations processes. ::: warning Using Vite on Windows Subsystem for Linux (WSL) 2 There are cases that file system watching does not work with WSL2. See [`server.watch`](./server-options.md#server-watch) for more details. ::: --- --- url: /guide/build.md --- # Building for Production When it is time to deploy your app for production, simply run the `vite build` command. By default, it uses `/index.html` as the build entry point, and produces an application bundle that is suitable to be served over a static hosting service. Check out the [Deploying a Static Site](./static-deploy) for guides about popular services. ## Browser Compatibility By default, the production bundle assumes support for modern JavaScript, such as [native ES Modules](https://caniuse.com/es6-module), [native ESM dynamic import](https://caniuse.com/es6-module-dynamic-import), [`import.meta`](https://caniuse.com/mdn-javascript_operators_import_meta), [nullish coalescing](https://caniuse.com/mdn-javascript_operators_nullish_coalescing), and [BigInt](https://caniuse.com/bigint). The default browser support range is: * Chrome >=87 * Firefox >=78 * Safari >=14 * Edge >=88 You can specify custom targets via the [`build.target` config option](/config/build-options.md#build-target), where the lowest target is `es2015`. If a lower target is set, Vite will still require these minimum browser support ranges as it relies on [native ESM dynamic import](https://caniuse.com/es6-module-dynamic-import), and [`import.meta`](https://caniuse.com/mdn-javascript_operators_import_meta): * Chrome >=64 * Firefox >=67 * Safari >=11.1 * Edge >=79 Note that by default, Vite only handles syntax transforms and **does not cover polyfills**. You can check out https://cdnjs.cloudflare.com/polyfill/ which automatically generates polyfill bundles based on the user's browser UserAgent string. Legacy browsers can be supported via [@vitejs/plugin-legacy](https://github.com/vitejs/vite/tree/main/packages/plugin-legacy), which will automatically generate legacy chunks and corresponding ES language feature polyfills. The legacy chunks are conditionally loaded only in browsers that do not have native ESM support. ## Public Base Path * Related: [Asset Handling](./assets) If you are deploying your project under a nested public path, simply specify the [`base` config option](/config/shared-options.md#base) and all asset paths will be rewritten accordingly. This option can also be specified as a command line flag, e.g. `vite build --base=/my/public/path/`. JS-imported asset URLs, CSS `url()` references, and asset references in your `.html` files are all automatically adjusted to respect this option during build. The exception is when you need to dynamically concatenate URLs on the fly. In this case, you can use the globally injected `import.meta.env.BASE_URL` variable which will be the public base path. Note this variable is statically replaced during build so it must appear exactly as-is (i.e. `import.meta.env['BASE_URL']` won't work). For advanced base path control, check out [Advanced Base Options](#advanced-base-options). ### Relative base If you don't know the base path in advance, you may set a relative base path with `"base": "./"` or `"base": ""`. This will make all generated URLs to be relative to each file. :::warning Support for older browsers when using relative bases `import.meta` support is required for relative bases. If you need to support [browsers that do not support `import.meta`](https://caniuse.com/mdn-javascript_operators_import_meta), you can use [the `legacy` plugin](https://github.com/vitejs/vite/tree/main/packages/plugin-legacy). ::: ## Customizing the Build The build can be customized via various [build config options](/config/build-options.md). Specifically, you can directly adjust the underlying [Rollup options](https://rollupjs.org/configuration-options/) via `build.rollupOptions`: ```js [vite.config.js] export default defineConfig({ build: { rollupOptions: { // https://rollupjs.org/configuration-options/ }, }, }) ``` For example, you can specify multiple Rollup outputs with plugins that are only applied during build. ## Chunking Strategy You can configure how chunks are split using `build.rollupOptions.output.manualChunks` (see [Rollup docs](https://rollupjs.org/configuration-options/#output-manualchunks)). If you use a framework, refer to their documentation for configuring how chunks are split. ## Load Error Handling Vite emits `vite:preloadError` event when it fails to load dynamic imports. `event.payload` contains the original import error. If you call `event.preventDefault()`, the error will not be thrown. ```js twoslash window.addEventListener('vite:preloadError', (event) => { window.location.reload() // for example, refresh the page }) ``` When a new deployment occurs, the hosting service may delete the assets from previous deployments. As a result, a user who visited your site before the new deployment might encounter an import error. This error happens because the assets running on that user's device are outdated and it tries to import the corresponding old chunk, which is deleted. This event is useful for addressing this situation. ## Rebuild on Files Changes You can enable rollup watcher with `vite build --watch`. Or, you can directly adjust the underlying [`WatcherOptions`](https://rollupjs.org/configuration-options/#watch) via `build.watch`: ```js [vite.config.js] export default defineConfig({ build: { watch: { // https://rollupjs.org/configuration-options/#watch }, }, }) ``` With the `--watch` flag enabled, changes to the `vite.config.js`, as well as any files to be bundled, will trigger a rebuild. ## Multi-Page App Suppose you have the following source code structure: ``` ├── package.json ├── vite.config.js ├── index.html ├── main.js └── nested ├── index.html └── nested.js ``` During dev, simply navigate or link to `/nested/` - it works as expected, just like for a normal static file server. During build, all you need to do is to specify multiple `.html` files as entry points: ```js twoslash [vite.config.js] import { dirname, resolve } from 'node:path' import { fileURLToPath } from 'node:url' import { defineConfig } from 'vite' const __dirname = dirname(fileURLToPath(import.meta.url)) export default defineConfig({ build: { rollupOptions: { input: { main: resolve(__dirname, 'index.html'), nested: resolve(__dirname, 'nested/index.html'), }, }, }, }) ``` If you specify a different root, remember that `__dirname` will still be the folder of your vite.config.js file when resolving the input paths. Therefore, you will need to add your `root` entry to the arguments for `resolve`. Note that for HTML files, Vite ignores the name given to the entry in the `rollupOptions.input` object and instead respects the resolved id of the file when generating the HTML asset in the dist folder. This ensures a consistent structure with the way the dev server works. ## Library Mode When you are developing a browser-oriented library, you are likely spending most of the time on a test/demo page that imports your actual library. With Vite, you can use your `index.html` for that purpose to get the smooth development experience. When it is time to bundle your library for distribution, use the [`build.lib` config option](/config/build-options.md#build-lib). Make sure to also externalize any dependencies that you do not want to bundle into your library, e.g. `vue` or `react`: ::: code-group ```js twoslash [vite.config.js (single entry)] import { dirname, resolve } from 'node:path' import { fileURLToPath } from 'node:url' import { defineConfig } from 'vite' const __dirname = dirname(fileURLToPath(import.meta.url)) export default defineConfig({ build: { lib: { entry: resolve(__dirname, 'lib/main.js'), name: 'MyLib', // the proper extensions will be added fileName: 'my-lib', }, rollupOptions: { // make sure to externalize deps that shouldn't be bundled // into your library external: ['vue'], output: { // Provide global variables to use in the UMD build // for externalized deps globals: { vue: 'Vue', }, }, }, }, }) ``` ```js twoslash [vite.config.js (multiple entries)] import { dirname, resolve } from 'node:path' import { fileURLToPath } from 'node:url' import { defineConfig } from 'vite' const __dirname = dirname(fileURLToPath(import.meta.url)) export default defineConfig({ build: { lib: { entry: { 'my-lib': resolve(__dirname, 'lib/main.js'), secondary: resolve(__dirname, 'lib/secondary.js'), }, name: 'MyLib', }, rollupOptions: { // make sure to externalize deps that shouldn't be bundled // into your library external: ['vue'], output: { // Provide global variables to use in the UMD build // for externalized deps globals: { vue: 'Vue', }, }, }, }, }) ``` ::: The entry file would contain exports that can be imported by users of your package: ```js [lib/main.js] import Foo from './Foo.vue' import Bar from './Bar.vue' export { Foo, Bar } ``` Running `vite build` with this config uses a Rollup preset that is oriented towards shipping libraries and produces two bundle formats: * `es` and `umd` (for single entry) * `es` and `cjs` (for multiple entries) The formats can be configured with the [`build.lib.formats`](/config/build-options.md#build-lib) option. ``` $ vite build building for production... dist/my-lib.js 0.08 kB / gzip: 0.07 kB dist/my-lib.umd.cjs 0.30 kB / gzip: 0.16 kB ``` Recommended `package.json` for your lib: ::: code-group ```json [package.json (single entry)] { "name": "my-lib", "type": "module", "files": ["dist"], "main": "./dist/my-lib.umd.cjs", "module": "./dist/my-lib.js", "exports": { ".": { "import": "./dist/my-lib.js", "require": "./dist/my-lib.umd.cjs" } } } ``` ```json [package.json (multiple entries)] { "name": "my-lib", "type": "module", "files": ["dist"], "main": "./dist/my-lib.cjs", "module": "./dist/my-lib.js", "exports": { ".": { "import": "./dist/my-lib.js", "require": "./dist/my-lib.cjs" }, "./secondary": { "import": "./dist/secondary.js", "require": "./dist/secondary.cjs" } } } ``` ::: ### CSS support If your library imports any CSS, it will be bundled as a single CSS file besides the built JS files, e.g. `dist/my-lib.css`. The name defaults to `build.lib.fileName`, but can also be changed with [`build.lib.cssFileName`](/config/build-options.md#build-lib). You can export the CSS file in your `package.json` to be imported by users: ```json {12} { "name": "my-lib", "type": "module", "files": ["dist"], "main": "./dist/my-lib.umd.cjs", "module": "./dist/my-lib.js", "exports": { ".": { "import": "./dist/my-lib.js", "require": "./dist/my-lib.umd.cjs" }, "./style.css": "./dist/my-lib.css" } } ``` ::: tip File Extensions If the `package.json` does not contain `"type": "module"`, Vite will generate different file extensions for Node.js compatibility. `.js` will become `.mjs` and `.cjs` will become `.js`. ::: ::: tip Environment Variables In library mode, all [`import.meta.env.*`](./env-and-mode.md) usage are statically replaced when building for production. However, `process.env.*` usage are not, so that consumers of your library can dynamically change it. If this is undesirable, you can use `define: { 'process.env.NODE_ENV': '"production"' }` for example to statically replace them, or use [`esm-env`](https://github.com/benmccann/esm-env) for better compatibility with bundlers and runtimes. ::: ::: warning Advanced Usage Library mode includes a simple and opinionated configuration for browser-oriented and JS framework libraries. If you are building non-browser libraries, or require advanced build flows, you can use [Rollup](https://rollupjs.org) or [esbuild](https://esbuild.github.io) directly. ::: ## Advanced Base Options ::: warning This feature is experimental. [Give Feedback](https://github.com/vitejs/vite/discussions/13834). ::: For advanced use cases, the deployed assets and public files may be in different paths, for example to use different cache strategies. A user may choose to deploy in three different paths: * The generated entry HTML files (which may be processed during SSR) * The generated hashed assets (JS, CSS, and other file types like images) * The copied [public files](assets.md#the-public-directory) A single static [base](#public-base-path) isn't enough in these scenarios. Vite provides experimental support for advanced base options during build, using `experimental.renderBuiltUrl`. ```ts twoslash import type { UserConfig } from 'vite' // prettier-ignore const config: UserConfig = { // ---cut-before--- experimental: { renderBuiltUrl(filename, { hostType }) { if (hostType === 'js') { return { runtime: `window.__toCdnUrl(${JSON.stringify(filename)})` } } else { return { relative: true } } }, }, // ---cut-after--- } ``` If the hashed assets and public files aren't deployed together, options for each group can be defined independently using asset `type` included in the second `context` param given to the function. ```ts twoslash import type { UserConfig } from 'vite' import path from 'node:path' // prettier-ignore const config: UserConfig = { // ---cut-before--- experimental: { renderBuiltUrl(filename, { hostId, hostType, type }) { if (type === 'public') { return 'https://www.domain.com/' + filename } else if (path.extname(hostId) === '.js') { return { runtime: `window.__assetsPath(${JSON.stringify(filename)})` } } else { return 'https://cdn.domain.com/assets/' + filename } }, }, // ---cut-after--- } ``` Note that the `filename` passed is a decoded URL, and if the function returns a URL string, it should also be decoded. Vite will handle the encoding automatically when rendering the URLs. If an object with `runtime` is returned, encoding should be handled yourself where needed as the runtime code will be rendered as is. --- --- url: /guide/cli.md --- # Command Line Interface ## Dev server ### `vite` Start Vite dev server in the current directory. `vite dev` and `vite serve` are aliases for `vite`. #### Usage ```bash vite [root] ``` #### Options | Options | | | ------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | `--host [host]` | Specify hostname (`string`) | | `--port ` | Specify port (`number`) | | `--open [path]` | Open browser on startup (`boolean \| string`) | | `--cors` | Enable CORS (`boolean`) | | `--strictPort` | Exit if specified port is already in use (`boolean`) | | `--force` | Force the optimizer to ignore the cache and re-bundle (`boolean`) | | `-c, --config ` | Use specified config file (`string`) | | `--base ` | Public base path (default: `/`) (`string`) | | `-l, --logLevel ` | info | warn | error | silent (`string`) | | `--clearScreen` | Allow/disable clear screen when logging (`boolean`) | | `--configLoader ` | Use `bundle` to bundle the config with esbuild, or `runner` (experimental) to process it on the fly, or `native` (experimental) to load using the native runtime (default: `bundle`) | | `--profile` | Start built-in Node.js inspector (check [Performance bottlenecks](/guide/troubleshooting#performance-bottlenecks)) | | `-d, --debug [feat]` | Show debug logs (`string \| boolean`) | | `-f, --filter ` | Filter debug logs (`string`) | | `-m, --mode ` | Set env mode (`string`) | | `-h, --help` | Display available CLI options | | `-v, --version` | Display version number | ## Build ### `vite build` Build for production. #### Usage ```bash vite build [root] ``` #### Options | Options | | | ------------------------------ | ---------------------------------------------------------------------------------------------------------------------- | | `--target ` | Transpile target (default: `"modules"`) (`string`) | | `--outDir ` | Output directory (default: `dist`) (`string`) | | `--assetsDir ` | Directory under outDir to place assets in (default: `"assets"`) (`string`) | | `--assetsInlineLimit ` | Static asset base64 inline threshold in bytes (default: `4096`) (`number`) | | `--ssr [entry]` | Build specified entry for server-side rendering (`string`) | | `--sourcemap [output]` | Output source maps for build (default: `false`) (`boolean \| "inline" \| "hidden"`) | | `--minify [minifier]` | Enable/disable minification, or specify minifier to use (default: `"esbuild"`) (`boolean \| "terser" \| "esbuild"`) | | `--manifest [name]` | Emit build manifest json (`boolean \| string`) | | `--ssrManifest [name]` | Emit ssr manifest json (`boolean \| string`) | | `--emptyOutDir` | Force empty outDir when it's outside of root (`boolean`) | | `-w, --watch` | Rebuilds when modules have changed on disk (`boolean`) | | `-c, --config ` | Use specified config file (`string`) | | `--base ` | Public base path (default: `/`) (`string`) | | `-l, --logLevel ` | Info | warn | error | silent (`string`) | | `--clearScreen` | Allow/disable clear screen when logging (`boolean`) | | `--configLoader ` | Use `bundle` to bundle the config with esbuild or `runner` (experimental) to process it on the fly (default: `bundle`) | | `--profile` | Start built-in Node.js inspector (check [Performance bottlenecks](/guide/troubleshooting#performance-bottlenecks)) | | `-d, --debug [feat]` | Show debug logs (`string \| boolean`) | | `-f, --filter ` | Filter debug logs (`string`) | | `-m, --mode ` | Set env mode (`string`) | | `-h, --help` | Display available CLI options | | `--app` | Build all environments, same as `builder: {}` (`boolean`, experimental) | ## Others ### `vite optimize` Pre-bundle dependencies. **Deprecated**: the pre-bundle process runs automatically and does not need to be called. #### Usage ```bash vite optimize [root] ``` #### Options | Options | | | ------------------------- | ---------------------------------------------------------------------------------------------------------------------- | | `--force` | Force the optimizer to ignore the cache and re-bundle (`boolean`) | | `-c, --config ` | Use specified config file (`string`) | | `--base ` | Public base path (default: `/`) (`string`) | | `-l, --logLevel ` | Info | warn | error | silent (`string`) | | `--clearScreen` | Allow/disable clear screen when logging (`boolean`) | | `--configLoader ` | Use `bundle` to bundle the config with esbuild or `runner` (experimental) to process it on the fly (default: `bundle`) | | `-d, --debug [feat]` | Show debug logs (`string \| boolean`) | | `-f, --filter ` | Filter debug logs (`string`) | | `-m, --mode ` | Set env mode (`string`) | | `-h, --help` | Display available CLI options | ### `vite preview` Locally preview the production build. Do not use this as a production server as it's not designed for it. #### Usage ```bash vite preview [root] ``` #### Options | Options | | | ------------------------- | ---------------------------------------------------------------------------------------------------------------------- | | `--host [host]` | Specify hostname (`string`) | | `--port ` | Specify port (`number`) | | `--strictPort` | Exit if specified port is already in use (`boolean`) | | `--open [path]` | Open browser on startup (`boolean \| string`) | | `--outDir ` | Output directory (default: `dist`)(`string`) | | `-c, --config ` | Use specified config file (`string`) | | `--base ` | Public base path (default: `/`) (`string`) | | `-l, --logLevel ` | Info | warn | error | silent (`string`) | | `--clearScreen` | Allow/disable clear screen when logging (`boolean`) | | `--configLoader ` | Use `bundle` to bundle the config with esbuild or `runner` (experimental) to process it on the fly (default: `bundle`) | | `-d, --debug [feat]` | Show debug logs (`string \| boolean`) | | `-f, --filter ` | Filter debug logs (`string`) | | `-m, --mode ` | Set env mode (`string`) | | `-h, --help` | Display available CLI options | --- --- url: /config.md --- # Configuring Vite When running `vite` from the command line, Vite will automatically try to resolve a config file named `vite.config.js` inside [project root](/guide/#index-html-and-project-root) (other JS and TS extensions are also supported). The most basic config file looks like this: ```js [vite.config.js] export default { // config options } ``` Note Vite supports using ES modules syntax in the config file even if the project is not using native Node ESM, e.g. `type: "module"` in `package.json`. In this case, the config file is auto pre-processed before load. You can also explicitly specify a config file to use with the `--config` CLI option (resolved relative to `cwd`): ```bash vite --config my-config.js ``` ::: tip CONFIG LOADING By default, Vite uses `esbuild` to bundle the config into a temporary file and load it. This may cause issues when importing TypeScript files in a monorepo. If you encounter any issues with this approach, you can specify `--configLoader runner` to use the [module runner](/guide/api-environment-runtimes.html#modulerunner) instead, which will not create a temporary config and will transform any files on the fly. Note that module runner doesn't support CJS in config files, but external CJS packages should work as usual. Alternatively, if you're using an environment that supports TypeScript (e.g. `node --experimental-strip-types`), or if you're only writing plain JavaScript, you can specify `--configLoader native` to use the environment's native runtime to load the config file. Note that updates to modules imported by the config file are not detected and hence would not auto-restart the Vite server. ::: ## Config Intellisense Since Vite ships with TypeScript typings, you can leverage your IDE's intellisense with jsdoc type hints: ```js /** @type {import('vite').UserConfig} */ export default { // ... } ``` Alternatively, you can use the `defineConfig` helper which should provide intellisense without the need for jsdoc annotations: ```js import { defineConfig } from 'vite' export default defineConfig({ // ... }) ``` Vite also supports TypeScript config files. You can use `vite.config.ts` with the `defineConfig` helper function above, or with the `satisfies` operator: ```ts import type { UserConfig } from 'vite' export default { // ... } satisfies UserConfig ``` ## Conditional Config If the config needs to conditionally determine options based on the command (`serve` or `build`), the [mode](/guide/env-and-mode#modes) being used, if it's an SSR build (`isSsrBuild`), or is previewing the build (`isPreview`), it can export a function instead: ```js twoslash import { defineConfig } from 'vite' // ---cut--- export default defineConfig(({ command, mode, isSsrBuild, isPreview }) => { if (command === 'serve') { return { // dev specific config } } else { // command === 'build' return { // build specific config } } }) ``` It is important to note that in Vite's API the `command` value is `serve` during dev (in the cli [`vite`](/guide/cli#vite), `vite dev`, and `vite serve` are aliases), and `build` when building for production ([`vite build`](/guide/cli#vite-build)). `isSsrBuild` and `isPreview` are additional optional flags to differentiate the kind of `build` and `serve` commands respectively. Some tools that load the Vite config may not support these flags and will pass `undefined` instead. Hence, it's recommended to use explicit comparison against `true` and `false`. ## Async Config If the config needs to call async functions, it can export an async function instead. And this async function can also be passed through `defineConfig` for improved intellisense support: ```js twoslash import { defineConfig } from 'vite' // ---cut--- export default defineConfig(async ({ command, mode }) => { const data = await asyncFunction() return { // vite config } }) ``` ## Using Environment Variables in Config Environmental Variables can be obtained from `process.env` as usual. Note that Vite doesn't load `.env` files by default as the files to load can only be determined after evaluating the Vite config, for example, the `root` and `envDir` options affect the loading behaviour. However, you can use the exported `loadEnv` helper to load the specific `.env` file if needed. ```js twoslash import { defineConfig, loadEnv } from 'vite' export default defineConfig(({ mode }) => { // Load env file based on `mode` in the current working directory. // Set the third parameter to '' to load all env regardless of the // `VITE_` prefix. const env = loadEnv(mode, process.cwd(), '') return { // vite config define: { __APP_ENV__: JSON.stringify(env.APP_ENV), }, } }) ``` ## Debugging the Config File on VS Code With the default `--configLoader bundle` behavior, Vite writes the generated temporary configuration file to the `node_modules/.vite-temp` folder and a file not found error will occur when setting breakpoint debugging in the Vite config file. To fix the issue, add the following configuration to `.vscode/settings.json`: ```json { "debug.javascript.terminalOptions": { "resolveSourceMapLocations": [ "${workspaceFolder}/**", "!**/node_modules/**", "**/node_modules/.vite-temp/**" ] } } ``` --- --- url: /config/dep-optimization-options.md --- # Dep Optimization Options * **Related:** [Dependency Pre-Bundling](/guide/dep-pre-bundling) Unless noted, the options in this section are only applied to the dependency optimizer, which is only used in dev. ## optimizeDeps.entries * **Type:** `string | string[]` By default, Vite will crawl all your `.html` files to detect dependencies that need to be pre-bundled (ignoring `node_modules`, `build.outDir`, `__tests__` and `coverage`). If `build.rollupOptions.input` is specified, Vite will crawl those entry points instead. If neither of these fit your needs, you can specify custom entries using this option - the value should be a [`tinyglobby` pattern](https://github.com/SuperchupuDev/tinyglobby) or array of patterns that are relative from Vite project root. This will overwrite default entries inference. Only `node_modules` and `build.outDir` folders will be ignored by default when `optimizeDeps.entries` is explicitly defined. If other folders need to be ignored, you can use an ignore pattern as part of the entries list, marked with an initial `!`. If you don't want to ignore `node_modules` and `build.outDir`, you can specify using literal string paths (without `tinyglobby` patterns) instead. ## optimizeDeps.exclude * **Type:** `string[]` Dependencies to exclude from pre-bundling. :::warning CommonJS CommonJS dependencies should not be excluded from optimization. If an ESM dependency is excluded from optimization, but has a nested CommonJS dependency, the CommonJS dependency should be added to `optimizeDeps.include`. Example: ```js twoslash import { defineConfig } from 'vite' // ---cut--- export default defineConfig({ optimizeDeps: { include: ['esm-dep > cjs-dep'], }, }) ``` ::: ## optimizeDeps.include * **Type:** `string[]` By default, linked packages not inside `node_modules` are not pre-bundled. Use this option to force a linked package to be pre-bundled. **Experimental:** If you're using a library with many deep imports, you can also specify a trailing glob pattern to pre-bundle all deep imports at once. This will avoid constantly pre-bundling whenever a new deep import is used. [Give Feedback](https://github.com/vitejs/vite/discussions/15833). For example: ```js twoslash import { defineConfig } from 'vite' // ---cut--- export default defineConfig({ optimizeDeps: { include: ['my-lib/components/**/*.vue'], }, }) ``` ## optimizeDeps.esbuildOptions * **Type:** [`Omit`](https://www.typescriptlang.org/docs/handbook/utility-types.html#omittype-keys)`<`[`EsbuildBuildOptions`](https://esbuild.github.io/api/#general-options)`, | 'bundle' | 'entryPoints' | 'external' | 'write' | 'watch' | 'outdir' | 'outfile' | 'outbase' | 'outExtension' | 'metafile'>` Options to pass to esbuild during the dep scanning and optimization. Certain options are omitted since changing them would not be compatible with Vite's dep optimization. * `external` is also omitted, use Vite's `optimizeDeps.exclude` option * `plugins` are merged with Vite's dep plugin ## optimizeDeps.force * **Type:** `boolean` Set to `true` to force dependency pre-bundling, ignoring previously cached optimized dependencies. ## optimizeDeps.holdUntilCrawlEnd * **Experimental:** [Give Feedback](https://github.com/vitejs/vite/discussions/15834) * **Type:** `boolean` * **Default:** `true` When enabled, it will hold the first optimized deps results until all static imports are crawled on cold start. This avoids the need for full-page reloads when new dependencies are discovered and they trigger the generation of new common chunks. If all dependencies are found by the scanner plus the explicitly defined ones in `include`, it is better to disable this option to let the browser process more requests in parallel. ## optimizeDeps.disabled * **Deprecated** * **Experimental:** [Give Feedback](https://github.com/vitejs/vite/discussions/13839) * **Type:** `boolean | 'build' | 'dev'` * **Default:** `'build'` This option is deprecated. As of Vite 5.1, pre-bundling of dependencies during build have been removed. Setting `optimizeDeps.disabled` to `true` or `'dev'` disables the optimizer, and configured to `false` or `'build'` leaves the optimizer during dev enabled. To disable the optimizer completely, use `optimizeDeps.noDiscovery: true` to disallow automatic discovery of dependencies and leave `optimizeDeps.include` undefined or empty. :::warning Optimizing dependencies during build time was an **experimental** feature. Projects trying out this strategy also removed `@rollup/plugin-commonjs` using `build.commonjsOptions: { include: [] }`. If you did so, a warning will guide you to re-enable it to support CJS only packages while bundling. ::: ## optimizeDeps.needsInterop * **Experimental** * **Type:** `string[]` Forces ESM interop when importing these dependencies. Vite is able to properly detect when a dependency needs interop, so this option isn't generally needed. However, different combinations of dependencies could cause some of them to be prebundled differently. Adding these packages to `needsInterop` can speed up cold start by avoiding full-page reloads. You'll receive a warning if this is the case for one of your dependencies, suggesting to add the package name to this array in your config. --- --- url: /guide/dep-pre-bundling.md --- # Dependency Pre-Bundling When you run `vite` for the first time, Vite prebundles your project dependencies before loading your site locally. It is done automatically and transparently by default. ## The Why This is Vite performing what we call "dependency pre-bundling". This process serves two purposes: 1. **CommonJS and UMD compatibility:** During development, Vite's dev serves all code as native ESM. Therefore, Vite must convert dependencies that are shipped as CommonJS or UMD into ESM first. When converting CommonJS dependencies, Vite performs smart import analysis so that named imports to CommonJS modules will work as expected even if the exports are dynamically assigned (e.g. React): ```js // works as expected import React, { useState } from 'react' ``` 2. **Performance:** Vite converts ESM dependencies with many internal modules into a single module to improve subsequent page load performance. Some packages ship their ES modules builds as many separate files importing one another. For example, [`lodash-es` has over 600 internal modules](https://unpkg.com/browse/lodash-es/)! When we do `import { debounce } from 'lodash-es'`, the browser fires off 600+ HTTP requests at the same time! Even though the server has no problem handling them, the large amount of requests create a network congestion on the browser side, causing the page to load noticeably slower. By pre-bundling `lodash-es` into a single module, we now only need one HTTP request instead! ::: tip NOTE Dependency pre-bundling only applies in development mode, and uses `esbuild` to convert dependencies to ESM. In production builds, `@rollup/plugin-commonjs` is used instead. ::: ## Automatic Dependency Discovery If an existing cache is not found, Vite will crawl your source code and automatically discover dependency imports (i.e. "bare imports" that expect to be resolved from `node_modules`) and use these found imports as entry points for the pre-bundle. The pre-bundling is performed with `esbuild` so it's typically very fast. After the server has already started, if a new dependency import is encountered that isn't already in the cache, Vite will re-run the dep bundling process and reload the page if needed. ## Monorepos and Linked Dependencies In a monorepo setup, a dependency may be a linked package from the same repo. Vite automatically detects dependencies that are not resolved from `node_modules` and treats the linked dep as source code. It will not attempt to bundle the linked dep, and will analyze the linked dep's dependency list instead. However, this requires the linked dep to be exported as ESM. If not, you can add the dependency to [`optimizeDeps.include`](/config/dep-optimization-options.md#optimizedeps-include) and [`build.commonjsOptions.include`](/config/build-options.md#build-commonjsoptions) in your config. ```js twoslash [vite.config.js] import { defineConfig } from 'vite' // ---cut--- export default defineConfig({ optimizeDeps: { include: ['linked-dep'], }, build: { commonjsOptions: { include: [/linked-dep/, /node_modules/], }, }, }) ``` When making changes to the linked dep, restart the dev server with the `--force` command line option for the changes to take effect. ## Customizing the Behavior The default dependency discovery heuristics may not always be desirable. In cases where you want to explicitly include/exclude dependencies from the list, use the [`optimizeDeps` config options](/config/dep-optimization-options.md). A typical use case for `optimizeDeps.include` or `optimizeDeps.exclude` is when you have an import that is not directly discoverable in the source code. For example, maybe the import is created as a result of a plugin transform. This means Vite won't be able to discover the import on the initial scan - it can only discover it after the file is requested by the browser and transformed. This will cause the server to immediately re-bundle after server start. Both `include` and `exclude` can be used to deal with this. If the dependency is large (with many internal modules) or is CommonJS, then you should include it; If the dependency is small and is already valid ESM, you can exclude it and let the browser load it directly. You can further customize esbuild too with the [`optimizeDeps.esbuildOptions` option](/config/dep-optimization-options.md#optimizedeps-esbuildoptions). For example, adding an esbuild plugin to handle special files in dependencies or changing the [build `target`](https://esbuild.github.io/api/#target). ## Caching ### File System Cache Vite caches the pre-bundled dependencies in `node_modules/.vite`. It determines whether it needs to re-run the pre-bundling step based on a few sources: * Package manager lockfile content, e.g. `package-lock.json`, `yarn.lock`, `pnpm-lock.yaml` or `bun.lockb`. * Patches folder modification time. * Relevant fields in your `vite.config.js`, if present. * `NODE_ENV` value. The pre-bundling step will only need to be re-run when one of the above has changed. If for some reason you want to force Vite to re-bundle deps, you can either start the dev server with the `--force` command line option, or manually delete the `node_modules/.vite` cache directory. ### Browser Cache Resolved dependency requests are strongly cached with HTTP headers `max-age=31536000,immutable` to improve page reload performance during dev. Once cached, these requests will never hit the dev server again. They are auto invalidated by the appended version query if a different version is installed (as reflected in your package manager lockfile). If you want to debug your dependencies by making local edits, you can: 1. Temporarily disable cache via the Network tab of your browser devtools; 2. Restart Vite dev server with the `--force` flag to re-bundle the deps; 3. Reload the page. --- --- url: /guide/static-deploy.md --- # Deploying a Static Site The following guides are based on some shared assumptions: * You are using the default build output location (`dist`). This location [can be changed using `build.outDir`](/config/build-options.md#build-outdir), and you can extrapolate instructions from these guides in that case. * You are using npm. You can use equivalent commands to run the scripts if you are using Yarn or other package managers. * Vite is installed as a local dev dependency in your project, and you have setup the following npm scripts: ```json [package.json] { "scripts": { "build": "vite build", "preview": "vite preview" } } ``` It is important to note that `vite preview` is intended for previewing the build locally and not meant as a production server. ::: tip NOTE These guides provide instructions for performing a static deployment of your Vite site. Vite also supports Server Side Rendering. SSR refers to front-end frameworks that support running the same application in Node.js, pre-rendering it to HTML, and finally hydrating it on the client. Check out the [SSR Guide](./ssr) to learn about this feature. On the other hand, if you are looking for integration with traditional server-side frameworks, check out the [Backend Integration guide](./backend-integration) instead. ::: ## Building the App You may run `npm run build` command to build the app. ```bash $ npm run build ``` By default, the build output will be placed at `dist`. You may deploy this `dist` folder to any of your preferred platforms. ### Testing the App Locally Once you've built the app, you may test it locally by running `npm run preview` command. ```bash $ npm run preview ``` The `vite preview` command will boot up a local static web server that serves the files from `dist` at `http://localhost:4173`. It's an easy way to check if the production build looks OK in your local environment. You may configure the port of the server by passing the `--port` flag as an argument. ```json [package.json] { "scripts": { "preview": "vite preview --port 8080" } } ``` Now the `preview` command will launch the server at `http://localhost:8080`. ## GitHub Pages 1. Set the correct `base` in `vite.config.js`. If you are deploying to `https://.github.io/`, or to a custom domain through GitHub Pages (eg. `www.example.com`), set `base` to `'/'`. Alternatively, you can remove `base` from the configuration, as it defaults to `'/'`. If you are deploying to `https://.github.io//` (eg. your repository is at `https://github.com//`), then set `base` to `'//'`. 2. Go to your GitHub Pages configuration in the repository settings page and choose the source of deployment as "GitHub Actions", this will lead you to create a workflow that builds and deploys your project, a sample workflow that installs dependencies and builds using npm is provided: <<< ./static-deploy-github-pages.yaml#content ## GitLab Pages and GitLab CI 1. Set the correct `base` in `vite.config.js`. If you are deploying to `https://.gitlab.io/`, you can omit `base` as it defaults to `'/'`. If you are deploying to `https://.gitlab.io//`, for example your repository is at `https://gitlab.com//`, then set `base` to `'//'`. 2. Create a file called `.gitlab-ci.yml` in the root of your project with the content below. This will build and deploy your site whenever you make changes to your content: ```yaml [.gitlab-ci.yml] image: node:lts pages: stage: deploy cache: key: files: - package-lock.json prefix: npm paths: - node_modules/ script: - npm install - npm run build - cp -a dist/. public/ artifacts: paths: - public rules: - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH ``` ## Netlify ### Netlify CLI 1. Install the [Netlify CLI](https://cli.netlify.com/). 2. Create a new site using `ntl init`. 3. Deploy using `ntl deploy`. ```bash # Install the Netlify CLI $ npm install -g netlify-cli # Create a new site in Netlify $ ntl init # Deploy to a unique preview URL $ ntl deploy ``` The Netlify CLI will share with you a preview URL to inspect. When you are ready to go into production, use the `prod` flag: ```bash # Deploy the site into production $ ntl deploy --prod ``` ### Netlify with Git 1. Push your code to a git repository (GitHub, GitLab, BitBucket, Azure DevOps). 2. [Import the project](https://app.netlify.com/start) to Netlify. 3. Choose the branch, output directory, and set up environment variables if applicable. 4. Click on **Deploy**. 5. Your Vite app is deployed! After your project has been imported and deployed, all subsequent pushes to branches other than the production branch along with pull requests will generate [Preview Deployments](https://docs.netlify.com/site-deploys/deploy-previews/), and all changes made to the Production Branch (commonly “main”) will result in a [Production Deployment](https://docs.netlify.com/site-deploys/overview/#definitions). ## Vercel ### Vercel CLI 1. Install the [Vercel CLI](https://vercel.com/cli) and run `vercel` to deploy. 2. Vercel will detect that you are using Vite and will enable the correct settings for your deployment. 3. Your application is deployed! (e.g. [vite-vue-template.vercel.app](https://vite-vue-template.vercel.app/)) ```bash $ npm i -g vercel $ vercel init vite Vercel CLI > Success! Initialized "vite" example in ~/your-folder. - To deploy, `cd vite` and run `vercel`. ``` ### Vercel for Git 1. Push your code to your git repository (GitHub, GitLab, Bitbucket). 2. [Import your Vite project](https://vercel.com/new) into Vercel. 3. Vercel will detect that you are using Vite and will enable the correct settings for your deployment. 4. Your application is deployed! (e.g. [vite-vue-template.vercel.app](https://vite-vue-template.vercel.app/)) After your project has been imported and deployed, all subsequent pushes to branches will generate [Preview Deployments](https://vercel.com/docs/concepts/deployments/environments#preview), and all changes made to the Production Branch (commonly “main”) will result in a [Production Deployment](https://vercel.com/docs/concepts/deployments/environments#production). Learn more about Vercel’s [Git Integration](https://vercel.com/docs/concepts/git). ## Cloudflare Pages ### Cloudflare Pages via Wrangler 1. Install [Wrangler CLI](https://developers.cloudflare.com/workers/wrangler/get-started/). 2. Authenticate Wrangler with your Cloudflare account using `wrangler login`. 3. Run your build command. 4. Deploy using `npx wrangler pages deploy dist`. ```bash # Install Wrangler CLI $ npm install -g wrangler # Login to Cloudflare account from CLI $ wrangler login # Run your build command $ npm run build # Create new deployment $ npx wrangler pages deploy dist ``` After your assets are uploaded, Wrangler will give you a preview URL to inspect your site. When you log into the Cloudflare Pages dashboard, you will see your new project. ### Cloudflare Pages with Git 1. Push your code to your git repository (GitHub, GitLab). 2. Log in to the Cloudflare dashboard and select your account in **Account Home** > **Pages**. 3. Select **Create a new Project** and the **Connect Git** option. 4. Select the git project you want to deploy and click **Begin setup** 5. Select the corresponding framework preset in the build setting depending on the Vite framework you have selected. 6. Then save and deploy! 7. Your application is deployed! (e.g `https://.pages.dev/`) After your project has been imported and deployed, all subsequent pushes to branches will generate [Preview Deployments](https://developers.cloudflare.com/pages/platform/preview-deployments/) unless specified not to in your [branch build controls](https://developers.cloudflare.com/pages/platform/branch-build-controls/). All changes to the Production Branch (commonly “main”) will result in a Production Deployment. You can also add custom domains and handle custom build settings on Pages. Learn more about [Cloudflare Pages Git Integration](https://developers.cloudflare.com/pages/get-started/#manage-your-site). ## Google Firebase 1. Make sure you have [firebase-tools](https://www.npmjs.com/package/firebase-tools) installed. 2. Create `firebase.json` and `.firebaserc` at the root of your project with the following content: ```json [firebase.json] { "hosting": { "public": "dist", "ignore": [], "rewrites": [ { "source": "**", "destination": "/index.html" } ] } } ``` ```js [.firebaserc] { "projects": { "default": "" } } ``` 3. After running `npm run build`, deploy using the command `firebase deploy`. ## Surge 1. First install [surge](https://www.npmjs.com/package/surge), if you haven’t already. 2. Run `npm run build`. 3. Deploy to surge by typing `surge dist`. You can also deploy to a [custom domain](http://surge.sh/help/adding-a-custom-domain) by adding `surge dist yourdomain.com`. ## Azure Static Web Apps You can quickly deploy your Vite app with Microsoft Azure [Static Web Apps](https://aka.ms/staticwebapps) service. You need: * An Azure account and a subscription key. You can create a [free Azure account here](https://azure.microsoft.com/free). * Your app code pushed to [GitHub](https://github.com). * The [SWA Extension](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-azurestaticwebapps) in [Visual Studio Code](https://code.visualstudio.com). Install the extension in VS Code and navigate to your app root. Open the Static Web Apps extension, sign in to Azure, and click the '+' sign to create a new Static Web App. You will be prompted to designate which subscription key to use. Follow the wizard started by the extension to give your app a name, choose a framework preset, and designate the app root (usually `/`) and built file location `/dist`. The wizard will run and will create a GitHub action in your repo in a `.github` folder. The action will work to deploy your app (watch its progress in your repo's Actions tab) and, when successfully completed, you can view your app in the address provided in the extension's progress window by clicking the 'Browse Website' button that appears when the GitHub action has run. ## Render You can deploy your Vite app as a Static Site on [Render](https://render.com/). 1. Create a [Render account](https://dashboard.render.com/register). 2. In the [Dashboard](https://dashboard.render.com/), click the **New** button and select **Static Site**. 3. Connect your GitHub/GitLab account or use a public repository. 4. Specify a project name and branch. * **Build Command**: `npm install && npm run build` * **Publish Directory**: `dist` 5. Click **Create Static Site**. Your app should be deployed at `https://.onrender.com/`. By default, any new commit pushed to the specified branch will automatically trigger a new deployment. [Auto-Deploy](https://render.com/docs/deploys#toggling-auto-deploy-for-a-service) can be configured in the project settings. You can also add a [custom domain](https://render.com/docs/custom-domains) to your project. ## Flightcontrol Deploy your static site using [Flightcontrol](https://www.flightcontrol.dev/?ref=docs-vite) by following these [instructions](https://www.flightcontrol.dev/docs/reference/examples/vite?ref=docs-vite). ## Kinsta Static Site Hosting Deploy your static site using [Kinsta](https://kinsta.com/static-site-hosting/) by following these [instructions](https://kinsta.com/docs/react-vite-example/). ## xmit Static Site Hosting Deploy your static site using [xmit](https://xmit.co) by following this [guide](https://xmit.dev/posts/vite-quickstart/). --- --- url: /guide/env-and-mode.md --- # Env Variables and Modes Vite exposes certain constants under the special `import.meta.env` object. These constants are defined as global variables during dev and statically replaced at build time to make tree-shaking effective. ## Built-in Constants Some built-in constants are available in all cases: * **`import.meta.env.MODE`**: {string} the [mode](#modes) the app is running in. * **`import.meta.env.BASE_URL`**: {string} the base url the app is being served from. This is determined by the [`base` config option](/config/shared-options.md#base). * **`import.meta.env.PROD`**: {boolean} whether the app is running in production (running the dev server with `NODE_ENV='production'` or running an app built with `NODE_ENV='production'`). * **`import.meta.env.DEV`**: {boolean} whether the app is running in development (always the opposite of `import.meta.env.PROD`) * **`import.meta.env.SSR`**: {boolean} whether the app is running in the [server](./ssr.md#conditional-logic). ## Env Variables Vite exposes env variables under `import.meta.env` object as strings automatically. To prevent accidentally leaking env variables to the client, only variables prefixed with `VITE_` are exposed to your Vite-processed code. e.g. for the following env variables: ```[.env] VITE_SOME_KEY=123 DB_PASSWORD=foobar ``` Only `VITE_SOME_KEY` will be exposed as `import.meta.env.VITE_SOME_KEY` to your client source code, but `DB_PASSWORD` will not. ```js console.log(import.meta.env.VITE_SOME_KEY) // "123" console.log(import.meta.env.DB_PASSWORD) // undefined ``` If you want to customize the env variables prefix, see the [envPrefix](/config/shared-options.html#envprefix) option. :::tip Env parsing As shown above, `VITE_SOME_KEY` is a number but returns a string when parsed. The same would also happen for boolean env variables. Make sure to convert to the desired type when using it in your code. ::: ### `.env` Files Vite uses [dotenv](https://github.com/motdotla/dotenv) to load additional environment variables from the following files in your [environment directory](/config/shared-options.md#envdir): ``` .env # loaded in all cases .env.local # loaded in all cases, ignored by git .env.[mode] # only loaded in specified mode .env.[mode].local # only loaded in specified mode, ignored by git ``` :::tip Env Loading Priorities An env file for a specific mode (e.g. `.env.production`) will take higher priority than a generic one (e.g. `.env`). Vite will always load `.env` and `.env.local` in addition to the mode-specific `.env.[mode]` file. Variables declared in mode-specific files will take precedence over those in generic files, but variables defined only in `.env` or `.env.local` will still be available in the environment. In addition, environment variables that already exist when Vite is executed have the highest priority and will not be overwritten by `.env` files. For example, when running `VITE_SOME_KEY=123 vite build`. `.env` files are loaded at the start of Vite. Restart the server after making changes. ::: Also, Vite uses [dotenv-expand](https://github.com/motdotla/dotenv-expand) to expand variables written in env files out of the box. To learn more about the syntax, check out [their docs](https://github.com/motdotla/dotenv-expand#what-rules-does-the-expansion-engine-follow). Note that if you want to use `$` inside your environment value, you have to escape it with `\`. ```[.env] KEY=123 NEW_KEY1=test$foo # test NEW_KEY2=test\$foo # test$foo NEW_KEY3=test$KEY # test123 ``` :::warning SECURITY NOTES * `.env.*.local` files are local-only and can contain sensitive variables. You should add `*.local` to your `.gitignore` to avoid them being checked into git. * Since any variables exposed to your Vite source code will end up in your client bundle, `VITE_*` variables should *not* contain any sensitive information. ::: ::: details Expanding variables in reverse order Vite supports expanding variables in reverse order. For example, the `.env` below will be evaluated as `VITE_FOO=foobar`, `VITE_BAR=bar`. ```[.env] VITE_FOO=foo${VITE_BAR} VITE_BAR=bar ``` This does not work in shell scripts and other tools like `docker-compose`. That said, Vite supports this behavior as this has been supported by `dotenv-expand` for a long time and other tools in JavaScript ecosystem uses older versions that supports this behavior. To avoid interop issues, it is recommended to avoid relying on this behavior. Vite may start emitting warnings for this behavior in the future. ::: ## IntelliSense for TypeScript By default, Vite provides type definitions for `import.meta.env` in [`vite/client.d.ts`](https://github.com/vitejs/vite/blob/main/packages/vite/client.d.ts). While you can define more custom env variables in `.env.[mode]` files, you may want to get TypeScript IntelliSense for user-defined env variables that are prefixed with `VITE_`. To achieve this, you can create an `vite-env.d.ts` in `src` directory, then augment `ImportMetaEnv` like this: ```typescript [vite-env.d.ts] /// interface ViteTypeOptions { // By adding this line, you can make the type of ImportMetaEnv strict // to disallow unknown keys. // strictImportMetaEnv: unknown } interface ImportMetaEnv { readonly VITE_APP_TITLE: string // more env variables... } interface ImportMeta { readonly env: ImportMetaEnv } ``` If your code relies on types from browser environments such as [DOM](https://github.com/microsoft/TypeScript/blob/main/src/lib/dom.generated.d.ts) and [WebWorker](https://github.com/microsoft/TypeScript/blob/main/src/lib/webworker.generated.d.ts), you can update the [lib](https://www.typescriptlang.org/tsconfig#lib) field in `tsconfig.json`. ```json [tsconfig.json] { "lib": ["WebWorker"] } ``` :::warning Imports will break type augmentation If the `ImportMetaEnv` augmentation does not work, make sure you do not have any `import` statements in `vite-env.d.ts`. See the [TypeScript documentation](https://www.typescriptlang.org/docs/handbook/2/modules.html#how-javascript-modules-are-defined) for more information. ::: ## HTML Constant Replacement Vite also supports replacing constants in HTML files. Any properties in `import.meta.env` can be used in HTML files with a special `%CONST_NAME%` syntax: ```html

Vite is running in %MODE%

Using data from %VITE_API_URL%

``` If the env doesn't exist in `import.meta.env`, e.g. `%NON_EXISTENT%`, it will be ignored and not replaced, unlike `import.meta.env.NON_EXISTENT` in JS where it's replaced as `undefined`. Given that Vite is used by many frameworks, it is intentionally unopinionated about complex replacements like conditionals. Vite can be extended using [an existing userland plugin](https://github.com/vitejs/awesome-vite#transformers) or a custom plugin that implements the [`transformIndexHtml` hook](./api-plugin#transformindexhtml). ## Modes By default, the dev server (`dev` command) runs in `development` mode and the `build` command runs in `production` mode. This means when running `vite build`, it will load the env variables from `.env.production` if there is one: ```[.env.production] VITE_APP_TITLE=My App ``` In your app, you can render the title using `import.meta.env.VITE_APP_TITLE`. In some cases, you may want to run `vite build` with a different mode to render a different title. You can overwrite the default mode used for a command by passing the `--mode` option flag. For example, if you want to build your app for a staging mode: ```bash vite build --mode staging ``` And create a `.env.staging` file: ```[.env.staging] VITE_APP_TITLE=My App (staging) ``` As `vite build` runs a production build by default, you can also change this and run a development build by using a different mode and `.env` file configuration: ```[.env.testing] NODE_ENV=development ``` ### NODE\_ENV and Modes It's important to note that `NODE_ENV` (`process.env.NODE_ENV`) and modes are two different concepts. Here's how different commands affect the `NODE_ENV` and mode: | Command | NODE\_ENV | Mode | | ---------------------------------------------------- | --------------- | --------------- | | `vite build` | `"production"` | `"production"` | | `vite build --mode development` | `"production"` | `"development"` | | `NODE_ENV=development vite build` | `"development"` | `"production"` | | `NODE_ENV=development vite build --mode development` | `"development"` | `"development"` | The different values of `NODE_ENV` and mode also reflect on its corresponding `import.meta.env` properties: | Command | `import.meta.env.PROD` | `import.meta.env.DEV` | | ---------------------- | ---------------------- | --------------------- | | `NODE_ENV=production` | `true` | `false` | | `NODE_ENV=development` | `false` | `true` | | `NODE_ENV=other` | `false` | `true` | | Command | `import.meta.env.MODE` | | -------------------- | ---------------------- | | `--mode production` | `"production"` | | `--mode development` | `"development"` | | `--mode staging` | `"staging"` | :::tip `NODE_ENV` in `.env` files `NODE_ENV=...` can be set in the command, and also in your `.env` file. If `NODE_ENV` is specified in a `.env.[mode]` file, the mode can be used to control its value. However, both `NODE_ENV` and modes remain as two different concepts. The main benefit with `NODE_ENV=...` in the command is that it allows Vite to detect the value early. It also allows you to read `process.env.NODE_ENV` in your Vite config as Vite can only load the env files once the config is evaluated. ::: --- --- url: /guide/api-environment.md --- # Environment API :::warning Experimental Environment API is experimental. We'll keep the APIs stable during Vite 6 to let the ecosystem experiment and build on top of it. We're planning to stabilize these new APIs with potential breaking changes in Vite 7. Resources: * [Feedback discussion](https://github.com/vitejs/vite/discussions/16358) where we are gathering feedback about the new APIs. * [Environment API PR](https://github.com/vitejs/vite/pull/16471) where the new API were implemented and reviewed. Please share your feedback with us. ::: ## Formalizing Environments Vite 6 formalizes the concept of Environments. Until Vite 5, there were two implicit Environments (`client`, and optionally `ssr`). The new Environment API allows users and framework authors to create as many environments as needed to map the way their apps work in production. This new capability required a big internal refactoring, but a lot of effort has been placed on backward compatibility. The initial goal of Vite 6 is to move the ecosystem to the new major as smoothly as possible, delaying the adoption of these new experimental APIs until enough users have migrated and frameworks and plugin authors have validated the new design. ## Closing the Gap Between Build and Dev For a simple SPA/MPA, no new APIs around environments are exposed to the config. Internally, Vite will apply the options to a `client` environment, but it's not necessary to know of this concept when configuring Vite. The config and behavior from Vite 5 should work seamlessly here. When we move to a typical server-side rendered (SSR) app, we'll have two environments: * `client`: runs the app in the browser. * `server`: runs the app in node (or other server runtimes) which renders pages before sending them to the browser. In dev, Vite executes the server code in the same Node process as the Vite dev server, giving a close approximation to the production environment. However, it is also possible for servers to run in other JS runtimes, like [Cloudflare's workerd](https://github.com/cloudflare/workerd) which have different constraints. Modern apps may also run in more than two environments, e.g. a browser, a node server, and an edge server. Vite 5 didn't allow to properly represent these environments. Vite 6 allows users to configure their app during build and dev to map all of its environments. During dev, a single Vite dev server can now be used to run code in multiple different environments concurrently. The app source code is still transformed by Vite dev server. On top of the shared HTTP server, middlewares, resolved config, and plugins pipeline, the Vite dev server now has a set of independent dev environments. Each of them is configured to match the production environment as closely as possible, and is connected to a dev runtime where the code is executed (for workerd, the server code can now run in miniflare locally). In the client, the browser imports and executes the code. In other environments, a module runner fetches and evaluates the transformed code. ![Vite Environments](../images/vite-environments.svg) ## Environments Configuration For an SPA/MPA, the configuration will look similar to Vite 5. Internally these options are used to configure the `client` environment. ```js export default defineConfig({ build: { sourcemap: false, }, optimizeDeps: { include: ['lib'], }, }) ``` This is important because we'd like to keep Vite approachable and avoid exposing new concepts until they are needed. If the app is composed of several environments, then these environments can be configured explicitly with the `environments` config option. ```js export default { build: { sourcemap: false, }, optimizeDeps: { include: ['lib'], }, environments: { server: {}, edge: { resolve: { noExternal: true, }, }, }, } ``` When not explicitly documented, environment inherits the configured top-level config options (for example, the new `server` and `edge` environments will inherit the `build.sourcemap: false` option). A small number of top-level options, like `optimizeDeps`, only apply to the `client` environment, as they don't work well when applied as a default to server environments. The `client` environment can also be configured explicitly through `environments.client`, but we recommend to do it with the top-level options so the client config remains unchanged when adding new environments. The `EnvironmentOptions` interface exposes all the per-environment options. There are environment options that apply to both `build` and `dev`, like `resolve`. And there are `DevEnvironmentOptions` and `BuildEnvironmentOptions` for dev and build specific options (like `dev.warmup` or `build.outDir`). Some options like `optimizeDeps` only applies to dev, but is kept as top level instead of nested in `dev` for backward compatibility. ```ts interface EnvironmentOptions { define?: Record resolve?: EnvironmentResolveOptions optimizeDeps: DepOptimizationOptions consumer?: 'client' | 'server' dev: DevOptions build: BuildOptions } ``` The `UserConfig` interface extends from the `EnvironmentOptions` interface, allowing to configure the client and defaults for other environments, configured through the `environments` option. The `client` and a server environment named `ssr` are always present during dev. This allows backward compatibility with `server.ssrLoadModule(url)` and `server.moduleGraph`. During build, the `client` environment is always present, and the `ssr` environment is only present if it is explicitly configured (using `environments.ssr` or for backward compatibility `build.ssr`). An app doesn't need to use the `ssr` name for its SSR environment, it could name it `server` for example. ```ts interface UserConfig extends EnvironmentOptions { environments: Record // other options } ``` Note that the `ssr` top-level property is going to be deprecated once the Environment API is stable. This option has the same role as `environments`, but for the default `ssr` environment and only allowed configuring of a small set of options. ## Custom Environment Instances Low level configuration APIs are available so runtime providers can provide environments with proper defaults for their runtimes. These environments can also spawn other processes or threads to run the modules during dev in a closer runtime to the production environment. ```js import { customEnvironment } from 'vite-environment-provider' export default { build: { outDir: '/dist/client', }, environments: { ssr: customEnvironment({ build: { outDir: '/dist/ssr', }, }), }, } ``` ## Backward Compatibility The current Vite server API are not yet deprecated and are backward compatible with Vite 5. The new Environment API is experimental. The `server.moduleGraph` returns a mixed view of the client and ssr module graphs. Backward compatible mixed module nodes will be returned from all its methods. The same scheme is used for the module nodes passed to `handleHotUpdate`. We don't recommend switching to Environment API yet. We are aiming for a good portion of the user base to adopt Vite 6 before so plugins don't need to maintain two versions. Checkout the future breaking changes section for information on future deprecations and upgrade path: * [`this.environment` in Hooks](/changes/this-environment-in-hooks) * [HMR `hotUpdate` Plugin Hook](/changes/hotupdate-hook) * [Move to per-environment APIs](/changes/per-environment-apis) * [SSR using `ModuleRunner` API](/changes/ssr-using-modulerunner) * [Shared plugins during build](/changes/shared-plugins-during-build) ## Target Users This guide provides the basic concepts about environments for end users. Plugin authors have a more consistent API available to interact with the current environment configuration. If you're building on top of Vite, the [Environment API Plugins Guide](./api-environment-plugins.md) guide describes the way extended plugin APIs available to support multiple custom environments. Frameworks could decide to expose environments at different levels. If you're a framework author, continue reading the [Environment API Frameworks Guide](./api-environment-frameworks) to learn about the Environment API programmatic side. For Runtime providers, the [Environment API Runtimes Guide](./api-environment-runtimes.md) explains how to offer custom environment to be consumed by frameworks and users. --- --- url: /guide/api-environment-frameworks.md --- # Environment API for Frameworks :::warning Experimental Environment API is experimental. We'll keep the APIs stable during Vite 6 to let the ecosystem experiment and build on top of it. We're planning to stabilize these new APIs with potential breaking changes in Vite 7. Resources: * [Feedback discussion](https://github.com/vitejs/vite/discussions/16358) where we are gathering feedback about the new APIs. * [Environment API PR](https://github.com/vitejs/vite/pull/16471) where the new API were implemented and reviewed. Please share your feedback with us. ::: ## Environments and frameworks The implicit `ssr` environment and other non-client environments use a `RunnableDevEnvironment` by default during dev. While this requires the runtime to be the same with the one the Vite server is running in, this works similarly with `ssrLoadModule` and allows frameworks to migrate and enable HMR for their SSR dev story. You can guard any runnable environment with an `isRunnableDevEnvironment` function. ```ts export class RunnableDevEnvironment extends DevEnvironment { public readonly runner: ModuleRunner } class ModuleRunner { /** * URL to execute. * Accepts file path, server path, or id relative to the root. * Returns an instantiated module (same as in ssrLoadModule) */ public async import(url: string): Promise> /** * Other ModuleRunner methods... */ } if (isRunnableDevEnvironment(server.environments.ssr)) { await server.environments.ssr.runner.import('/entry-point.js') } ``` :::warning The `runner` is evaluated lazily only when it's accessed for the first time. Beware that Vite enables source map support when the `runner` is created by calling `process.setSourceMapsEnabled` or by overriding `Error.prepareStackTrace` if it's not available. ::: Frameworks that communicate with their runtime via the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch) can utilize the `FetchableDevEnvironment` that provides a standardized way of handling requests via the `handleRequest` method: ```ts import { createServer, createFetchableDevEnvironment, isFetchableDevEnvironment, } from 'vite' const server = await createServer({ server: { middlewareMode: true }, appType: 'custom', environments: { custom: { dev: { createEnvironment(name, config) { return createFetchableDevEnvironment(name, config, { handleRequest(request: Request): Promise | Response { // handle Request and return a Response }, }) }, }, }, }, }) // Any consumer of the environment API can now call `dispatchFetch` if (isFetchableDevEnvironment(server.environments.custom)) { const response: Response = await server.environments.custom.dispatchFetch( new Request('/request-to-handle'), ) } ``` :::warning Vite validates the input and output of the `dispatchFetch` method: the request must be an instance of the global `Request` class and the response must be the instance of the global `Response` class. Vite will throw a `TypeError` if this is not the case. Note that although the `FetchableDevEnvironment` is implemented as a class, it is considered an implementation detail by the Vite team and might change at any moment. ::: ## Default `RunnableDevEnvironment` Given a Vite server configured in middleware mode as described by the [SSR setup guide](/guide/ssr#setting-up-the-dev-server), let's implement the SSR middleware using the environment API. Error handling is omitted. ```js import fs from 'node:fs' import path from 'node:path' import { fileURLToPath } from 'node:url' import { createServer } from 'vite' const __dirname = path.dirname(fileURLToPath(import.meta.url)) const server = await createServer({ server: { middlewareMode: true }, appType: 'custom', environments: { server: { // by default, modules are run in the same process as the vite server }, }, }) // You might need to cast this to RunnableDevEnvironment in TypeScript or // use isRunnableDevEnvironment to guard the access to the runner const environment = server.environments.node app.use('*', async (req, res, next) => { const url = req.originalUrl // 1. Read index.html const indexHtmlPath = path.resolve(__dirname, 'index.html') let template = fs.readFileSync(indexHtmlPath, 'utf-8') // 2. Apply Vite HTML transforms. This injects the Vite HMR client, // and also applies HTML transforms from Vite plugins, e.g. global // preambles from @vitejs/plugin-react template = await server.transformIndexHtml(url, template) // 3. Load the server entry. import(url) automatically transforms // ESM source code to be usable in Node.js! There is no bundling // required, and provides full HMR support. const { render } = await environment.runner.import('/src/entry-server.js') // 4. render the app HTML. This assumes entry-server.js's exported // `render` function calls appropriate framework SSR APIs, // e.g. ReactDOMServer.renderToString() const appHtml = await render(url) // 5. Inject the app-rendered HTML into the template. const html = template.replace(``, appHtml) // 6. Send the rendered HTML back. res.status(200).set({ 'Content-Type': 'text/html' }).end(html) }) ``` ## Runtime Agnostic SSR Since the `RunnableDevEnvironment` can only be used to run the code in the same runtime as the Vite server, it requires a runtime that can run the Vite Server (a runtime that is compatible with Node.js). This means that you will need to use the raw `DevEnvironment` to make it runtime agnostic. :::info `FetchableDevEnvironment` proposal The initial proposal had a `run` method on the `DevEnvironment` class that would allow consumers to invoke an import on the runner side by using the `transport` option. During our testing we found out that the API was not universal enough to start recommending it. At the moment, we are looking for feedback on [the `FetchableDevEnvironment` proposal](https://github.com/vitejs/vite/discussions/18191). ::: `RunnableDevEnvironment` has a `runner.import` function that returns the value of the module. But this function is not available in the raw `DevEnvironment` and requires the code using the Vite's APIs and the user modules to be decoupled. For example, the following example uses the value of the user module from the code using the Vite's APIs: ```ts // code using the Vite's APIs import { createServer } from 'vite' const server = createServer() const ssrEnvironment = server.environment.ssr const input = {} const { createHandler } = await ssrEnvironment.runner.import('./entrypoint.js') const handler = createHandler(input) const response = handler(new Request('/')) // ------------------------------------- // ./entrypoint.js export function createHandler(input) { return function handler(req) { return new Response('hello') } } ``` If your code can run in the same runtime as the user modules (i.e., it does not rely on Node.js-specific APIs), you can use a virtual module. This approach eliminates the need to access the value from the code using Vite's APIs. ```ts // code using the Vite's APIs import { createServer } from 'vite' const server = createServer({ plugins: [ // a plugin that handles `virtual:entrypoint` { name: 'virtual-module', /* plugin implementation */ }, ], }) const ssrEnvironment = server.environment.ssr const input = {} // use exposed functions by each environment factories that runs the code // check for each environment factories what they provide if (ssrEnvironment instanceof RunnableDevEnvironment) { ssrEnvironment.runner.import('virtual:entrypoint') } else if (ssrEnvironment instanceof CustomDevEnvironment) { ssrEnvironment.runEntrypoint('virtual:entrypoint') } else { throw new Error(`Unsupported runtime for ${ssrEnvironment.name}`) } // ------------------------------------- // virtual:entrypoint const { createHandler } = await import('./entrypoint.js') const handler = createHandler(input) const response = handler(new Request('/')) // ------------------------------------- // ./entrypoint.js export function createHandler(input) { return function handler(req) { return new Response('hello') } } ``` For example, to call `transformIndexHtml` on the user module, the following plugin can be used: ```ts {13-21} function vitePluginVirtualIndexHtml(): Plugin { let server: ViteDevServer | undefined return { name: vitePluginVirtualIndexHtml.name, configureServer(server_) { server = server_ }, resolveId(source) { return source === 'virtual:index-html' ? '\0' + source : undefined }, async load(id) { if (id === '\0' + 'virtual:index-html') { let html: string if (server) { this.addWatchFile('index.html') html = fs.readFileSync('index.html', 'utf-8') html = await server.transformIndexHtml('/', html) } else { html = fs.readFileSync('dist/client/index.html', 'utf-8') } return `export default ${JSON.stringify(html)}` } return }, } } ``` If your code requires Node.js APIs, you can use `hot.send` to communicate with the code that uses Vite's APIs from the user modules. However, be aware that this approach may not work the same way after the build process. ```ts // code using the Vite's APIs import { createServer } from 'vite' const server = createServer({ plugins: [ // a plugin that handles `virtual:entrypoint` { name: 'virtual-module', /* plugin implementation */ }, ], }) const ssrEnvironment = server.environment.ssr const input = {} // use exposed functions by each environment factories that runs the code // check for each environment factories what they provide if (ssrEnvironment instanceof RunnableDevEnvironment) { ssrEnvironment.runner.import('virtual:entrypoint') } else if (ssrEnvironment instanceof CustomDevEnvironment) { ssrEnvironment.runEntrypoint('virtual:entrypoint') } else { throw new Error(`Unsupported runtime for ${ssrEnvironment.name}`) } const req = new Request('/') const uniqueId = 'a-unique-id' ssrEnvironment.send('request', serialize({ req, uniqueId })) const response = await new Promise((resolve) => { ssrEnvironment.on('response', (data) => { data = deserialize(data) if (data.uniqueId === uniqueId) { resolve(data.res) } }) }) // ------------------------------------- // virtual:entrypoint const { createHandler } = await import('./entrypoint.js') const handler = createHandler(input) import.meta.hot.on('request', (data) => { const { req, uniqueId } = deserialize(data) const res = handler(req) import.meta.hot.send('response', serialize({ res: res, uniqueId })) }) const response = handler(new Request('/')) // ------------------------------------- // ./entrypoint.js export function createHandler(input) { return function handler(req) { return new Response('hello') } } ``` ## Environments During Build In the CLI, calling `vite build` and `vite build --ssr` will still build the client only and ssr only environments for backward compatibility. When `builder` is not `undefined` (or when calling `vite build --app`), `vite build` will opt-in into building the entire app instead. This would later on become the default in a future major. A `ViteBuilder` instance will be created (build-time equivalent to a `ViteDevServer`) to build all configured environments for production. By default the build of environments is run in series respecting the order of the `environments` record. A framework or user can further configure how the environments are built using: ```js export default { builder: { buildApp: async (builder) => { const environments = Object.values(builder.environments) return Promise.all( environments.map((environment) => builder.build(environment)), ) }, }, } ``` ## Environment Agnostic Code Most of the time, the current `environment` instance will be available as part of the context of the code being run so the need to access them through `server.environments` should be rare. For example, inside plugin hooks the environment is exposed as part of the `PluginContext`, so it can be accessed using `this.environment`. See [Environment API for Plugins](./api-environment-plugins.md) to learn about how to build environment aware plugins. --- --- url: /guide/api-environment-plugins.md --- # Environment API for Plugins :::warning Experimental Environment API is experimental. We'll keep the APIs stable during Vite 6 to let the ecosystem experiment and build on top of it. We're planning to stabilize these new APIs with potential breaking changes in Vite 7. Resources: * [Feedback discussion](https://github.com/vitejs/vite/discussions/16358) where we are gathering feedback about the new APIs. * [Environment API PR](https://github.com/vitejs/vite/pull/16471) where the new API were implemented and reviewed. Please share your feedback with us. ::: ## Accessing the Current Environment in Hooks Given that there were only two Environments until Vite 6 (`client` and `ssr`), a `ssr` boolean was enough to identify the current environment in Vite APIs. Plugin Hooks received a `ssr` boolean in the last options parameter, and several APIs expected an optional last `ssr` parameter to properly associate modules to the correct environment (for example `server.moduleGraph.getModuleByUrl(url, { ssr })`). With the advent of configurable environments, we now have a uniform way to access their options and instance in plugins. Plugin hooks now expose `this.environment` in their context, and APIs that previously expected a `ssr` boolean are now scoped to the proper environment (for example `environment.moduleGraph.getModuleByUrl(url)`). The Vite server has a shared plugin pipeline, but when a module is processed it is always done in the context of a given environment. The `environment` instance is available in the plugin context. A plugin could use the `environment` instance to change how a module is processed depending on the configuration for the environment (which can be accessed using `environment.config`). ```ts transform(code, id) { console.log(this.environment.config.resolve.conditions) } ``` ## Registering New Environments Using Hooks Plugins can add new environments in the `config` hook (for example to have a separate module graph for [RSC](https://react.dev/blog/2023/03/22/react-labs-what-we-have-been-working-on-march-2023#react-server-components)): ```ts config(config: UserConfig) { config.environments.rsc ??= {} } ``` An empty object is enough to register the environment, default values from the root level environment config. ## Configuring Environment Using Hooks While the `config` hook is running, the complete list of environments isn't yet known and the environments can be affected by both the default values from the root level environment config or explicitly through the `config.environments` record. Plugins should set default values using the `config` hook. To configure each environment, they can use the new `configEnvironment` hook. This hook is called for each environment with its partially resolved config including resolution of final defaults. ```ts configEnvironment(name: string, options: EnvironmentOptions) { if (name === 'rsc') { options.resolve.conditions = // ... ``` ## The `hotUpdate` Hook * **Type:** `(this: { environment: DevEnvironment }, options: HotUpdateOptions) => Array | void | Promise | void>` * **See also:** [HMR API](./api-hmr) The `hotUpdate` hook allows plugins to perform custom HMR update handling for a given environment. When a file changes, the HMR algorithm is run for each environment in series according to the order in `server.environments`, so the `hotUpdate` hook will be called multiple times. The hook receives a context object with the following signature: ```ts interface HotUpdateOptions { type: 'create' | 'update' | 'delete' file: string timestamp: number modules: Array read: () => string | Promise server: ViteDevServer } ``` * `this.environment` is the module execution environment where a file update is currently being processed. * `modules` is an array of modules in this environment that are affected by the changed file. It's an array because a single file may map to multiple served modules (e.g. Vue SFCs). * `read` is an async read function that returns the content of the file. This is provided because, on some systems, the file change callback may fire too fast before the editor finishes updating the file, and direct `fs.readFile` will return empty content. The read function passed in normalizes this behavior. The hook can choose to: * Filter and narrow down the affected module list so that the HMR is more accurate. * Return an empty array and perform a full reload: ```js hotUpdate({ modules, timestamp }) { if (this.environment.name !== 'client') return // Invalidate modules manually const invalidatedModules = new Set() for (const mod of modules) { this.environment.moduleGraph.invalidateModule( mod, invalidatedModules, timestamp, true ) } this.environment.hot.send({ type: 'full-reload' }) return [] } ``` * Return an empty array and perform complete custom HMR handling by sending custom events to the client: ```js hotUpdate() { if (this.environment.name !== 'client') return this.environment.hot.send({ type: 'custom', event: 'special-update', data: {} }) return [] } ``` Client code should register the corresponding handler using the [HMR API](./api-hmr) (this could be injected by the same plugin's `transform` hook): ```js if (import.meta.hot) { import.meta.hot.on('special-update', (data) => { // perform custom update }) } ``` ## Per-environment Plugins A plugin can define what are the environments it should apply to with the `applyToEnvironment` function. ```js const UnoCssPlugin = () => { // shared global state return { buildStart() { // init per-environment state with WeakMap // using this.environment }, configureServer() { // use global hooks normally }, applyToEnvironment(environment) { // return true if this plugin should be active in this environment, // or return a new plugin to replace it. // if the hook is not used, the plugin is active in all environments }, resolveId(id, importer) { // only called for environments this plugin apply to }, } } ``` If a plugin isn't environment aware and has state that isn't keyed on the current environment, the `applyToEnvironment` hook allows to easily make it per-environment. ```js import { nonShareablePlugin } from 'non-shareable-plugin' export default defineConfig({ plugins: [ { name: 'per-environment-plugin', applyToEnvironment(environment) { return nonShareablePlugin({ outputName: environment.name }) }, }, ], }) ``` Vite exports a `perEnvironmentPlugin` helper to simplify these cases where no other hooks are required: ```js import { nonShareablePlugin } from 'non-shareable-plugin' export default defineConfig({ plugins: [ perEnvironmentPlugin('per-environment-plugin', (environment) => nonShareablePlugin({ outputName: environment.name }), ), ], }) ``` ## Environment in Build Hooks In the same way as during dev, plugin hooks also receive the environment instance during build, replacing the `ssr` boolean. This also works for `renderChunk`, `generateBundle`, and other build only hooks. ## Shared Plugins During Build Before Vite 6, the plugins pipelines worked in a different way during dev and build: * **During dev:** plugins are shared * **During Build:** plugins are isolated for each environment (in different processes: `vite build` then `vite build --ssr`). This forced frameworks to share state between the `client` build and the `ssr` build through manifest files written to the file system. In Vite 6, we are now building all environments in a single process so the way the plugins pipeline and inter-environment communication can be aligned with dev. In a future major (Vite 7 or 8), we aim to have complete alignment: * **During both dev and build:** plugins are shared, with [per-environment filtering](#per-environment-plugins) There will also be a single `ResolvedConfig` instance shared during build, allowing for caching at entire app build process level in the same way as we have been doing with `WeakMap` during dev. For Vite 6, we need to do a smaller step to keep backward compatibility. Ecosystem plugins are currently using `config.build` instead of `environment.config.build` to access configuration, so we need to create a new `ResolvedConfig` per-environment by default. A project can opt-in into sharing the full config and plugins pipeline setting `builder.sharedConfigBuild` to `true`. This option would only work of a small subset of projects at first, so plugin authors can opt-in for a particular plugin to be shared by setting the `sharedDuringBuild` flag to `true`. This allows for easily sharing state both for regular plugins: ```js function myPlugin() { // Share state among all environments in dev and build const sharedState = ... return { name: 'shared-plugin', transform(code, id) { ... }, // Opt-in into a single instance for all environments sharedDuringBuild: true, } } ``` --- --- url: /guide/api-environment-runtimes.md --- # Environment API for Runtimes :::warning Experimental Environment API is experimental. We'll keep the APIs stable during Vite 6 to let the ecosystem experiment and build on top of it. We're planning to stabilize these new APIs with potential breaking changes in Vite 7. Resources: * [Feedback discussion](https://github.com/vitejs/vite/discussions/16358) where we are gathering feedback about the new APIs. * [Environment API PR](https://github.com/vitejs/vite/pull/16471) where the new API were implemented and reviewed. Please share your feedback with us. ::: ## Environment Factories Environments factories are intended to be implemented by Environment providers like Cloudflare, and not by end users. Environment factories return a `EnvironmentOptions` for the most common case of using the target runtime for both dev and build environments. The default environment options can also be set so the user doesn't need to do it. ```ts function createWorkerdEnvironment( userConfig: EnvironmentOptions, ): EnvironmentOptions { return mergeConfig( { resolve: { conditions: [ /*...*/ ], }, dev: { createEnvironment(name, config) { return createWorkerdDevEnvironment(name, config, { hot: true, transport: customHotChannel(), }) }, }, build: { createEnvironment(name, config) { return createWorkerdBuildEnvironment(name, config) }, }, }, userConfig, ) } ``` Then the config file can be written as: ```js import { createWorkerdEnvironment } from 'vite-environment-workerd' export default { environments: { ssr: createWorkerdEnvironment({ build: { outDir: '/dist/ssr', }, }), rsc: createWorkerdEnvironment({ build: { outDir: '/dist/rsc', }, }), }, } ``` and frameworks can use an environment with the workerd runtime to do SSR using: ```js const ssrEnvironment = server.environments.ssr ``` ## Creating a New Environment Factory A Vite dev server exposes two environments by default: a `client` environment and an `ssr` environment. The client environment is a browser environment by default, and the module runner is implemented by importing the virtual module `/@vite/client` to client apps. The SSR environment runs in the same Node runtime as the Vite server by default and allows application servers to be used to render requests during dev with full HMR support. The transformed source code is called a module, and the relationships between the modules processed in each environment are kept in a module graph. The transformed code for these modules is sent to the runtimes associated with each environment to be executed. When a module is evaluated in the runtime, its imported modules will be requested triggering the processing of a section of the module graph. A Vite Module Runner allows running any code by processing it with Vite plugins first. It is different from `server.ssrLoadModule` because the runner implementation is decoupled from the server. This allows library and framework authors to implement their layer of communication between the Vite server and the runner. The browser communicates with its corresponding environment using the server Web Socket and through HTTP requests. The Node Module runner can directly do function calls to process modules as it is running in the same process. Other environments could run modules connecting to a JS runtime like workerd, or a Worker Thread as Vitest does. One of the goals of this feature is to provide a customizable API to process and run code. Users can create new environment factories using the exposed primitives. ```ts import { DevEnvironment, HotChannel } from 'vite' function createWorkerdDevEnvironment( name: string, config: ResolvedConfig, context: DevEnvironmentContext ) { const connection = /* ... */ const transport: HotChannel = { on: (listener) => { connection.on('message', listener) }, send: (data) => connection.send(data), } const workerdDevEnvironment = new DevEnvironment(name, config, { options: { resolve: { conditions: ['custom'] }, ...context.options, }, hot: true, transport, }) return workerdDevEnvironment } ``` ## `ModuleRunner` A module runner is instantiated in the target runtime. All APIs in the next section are imported from `vite/module-runner` unless stated otherwise. This export entry point is kept as lightweight as possible, only exporting the minimal needed to create module runners. **Type Signature:** ```ts export class ModuleRunner { constructor( public options: ModuleRunnerOptions, public evaluator: ModuleEvaluator = new ESModulesEvaluator(), private debug?: ModuleRunnerDebugger, ) {} /** * URL to execute. * Accepts file path, server path, or id relative to the root. */ public async import(url: string): Promise /** * Clear all caches including HMR listeners. */ public clearCache(): void /** * Clear all caches, remove all HMR listeners, reset sourcemap support. * This method doesn't stop the HMR connection. */ public async close(): Promise /** * Returns `true` if the runner has been closed by calling `close()`. */ public isClosed(): boolean } ``` The module evaluator in `ModuleRunner` is responsible for executing the code. Vite exports `ESModulesEvaluator` out of the box, it uses `new AsyncFunction` to evaluate the code. You can provide your own implementation if your JavaScript runtime doesn't support unsafe evaluation. Module runner exposes `import` method. When Vite server triggers `full-reload` HMR event, all affected modules will be re-executed. Be aware that Module Runner doesn't update `exports` object when this happens (it overrides it), you would need to run `import` or get the module from `evaluatedModules` again if you rely on having the latest `exports` object. **Example Usage:** ```js import { ModuleRunner, ESModulesEvaluator } from 'vite/module-runner' import { transport } from './rpc-implementation.js' const moduleRunner = new ModuleRunner( { transport, }, new ESModulesEvaluator(), ) await moduleRunner.import('/src/entry-point.js') ``` ## `ModuleRunnerOptions` ```ts twoslash import type { InterceptorOptions as InterceptorOptionsRaw, ModuleRunnerHmr as ModuleRunnerHmrRaw, EvaluatedModules, } from 'vite/module-runner' import type { Debug } from '@type-challenges/utils' type InterceptorOptions = Debug type ModuleRunnerHmr = Debug /** see below */ type ModuleRunnerTransport = unknown // ---cut--- interface ModuleRunnerOptions { /** * A set of methods to communicate with the server. */ transport: ModuleRunnerTransport /** * Configure how source maps are resolved. * Prefers `node` if `process.setSourceMapsEnabled` is available. * Otherwise it will use `prepareStackTrace` by default which overrides * `Error.prepareStackTrace` method. * You can provide an object to configure how file contents and * source maps are resolved for files that were not processed by Vite. */ sourcemapInterceptor?: | false | 'node' | 'prepareStackTrace' | InterceptorOptions /** * Disable HMR or configure HMR options. * * @default true */ hmr?: boolean | ModuleRunnerHmr /** * Custom module cache. If not provided, it creates a separate module * cache for each module runner instance. */ evaluatedModules?: EvaluatedModules } ``` ## `ModuleEvaluator` **Type Signature:** ```ts twoslash import type { ModuleRunnerContext as ModuleRunnerContextRaw } from 'vite/module-runner' import type { Debug } from '@type-challenges/utils' type ModuleRunnerContext = Debug // ---cut--- export interface ModuleEvaluator { /** * Number of prefixed lines in the transformed code. */ startOffset?: number /** * Evaluate code that was transformed by Vite. * @param context Function context * @param code Transformed code * @param id ID that was used to fetch the module */ runInlinedModule( context: ModuleRunnerContext, code: string, id: string, ): Promise /** * evaluate externalized module. * @param file File URL to the external module */ runExternalModule(file: string): Promise } ``` Vite exports `ESModulesEvaluator` that implements this interface by default. It uses `new AsyncFunction` to evaluate code, so if the code has inlined source map it should contain an [offset of 2 lines](https://tc39.es/ecma262/#sec-createdynamicfunction) to accommodate for new lines added. This is done automatically by the `ESModulesEvaluator`. Custom evaluators will not add additional lines. ## `ModuleRunnerTransport` **Type Signature:** ```ts twoslash import type { ModuleRunnerTransportHandlers } from 'vite/module-runner' /** an object */ type HotPayload = unknown // ---cut--- interface ModuleRunnerTransport { connect?(handlers: ModuleRunnerTransportHandlers): Promise | void disconnect?(): Promise | void send?(data: HotPayload): Promise | void invoke?(data: HotPayload): Promise<{ result: any } | { error: any }> timeout?: number } ``` Transport object that communicates with the environment via an RPC or by directly calling the function. When `invoke` method is not implemented, the `send` method and `connect` method is required to be implemented. Vite will construct the `invoke` internally. You need to couple it with the `HotChannel` instance on the server like in this example where module runner is created in the worker thread: ::: code-group ```js [worker.js] import { parentPort } from 'node:worker_threads' import { fileURLToPath } from 'node:url' import { ESModulesEvaluator, ModuleRunner } from 'vite/module-runner' /** @type {import('vite/module-runner').ModuleRunnerTransport} */ const transport = { connect({ onMessage, onDisconnection }) { parentPort.on('message', onMessage) parentPort.on('close', onDisconnection) }, send(data) { parentPort.postMessage(data) }, } const runner = new ModuleRunner( { transport, }, new ESModulesEvaluator(), ) ``` ```js [server.js] import { BroadcastChannel } from 'node:worker_threads' import { createServer, RemoteEnvironmentTransport, DevEnvironment } from 'vite' function createWorkerEnvironment(name, config, context) { const worker = new Worker('./worker.js') const handlerToWorkerListener = new WeakMap() const workerHotChannel = { send: (data) => worker.postMessage(data), on: (event, handler) => { if (event === 'connection') return const listener = (value) => { if (value.type === 'custom' && value.event === event) { const client = { send(payload) { worker.postMessage(payload) }, } handler(value.data, client) } } handlerToWorkerListener.set(handler, listener) worker.on('message', listener) }, off: (event, handler) => { if (event === 'connection') return const listener = handlerToWorkerListener.get(handler) if (listener) { worker.off('message', listener) handlerToWorkerListener.delete(handler) } }, } return new DevEnvironment(name, config, { transport: workerHotChannel, }) } await createServer({ environments: { worker: { dev: { createEnvironment: createWorkerEnvironment, }, }, }, }) ``` ::: A different example using an HTTP request to communicate between the runner and the server: ```ts import { ESModulesEvaluator, ModuleRunner } from 'vite/module-runner' export const runner = new ModuleRunner( { transport: { async invoke(data) { const response = await fetch(`http://my-vite-server/invoke`, { method: 'POST', body: JSON.stringify(data), }) return response.json() }, }, hmr: false, // disable HMR as HMR requires transport.connect }, new ESModulesEvaluator(), ) await runner.import('/entry.js') ``` In this case, the `handleInvoke` method in the `NormalizedHotChannel` can be used: ```ts const customEnvironment = new DevEnvironment(name, config, context) server.onRequest((request: Request) => { const url = new URL(request.url) if (url.pathname === '/invoke') { const payload = (await request.json()) as HotPayload const result = customEnvironment.hot.handleInvoke(payload) return new Response(JSON.stringify(result)) } return Response.error() }) ``` But note that for HMR support, `send` and `connect` methods are required. The `send` method is usually called when the custom event is triggered (like, `import.meta.hot.send("my-event")`). Vite exports `createServerHotChannel` from the main entry point to support HMR during Vite SSR. --- --- url: /guide/features.md --- # Features At the very basic level, developing using Vite is not that different from using a static file server. However, Vite provides many enhancements over native ESM imports to support various features that are typically seen in bundler-based setups. ## npm Dependency Resolving and Pre-Bundling Native ES imports do not support bare module imports like the following: ```js import { someMethod } from 'my-dep' ``` The above will throw an error in the browser. Vite will detect such bare module imports in all served source files and perform the following: 1. [Pre-bundle](./dep-pre-bundling) them to improve page loading speed and convert CommonJS / UMD modules to ESM. The pre-bundling step is performed with [esbuild](http://esbuild.github.io/) and makes Vite's cold start time significantly faster than any JavaScript-based bundler. 2. Rewrite the imports to valid URLs like `/node_modules/.vite/deps/my-dep.js?v=f3sf2ebd` so that the browser can import them properly. **Dependencies are Strongly Cached** Vite caches dependency requests via HTTP headers, so if you wish to locally edit/debug a dependency, follow the steps [here](./dep-pre-bundling#browser-cache). ## Hot Module Replacement Vite provides an [HMR API](./api-hmr) over native ESM. Frameworks with HMR capabilities can leverage the API to provide instant, precise updates without reloading the page or blowing away application state. Vite provides first-party HMR integrations for [Vue Single File Components](https://github.com/vitejs/vite-plugin-vue/tree/main/packages/plugin-vue) and [React Fast Refresh](https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-react). There are also official integrations for Preact via [@prefresh/vite](https://github.com/JoviDeCroock/prefresh/tree/main/packages/vite). Note you don't need to manually set these up - when you [create an app via `create-vite`](./), the selected templates would have these pre-configured for you already. ## TypeScript Vite supports importing `.ts` files out of the box. ### Transpile Only Note that Vite only performs transpilation on `.ts` files and does **NOT** perform type checking. It assumes type checking is taken care of by your IDE and build process. The reason Vite does not perform type checking as part of the transform process is because the two jobs work fundamentally differently. Transpilation can work on a per-file basis and aligns perfectly with Vite's on-demand compile model. In comparison, type checking requires knowledge of the entire module graph. Shoe-horning type checking into Vite's transform pipeline will inevitably compromise Vite's speed benefits. Vite's job is to get your source modules into a form that can run in the browser as fast as possible. To that end, we recommend separating static analysis checks from Vite's transform pipeline. This principle applies to other static analysis checks such as ESLint. * For production builds, you can run `tsc --noEmit` in addition to Vite's build command. * During development, if you need more than IDE hints, we recommend running `tsc --noEmit --watch` in a separate process, or use [vite-plugin-checker](https://github.com/fi3ework/vite-plugin-checker) if you prefer having type errors directly reported in the browser. Vite uses [esbuild](https://github.com/evanw/esbuild) to transpile TypeScript into JavaScript which is about 20~30x faster than vanilla `tsc`, and HMR updates can reflect in the browser in under 50ms. Use the [Type-Only Imports and Export](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export) syntax to avoid potential problems like type-only imports being incorrectly bundled, for example: ```ts import type { T } from 'only/types' export type { T } ``` ### TypeScript Compiler Options Some configuration fields under `compilerOptions` in `tsconfig.json` require special attention. #### `isolatedModules` * [TypeScript documentation](https://www.typescriptlang.org/tsconfig#isolatedModules) Should be set to `true`. It is because `esbuild` only performs transpilation without type information, it doesn't support certain features like const enum and implicit type-only imports. You must set `"isolatedModules": true` in your `tsconfig.json` under `compilerOptions`, so that TS will warn you against the features that do not work with isolated transpilation. If a dependency doesn't work well with `"isolatedModules": true`. You can use `"skipLibCheck": true` to temporarily suppress the errors until it is fixed upstream. #### `useDefineForClassFields` * [TypeScript documentation](https://www.typescriptlang.org/tsconfig#useDefineForClassFields) The default value will be `true` if the TypeScript target is `ES2022` or newer including `ESNext`. It is consistent with the [behavior of TypeScript 4.3.2+](https://github.com/microsoft/TypeScript/pull/42663). Other TypeScript targets will default to `false`. `true` is the standard ECMAScript runtime behavior. If you are using a library that heavily relies on class fields, please be careful about the library's intended usage of it. While most libraries expect `"useDefineForClassFields": true`, you can explicitly set `useDefineForClassFields` to `false` if your library doesn't support it. #### `target` * [TypeScript documentation](https://www.typescriptlang.org/tsconfig#target) Vite ignores the `target` value in the `tsconfig.json`, following the same behavior as `esbuild`. To specify the target in dev, the [`esbuild.target`](/config/shared-options.html#esbuild) option can be used, which defaults to `esnext` for minimal transpilation. In builds, the [`build.target`](/config/build-options.html#build-target) option takes higher priority over `esbuild.target` and can also be set if needed. ::: warning `useDefineForClassFields` If `target` in `tsconfig.json` is not `ESNext` or `ES2022` or newer, or if there's no `tsconfig.json` file, `useDefineForClassFields` will default to `false` which can be problematic with the default `esbuild.target` value of `esnext`. It may transpile to [static initialization blocks](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Static_initialization_blocks#browser_compatibility) which may not be supported in your browser. As such, it is recommended to set `target` to `ESNext` or `ES2022` or newer, or set `useDefineForClassFields` to `true` explicitly when configuring `tsconfig.json`. ::: #### Other Compiler Options Affecting the Build Result * [`extends`](https://www.typescriptlang.org/tsconfig#extends) * [`importsNotUsedAsValues`](https://www.typescriptlang.org/tsconfig#importsNotUsedAsValues) * [`preserveValueImports`](https://www.typescriptlang.org/tsconfig#preserveValueImports) * [`verbatimModuleSyntax`](https://www.typescriptlang.org/tsconfig#verbatimModuleSyntax) * [`jsx`](https://www.typescriptlang.org/tsconfig#jsx) * [`jsxFactory`](https://www.typescriptlang.org/tsconfig#jsxFactory) * [`jsxFragmentFactory`](https://www.typescriptlang.org/tsconfig#jsxFragmentFactory) * [`jsxImportSource`](https://www.typescriptlang.org/tsconfig#jsxImportSource) * [`experimentalDecorators`](https://www.typescriptlang.org/tsconfig#experimentalDecorators) * [`alwaysStrict`](https://www.typescriptlang.org/tsconfig#alwaysStrict) ::: tip `skipLibCheck` Vite starter templates have `"skipLibCheck": "true"` by default to avoid typechecking dependencies, as they may choose to only support specific versions and configurations of TypeScript. You can learn more at [vuejs/vue-cli#5688](https://github.com/vuejs/vue-cli/pull/5688). ::: ### Client Types Vite's default types are for its Node.js API. To shim the environment of client side code in a Vite application, add a `d.ts` declaration file: ```typescript /// ``` ::: details Using `compilerOptions.types` Alternatively, you can add `vite/client` to `compilerOptions.types` inside `tsconfig.json`: ```json [tsconfig.json] { "compilerOptions": { "types": ["vite/client", "some-other-global-lib"] } } ``` Note that if [`compilerOptions.types`](https://www.typescriptlang.org/tsconfig#types) is specified, only these packages will be included in the global scope (instead of all visible ”@types” packages). ::: `vite/client` provides the following type shims: * Asset imports (e.g. importing an `.svg` file) * Types for the Vite-injected [constants](./env-and-mode#env-variables) on `import.meta.env` * Types for the [HMR API](./api-hmr) on `import.meta.hot` ::: tip To override the default typing, add a type definition file that contains your typings. Then, add the type reference before `vite/client`. For example, to make the default import of `*.svg` a React component: * `vite-env-override.d.ts` (the file that contains your typings): ```ts declare module '*.svg' { const content: React.FC> export default content } ``` * The file containing the reference to `vite/client`: ```ts /// /// ``` ::: ## HTML HTML files stand [front-and-center](/guide/#index-html-and-project-root) of a Vite project, serving as the entry points for your application, making it simple to build single-page and [multi-page applications](/guide/build.html#multi-page-app). Any HTML files in your project root can be directly accessed by its respective directory path: * `/index.html` -> `http://localhost:5173/` * `/about.html` -> `http://localhost:5173/about.html` * `/blog/index.html` -> `http://localhost:5173/blog/index.html` Assets referenced by HTML elements such as ` ``` To opt-out of HTML processing on certain elements, you can add the `vite-ignore` attribute on the element, which can be useful when referencing external assets or CDN. ## Frameworks All modern frameworks maintain integrations with Vite. Most framework plugins are maintained by each framework team, with the exception of the official Vue and React Vite plugins that are maintained in the vite org: * Vue support via [@vitejs/plugin-vue](https://github.com/vitejs/vite-plugin-vue/tree/main/packages/plugin-vue) * Vue JSX support via [@vitejs/plugin-vue-jsx](https://github.com/vitejs/vite-plugin-vue/tree/main/packages/plugin-vue-jsx) * React support via [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-react) * React using SWC support via [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) Check out the [Plugins Guide](https://vite.dev/plugins) for more information. ## JSX `.jsx` and `.tsx` files are also supported out of the box. JSX transpilation is also handled via [esbuild](https://esbuild.github.io). Your framework of choice will already configure JSX out of the box (for example, Vue users should use the official [@vitejs/plugin-vue-jsx](https://github.com/vitejs/vite-plugin-vue/tree/main/packages/plugin-vue-jsx) plugin, which provides Vue 3 specific features including HMR, global component resolving, directives and slots). If using JSX with your own framework, custom `jsxFactory` and `jsxFragment` can be configured using the [`esbuild` option](/config/shared-options.md#esbuild). For example, the Preact plugin would use: ```js twoslash [vite.config.js] import { defineConfig } from 'vite' export default defineConfig({ esbuild: { jsxFactory: 'h', jsxFragment: 'Fragment', }, }) ``` More details in [esbuild docs](https://esbuild.github.io/content-types/#jsx). You can inject the JSX helpers using `jsxInject` (which is a Vite-only option) to avoid manual imports: ```js twoslash [vite.config.js] import { defineConfig } from 'vite' export default defineConfig({ esbuild: { jsxInject: `import React from 'react'`, }, }) ``` ## CSS Importing `.css` files will inject its content to the page via a `