Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-react-docgen

Package Overview
Dependencies
Maintainers
2
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-react-docgen - npm Package Compare versions

Comparing version 1.2.0 to 1.3.0

LICENSE.md

7

CHANGELOG.md
# ChangeLog
### v1.3.0
20-October-2016
* Use docgen version 2.11.0. [PR11](https://github.com/kadirahq/babel-plugin-react-docgen/pull/11)
* Rename DOC_GEN_GLOBAL to DOC_GEN_COLLECTION_NAME. [PR12](https://github.com/kadirahq/babel-plugin-react-docgen/pull/12)
* Update the README. [PR13](https://github.com/kadirahq/babel-plugin-react-docgen/pull/13)
### v1.1.0

@@ -4,0 +11,0 @@ 20-October-2016

4

lib/index.js

@@ -98,7 +98,7 @@ 'use strict';

if (!state.opts.DOC_GEN_GLOBAL) {
if (!state.opts.DOC_GEN_COLLECTION_NAME) {
return;
}
var globalName = state.opts.DOC_GEN_GLOBAL;
var globalName = state.opts.DOC_GEN_COLLECTION_NAME;
var filePath = p.relative('./', p.resolve('./', path.hub.file.opts.filename));

@@ -105,0 +105,0 @@ var globalNode = t.ifStatement(t.binaryExpression('!==', t.unaryExpression('typeof', t.identifier(globalName)), t.stringLiteral('undefined')), t.blockStatement([t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.identifier(globalName), t.stringLiteral(filePath), true), t.objectExpression([t.objectProperty(t.identifier('name'), t.stringLiteral(className)), t.objectProperty(t.identifier('docgenInfo'), t.memberExpression(t.identifier(className), t.identifier('__docgenInfo'))), t.objectProperty(t.identifier('path'), t.stringLiteral(filePath))])))]));

{
"version": "1.2.0",
"version": "1.3.0",
"name": "babel-plugin-react-docgen",

@@ -31,5 +31,5 @@ "description": "Add propType doc to react classes",

"lodash": "4.x.x",
"react-docgen": "2.x.x"
"react-docgen": "2.11.0"
},
"license": "MIT"
}
# babel-plugin-react-docgen
Add propType doc to react classes
[react-docgen](https://github.com/reactjs/react-docgen) allows you to write propType descriptions, class descriptions and access propType metadata programatically.
## Installation
This babel plugin allow you to access those information right inside your React class.
```sh
$ npm install babel-plugin-react-docgen
For an example, let's say you've a React class like this:
```js
/**
This is an awesome looking button for React.
*/
import React from 'react';
export default class Button extends React.Component {
render() {
const { label, onClick } = this.props;
return (
<button onClick={onClick}>{ label }</button>
);
}
}
Button.propTypes = {
/**
Label for the button.
*/
label: React.PropTypes.string,
/**
Triggered when clicked on the button.
*/
onClick: React.PropTypes.func,
};
```
With this babel plugin, you can access all these information right inside your app with:
```js
console.log(Button.__docgenInfo);
```
<details>
<summary>Click to see the output</summary>
```js
{
description: 'This is an awesome looking button for React.',
props: {
label: {
type: {
name: 'string'
},
required: false,
description: 'Label for the button.'
},
onClick: {
type: {
name: 'func'
},
required: false,
description: 'Triggered when clicked on the button.'
}
}
}
```
</details>
This will be pretty useful for documentations and some other React devtools like [Storybook](https://github.com/kadirahq/react-storybook).
## Usage
### Via `.babelrc` (Recommended)
Install the plugin:
**.babelrc**
```sh
npm install -D babel-plugin-react-docgen
```
Use it inside your `.babelrc`
```json
{
"plugins": ["babel-plugin-react-docgen"]
"plugins": ["react-docgen"]
}
```
### Via CLI
## Collect All Docgen Info
```sh
$ babel --plugins babel-plugin-react-docgen script.js
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:
```json
{
"plugins":[
["babel-plugin-react-docgen", { "DOC_GEN_COLLECTION_NAME": "MY_REACT_DOCS"}]
]
}
```
### Via Node API
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:
```javascript
require("babel-core").transform("code", {
plugins: ["babel-plugin-react-docgen"]
});
```js
if (typeof MY_REACT_DOCS !== 'undefined') {
MY_REACT_DOCS['test/fixtures/case4/actual.js'] = {
name: 'Button',
docgenInfo: Button.__docgenInfo,
path: 'path/to/my/button.js'
};
}
```
## Guide
## Compile Performance
* React class information is avalable via `<ClassName>.__docgenInfo` as a JSON parsabel string.
* This plugin uses `react-docgen` under the hood, so every [limitation](https://github.com/reactjs/react-docgen#guidelines-for-default-resolvers-and-handlers) it has, applies.
* If you need a global object with all the react component docs, set plugin options like this in `.babelrc` (replace <your global name> with variable name you want)
```
"plugins":[["babel-plugin-react-docgen", {"DOC_GEN_GLOBAL": "<your global name>"}]]
```
Now, we parse your code with `react-docgen` to get these info.
But we only do it for files which has a React component.
Yes, this will add some overhead to your project. But once you turned on [babel cache directory](http://stackoverflow.com/a/30384710) this won't be a big issue.
## 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](https://github.com/kadirahq/react-storybook). So, you may not need to use this on the production version of your app.

@@ -24,3 +24,3 @@ import path from 'path';

[plugin, {
"DOC_GEN_GLOBAL": "STORYBOOK_REACT_CLASSES"
"DOC_GEN_COLLECTION_NAME": "STORYBOOK_REACT_CLASSES"
}]

@@ -27,0 +27,0 @@ ],

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc