Skip to content

Commit edd608f

Browse files
Merge pull request #49 from zhzz/fix-custom-options
fix: respect falsy custom options
2 parents 31ce4e8 + 222d854 commit edd608f

2 files changed

Lines changed: 45 additions & 3 deletions

File tree

src/__tests__/plugin.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import webpack, { Configuration } from "webpack";
22
import { createFsFromVolume, IFs, Volume } from "memfs";
33
import ReactDocgenTypeScriptPlugin from "..";
4+
import { LoaderOptions } from "../types";
45

56
// eslint-disable-next-line
67
const joinPath = require("memory-fs/lib/join");
@@ -84,3 +85,34 @@ test("default options", async () => {
8485

8586
expect(result).toContain("STORYBOOK_REACT_CLASSES");
8687
});
88+
89+
describe("custom options", () => {
90+
describe("loader options", () => {
91+
const options: Record<
92+
keyof LoaderOptions,
93+
Array<LoaderOptions[keyof LoaderOptions]>
94+
> = {
95+
setDisplayName: [true, false, undefined],
96+
typePropName: ["customValue", undefined],
97+
docgenCollectionName: ["customValue", null, undefined],
98+
};
99+
const { defaultOptions } = ReactDocgenTypeScriptPlugin;
100+
101+
(Object.keys(options) as Array<keyof LoaderOptions>).forEach(
102+
(optionName) => {
103+
const values = options[optionName];
104+
105+
test.each(values)(`${optionName}: %p`, (value) => {
106+
const plugin = new ReactDocgenTypeScriptPlugin({
107+
[optionName]: value,
108+
});
109+
const { generateOptions: resultOptions } = plugin.getOptions();
110+
111+
expect(resultOptions[optionName]).toBe(
112+
value === undefined ? defaultOptions[optionName] : value
113+
);
114+
});
115+
}
116+
);
117+
});
118+
});

src/plugin.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ function processModule(
135135

136136
/** Inject typescript docgen information into modules at the end of a build */
137137
export default class DocgenPlugin implements webpack.WebpackPluginInstance {
138+
public static defaultOptions = {
139+
setDisplayName: true,
140+
typePropName: "type",
141+
docgenCollectionName: "STORYBOOK_REACT_CLASSES",
142+
};
143+
138144
private name = "React Docgen Typescript Plugin";
139145
private options: PluginOptions;
140146

@@ -266,6 +272,7 @@ export default class DocgenPlugin implements webpack.WebpackPluginInstance {
266272
typePropName,
267273
...docgenOptions
268274
} = this.options;
275+
const { defaultOptions } = DocgenPlugin;
269276

270277
let compilerOptions = {
271278
jsx: ts.JsxEmit.React,
@@ -286,9 +293,12 @@ export default class DocgenPlugin implements webpack.WebpackPluginInstance {
286293
return {
287294
docgenOptions,
288295
generateOptions: {
289-
docgenCollectionName: docgenCollectionName || "STORYBOOK_REACT_CLASSES",
290-
setDisplayName: setDisplayName || true,
291-
typePropName: typePropName || "type",
296+
docgenCollectionName:
297+
docgenCollectionName === undefined
298+
? defaultOptions.docgenCollectionName
299+
: docgenCollectionName,
300+
setDisplayName: setDisplayName ?? defaultOptions.setDisplayName,
301+
typePropName: typePropName ?? defaultOptions.typePropName,
292302
},
293303
compilerOptions,
294304
};

0 commit comments

Comments
 (0)