Adding a Plugin
To use a plugin, it needs to be added to thedevDependencies of the project and included in the plugins array in the vite.config.js config file.
Install the plugin
For example, to provide support for legacy browsers, install the official
@vitejs/plugin-legacy:plugins also accepts presets including several plugins as a single element. This is useful for complex features (like framework integration) that are implemented using several plugins. The array will be flattened internally.Falsy plugins will be ignored, which can be used to easily activate or deactivate plugins.Finding Plugins
Check out the Plugins section for information about official plugins. Community plugins that are published to npm are listed in Vite Plugin Registry.Enforcing Plugin Ordering
For compatibility with some Rollup plugins, it may be needed to enforce the order of the plugin or only apply at build time. This should be an implementation detail for Vite plugins. You can enforce the position of a plugin with theenforce modifier:
pre: invoke plugin before Vite core plugins- default: invoke plugin after Vite core plugins
post: invoke plugin after Vite build plugins
vite.config.js
Check out Plugins API Guide for detailed information.
Conditional Application
By default, plugins are invoked for both serve and build. In cases where a plugin needs to be conditionally applied only during serve or build, use theapply property to only invoke them during 'build' or 'serve':
vite.config.js