Vue Macros Unplugin
unplugin-vue-macros is a comprehensive collection of macros and syntax sugar designed to enhance the developer experience in Vue.js projects. It operates as an `unplugin`, providing broad compatibility across various bundlers like Vite, Webpack, Rollup, and esbuild. This library introduces experimental and advanced features that may not yet be officially adopted by Vue's core, simplifying common patterns and offering more expressive ways to write components. The current stable version, as per the recent changelog, is v3.1.2. The project maintains an active release cadence, frequently pushing minor bug fixes and features, with significant breaking changes typically occurring in major version bumps. Key differentiators include its modular design, allowing developers to pick and choose specific macros, its seamless integration via the `unplugin` system, and robust TypeScript and Volar support.
Common errors
-
Error: Cannot find module 'unplugin-vue-macros'
cause Attempting to import the plugin without specifying the bundler-specific entry point.fixChange the import statement to include the bundler, e.g., `import VueMacros from 'unplugin-vue-macros/vite'` for Vite. -
ReferenceError: defineOptions is not defined
cause The `defineOptions` macro or other Vue Macros features are not recognized by the compiler or IDE.fixEnsure `unplugin-vue-macros` is correctly configured in your bundler and that TypeScript type references (`tsconfig.json` `types` array or `/// <reference>`) are set up for `macros-global`. -
Vue 3: 'defineEmits' is not defined in current scope (when using shortEmits)
cause The `shortEmits` macro was disabled in Vue 3.3 for Vue Macros v3, but the older syntax is still being used.fixUpdate your `defineEmits` syntax to the standard Vue 3.3+ approach, or verify if an alternative macro is available and enabled in your configuration. -
Node.js version x.y.z is not supported. Required Node.js >=20.19.0
cause Your Node.js environment does not meet the minimum requirement for Vue Macros v3.fixUpgrade your Node.js version to 20.19.0 or higher.
Warnings
- breaking Version 3.0.0 of unplugin-vue-macros introduced significant breaking changes, dropping support for Vue 2, Webpack 4, and Node.js versions 16 and 18. Users must migrate their projects to Vue 3, Webpack 5+, and Node.js 20.19.0+ when upgrading to v3.
- breaking The `shortEmits` macro functionality was disabled in Vue 3.3 for `unplugin-vue-macros` v3.0.0.
- breaking With the v3 release, the minimum Node.js requirement was raised to `20.19.0`.
- gotcha When integrating `unplugin-vue-macros` into a bundler, you must use the correct bundler-specific entry point (e.g., `unplugin-vue-macros/vite`, `unplugin-vue-macros/webpack`).
- gotcha To ensure proper TypeScript support for global macros (like `defineOptions`, `defineModels`), you need to configure `tsconfig.json` to include the `macros-global` types.
Install
-
npm install unplugin-vue-macros -
yarn add unplugin-vue-macros -
pnpm add unplugin-vue-macros
Imports
- VueMacros
import VueMacros from 'unplugin-vue-macros'
import VueMacros from 'unplugin-vue-macros/vite'
- VueMacrosResolver
import { VueMacrosResolver } from 'unplugin-vue-macros'import { VueMacrosResolver } from 'unplugin-vue-macros/dist/resolvers' - macros-global.d.ts
import 'unplugin-vue-macros/macros-global'
/// <reference types="unplugin-vue-macros/macros-global" />
Quickstart
import { defineConfig } from 'vite';
import Vue from '@vitejs/plugin-vue';
import VueMacros from 'unplugin-vue-macros/vite';
import AutoImport from 'unplugin-auto-import/vite';
export default defineConfig({
plugins: [
VueMacros({
// The `plugins` option is essential for Vue Macros to correctly process Vue SFCs.
// It should include the `@vitejs/plugin-vue` instance.
plugins: {
vue: Vue(),
// vueJsx: VueJsx(), // Uncomment if you are using JSX
},
// You can also enable/disable specific macros here
defineOptions: true,
defineModels: true,
}),
// Optional: AutoImport can be used to automatically import Vue Macros APIs
AutoImport({
imports: ['vue', '@vue/composition-api'],
dts: './auto-imports.d.ts',
macros: {
// Enable definePropsRefs if you use it globally or want auto-import
// definePropsRefs: true,
},
eslintrc: {
enabled: true,
filepath: './.eslintrc-auto-import.json',
},
}),
],
});