Gathering detailed insights and metrics for react-docgen-typescript-loader
Gathering detailed insights and metrics for react-docgen-typescript-loader
Gathering detailed insights and metrics for react-docgen-typescript-loader
Gathering detailed insights and metrics for react-docgen-typescript-loader
Webpack loader to generate docgen information from Typescript React components.
npm install react-docgen-typescript-loader
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
361 Stars
162 Commits
46 Forks
6 Watching
4 Branches
14 Contributors
Updated on 30 Oct 2024
TypeScript (99.26%)
JavaScript (0.74%)
Cumulative downloads
Total Downloads
Last day
-5.5%
40,321
Compared to previous day
Last week
-9.8%
188,601
Compared to previous week
Last month
5.9%
865,290
Compared to previous month
Last year
-2.8%
10,656,245
Compared to previous year
1
Webpack loader to generate docgen information from TypeScript React components. The primary use case is to get the prop types table populated in the Storybook Info Addon.
Example Storybook project
Live Storybook
View Changelog.
Version 2 supported the options includes
and excludes
, which were arrays of regular expressions. If you made use of these options, remove them from and use the Webpack equivalents.
includes
would default to ["\\.tsx$"]
which meant that only files ending in the extension .ts
or .tsx
would be processed. This default behavior is already covered by Webpack's test
field.
excludes
would default to ["node_modules"]
. This would prevent the processing of files in node_modules. This option was added to allow further filtering to hopefully speed up processing. When this loader is used in monorepo environments, this option would complicate configuration.
In version 3, the loader no longer performs its own filtering. If you relied on the additional filtering behavior, you should be able to reimplement it using options in Webpack.
This change shouldn't affect the majority of projects.
The source code generation relies on being able to insert // @ts-ignore
in a few select places and as such requires TypeScript 2.3 or above.
1$ npm install --save-dev react-docgen-typescript-loader 2 3or 4 5$ yarn add --dev react-docgen-typescript-loader
IMPORTANT: Webpack loaders are executed right-to-left (or bottom-to-top). react-docgen-typescript-loader
needs to be added under ts-loader
.
Example Storybook config /.storybook/webpack.config.js
:
1const path = require("path"); 2 3module.exports = (baseConfig, env, config) => { 4 config.module.rules.push({ 5 test: /\.tsx?$/, 6 include: path.resolve(__dirname, "../src"), 7 use: [ 8 require.resolve("ts-loader"), 9 { 10 loader: require.resolve("react-docgen-typescript-loader"), 11 options: { 12 // Provide the path to your tsconfig.json so that your stories can 13 // display types from outside each individual story. 14 tsconfigPath: path.resolve(__dirname, "../tsconfig.json"), 15 }, 16 }, 17 ], 18 }); 19 20 config.resolve.extensions.push(".ts", ".tsx"); 21 22 return config; 23};
1const path = require("path"); 2const genDefaultConfig = require("@storybook/react/dist/server/config/defaults/webpack.config.js"); 3 4module.exports = (baseConfig, env) => { 5 const config = genDefaultConfig(baseConfig); 6 7 config.module.rules.push({ 8 test: /\.tsx?$/, 9 include: path.resolve(__dirname, "../src"), 10 use: [ 11 require.resolve("ts-loader"), 12 { 13 loader: require.resolve("react-docgen-typescript-loader"), 14 options: { 15 // Provide the path to your tsconfig.json so that your stories can 16 // display types from outside each individual story. 17 tsconfigPath: path.resolve(__dirname, "../tsconfig.json"), 18 }, 19 }, 20 ], 21 }); 22 23 config.resolve.extensions.push(".ts", ".tsx"); 24 25 return config; 26};
Include the withInfo
decorator as normal.
Reference the addon documentation for the latest usage instructions:
https://github.com/storybooks/storybook/tree/master/addons/info
The Storybook Info Addon is able to populate the component description from your component's documentation. It does this when your story name matches the display name of your component. The prop tables will populate in either case.
In the first example you are able to see above Story Source
the component description. The second example shows a story with a name different from the component. In the second example the component description is missing.
If you have a component named
TicTacToeCell
, then you would have to use something like: storiesOf("...", module).add("TicTacToeCell", ...)
to have the story description come from the component description.
In addition to the description from the component, you may still include story description text using the normal withInfo api.
It is important to export your component using a named export for docgen information to be generated properly.
TicTacToeCell.tsx
:
1import React, { Component } from "react"; 2import * as styles from "./TicTacToeCell.css"; 3 4interface Props { 5 /** 6 * Value to display, either empty (" ") or "X" / "O". 7 * 8 * @default " " 9 **/ 10 value?: " " | "X" | "O"; 11 12 /** Cell position on game board. */ 13 position: { x: number, y: number }; 14 15 /** Called when an empty cell is clicked. */ 16 onClick?: (x: number, y: number) => void; 17} 18 19/** 20 * TicTacToe game cell. Displays a clickable button when the value is " ", 21 * otherwise displays "X" or "O". 22 */ 23// Notice the named export here, this is required for docgen information 24// to be generated correctly. 25export class TicTacToeCell extends Component<Props> { 26 handleClick = () => { 27 const { 28 position: { x, y }, 29 onClick, 30 } = this.props; 31 if (!onClick) return; 32 33 onClick(x, y); 34 }; 35 36 render() { 37 const { value = " " } = this.props; 38 const disabled = value !== " "; 39 const classes = `${styles.button} ${disabled ? styles.disabled : ""}`; 40 41 return ( 42 <button 43 className={classes} 44 disabled={disabled} 45 onClick={this.handleClick} 46 > 47 {value} 48 </button> 49 ); 50 } 51} 52 53// Component can still be exported as default. 54export default TicTacToeCell;
ColorButton.stories.tsx
:
1import React from "react"; 2import { storiesOf } from "@storybook/react"; 3import { withInfo } from "@storybook/addon-info"; 4import { action } from "@storybook/addon-actions"; 5import TicTacToeCell from "./TicTacToeCell"; 6 7const stories = storiesOf("Components", module); 8 9stories.add( 10 "TicTacToeCell", 11 withInfo({ inline: true })(() => ( 12 <TicTacToeCell 13 value="X" 14 position={{ x: 0, y: 0 }} 15 onClick={action("onClick")} 16 /> 17 )), 18);
Option | Type | Description |
---|---|---|
skipPropsWithName | string[] or string | Avoid including docgen information for the prop or props specified. |
skipPropsWithoutDoc | boolean | Avoid including docgen information for props without documentation. |
componentNameResolver | function | If a string is returned, then the component will use that name. Else it will fallback to the default logic of parser. https://github.com/styleguidist/react-docgen-typescript#parseroptions |
propFilter | function | Filter props using a function. If skipPropsWithName or skipPropsWithoutDoc is defined the function will not be used. Function accepts two arguments: object with information about prop and an object with information about component. Return true to include prop in documentation. https://github.com/styleguidist/react-docgen-typescript#parseroptions |
tsconfigPath | string | Specify the location of the tsconfig.json to use. Can not be used with compilerOptions. |
compilerOptions | typescript.CompilerOptions | Specify TypeScript compiler options. Can not be used with tsconfigPath. |
docgenCollectionName | string or null | Specify the docgen collection name to use. All docgen information will be collected into this global object. Set to null to disable. Defaults to STORYBOOK_REACT_CLASSES for use with the Storybook Info Addon. https://github.com/gongreg/react-storybook-addon-docgen |
setDisplayName | boolean | Automatically set the components' display name. If you want to set display names yourself or are using another plugin to do this, you should disable this option. Defaults to true . This is used to preserve component display names during a production build of Storybook. |
shouldExtractLiteralValuesFromEnum | boolean | If set to true, string enums and unions will be converted to docgen enum format. Useful if you use Storybook and want to generate knobs automatically using addon-smart-knobs. https://github.com/styleguidist/react-docgen-typescript#parseroptions |
savePropValueAsString | boolean | If set to true, defaultValue to props will be string. https://github.com/styleguidist/react-docgen-typescript#parseroptions |
typePropName | string | Specify the name of the property for docgen info prop type. |
shouldRemoveUndefinedFromOptional | boolean | If set to true, types that are optional will not display ` |
There is a significant startup cost due to the initial type parsing. Once the project is running in watch mode, things should be smoother due to Webpack caching.
This plugin uses a Webpack loader to inject the docgen information. There is also a version which works as a Webpack plugin. I will be supporting both versions. The loader version more accurately generates the injected code blocks and should work with all module types but at the cost of a longer initial startup. The plugin version may be faster.
The Webpack plugin version is available here: https://github.com/strothj/react-docgen-typescript-loader/tree/plugin
This plugin makes use of the project: https://github.com/styleguidist/react-docgen-typescript. It is subject to the same limitations.
When extending from React.Component
as opposed to Component
, docgens don't seem to be created. Ref issue #10 (thanks @StevenLangbroek for tracking down the cause).
Doesn't work:
import React from 'react';
interface IProps {
whatever?: string;
};
export default class MyComponent extends React.Component<IProps> {}
Works:
import React, { Component } from 'react';
export default class MyComponent extends Component<IProps> {}
Component docgen information can not be generated for components that are only exported as default. You can work around the issue by exporting the component using a named export.
1import * as React from "react"; 2 3interface ColorButtonProps { 4 /** Buttons background color */ 5 color: "blue" | "green"; 6} 7 8/** A button with a configurable background color. */ 9export const ColorButton: React.SFC<ColorButtonProps> = props => ( 10 <button 11 style={{ 12 padding: 40, 13 color: "#eee", 14 backgroundColor: props.color, 15 fontSize: "2rem", 16 }} 17 > 18 {props.children} 19 </button> 20); 21 22export default ColorButton;
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 7/22 approved changesets -- score normalized to 3
Reason
project is archived
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
125 existing vulnerabilities detected
Details
Score
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 Morereact-docgen-typescript
[![Build Status](https://github.com/styleguidist/react-docgen-typescript/actions/workflows/nodejs.yml/badge.svg)](https://github.com/styleguidist/react-docgen-typescript/actions/workflows/nodejs.yml)
@storybook/react-docgen-typescript-plugin
A webpack plugin to inject react typescript docgen information.
react-docgen-typescript-plugin
A webpack plugin to inject react typescript docgen information.
vue-docgen-loader
Vue docgen loader for webpack