Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow more configuration of react-docgen #88

Merged
merged 3 commits into from
Sep 23, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,11 @@ Use it inside your `.babelrc`

| option | description | default |
| --- | --- | --- |
| resolver | [react-docgen](https://github.com/reactjs/react-docgen) has 3 built in resolvers which may be used. Resolvers define how/what the doc generator will inspect. You may inspect the existing resolvers in [react-docgen/tree/master/src/resolver](https://github.com/reactjs/react-docgen/tree/master/src/resolver). | ```"findAllExportedComponentDefinition"``` |
| removeMethods | optionally remove docgen information about methods | ```false``` |
| resolver | You may use the 3 built-in [react-docgen resolvers](https://github.com/reactjs/react-docgen#resolver-1) 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](https://github.com/reactjs/react-docgen#handlers-1) 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](#collect-all-docgen-info) for more information. | |
| ...options | Remaining options will be passed directly as [react-docgen options](https://github.com/reactjs/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

Expand All @@ -105,7 +108,7 @@ So, we allow you to collect all the docgen info into a global collection. To do
"DOC_GEN_COLLECTION_NAME": "MY_REACT_DOCS",
"resolver": "findAllComponentDefinitions", // optional (default: findAllExportedComponentDefinitions)
"removeMethods": true, // optional (default: false)
"handlers:": ["react-docgen-deprecation-handler"] // optional array of custom handlers (use the string name of the package in the array)
"handlers": ["react-docgen-deprecation-handler"] // optional array of custom handlers
}
]
]
Expand All @@ -127,10 +130,9 @@ if (typeof MY_REACT_DOCS !== 'undefined') {

## Compile Performance

Now, we parse your code with `react-docgen` to get these info.
But we only do it for files which has a React component.
We parse your code with `react-docgen` to get this info, but we only do it for files which contain 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.
There will be some overhead to your project, but you can leverage [babel's cache directory](http://stackoverflow.com/a/30384710) to avoid this a huge performance hit.

## Output Size

Expand Down
27 changes: 20 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import * as _ from 'lodash';
import * as ReactDocgen from 'react-docgen';
import * as reactDocgenHandlers from 'react-docgen/dist/handlers';
import actualNameHandler from './actualNameHandler';
import { relativePath } from './relativePath';

const defaultHandlers = Object.values(reactDocgenHandlers).map(handler => handler);
const defaultHandlers = Object.values(ReactDocgen.handlers).map(handler => handler);

export default function({ types: t }) {
return {
Expand All @@ -22,22 +21,36 @@ function injectReactDocgenInfo(path, state, code, t) {
const { filename } = state.file.opts;
const program = path.scope.getProgramParent().path;

const {
resolver: resolverOpt,
handlers: handlersOpt,
DOC_GEN_COLLECTION_NAME,
...opts
} = state.opts;

let docgenResults = [];
try {
let resolver = ReactDocgen.resolver.findAllExportedComponentDefinitions;
if (state.opts.resolver) {
resolver = ReactDocgen.resolver[state.opts.resolver];
if (typeof resolverOpt === 'string') {
resolver = ReactDocgen.resolver[resolverOpt];
} else if (typeof resolverOpt === 'function') {
resolver = resolverOpt;
}

let customHandlers = [];
if (state.opts.handlers) {
state.opts.handlers.forEach(handler => {
customHandlers.push(require(handler));
if (handlersOpt) {
handlersOpt.forEach(handler => {
if (typeof handler === 'string') {
customHandlers.push(require(handler));
} else if (typeof handler === 'function') {
customHandlers.push(handler);
}
});
}

const handlers = [...defaultHandlers, ...customHandlers, actualNameHandler];
docgenResults = ReactDocgen.parse(code, resolver, handlers, {
...opts,
filename,
});

Expand Down