React Docgen TypeScript Webpack Plugin
react-docgen-typescript-plugin is a webpack plugin designed to automatically extract and inject TypeScript-based documentation (docgen information) for React components directly into the webpack build output. This is particularly useful for tools like Storybook that consume docgen information to generate component documentation. The current stable version is `1.0.8`. The package appears to follow a frequent bug-fix release cadence, with minor updates addressing specific issues or dependency bumps, as evidenced by the recent v1.0.x patches. Its key differentiator is its seamless integration into the webpack build process, providing a 'fire-and-forget' solution for generating React component props documentation from TypeScript types, contrasting with loaders that might require more explicit configuration per file. It supports both Webpack 4 and 5 and allows for fine-grained control over `tsconfig.json` paths, compiler options, and file inclusion/exclusion.
Common errors
-
TypeError: (0, _reactDocgenTypescriptPlugin.default) is not a constructor or TypeError: ReactDocgenTypescriptPlugin is not a constructor
cause Incorrect import of the plugin when using CommonJS `require` or when `esModuleInterop` is false.fixIf using `require`, ensure you access the `.default` export: `const ReactDocgenTypescriptPlugin = require('react-docgen-typescript-plugin').default;`. If using `import` in a mixed ESM/CJS environment, ensure your `tsconfig.json` or bundler settings correctly handle default imports. -
Error: Cannot find module 'typescript' or Cannot find module 'webpack'
cause The peer dependencies `typescript` or `webpack` are not installed or are not accessible.fixInstall the required peer dependencies: `npm install --save-dev typescript webpack` or `yarn add -D typescript webpack`. Ensure their versions meet the plugin's requirements (e.g., `typescript: >= 4.x`, `webpack: >= 4`). -
Webpack compilation failed: TS2304: Cannot find name 'ts' in compilerOptions
cause When providing `compilerOptions` directly, the `ts` object from the TypeScript compiler API needs to be imported or available in scope.fixMake sure `import ts from 'typescript';` is present in your webpack configuration file if you're using `ts.JsxEmit.Preserve` or similar enum values directly in `compilerOptions`. -
Docgen information missing or incorrect for components
cause The plugin might be excluding your component files, or `allowSyntheticDefaultImports`/`esModuleInterop` might be interfering.fixCheck the `include` and `exclude` options to ensure your component files are being processed. Set `DEBUG=docgen:*` environment variable to get detailed logs on which modules are included/excluded and which docs are generated. Also, consider disabling `allowSyntheticDefaultImports` and `esModuleInterop` in your `tsconfig.json`.
Warnings
- breaking Version 1.0.0 introduced breaking changes by adding support for Webpack 5. Projects migrating from older versions of webpack might need to upgrade the plugin.
- gotcha TypeScript compiler options `allowSyntheticDefaultImports` and `esModuleInterop` can significantly impact the plugin's performance, making it harder and slower to process documentation.
- gotcha The `ReactDocgenTypescriptPlugin` is a default export, which can lead to incorrect `require` or `import` syntax depending on your module system configuration.
- gotcha The `docgenCollectionName` option defaults to `STORYBOOK_REACT_CLASSES`. If you're not using Storybook or need a different global object, ensure to configure this option or set it to `null` to disable collection.
Install
-
npm install react-docgen-typescript-plugin -
yarn add react-docgen-typescript-plugin -
pnpm add react-docgen-typescript-plugin
Imports
- ReactDocgenTypescriptPlugin
const ReactDocgenTypescriptPlugin = require('react-docgen-typescript-plugin');import ReactDocgenTypescriptPlugin from 'react-docgen-typescript-plugin';
- ReactDocgenTypescriptPlugin with options
new ReactDocgenTypescriptPlugin({ 'tsconfigPath': './tsconfig.dev.json' });import ReactDocgenTypescriptPlugin from 'react-docgen-typescript-plugin'; new ReactDocgenTypescriptPlugin({ tsconfigPath: './tsconfig.dev.json' }); - TypeScript compiler API
const ts = require('typescript');import ts from 'typescript'; new ReactDocgenTypescriptPlugin({ compilerOptions: { jsx: ts.JsxEmit.Preserve } });
Quickstart
import path from 'path';
import webpack from 'webpack';
import ReactDocgenTypescriptPlugin from 'react-docgen-typescript-plugin';
import ts from 'typescript';
const config: webpack.Configuration = {
mode: 'development',
entry: './src/index.tsx',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.jsx'],
},
module: {
rules: [
{
test: /\.(ts|tsx)$/,
loader: 'ts-loader',
exclude: /node_modules/,
},
],
},
plugins: [
// Initializes the plugin with default options, loading the root tsconfig.json
new ReactDocgenTypescriptPlugin(),
// Alternatively, specify a custom tsconfig path
// new ReactDocgenTypescriptPlugin({ tsconfigPath: './tsconfig.build.json' }),
// Or pass specific TypeScript compiler options directly
// new ReactDocgenTypescriptPlugin({ compilerOptions: { jsx: ts.JsxEmit.Preserve } }),
],
};
export default config;