Vue Component Documentation API

4.79.2 · active · verified Sun Apr 19

vue-docgen-api is a robust toolbox designed to programmatically extract detailed structural and behavioral information from Vue component files, whether they are Single File Components (SFCs), JavaScript, or TypeScript. It leverages `@babel/parser` to analyze component code, providing insights into props, events, slots, and methods, which is crucial for automated documentation generation. The current stable version is `4.79.2`, with a consistent release cadence of patch and minor updates, indicating active development and responsiveness to bug fixes and new Vue features (like `defineEmits` syntax in Vue 3.3). Its key differentiators include a highly configurable API with options for custom handlers to extend parsing logic, support for aliased paths, and the ability to handle both single and multiple component exports within a single file. This makes it an essential utility for projects building design systems, component libraries, or custom documentation platforms, often used as the core parser for tools like `vue-styleguidist`.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `parseSource` to extract documentation information from a Vue 3 component defined as a string, including props, emits, and methods. It also shows how to configure `jsx` and `alias` options.

import { parseSource } from 'vue-docgen-api';
import { resolve } from 'path';

async function generateComponentDoc() {
  const componentSource = `
    <template>
      <div>{{ message }} <button @click="increment">Click me</button></div>
    </template>
    <script lang="ts">
    import { defineComponent, ref } from 'vue';

    /**
     * A simple counter component demonstrating props, emits, and methods.
     * @displayName MyCounterComponent
     */
    export default defineComponent({
      props: {
        /**
         * Initial count value for the component.
         */
        initialCount: {
          type: Number,
          default: 0
        },
        /**
         * Message to display alongside the count.
         */
        message: {
          type: String,
          required: true
        }
      },
      emits: {
        /**
         * Emitted when the count value changes.
         * @property {number} newCount - The updated count value.
         */
        'update:count': (newCount: number) => newCount >= 0
      },
      setup(props, { emit }) {
        const count = ref(props.initialCount);

        /**
         * Increments the internal count and emits the updated value.
         */
        function increment() {
          count.value++;
          emit('update:count', count.value);
        }

        return {
          count,
          increment
        };
      }
    });
    </script>
    `;

  try {
    // The second argument 'MyCounter.vue' is a dummy path used for resolution contexts.
    const componentInfo = await parseSource(componentSource, 'MyCounter.vue', {
      jsx: true, // Enable JSX parsing if your components use it
      alias: {
        '@': resolve(__dirname, './src') // Configure path aliases similar to webpack or tsconfig
      }
    });
    console.log('Component Display Name:', componentInfo.displayName);
    console.log('Props:', componentInfo.props?.map(p => `${p.name} (${p.type?.name || 'any'})`));
    console.log('Events:', componentInfo.events?.map(e => e.name));
    // console.log(JSON.stringify(componentInfo, null, 2)); // Uncomment for full output
  } catch (error) {
    console.error('Error parsing component:', error);
  }
}

generateComponentDoc();

view raw JSON →