📝 Babel plugin to add react-docgen info into your code.
Installations
npm install babel-plugin-react-docgen
Developer
storybooks
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
No
Node Version
12.19.0
NPM Version
6.14.8
Statistics
162 Stars
225 Commits
67 Forks
56 Watching
6 Branches
115 Contributors
Updated on 10 Mar 2024
Languages
JavaScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
616,607,849
Last day
3.1%
278,326
Compared to previous day
Last week
7.8%
1,421,151
Compared to previous week
Last month
12.3%
5,793,311
Compared to previous month
Last year
-47.7%
89,020,433
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
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:
1/** 2 This is an awesome looking button for React. 3*/ 4import React from 'react'; 5 6export default class Button extends React.Component { 7 render() { 8 const { label, onClick } = this.props; 9 return ( 10 <button onClick={onClick}>{ label }</button> 11 ); 12 } 13} 14 15Button.propTypes = { 16 /** 17 Label for the button. 18 */ 19 label: React.PropTypes.string, 20 21 /** 22 Triggered when clicked on the button. 23 */ 24 onClick: React.PropTypes.func, 25};
With this babel plugin, you can access all these information right inside your app with:
1console.log(Button.__docgenInfo);
Click to see the output
1{ 2 description: 'This is an awesome looking button for React.', 3 props: { 4 label: { 5 type: { 6 name: 'string' 7 }, 8 required: false, 9 description: 'Label for the button.' 10 }, 11 onClick: { 12 type: { 13 name: 'func' 14 }, 15 required: false, 16 description: 'Triggered when clicked on the button.' 17 } 18 } 19}
This will be pretty useful for documentations and some other React devtools like Storybook.
Usage
Install the plugin:
1npm install -D babel-plugin-react-docgen
Use it inside your .babelrc
1{ 2 "plugins": ["react-docgen"] 3}
.babelrc Options
option | description | default |
---|---|---|
resolver | You 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" |
handlers | All 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. | |
removeMethods | Used to remove docgen information about methods. | false |
DOC_GEN_COLLECTION_NAME | The name of a global variable where all docgen information can be stored. See below for more information. | |
...options | Remaining 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:
1{ 2 "plugins":[ 3 [ 4 "babel-plugin-react-docgen", 5 { 6 "DOC_GEN_COLLECTION_NAME": "MY_REACT_DOCS", 7 "resolver": "findAllComponentDefinitions", // optional (default: findAllExportedComponentDefinitions) 8 "removeMethods": true, // optional (default: false) 9 "handlers": ["react-docgen-deprecation-handler"] // optional array of custom handlers 10 } 11 ] 12 ] 13}
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:
1if (typeof MY_REACT_DOCS !== 'undefined') { 2 MY_REACT_DOCS['test/fixtures/case4/actual.js'] = { 3 name: 'Button', 4 docgenInfo: Button.__docgenInfo, 5 path: 'path/to/my/button.js' 6 }; 7}
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.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE.md:0
- Info: FSF or OSI recognized license: MIT License: LICENSE.md:0
Reason
Found 2/3 approved changesets -- score normalized to 6
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 29 are checked with a SAST tool
Reason
19 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-6chw-6frg-f759
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-8r6j-v8pm-fqw3
- Warn: Project is vulnerable to: MAL-2023-462
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-6c8f-qphg-qjgp
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-5fw9-fq32-wv5p
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
Score
2.5
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to babel-plugin-react-docgen
@types/babel-plugin-react-docgen
TypeScript definitions for babel-plugin-react-docgen
@utilitywarehouse/storybook-addon-prop-types
Can be used to create better component documentation inside storybook. Based on [React DocGen babel plugin](https://github.com/storybooks/babel-plugin-react-docgen).
babel-plugin-react-docgen-typescript
Babel Plugin to generate docgen data from React components written in TypeScript.
@storybook/react-docgen-typescript-plugin
A webpack plugin to inject react typescript docgen information.