Common Vite Plugins

Learn what Vite plugins do and how to install and use official Vue plugins and community plugins.

🎯 Introduction

By the end of this article, you will understand the role of Vite plugins, install and use official plugins for Vue and older browser compatibility, and know where to find community plugins. Plugins are the main way to extend Vite.


🧱 What Is a Plugin?

Vite itself handles the core development server and build workflow. Projects also need features such as converting .vue files and TypeScript to JavaScript, compressing images, and generating PWAs. Plugins provide these capabilities.

Plugins are like apps on a phone: the phone can make calls on its own, but navigation, music, and video require the corresponding apps.

Vite plugins generally come from three sources:

  • Official plugins: names begin with @vitejs/plugin-.
  • Framework plugins: examples include @vitejs/plugin-vue.
  • Community plugins: third-party plugins with names beginning with vite-plugin-.

✨ Common Official Plugins

@vitejs/plugin-vue

Essential for Vue 3 projects, this plugin lets Vite process .vue single-file components.

npm install -D @vitejs/plugin-vue
vite.config.js
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';

export default defineConfig({
    plugins: [vue()],
});

@vitejs/plugin-legacy

This plugin makes built code compatible with older browsers, such as IE11, by generating additional polyfill bundles for them.

npm install -D @vitejs/plugin-legacy
vite.config.js
import { defineConfig } from 'vite';
import legacy from '@vitejs/plugin-legacy';

export default defineConfig({
    plugins: [
        legacy({
            targets: ['defaults', 'not IE 11'],
        }),
    ],
});
You can specify browser ranges such as ['ie >= 11'] in targets. Refer to Browserslist for the syntax.

🛠 A Community Plugin Example

There are many community plugins. Here is one common use case:

unplugin-auto-import

Automatically import frequently used APIs, such as Vue's ref, computed, and watch, without writing an import in every file.

npm install -D unplugin-auto-import
vite.config.js
import { defineConfig } from 'vite';
import AutoImport from 'unplugin-auto-import/vite';

export default defineConfig({
    plugins: [
        AutoImport({
            imports: ['vue'],
        }),
    ],
});
Automatic imports are convenient, but beginners should write import statements manually first. Once you are familiar with them, use the plugin to simplify your code; otherwise, it can be hard to tell where an API comes from.

❓ Questions and Answers

Q1: Should plugins go in dependencies or devDependencies?

Build tool plugins generally belong in devDependencies because they run only during development and builds, rather than becoming part of the final output.

Q2: How do Vite plugins differ from Webpack loaders and plugins?

They serve a similar purpose: extending the build workflow. Vite plugins follow the Rollup plugin API, providing a more consistent format.

Q3: Does plugin order matter?

Usually it does not, but some plugins depend on others. Follow the order recommended in their official documentation.


🧪 Exercise

Add @vitejs/plugin-vue and @vitejs/plugin-legacy to your Vue project:

  1. Install both plugins.
  2. Register them in vite.config.js.
  3. Run npm run build and check whether additional legacy files appear in dist.
vite.config.js
import { defineConfig } from 'vite';

// Import and register the plugins here

🎉 You have learned how to use Vite plugins! Next, we will cover environment variables and modes.