storybooks/babel-plugin-react-docgen

Name: babel-plugin-react-docgen

Owner: Storybook

Description: Babel plugin to add react-docgen info into your code.

Created: 2016-10-06 12:18:15.0

Updated: 2018-01-18 15:07:34.0

Pushed: 2018-01-14 18:57:32.0

Homepage:

Size: 155

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

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:


is is an awesome looking button for React.

rt React from 'react';

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



on.propTypes = {
*
Label for the button.

bel: React.PropTypes.string,

*
Triggered when clicked on the button.

Click: React.PropTypes.func,

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

ole.log(Button.__docgenInfo);

Click to see the output


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:

install -D babel-plugin-react-docgen

Use it inside your .babelrc


lugins": ["react-docgen"]

.babelrc Options

| option | description | default | | — | — | — | | resolver | react-docgen has 3 built in resolvers which may be used. Resolvers define how/what the doc generator will inspect. You may inspect the existing resolvers in https://github.com/reactjs/react-docgen/tree/master/src/resolver. | `"findExportedComponentDefinition"| | includeMethods | by default this plugin will remove method information about react components since it should not be needed in most cases | ``false``` |

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:


lugins":[
[
  "babel-plugin-react-docgen", 
  { 
    "DOC_GEN_COLLECTION_NAME": "MY_REACT_DOCS",
    "resolver": "findAllExportedComponentDefinitions", // optional (default: undefined)
    "includeMethods": true // optional (default: false)
  }
]


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:

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


Compile Performance

Now, we parse your code with react-docgen to get these info. But we only do it for files which has a React component.

Yes, this will add some overhead to your project. But once you turned on babel cache directory this won't be a big issue.

Output Size

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

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.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.