Unplugin Vue 3 SFC Transformer
unplugin-vue is a versatile utility designed to transform Vue 3 Single File Components (SFCs) into executable JavaScript across various build tools. Currently stable at version 7.1.1, it sees regular updates, often with weekly or bi-weekly bug fixes and minor feature enhancements, mirroring its close ties to the @vitejs/plugin-vue codebase. Its core strength lies in its "unplugin" architecture, enabling a single plugin to integrate seamlessly with Vite, Webpack, Rollup, esbuild, Rspack, Farm, and Rolldown. This broad compatibility differentiates it significantly from bundler-specific alternatives. It fully supports modern Vue features like <script setup> and component macros, and notably provides Hot Module Replacement (HMR) for Vite-based projects. Developers leverage it to process .vue files, making it an essential part of the Vue 3 development ecosystem for cross-platform build environments.
Common errors
-
TypeError: (0 , unplugin_vue_vite__WEBPACK_IMPORTED_MODULE_0__.default) is not a function
cause Attempting to import the plugin as a named export (`import { Vue } from '...'`) when it's provided as a default export.fixChange your import statement to `import Vue from 'unplugin-vue/vite'` (or the relevant bundler path) to correctly import the default export. -
Error: Cannot find module 'unplugin-vue/vite' or its corresponding type declarations.
cause The package is not installed, or an incorrect bundler-specific path is used (e.g., 'unplugin-vue/vite' when using Webpack), or missing TypeScript types.fixEnsure `unplugin-vue` is installed (`npm i -D unplugin-vue`) and you are using the correct import path for your build tool (e.g., `unplugin-vue/webpack` for Webpack, `unplugin-vue/rollup` for Rollup). -
Error: [unplugin-vue] Missing peer dependency "vue@^3.2.25"
cause The project's `vue` version does not satisfy the `unplugin-vue` peer dependency requirements.fixInstall a compatible version of Vue 3: `npm install vue@latest` or specifically `npm install vue@3.x.x` that matches the peer dependency range.
Warnings
- gotcha Hot Module Replacement (HMR) is currently not supported when using `unplugin-vue` with Webpack, Vue CLI, or Rspack. HMR is fully functional only for Vite environments.
- breaking `unplugin-vue` requires Node.js version 20.19.0 or higher. Older Node.js versions will result in installation or runtime errors.
- gotcha The package has a peer dependency on `vue` at `^3.2.25`. Ensure your project's `vue` version meets this requirement to avoid potential conflicts or unexpected behavior during SFC compilation.
- breaking Breaking changes and new features in `@vitejs/plugin-vue` are periodically synced into `unplugin-vue`. This means that upgrading `unplugin-vue` might implicitly introduce breaking changes from upstream, even if not explicitly listed in its own changelog.
Install
-
npm install unplugin-vue -
yarn add unplugin-vue -
pnpm add unplugin-vue
Imports
- Vue
import { Vue } from 'unplugin-vue/vite'import Vue from 'unplugin-vue/vite'
- Vue
const Vue = require('unplugin-vue/esbuild')import Vue from 'unplugin-vue/esbuild'
- Vue
import { default as Vue } from 'unplugin-vue/webpack'import Vue from 'unplugin-vue/webpack'
Quickstart
import { defineConfig } from 'vite';
import Vue from 'unplugin-vue/vite';
export default defineConfig({
plugins: [
Vue({
/* options */
}),
],
// Example of a basic Vue component (src/App.vue)
// <template>
// <div>Hello, Vue!</div>
// </template>
// <script setup>
// import { ref } from 'vue';
// const msg = ref('World');
// </script>
// <style scoped>
// div { color: blue; }
// </style>
});