Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 38 ยท Plugins

Official and Community Vite Plugins

A guided tour of the Vite plugins you will actually use: framework plugins, legacy browser support, PWA, compression, SVG, checker and inspect, with config examples.

You rarely write plugins from scratch. The Vite ecosystem already covers the common needs, and knowing which plugin exists saves hours. This lesson is a map: what each plugin family does, how to configure it, and how to judge a community plugin before adding it to your project. All configuration snippets are real Vite and read-only; a runnable stand-in at the end shows how a plugin list is resolved and ordered.

Framework plugins

Vite's core only knows plain JavaScript, CSS and static assets. Framework syntax comes from official plugins:

Plugin For What it adds
@vitejs/plugin-react React JSX transform, Fast Refresh
@vitejs/plugin-react-swc React Same, using the SWC compiler
@vitejs/plugin-vue Vue 3 Single-file components
@vitejs/plugin-vue-jsx Vue 3 JSX in Vue
@sveltejs/vite-plugin-svelte Svelte .svelte compiler
@vitejs/plugin-legacy Any Support for old browsers

A typical config:

JavaScript
// vite.config.js (real Vite)
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

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

The React plugin enables Fast Refresh, which keeps component state when you edit a file. It also lets you configure Babel plugins when you need something like the React Compiler.

Supporting older browsers

By default Vite targets modern browsers. When you must support older ones, add the legacy plugin:

JavaScript
// real Vite
import legacy from '@vitejs/plugin-legacy';

export default defineConfig({
  plugins: [legacy({ targets: ['defaults', 'not IE 11'] })],
});

It produces a second, transpiled bundle plus polyfills, loaded only by browsers that need it through nomodule scripts. The cost is a larger build, so check your analytics before enabling it. See Build Targets and Browser Support for the cheaper alternative, lowering build.target.

Plugins by need

Progressive web app. vite-plugin-pwa generates a service worker and a web app manifest and wires up update prompts.

Compression. vite-plugin-compression2 writes .gz or .br files next to your assets, so a server like nginx can serve them pre-compressed.

SVG as components. vite-plugin-svgr lets you write import Logo from './logo.svg?react' and use it as a component.

Type and lint checks in dev. Vite does not type-check. vite-plugin-checker runs tsc, ESLint or vue-tsc in a worker and shows errors in the overlay.

Inspecting the pipeline. vite-plugin-inspect adds a page at /__inspect/ showing every transform each module went through. It is the best debugging tool when a file looks wrong in the browser.

Bundle analysis. rollup-plugin-visualizer writes an HTML treemap of your bundle. See Bundle Analysis and Performance.

Auto imports and file routing. unplugin-auto-import, unplugin-vue-components and file-based router plugins reduce boilerplate. Use them with care: implicit imports make code harder to read for newcomers.

A combined config for an app might look like this:

JavaScript
// real Vite
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { visualizer } from 'rollup-plugin-visualizer';
import checker from 'vite-plugin-checker';

export default defineConfig(({ mode }) => ({
  plugins: [
    react(),
    checker({ typescript: true }),
    mode === 'analyze' && visualizer({ open: true, gzipSize: true }),
  ],
}));

Falsy values in the array are ignored, so condition && plugin() is a safe pattern. Vite also flattens nested arrays, which lets a package export a bundle of several plugins as one.

Unplugin: write once, run in many tools

Some community plugins come from the unplugin project. An unplugin has one source that works in Vite, Rollup, webpack, esbuild and Rspack. If you maintain a plugin that should help users of other bundlers too, this is worth a look.

How to judge a community plugin

  1. Maintenance. Last release, open issues, and whether the README names the Vite major version you use.
  2. Size and dependencies. A plugin runs in your build. Check what it pulls in.
  3. Does it already exist in core? Vite handles JSON, CSS modules, ?raw, ?url and import.meta.glob natively. Many old plugins are now unnecessary.
  4. Ordering needs. Some plugins require enforce: 'pre'; the README should say so.
  5. Rollup-only or Vite-ready. Rollup plugins work in the build, but ones that touch dev-server behavior need Vite-specific hooks.

A runnable stand-in: flattening and ordering

This stand-in normalizes a plugin list the way Vite does: it removes falsy values, flattens arrays and sorts by the enforce field.

JS Stand-in: resolving the plugins array

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The order printed is the order the hooks run. Note that the core plugins sit between pre and normal plugins, which is why a pre plugin sees raw source.

JS Stand-in: pick plugins for a project

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which plugin gives a React project Fast Refresh?

    @vitejs/plugin-react handles JSX and Fast Refresh (the SWC variant does the same with a different compiler).
  2. 2. Does Vite type-check TypeScript during dev?

    Vite transpiles TypeScript without checking it. A checker plugin or a separate tsc run reports type errors.
  3. 3. What is a safe way to enable a plugin only in an 'analyze' mode?

    Vite ignores falsy values in the plugins array, so a conditional expression works.
  4. 4. Which tool shows every transform a module went through?

    vite-plugin-inspect serves a page at /__inspect/ with the transform steps of each module.

Summary

  • Framework support comes from plugins such as plugin-react and plugin-vue.
  • Use plugin-legacy only if you must support old browsers; consider lowering build.target first.
  • Handy extras: PWA, compression, SVGR, checker, inspect and visualizer.
  • Falsy values and nested arrays in plugins are allowed, and enforce decides ordering.
  • Vet community plugins for maintenance, size and whether core already covers the need.

Next: The Production Build