4.2.1 • Published 4 years ago

babel-plugin-react-docgen v4.2.1

Weekly downloads
1,898,731
License
MIT
Repository
github
Last release
4 years ago

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);
{
  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

optiondescriptiondefault
resolverYou 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"
handlersAll 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.
removeMethodsUsed to remove docgen information about methods.false
DOC_GEN_COLLECTION_NAMEThe name of a global variable where all docgen information can be stored. See below for more information.
...optionsRemaining 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.

@pomona/pomona3-uiteasim-samples@infinitebrahmanuniverse/nolb-babel-plugin-r@saaspe/components@everything-registry/sub-chunk-1203@pxwlab/with-katana@pxwlab/next-katana-plugin@wacoco/ystorybook-addon-smart-knobs-flowstorybook-vue-hmr-fixstorybook-preset-react-rspackstorybook-react-tmpsequelcomponentvott-reactparvan_componentsparvan_reactjs_componentspolyglot-component-librarynlx-storybookreact-form-component-librarytribe-api-wrapper@ayaseaki/storybook-preset-react-rspack@c4605/preset-create-react-app@a8k/plugin-sb-reactxunwei-npm@idesigncode/storybook-tools@fy-dev/preset-react-rspack@fy-dev/storybook-builder@luciadias/storybook-notimation@lego-js/tests@lighting-beetle/lighter-react-scripts@majik/storybook-react-faster@nlabs/storybook-react@natahouse/components@enjoylife/react@enjoylife/reactstorybook@meniga/cli@modusbox/react-components@kadira/storybook@img-arena/img-ui-mui-theme@img-arena/ui-core@juanaraneta/dept-central-lib-client@junk-temporary-prototypes/preset-create-react-app@junk-temporary-prototypes/preset-react-webpack@digitalmaster/react@digitalstudio/play@defunctzombie/storybook@blkmarketco/components-library@beisen/storybook-reactdesign-system-fitbank-450doc-scriptsdoloribusquas@nonlux/storybook@moonwalker/jetpack@pxwlab/configs@ryanshaug/gl-controls@theforeman/stories@thinkei/storybook@oneoff/storybook-webpack2-patch@podlubnaja/ui-kit@nppkg/react-dnd@ntucker/storybook@ouihelp/storybook-react@peeja/storybook-react@unional/preset-create-react-app@widget-dev/react@vitali_shcherbina/styled-lib@skeetboothppq/component-library@soyjak/utils@shivarajapple/first-librarybabel-preset-docmbabel-preset-doczbabel-preset-docz-fannypackappcharge-checkoutfn-lib-example
4.2.1

4 years ago

4.2.0

4 years ago

4.1.0

5 years ago

4.0.0

5 years ago

4.0.0-beta.3

5 years ago

4.0.0-beta.2

5 years ago

4.0.0-beta.1

5 years ago

3.2.0

5 years ago

3.1.0

6 years ago

3.0.0

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

2.0.0-babel7.0

6 years ago

2.0.0-rc.2

6 years ago

2.0.0-rc.1

6 years ago

2.0.0-rc.0

7 years ago

1.9.0

7 years ago

1.8.3

7 years ago

1.8.2

7 years ago

1.8.1

7 years ago

1.8.0

7 years ago

1.7.0

7 years ago

1.6.0

7 years ago

1.5.0

7 years ago

1.4.2

8 years ago

1.4.1

8 years ago

1.4.0

8 years ago

1.3.2

8 years ago

1.3.1

8 years ago

1.3.0

8 years ago

1.2.0

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago