Babel Plugin for React Docgen
The `babel-plugin-react-docgen` package is a Babel plugin designed to embed documentation metadata, generated by `react-docgen`, directly into React component definitions during the build process. Instead of requiring a separate build step or runtime parsing, this plugin augments your React components with a `__docgenInfo` static property, making propType descriptions, component descriptions, and other metadata programmatically accessible at runtime. This capability is highly beneficial for tools such as Storybook, style guides, or developer tools that consume component metadata. The current stable version is 4.2.1. The project appears to have an infrequent release cadence, with major versions released every few years, often in response to updates in `react-docgen` or the Babel ecosystem itself. Its key differentiator is the seamless integration of docgen output directly into the component's generated code, simplifying access to documentation metadata.
Common errors
-
TypeError: Cannot read properties of undefined (reading '__docgenInfo')
cause The plugin was not applied to the component's file, or Babel did not process the file with the plugin.fixVerify that `babel-plugin-react-docgen` is correctly listed in your `babel.config.js` or `.babelrc` and that the file containing the component is being transpiled by Babel. -
ReferenceError: MY_GLOBAL_DOCS is not defined
cause The `DOC_GEN_COLLECTION_NAME` option was used, but the specified global variable (`MY_GLOBAL_DOCS`) was not initialized before the transformed code ran.fixBefore any components processed by the plugin are executed, ensure the global variable is initialized as an empty object (e.g., `window.MY_GLOBAL_DOCS = {};` in browsers or `global.MY_GLOBAL_DOCS = {};` in Node.js). -
Error: Could not resolve file path for resolver 'myCustomResolver'
cause A custom resolver was specified by name in the plugin options, but Babel or `react-docgen` could not locate or load the resolver module.fixEnsure the path to your custom resolver is correct and that it can be resolved by Node.js's module resolution system. Alternatively, provide the resolver function directly rather than its string name.
Warnings
- breaking Version 3.0.0 introduced breaking changes by upgrading to Babel 7 and `react-docgen` v5. This requires updating Babel-related dependencies to their v7+ counterparts and reviewing Babel configuration syntax.
- breaking Version 4.0.0 bumped `react-docgen` to v6.0.0, which included significant changes to resolvers and handlers. Custom resolvers or handlers might require updates.
- gotcha When using the `DOC_GEN_COLLECTION_NAME` option to collect all docgen information into a global variable, that global variable must be initialized (e.g., `window.MY_DOCS = {}` or `global.MY_DOCS = {}`) before any code processed by this plugin executes.
- gotcha The underlying `react-docgen` library expects `propTypes` to be imported from the `prop-types` package, not `React.PropTypes`. Using `React.PropTypes` (deprecated since React v15.5) may lead to incorrect or incomplete docgen information.
Install
-
npm install babel-plugin-react-docgen -
yarn add babel-plugin-react-docgen -
pnpm add babel-plugin-react-docgen
Imports
- default
const babelPluginReactDocgen = require('babel-plugin-react-docgen');import babelPluginReactDocgen from 'babel-plugin-react-docgen';
- PluginReferenceString
plugins: ['babel-plugin-react-docgen']
plugins: ['react-docgen']
- PluginWithOptions
plugins: ['react-docgen', { resolver: 'findAllExportedComponentDefinition' }]plugins: [['react-docgen', { resolver: 'findAllExportedComponentDefinition' }]]
Quickstart
npm install -D babel-plugin-react-docgen @babel/core @babel/cli
// babel.config.js
module.exports = {
plugins: [
"react-docgen"
],
// Ensure Babel processes your React files, e.g., with @babel/preset-react
// presets: ['@babel/preset-react', '@babel/preset-env']
};
// src/Button.jsx
import React from 'react';
import PropTypes from 'prop-types'; // Use prop-types package
/**
* This is an example button component for demonstration purposes.
*/
export default class Button extends React.Component {
render() {
const { label, onClick } = this.props;
return (
<button onClick={onClick}>{ label }</button>
);
}
}
Button.propTypes = {
/**
* The text label for the button.
*/
label: PropTypes.string,
/**
* Function called when the button is clicked.
*/
onClick: PropTypes.func,
};
// To access the generated info after Babel compilation (e.g., in a documentation builder)
// console.log(Button.__docgenInfo);
/* Expected structure of Button.__docgenInfo:
{
description: 'This is an example button component for demonstration purposes.',
props: {
label: {
type: { name: 'string' },
required: false,
description: 'The text label for the button.'
},
onClick: {
type: { name: 'func' },
required: false,
description: 'Function called when the button is clicked.'
}
}
}
*/