Vue Macros: Enhanced SFC Syntax Sugar

3.1.2 · active · verified Sun Apr 19

Vue Macros is a collection of experimental and advanced compiler macros and syntax sugar designed to significantly enhance the development experience for Vue.js Single File Components (SFCs). It serves as a playground for new language features and proposals that extend Vue's capabilities beyond its official API, often simplifying boilerplate and improving reactivity patterns. The current stable version is 3.1.2, with an active release cadence reflecting ongoing development and frequent updates. Key differentiators include its modular nature, allowing developers to selectively enable specific macros, and its integration as an `unplugin`, providing broad compatibility across various build tools like Vite, Rollup, Webpack, and Nuxt. It brings features like `defineOptions`, `defineModels`, and `definePropsRefs` that streamline component definition and data flow, offering type safety and improved developer ergonomics.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to configure Vue Macros in a Vite project and use the `defineOptions` macro in a Vue 3 SFC to set component options directly within `<script setup>`.

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import VueMacros from 'unplugin-vue-macros/vite';

export default defineConfig({
  plugins: [
    VueMacros({
      plugins: {
        vue: vue(), // Required for Vue 3 projects
      },
    }),
  ],
});

// src/components/MyComponent.vue
<script setup lang="ts">
import { ref } from 'vue';

defineOptions({
  name: 'MyComponent',
  inheritAttrs: false,
});

const count = ref(0);
const increment = () => count.value++;
</script>

<template>
  <div>
    <h1>{{ defineOptions.name }} Example</h1>
    <p>Count: {{ count }}</p>
    <button @click="increment">Increment</button>
  </div>
</template>

<style scoped>
div {
  padding: 1em;
  border: 1px solid #eee;
  border-radius: 8px;
  text-align: center;
}
button {
  margin-top: 10px;
  padding: 0.5em 1em;
  font-size: 1em;
  cursor: pointer;
}
</style>

view raw JSON →