Skip to content

Commit fcb6852

Browse files
Merge pull request #9 from hipstersmoothie/non-ts
default to loading root tsconfig.json when no typescript configuration is provided
2 parents 0418a0c + 7536c0f commit fcb6852

2 files changed

Lines changed: 31 additions & 24 deletions

File tree

readme.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ const ReactDocgenTypescriptPlugin = require("react-docgen-typescript-plugin").de
2424

2525
module.exports = {
2626
plugins: [
27+
// Will default to loading your root tsconfig.json
2728
new ReactDocgenTypescriptPlugin(),
28-
// or with a tsconfig
29-
new ReactDocgenTypescriptPlugin({ tsconfigPath: "./tsconfig.json" }),
30-
// or with options
31-
new ReactDocgenTypescriptPlugin({ jsx: ts.JsxEmit.Preserve }),
29+
// or with a specific tsconfig
30+
new ReactDocgenTypescriptPlugin({ tsconfigPath: "./tsconfig.dev.json" }),
31+
// or with compiler options
32+
new ReactDocgenTypescriptPlugin({ compilerOptions: { jsx: ts.JsxEmit.Preserve } }),
3233
],
3334
};
3435
```

src/plugin.ts

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,20 @@ function processModule(
110110

111111
/** Get the contents of the tsconfig in the system */
112112
function getTSConfigFile(tsconfigPath: string): ts.ParsedCommandLine {
113-
const basePath = path.dirname(tsconfigPath);
114-
const configFile = ts.readConfigFile(tsconfigPath, ts.sys.readFile);
115-
116-
return ts.parseJsonConfigFileContent(
117-
configFile.config,
118-
ts.sys,
119-
basePath,
120-
{},
121-
tsconfigPath
122-
);
113+
try {
114+
const basePath = path.dirname(tsconfigPath);
115+
const configFile = ts.readConfigFile(tsconfigPath, ts.sys.readFile);
116+
117+
return ts.parseJsonConfigFileContent(
118+
configFile.config,
119+
ts.sys,
120+
basePath,
121+
{},
122+
tsconfigPath
123+
);
124+
} catch (error) {
125+
return {} as ts.ParsedCommandLine;
126+
}
123127
}
124128

125129
/** Create a glob matching function. */
@@ -137,7 +141,7 @@ export default class DocgenPlugin {
137141

138142
apply(compiler: webpack.Compiler) {
139143
const {
140-
tsconfigPath,
144+
tsconfigPath = "./tsconfig.json",
141145
docgenCollectionName = "STORYBOOK_REACT_CLASSES",
142146
setDisplayName = true,
143147
typePropName = "type",
@@ -158,16 +162,12 @@ export default class DocgenPlugin {
158162

159163
if (userCompilerOptions) {
160164
compilerOptions = { ...compilerOptions, ...userCompilerOptions };
161-
}
162-
163-
if (tsconfigPath) {
165+
} else {
164166
const { options } = getTSConfigFile(tsconfigPath);
165167
compilerOptions = { ...compilerOptions, ...options };
166168
}
167169

168-
const parser =
169-
(tsconfigPath && docGen.withCustomConfig(tsconfigPath, docgenOptions)) ||
170-
docGen.withCompilerOptions(compilerOptions, docgenOptions);
170+
const parser = docGen.withCompilerOptions(compilerOptions, docgenOptions);
171171

172172
compiler.hooks.make.tap(this.name, (compilation) => {
173173
compilation.hooks.seal.tap(this.name, () => {
@@ -185,17 +185,23 @@ export default class DocgenPlugin {
185185
}
186186

187187
if (!module.rawRequest) {
188-
debugExclude(`Ignoring module without "rawRequest": ${module.userRequest}`);
188+
debugExclude(
189+
`Ignoring module without "rawRequest": ${module.userRequest}`
190+
);
189191
return;
190192
}
191193

192194
if (isExcluded(module.request)) {
193-
debugExclude(`Module not matched in "exclude": ${module.userRequest}`);
195+
debugExclude(
196+
`Module not matched in "exclude": ${module.userRequest}`
197+
);
194198
return;
195199
}
196200

197201
if (!isIncluded(module.request)) {
198-
debugExclude(`Module not matched in "include": ${module.userRequest}`);
202+
debugExclude(
203+
`Module not matched in "include": ${module.userRequest}`
204+
);
199205
return;
200206
}
201207

0 commit comments

Comments
 (0)