Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

babel-plugin-react-docgen

storybooks3.9mMIT4.2.1TypeScript support: definitely-typed

Babel plugin to add react-docgen info into your code

react, docs, docgen, babel-plugin

readme

babel-plugin-react-docgen

react-docgen allows you to write propType descriptions, class descriptions and access propType metadata programatically.

This babel plugin allow you to access those information right inside your React class.

For an example, let's say you've a React class like this:

/**
  This is an awesome looking button for React.
*/
import React from 'react';

export default class Button extends React.Component {
  render() {
    const { label, onClick } = this.props;
    return (
      <button onClick={onClick}>{ label }</button>
    );
  }
}

Button.propTypes = {
  /**
    Label for the button.
  */
  label: React.PropTypes.string,

  /**
    Triggered when clicked on the button.
  */
  onClick: React.PropTypes.func,
};

With this babel plugin, you can access all these information right inside your app with:

console.log(Button.__docgenInfo);
<summary>Click to see the output</summary>
  {
    description: 'This is an awesome looking button for React.',
    props: {
      label: {
        type: {
          name: 'string'
        },
        required: false,
        description: 'Label for the button.'
      },
      onClick: {
        type: {
          name: 'func'
        },
        required: false,
        description: 'Triggered when clicked on the button.'
      }
    }
  }

This will be pretty useful for documentations and some other React devtools like Storybook.

Usage

Install the plugin:

npm install -D babel-plugin-react-docgen

Use it inside your .babelrc

{
  "plugins": ["react-docgen"]
}

.babelrc Options

option description default
resolver You may use the 3 built-in react-docgen resolvers by specifying its name as a string, or you may specify a custom resolver by specifying the function explicitly. "findAllExportedComponentDefinition"
handlers All react-docgen handlers are automatically applied. However, custom handlers can be added by specifying them here. Any string value will be loaded by require, and a function will be used directly.
removeMethods Used to remove docgen information about methods. false
DOC_GEN_COLLECTION_NAME The name of a global variable where all docgen information can be stored. See below for more information.
...options Remaining options will be passed directly as react-docgen options. Any options they allowed will be passed through, but the filename will be overwritten by the filename provided by babel.

Collect All Docgen Info

Sometimes, it's a pretty good idea to collect all of the docgen info into a collection. Then you could use that to render style guide or similar.

So, we allow you to collect all the docgen info into a global collection. To do that, add following config to when loading this babel plugin:

{
  "plugins":[
    [
      "babel-plugin-react-docgen",
      {
        "DOC_GEN_COLLECTION_NAME": "MY_REACT_DOCS",
        "resolver": "findAllComponentDefinitions", // optional (default: findAllExportedComponentDefinitions)
        "removeMethods": true, // optional (default: false)
        "handlers": ["react-docgen-deprecation-handler"] // optional array of custom handlers
      }
    ]
  ]
}

Then you need to create a global variable(an object) in your app called MY_REACT_DOCS before any code get's executed. Then we'll save them into that object. We do it by adding a code block like this to the transpiled file:

if (typeof MY_REACT_DOCS !== 'undefined') {
  MY_REACT_DOCS['test/fixtures/case4/actual.js'] = {
    name: 'Button',
    docgenInfo: Button.__docgenInfo,
    path: 'path/to/my/button.js'
  };
}

Compile Performance

We parse your code with react-docgen to get this info, but we only do it for files which contain a React component.

There will be some overhead to your project, but you can leverage babel's cache directory to avoid this a huge performance hit.

Output Size

Yes this increase the output size of your transpiled files. The size increase varies depending on various factors like:

  • How many react classes you've
  • Amount of docs you've written
  • Amount of propTypes you've

Most of the time, you need this plugin when you are developing your app or with another tool like Storybook. So, you may not need to use this on the production version of your app.

changelog

ChangeLog

v4.2.1

13-10-2020

Bug fixes:

  • Fixed support for optional chaining #92

v4.2.0

24-09-2020

New Features:

  • Allow more configuration of react-docgen #88

Dependencies:

  • Bump acorn from 5.7.3 to 5.7.4 #89
  • Bump yargs-parser from 13.1.1 to 13.1.2 #87
  • Bump lodash from 4.17.15 to 4.17.19 #86

v4.1.0

14-01-2020

New Features:

  • Pass filename to React Docgen for better babel transpilation #81

v4.0.0

03-01-2020

Breaking changes:

Maintenance:

  • Overhaul tests to use jest instead of mocha

v3.1.0

16-04-2019

New Features:

  • Add support for custom React Docgen handlers #64, #65

v3.0.0

27-03-2019

Breaking changes:

Bug fixes:

  • Fixes #67 where forwardRef wrapped components are not detected
  • Upgrade to `lodash@4.17.10` to fix security vulnerability

v2.0.2

29-01-2019

  • Fixes #62 where recast is not in the right dependency tree

v2.0.1

29-01-2019

  • Updated to use release version of react-docgen@^3.0.0

v2.0.0

Breaking changes:

  • Use findAllExportedComponentDefinitions by default to generate info for named exports
  • Default to not remove method info and changed .babelrc key to removeMethods

Bug fixes:

  • Fix for named export using incorrect local name in export default ComponentName PR38
  • Relies on react-docgen for more React component detection functionalities PR54
  • Fix crash on name PR58
  • Upgraded to Babel 7 and react-docgen 3.0-rc.1 PR59

v1.9.0

04-April-2018

  • Use react-docgen 3.0-beta for enhanced Flow support

v1.8.3

28-February-2018

  • Add support for module.exports = className declaration PR47

v1.8.2

14-January-2018

  • Add support for stateless functional components declared as function(){ } PR41

v1.8.1

24-September-2017

  • Suppress errors caught during babel traversal PR37

v1.8.0

24-September-2017

  • Add support for custom resolvers
  • Add option to keep method info from docgen PR35

v1.7.0

11-August-2017

  • Add support for Higher Order Components with arbitrary depth PR32

v1.6.0

26-July-2017

  • Add support for React.createElement PR31

v1.5.0

06-June-2017

  • Uses docgen 2.15.0 and babel-types 6.24.1
  • Add support for components created with React.createClass or createReactClass PR27

v1.4.2

03-January-2017

Add support for hypen propTypes.

v1.4.1

03-November-2016

Fixes #19

v1.4.0

01-November-2016

Handle multiple components in the same file by checking with exported classes. PR17

v1.3.1

23-October-2016

  • Restrict JSX lookup only for direct JSX returns. PR15

v1.3.1

21-October-2016

Update the react-docgen NPM module to the latest as they fixed the bug related to default values.

v1.3.0

20-October-2016

  • Use docgen version 2.11.0. PR11
  • Rename DOC_GEN_GLOBAL to DOC_GEN_COLLECTION_NAME. PR12
  • Update the README. PR13

v1.1.0

20-October-2016

Initial public release.

v1.2.0

20-October-2016

  • Stateless component support
  • Global object with all the component docs
  • __docgenInfo is now an actual object instead of a JSON string