Vite's Plugin System: Extending Dev and Build

Vite's plugin system extends its dev server and build process, leveraging Rollup's mature ecosystem. Use it to add framework support or handle custom assets. The footgun is forgetting to control execution order with enforce, causing compatibility issues.
Why it exists
Vite provides fast, out-of-the-box support for common web patterns, but no single tool can cover every project's specific needs. The plugin system exists to provide a standardized way to extend Vite's core functionality, from transforming source code to adding custom server middleware, without bloating the core tool itself.
The mental model
Think of Vite's plugin system as a set of hooks into its development server and build pipeline. Because it's based on the well-established Rollup plugin API, you get access to a huge ecosystem of existing tools that work with Vite. Vite supercharges this API with its own hooks, giving you control over the dev server and server-side rendering, not just the final bundle.
How it works
You add a plugin to your project's devDependencies and then import it into your vite.config.js file, adding it to the plugins array. Configuration happens through options passed to the plugin function. For more control, you can use two key properties: apply and enforce. apply: 'serve' or apply: 'build' restricts the plugin to run only during development or production builds, respectively. enforce: 'pre' or enforce: 'post' forces a plugin to run before or after Vite's core plugins, which is crucial for resolving compatibility issues.
When to use it
Use plugins when Vite's built-in features aren't enough. Common scenarios include: integrating a specific UI framework (like @vitejs/plugin-react), handling non-standard file types (like @rollup/plugin-image), or adding support for older browsers that lack modern JavaScript features (like @vitejs/plugin-legacy).
When not to use it
Before reaching for a plugin, always check if Vite handles your use case natively. Many tasks that required a plugin in other bundlers, like handling CSS or JSON imports, are built directly into Vite. Overusing plugins for simple tasks can complicate your configuration and potentially slow down your dev server.
One canonical example
To add support for older browsers that don't support modern module syntax, you can use the official legacy plugin. First, install it: npm add -D @vitejs/plugin-legacy. Then, in vite.config.js, import it and add it to the plugins array, specifying which browsers to target. For example: plugins: [legacy({ targets: ['defaults', 'not IE 11'] })]. This tells Vite to generate a separate legacy bundle and polyfills during the build process.
Interview question
When configuring a Vite plugin, for what primary reason would you use the 'enforce' property?
- a.To define the specific options or parameters the plugin should use.
- b.To specify whether the plugin runs during development or production builds.
- c.To indicate that the plugin should only apply to certain file types or assets.
- d.To control the plugin's execution order relative to other plugins and Vite's core.Correct
Why? this is the answer
The card states that 'enforce: 'pre' or enforce: 'post' forces a plugin to run before or after Vite's core plugins, which is crucial for resolving compatibility issues.' The 'apply' property is used to restrict a plugin to run only during development or production builds.
Just read this? Test yourself on what you have been reading.
Read the original → vite.dev
- #vite
- #build-tools
- #rollup
- #javascript
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.
See open roles