react-docgen

一个 CLI 和工具箱,用于从 React 组件文件中提取信息,用于生成文档。「A CLI and toolbox to extract information from React component files for documentation generation purposes.」

Github stars Tracking Chart

react-docgen Build Status

react-docgen is a CLI and toolbox to help extracting information from [React][] components, and generate documentation from it.

It uses [ast-types][] and [@babel/parser][] to parse the source into an AST and provides methods to process this AST to extract the desired information. The output / return value is a JSON blob / JavaScript object.

It provides a default implementation for React components defined via
React.createClass, [ES2015 class definitions][classes] or functions
(stateless components). These component definitions must follow certain
guidelines in order to be analyzable (see below for more info).

react-docgen is a low level tool to extract information about react components. If you are searching for a more high level styleguide with nice interface try react-styleguidist or any of the other tools listed in the wiki.

Install

Install the module with yarn or npm:

yarn add react-docgen --dev
npm install --save-dev react-docgen

CLI

Installing the module adds a react-docgen executable which allows you to convert
a single file, multiple files or an input stream. We are trying to make the
executable as versatile as possible so that it can be integrated into many
workflows.

Usage: react-docgen [path]... [options]

path     A component file or directory. If no path is provided it reads from stdin.

Options:
   -o FILE, --out FILE   store extracted information in FILE
   --pretty              pretty print JSON
   -x, --extension       File extensions to consider. Repeat to define multiple extensions. Default:  [js,jsx]
   -e, --exclude         Filename pattern to exclude. Default:  []
   -i, --ignore          Folders to ignore. Default:  [node_modules,__tests__,__mocks__]
   --resolver RESOLVER   Resolver name (findAllComponentDefinitions, findExportedComponentDefinition) or
      path to a module that exports a resolver.  [findExportedComponentDefinition]

Extract meta information from React components.
If a directory is passed, it is recursively traversed.

By default, react-docgen will look for the exported component created through
React.createClass, a class definition or a function (stateless component) in
each file. You can change that behavior with the --resolver option, which
either expects the name of a built-in resolver or a path to JavaScript module
exporting a resolver function. Have a look below for more information about
resolvers
.

Have a look at example/ for an example of how to use the result to generate a
markdown version of the documentation.

react-docgen will look for a babel configuration and use it if available. If no config file is found
it will fallback to a default configuration, enabling all syntax extension of the babel-parser.

API

The tool can be used programmatically to extract component information and customize the extraction process:

var reactDocs = require('react-docgen');
var componentInfo = reactDocs.parse(src);

As with the CLI, this will look for the exported component created through React.createClass or a class definition in the provided source. The whole process of analyzing the source code is separated into two parts:

  • Locating/finding the nodes in the AST which define the component
  • Extracting information from those nodes

parse accepts more arguments with which this behavior can be customized.

parse(source [, resolver [, handlers [, options]]])

source

Type: `string

Main metrics

Overview
Name With Ownerreactjs/react-docgen
Primary LanguageTypeScript
Program languageJavaScript (Language Count: 4)
PlatformLinux, Mac, Windows
License:MIT License
所有者活动
Created At2015-02-27 22:03:34
Pushed At2025-06-17 05:15:40
Last Commit At
Release Count113
Last Release Namereact-docgen@8.0.0 (Posted on 2025-04-23 18:35:46)
First Release Namev1.0 (Posted on )
用户参与
Stargazers Count3.7k
Watchers Count47
Fork Count303
Commits Count2.5k
Has Issues Enabled
Issues Count344
Issue Open Count100
Pull Requests Count517
Pull Requests Open Count8
Pull Requests Close Count120
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private