Skip to content

Commit 0a9e7ba

Browse files
add include and exclude options
1 parent 9ee4493 commit 0a9e7ba

3 files changed

Lines changed: 37 additions & 11 deletions

File tree

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424
],
2525
"dependencies": {
2626
"endent": "^2.0.1",
27+
"micromatch": "^4.0.2",
2728
"react-docgen-typescript": "^1.16.6",
2829
"react-docgen-typescript-loader": "^3.7.2",
2930
"tslib": "^2.0.0"
3031
},
3132
"devDependencies": {
33+
"@types/micromatch": "^4.0.1",
3234
"@types/node": "^14.0.12",
3335
"@types/webpack": "^4.41.17",
3436
"@typescript-eslint/eslint-plugin": "2.31.0",

readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ yarn add -D react-docgen-typescript-plugin
1515

1616
## Usage
1717

18-
> NOTE: The TypeScript compiler options `allowSyntheticDefaultImports` and `esModuleInterop` will make
18+
> NOTE: The TypeScript compiler options `allowSyntheticDefaultImports` and `esModuleInterop` will make
1919
> `react-docgen-typescript-plugin` a lot harder! Turn them off for faster build times.
2020
2121
```ts
@@ -43,6 +43,8 @@ This plugins support all parser options from [react-docgen-typescript](https://g
4343
| 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. | `STORYBOOK_REACT_CLASSES` |
4444
| setDisplayName | boolean | 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. | `true` |
4545
| typePropName | string | Specify the name of the property for docgen info prop type. | `type` |
46+
| exclude | glob[] | Glob patterns to ignore and not generate docgen information for. (Great for ignoring large icon libraries) | [] |
47+
| include | glob[] | Glob patterns to generate docgen information for | ['**/**.tsx'] |
4648

4749
## Prior Art
4850

src/index.ts

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as webpack from "webpack";
33
import ts from "typescript";
44
import * as docGen from "react-docgen-typescript";
55
import generateDocgenCodeBlock from "react-docgen-typescript-loader/dist/generateDocgenCodeBlock";
6+
import match from "micromatch";
67

78
interface TypescriptOptions {
89
/**
@@ -51,10 +52,19 @@ interface LoaderOptions {
5152

5253
export type PluginOptions = docGen.ParserOptions &
5354
LoaderOptions &
54-
TypescriptOptions;
55+
TypescriptOptions & {
56+
/** Glob patterns to ignore */
57+
exclude?: [];
58+
/** Glob patterns to include. defaults to ts|tsx */
59+
include?: [];
60+
};
5561

5662
interface Module {
5763
userRequest: string;
64+
request: string;
65+
built?: boolean;
66+
rawRequest?: string;
67+
external?: boolean;
5868
_source: {
5969
_value: string;
6070
};
@@ -125,6 +135,8 @@ export default class DocgenPlugin {
125135
setDisplayName = true,
126136
typePropName = "type",
127137
compilerOptions: userCompilerOptions,
138+
exclude = [],
139+
include = ["**/**.tsx"],
128140
...docgenOptions
129141
} = this.options;
130142
let compilerOptions = {
@@ -142,25 +154,35 @@ export default class DocgenPlugin {
142154
compilerOptions = { ...compilerOptions, ...options };
143155
}
144156

157+
const isExcluded = (filename: string) =>
158+
Boolean(filename && exclude.find((i) => match([filename], i).length));
159+
const isIncluded = (filename: string) =>
160+
Boolean(filename && include.find((i) => match([filename], i).length));
161+
162+
const parser =
163+
(tsconfigPath && docGen.withCustomConfig(tsconfigPath, docgenOptions)) ||
164+
docGen.withCompilerOptions(compilerOptions, docgenOptions);
165+
145166
compiler.hooks.make.tap(this.name, (compilation) => {
146167
compilation.hooks.seal.tap(this.name, () => {
147168
const modulesToProcess: Module[] = [];
148169

149-
compilation.modules.forEach((module) => {
170+
compilation.modules.forEach((module: Module) => {
150171
// Skip ignored / external modules
151-
if (!module.built || module.external || !module.rawRequest) {
172+
if (
173+
!module.built ||
174+
module.external ||
175+
!module.rawRequest ||
176+
isExcluded(module.request) ||
177+
!isIncluded(module.request) ||
178+
!pathRegex.test(module.request)
179+
) {
152180
return;
153181
}
154182

155-
if (pathRegex.test(module.request)) {
156-
modulesToProcess.push(module);
157-
}
183+
modulesToProcess.push(module);
158184
});
159185

160-
const parser =
161-
(tsconfigPath &&
162-
docGen.withCustomConfig(tsconfigPath, docgenOptions)) ||
163-
docGen.withCompilerOptions(compilerOptions, docgenOptions);
164186
const tsProgram = ts.createProgram(
165187
modulesToProcess.map((v) => v.userRequest),
166188
compilerOptions

0 commit comments

Comments
 (0)