Skip to content

Commit f6e2133

Browse files
committed
refactor: Push option logic to apply
1 parent d7708ef commit f6e2133

1 file changed

Lines changed: 35 additions & 33 deletions

File tree

src/plugin.ts

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -61,42 +61,15 @@ const matchGlob = (globs?: string[]) => {
6161
export default class DocgenPlugin implements WebpackPluginInstance {
6262
private name = "React Docgen Typescript Plugin";
6363
private options: PluginOptions;
64-
private parser: docGen.FileParser;
65-
private docgenOptions: LoaderOptions;
6664

6765
constructor(options: PluginOptions = {}) {
68-
const {
69-
tsconfigPath = "./tsconfig.json",
70-
compilerOptions: userCompilerOptions,
71-
...docgenOptions
72-
} = options;
73-
74-
let compilerOptions = {
75-
jsx: ts.JsxEmit.React,
76-
module: ts.ModuleKind.CommonJS,
77-
target: ts.ScriptTarget.Latest,
78-
};
79-
80-
if (userCompilerOptions) {
81-
compilerOptions = {
82-
...compilerOptions,
83-
...userCompilerOptions,
84-
};
85-
} else {
86-
const { options: tsOptions } = getTSConfigFile(tsconfigPath);
87-
compilerOptions = { ...compilerOptions, ...tsOptions };
88-
}
89-
9066
this.options = options;
91-
this.docgenOptions = docgenOptions;
92-
93-
// Maintain the doc gen parser here to save time later
94-
this.parser = docGen.withCompilerOptions(compilerOptions, docgenOptions);
9567
}
9668

9769
apply(compiler: Compiler): void {
9870
const pluginName = "DocGenPlugin";
99-
71+
const { docgenOptions, compilerOptions } = this.getOptions();
72+
const docGenParser = docGen.withCompilerOptions(compilerOptions);
10073
const { exclude = [], include = ["**/**.tsx"] } = this.options;
10174
const isExcluded = matchGlob(exclude);
10275
const isIncluded = matchGlob(include);
@@ -134,7 +107,7 @@ export default class DocgenPlugin implements WebpackPluginInstance {
134107
return;
135108
}
136109

137-
const componentDocs = this.parser.parse(nameForCondition);
110+
const componentDocs = docGenParser.parse(nameForCondition);
138111

139112
module.addDependency(
140113
new DocGenDependency(
@@ -144,10 +117,10 @@ export default class DocgenPlugin implements WebpackPluginInstance {
144117
source: nameForCondition,
145118
componentDocs,
146119
docgenCollectionName:
147-
this.docgenOptions.docgenCollectionName ||
120+
docgenOptions.docgenCollectionName ||
148121
"STORYBOOK_REACT_CLASSES",
149-
setDisplayName: this.docgenOptions.setDisplayName || true,
150-
typePropName: this.docgenOptions.typePropName || "type",
122+
setDisplayName: docgenOptions.setDisplayName || true,
123+
typePropName: docgenOptions.typePropName || "type",
151124
}).substring(module.userRequest.length)
152125
)
153126
);
@@ -166,6 +139,35 @@ export default class DocgenPlugin implements WebpackPluginInstance {
166139
}
167140
);
168141
}
142+
143+
getOptions(): {
144+
docgenOptions: LoaderOptions;
145+
compilerOptions: ts.CompilerOptions;
146+
} {
147+
const {
148+
tsconfigPath = "./tsconfig.json",
149+
compilerOptions: userCompilerOptions,
150+
...docgenOptions
151+
} = this.options;
152+
153+
let compilerOptions = {
154+
jsx: ts.JsxEmit.React,
155+
module: ts.ModuleKind.CommonJS,
156+
target: ts.ScriptTarget.Latest,
157+
};
158+
159+
if (userCompilerOptions) {
160+
compilerOptions = {
161+
...compilerOptions,
162+
...userCompilerOptions,
163+
};
164+
} else {
165+
const { options: tsOptions } = getTSConfigFile(tsconfigPath);
166+
compilerOptions = { ...compilerOptions, ...tsOptions };
167+
}
168+
169+
return { docgenOptions, compilerOptions };
170+
}
169171
}
170172

171173
export type DocgenPluginType = typeof DocgenPlugin;

0 commit comments

Comments
 (0)