react-docgen

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

Github星跟踪图

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

主要指标

概览
名称与所有者reactjs/react-docgen
主编程语言TypeScript
编程语言JavaScript (语言数: 4)
平台Linux, Mac, Windows
许可证MIT License
所有者活动
创建于2015-02-27 22:03:34
推送于2025-08-31 15:24:05
最后一次提交
发布数116
最新版本名称react-docgen@8.0.1 (发布于 2025-08-17 17:08:10)
第一版名称v1.0 (发布于 )
用户参与
星数3.8k
关注者数48
派生数305
提交数2.6k
已启用问题?
问题数345
打开的问题数101
拉请求数536
打开的拉请求数6
关闭的拉请求数121
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?