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-06-17 05:15:40
最后一次提交
發布數113
最新版本名稱react-docgen@8.0.0 (發布於 2025-04-23 18:35:46)
第一版名稱v1.0 (發布於 )
用户参与
星數3.7k
關注者數47
派生數303
提交數2.5k
已啟用問題?
問題數344
打開的問題數100
拉請求數517
打開的拉請求數8
關閉的拉請求數120
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?