Using Plugins
Vite can be extended using plugins, which are based on Rollup's well-designed plugin interface with a few extra Vite-specific options. This means that Vite users can rely on the mature ecosystem of Rollup plugins, while also being able to extend the dev server and SSR functionality as needed.
Adding a Plugin
To use a plugin, it needs to be added to the devDependencies of the project and included in the plugins array in the vite.config.js config file. For example, to provide support for legacy browsers, the official @vitejs/plugin-legacy can be used:
$ npm add -D @vitejs/plugin-legacyimport legacyimport legacy from '@vitejs/plugin-legacy'
import { defineConfigfunction defineConfig(config: UserConfig): UserConfig (+5 overloads)Type helper to make it easier to use vite.config.ts
accepts a direct
UserConfig
object, or a function that returns it.
The function receives a
ConfigEnv
object.
} from 'vite'
export default defineConfigfunction defineConfig(config: UserConfig): UserConfig (+5 overloads)Type helper to make it easier to use vite.config.ts
accepts a direct
UserConfig
object, or a function that returns it.
The function receives a
ConfigEnv
object.
({
pluginsUserConfig.plugins?: PluginOption[] | undefinedArray of vite plugins to use.
: [
legacyimport legacy({
targetstargets: string[]: ['defaults', 'not IE 11'],
}),
],
})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
NOTE
Vite aims to provide out-of-the-box support for common web development patterns. Before searching for a Vite or compatible Rollup plugin, check out the Features Guide. A lot of the cases where a plugin would be needed in a Rollup project are already covered in Vite.
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 the enforce modifier:
pre: invoke plugin before Vite core plugins- default: invoke plugin after Vite core plugins
post: invoke plugin after Vite build plugins
import imageimport image from '@rollup/plugin-image'
import { defineConfigfunction defineConfig(config: UserConfig): UserConfig (+5 overloads)Type helper to make it easier to use vite.config.ts
accepts a direct
UserConfig
object, or a function that returns it.
The function receives a
ConfigEnv
object.
} from 'vite'
export default defineConfigfunction defineConfig(config: UserConfig): UserConfig (+5 overloads)Type helper to make it easier to use vite.config.ts
accepts a direct
UserConfig
object, or a function that returns it.
The function receives a
ConfigEnv
object.
({
pluginsUserConfig.plugins?: PluginOption[] | undefinedArray of vite plugins to use.
: [
{
...imageimport image(),
enforcePlugin<any>.enforce?: "pre" | "post" | undefinedEnforce plugin invocation tier similar to webpack loaders. Hooks ordering
is still subject to the order property in the hook object.
Plugin invocation order:
- alias resolution
enforce: 'pre' plugins
- vite core plugins
- normal plugins
- vite build plugins
enforce: 'post' plugins
- vite build post plugins
: 'pre',
},
],
})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 the apply property to only invoke them during 'build' or 'serve':
import typescript2import typescript2 from 'rollup-plugin-typescript2'
import { defineConfigfunction defineConfig(config: UserConfig): UserConfig (+5 overloads)Type helper to make it easier to use vite.config.ts
accepts a direct
UserConfig
object, or a function that returns it.
The function receives a
ConfigEnv
object.
} from 'vite'
export default defineConfigfunction defineConfig(config: UserConfig): UserConfig (+5 overloads)Type helper to make it easier to use vite.config.ts
accepts a direct
UserConfig
object, or a function that returns it.
The function receives a
ConfigEnv
object.
({
pluginsUserConfig.plugins?: PluginOption[] | undefinedArray of vite plugins to use.
: [
{
...typescript2import typescript2(),
applyPlugin<any>.apply?: "build" | "serve" | ((this: void, config: UserConfig, env: ConfigEnv) => boolean) | undefinedApply the plugin only for serve or build, or on certain conditions.
: 'build',
},
],
})Building Plugins
Check out the Plugins API Guide for documentation about creating plugins.
