Skip to content

Commit 516456a

Browse files
allow for compilerOption, get features in parity with react-docgen-typescript-loader
1 parent b5139f2 commit 516456a

2 files changed

Lines changed: 51 additions & 24 deletions

File tree

readme.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ const ReactDocgenTypescriptPlugin = require("react-docgen-typescript-plugin");
2121
module.exports = {
2222
plugins: [
2323
new ReactDocgenTypescriptPlugin(),
24-
// or with options
24+
// or with a tsconfig
2525
new ReactDocgenTypescriptPlugin({ tsconfigPath: "./tsconfig.json" }),
26+
// or with options
27+
new ReactDocgenTypescriptPlugin({ jsx: ts.JsxEmit.Preserve }),
2628
],
2729
};
2830
```
@@ -34,6 +36,7 @@ This plugins support all parser options from [react-docgen-typescript](https://g
3436
| Option | Type | Description | Default |
3537
| -------------------- | -------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------- |
3638
| tsconfigPath | string | Specify the location of the `tsconfig.json` to use. | `null` |
39+
| compilerOptions | object | Specify compiler options. Cannot be used with `tsconfigPath` | `null` |
3740
| 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` |
3841
| 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` |
3942
| typePropName | string | Specify the name of the property for docgen info prop type. | `type` |

src/index.ts

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ interface LoaderOptions {
1010
* compilerOptions.
1111
**/
1212
tsconfigPath?: string;
13-
13+
/** Specify TypeScript compiler options. Can not be used with tsconfigPath. */
14+
compilerOptions?: ts.CompilerOptions;
1415
/**
1516
* Specify the docgen collection name to use. All docgen information will
1617
* be collected into this global object. Set to null to disable.
@@ -51,9 +52,10 @@ interface Module {
5152
userRequest: string;
5253
_source: {
5354
_value: string;
54-
}
55+
};
5556
}
5657

58+
/** Run the docgen parser and inject the result into the output */
5759
function processModule(
5860
parser: docGen.FileParser,
5961
webpackModule: Module,
@@ -90,6 +92,21 @@ function processModule(
9092
webpackModule._source._value = source;
9193
}
9294

95+
/** Get the contents of the tsconfig in the system */
96+
function getTSConfigFile(tsconfigPath: string): ts.ParsedCommandLine {
97+
const basePath = path.dirname(tsconfigPath);
98+
const configFile = ts.readConfigFile(tsconfigPath, ts.sys.readFile);
99+
100+
return ts.parseJsonConfigFileContent(
101+
configFile.config,
102+
ts.sys,
103+
basePath,
104+
{},
105+
tsconfigPath
106+
);
107+
}
108+
109+
/** Inject typescript docgen information into modules at the end of a build */
93110
export default class DocgenPlugin {
94111
private name = "React Docgen Typescript Plugin";
95112
private options: PluginOptions;
@@ -102,21 +119,27 @@ export default class DocgenPlugin {
102119
const pathRegex = RegExp(`\\${path.sep}src.+\\.tsx`);
103120
const {
104121
tsconfigPath,
105-
propFilter,
106-
componentNameResolver,
107-
shouldExtractLiteralValuesFromEnum,
108-
shouldRemoveUndefinedFromOptional,
109-
savePropValueAsString,
110-
...loaderOptions
122+
docgenCollectionName,
123+
setDisplayName,
124+
typePropName,
125+
compilerOptions: userCompilerOptions,
126+
...docgenOptions
111127
} = this.options;
112-
const docgenOptions = {
113-
propFilter,
114-
componentNameResolver,
115-
shouldExtractLiteralValuesFromEnum,
116-
shouldRemoveUndefinedFromOptional,
117-
savePropValueAsString,
128+
let compilerOptions = {
129+
jsx: ts.JsxEmit.React,
130+
module: ts.ModuleKind.CommonJS,
131+
target: ts.ScriptTarget.Latest,
118132
};
119133

134+
if (userCompilerOptions) {
135+
compilerOptions = { ...compilerOptions, ...userCompilerOptions };
136+
}
137+
138+
if (tsconfigPath) {
139+
const { options } = getTSConfigFile(tsconfigPath);
140+
compilerOptions = { ...compilerOptions, ...options };
141+
}
142+
120143
compiler.hooks.make.tap(this.name, (compilation) => {
121144
compilation.hooks.seal.tap(this.name, () => {
122145
const modulesToProcess: Module[] = [];
@@ -132,20 +155,21 @@ export default class DocgenPlugin {
132155
}
133156
});
134157

158+
const parser =
159+
(tsconfigPath &&
160+
docGen.withCustomConfig(tsconfigPath, docgenOptions)) ||
161+
docGen.withCompilerOptions(compilerOptions, docgenOptions);
135162
const tsProgram = ts.createProgram(
136163
modulesToProcess.map((v) => v.userRequest),
137-
{
138-
jsx: ts.JsxEmit.React,
139-
module: ts.ModuleKind.CommonJS,
140-
target: ts.ScriptTarget.Latest,
141-
}
164+
compilerOptions
142165
);
143-
const parser = tsconfigPath
144-
? docGen.withCustomConfig(tsconfigPath, docgenOptions)
145-
: docGen.withDefaultConfig({});
146166

147167
modulesToProcess.forEach((m) =>
148-
processModule(parser, m, tsProgram, loaderOptions)
168+
processModule(parser, m, tsProgram, {
169+
docgenCollectionName,
170+
setDisplayName,
171+
typePropName,
172+
})
149173
);
150174
});
151175
});

0 commit comments

Comments
 (0)