Babel Plugin for React TypeScript Docgen

1.5.1 · active · verified Sun Apr 19

babel-plugin-react-docgen-typescript is a Babel plugin designed to extract component documentation (docgen data) from React components written in TypeScript. It works by integrating into the Babel compilation pipeline, identifying and parsing TypeScript React files to generate metadata about their props, types, and descriptions. This data is typically consumed by documentation tools like Storybook to automatically generate prop tables and API documentation. The plugin, currently at version 1.5.1, explicitly leverages the `react-docgen-typescript` library for its core parsing logic. While functional, the plugin's documentation notes it's 'a bit of a hack' and can be 'very inefficient' if not properly configured with `include` and `exclude` regular expressions to limit the scope of files it processes. It does not specify a fixed release cadence but generally follows updates in the `react-docgen-typescript` or Babel ecosystems. Its main differentiator is its direct integration into Babel builds, offering a streamlined approach to collecting docgen data without requiring separate parsing steps.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to install `babel-plugin-react-docgen-typescript` and configure it in your `.babelrc` to extract documentation from TypeScript React components, useful for tools like Storybook.

// 1. Install necessary packages:
// npm install --save-dev babel-plugin-react-docgen-typescript @babel/core typescript react @babel/preset-typescript @babel/preset-react

// 2. Configure your .babelrc (or babel.config.js):
// .babelrc
const babelConfig = {
  plugins: [
    [
      "babel-plugin-react-docgen-typescript",
      {
        // Name of the global object where docgen data will be collected.
        // Useful for integration with tools like Storybook.
        "docgenCollectionName": "STORYBOOK_REACT_CLASSES",
        // Regex to include files for parsing. Crucial for performance.
        "include": "src/components/.*\\.tsx$",
        // Regex to exclude files, e.g., stories or test files.
        "exclude": "src/components/.*\\.stories\\.tsx$"
      }
    ]
  ],
  // Add these presets if you're working with TypeScript and React
  "presets": ["@babel/preset-typescript", "@babel/preset-react"]
};

// 3. Example TypeScript React Component (e.g., src/components/Button.tsx):
/*
import React from 'react';

interface ButtonProps {
  /**
   * The text content for the button.
   */
  label: string;
  /**
   * Callback when the button is clicked.
   */
  onClick: () => void;
}

/**
 * A simple button component with JSDoc comments for docgen.
 */
export const Button: React.FC<ButtonProps> = ({ label, onClick }) => {
  return (
    <button onClick={onClick}>
      {label}
    </button>
  );
};
*/

// During the Babel compilation process, this plugin will extract
// documentation from 'Button.tsx' (if it matches 'include') and store it 
// in 'global.STORYBOOK_REACT_CLASSES'. This data can then be consumed by 
// documentation tools like Storybook to generate prop tables.

view raw JSON →