Vue Define Options Macro Unplugin

3.1.2 · active · verified Sun Apr 19

unplugin-vue-define-options is a build tool macro that enables the use of the `defineOptions` API within Vue 3's `<script setup>` syntax. This macro allows developers to define component options like `name`, `props`, `emits`, and `render` directly within the setup script, bridging the gap between Options API and Composition API for certain configurations. It is currently at version 3.1.2 and is part of the broader Vue Macros project, which sees frequent updates and feature additions, often with minor version bumps and occasional major releases (like v3.0.0) that introduce breaking changes. Its key differentiators include broad bundler support (Vite, Webpack, Rollup, esbuild via unplugin), full TypeScript compatibility, and support for both Vue 2.7 (though dropping in v3) and Vue 3.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to configure `unplugin-vue-define-options` in a Vite project and use the `defineOptions` macro in a Vue Single File Component (SFC) to set component options like `name`.

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import DefineOptions from 'unplugin-vue-define-options/vite';

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

// src/components/MyComponent.vue
// <script setup lang="ts">
// import { ref } from 'vue';
//
// defineOptions({
//   name: 'MyComponent',
//   inheritAttrs: false,
//   customOption: 'hello'
// });
//
// const count = ref(0);
//
// const increment = () => {
//   count.value++;
// };
//
// </script>
//
// <template>
//   <div>
//     <p>Count: {{ count }}</p>
//     <button @click="increment">Increment</button>
//   </div>
// </template>

view raw JSON →