vite build command. By default, it uses <root>/index.html as the build entry point, and produces an application bundle that is suitable to be served over a static hosting service.
Browser Compatibility
By default, the production bundle assumes a modern browser that is included in the Baseline Widely Available targets. The default browser support range is:- Chrome >=111
- Edge >=111
- Firefox >=114
- Safari >=16.4
build.target config option, 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 and import.meta:
- Chrome >=64
- Firefox >=67
- Safari >=11.1
- Edge >=79
Legacy browsers can be supported via @vitejs/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
If you are deploying your project under a nested public path, simply specify thebase config option 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).
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.
Customizing the Build
The build can be customized via various build config options. Specifically, you can directly adjust the underlying Rolldown options viabuild.rolldownOptions:
vite.config.js
Chunking Strategy
You can configure how chunks are split usingbuild.rolldownOptions.output.codeSplitting (see Rolldown docs). If you use a framework, refer to their documentation for configuring how chunks are split.
Load Error Handling
Vite emitsvite: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.
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. In this case, make sure to set
Cache-Control: no-cache on the HTML file, otherwise the old assets will be still referenced.Rebuild on File Changes
You can enable rollup watcher withvite build --watch. Or, you can directly adjust the underlying WatcherOptions via build.watch:
vite.config.js
With the
--watch flag enabled, changes to files to be bundled will trigger a rebuild. Note that changes to the config and its dependencies require restarting the build command.Multi-Page App
Suppose you have the following source code structure:/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:
vite.config.js
For HTML files, Vite ignores the name given to the entry in the
rolldownOptions.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 yourindex.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. Make sure to also externalize any dependencies that you do not want to bundle into your library, e.g. vue or react:
- Single Entry
- Multiple Entries
vite.config.js
lib/main.js
vite build with this config uses a Rollup preset that is oriented towards shipping libraries and produces two bundle formats:
esandumd(for single entry)esandcjs(for multiple entries)
build.lib.formats option.
package.json for your lib:
- Single Entry
- Multiple Entries
package.json
CSS Support in Libraries
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.
You can export the CSS file in your package.json to be imported by users:
package.json
File ExtensionsIf 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.Advanced Base Options
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
experimental.renderBuiltUrl.
vite.config.ts
type included in the second context param given to the function.
vite.config.ts
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.