Skip to content

Commit cea84f4

Browse files
committed
refactor: Push parsing earlier
1 parent ed02af9 commit cea84f4

2 files changed

Lines changed: 27 additions & 23 deletions

File tree

src/dependency.ts

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ import { LoaderOptions } from "./types";
2020

2121
class DocGenDependency extends NullDependency {
2222
public static Template: NullDependency.Template;
23+
private componentDocs: docGen.ComponentDoc[];
24+
25+
constructor(request: string, componentDocs: docGen.ComponentDoc[]) {
26+
super(request);
27+
28+
this.componentDocs = componentDocs;
29+
}
2330

2431
// TODO: Note if you want that modules correctly invalidate and cache you need to add updateHash to your Dependency and hash the type info (because that might change depending on outside factors (other modules)
2532
}
@@ -50,31 +57,23 @@ class DocGenTemplate extends NullDependency.Template {
5057
): void {
5158
const { userRequest } = module;
5259

53-
// Since parsing fails with a path including !, remove it.
54-
// The problem is that webpack injects that and there doesn't
55-
// seem to be a good way to get only the path itself from
56-
// a dependency.
57-
const modulePath = userRequest.includes("!")
58-
? userRequest.split("!")[1]
59-
: userRequest;
60-
const componentDocs = this.options.parser.parse(modulePath);
61-
62-
if (!componentDocs.length) {
60+
if (!dependency.componentDocs.length) {
6361
return;
6462
}
6563

66-
const docgenBlock = generateDocgenCodeBlock({
67-
filename: userRequest,
68-
source: userRequest,
69-
componentDocs,
70-
docgenCollectionName:
71-
this.options.docgenOptions.docgenCollectionName ||
72-
"STORYBOOK_REACT_CLASSES",
73-
setDisplayName: this.options.docgenOptions.setDisplayName || true,
74-
typePropName: this.options.docgenOptions.typePropName || "type",
75-
}).substring(userRequest.length);
76-
77-
source.insert(0, docgenBlock);
64+
source.insert(
65+
0,
66+
generateDocgenCodeBlock({
67+
filename: userRequest,
68+
source: userRequest,
69+
componentDocs: dependency.componentDocs,
70+
docgenCollectionName:
71+
this.options.docgenOptions.docgenCollectionName ||
72+
"STORYBOOK_REACT_CLASSES",
73+
setDisplayName: this.options.docgenOptions.setDisplayName || true,
74+
typePropName: this.options.docgenOptions.typePropName || "type",
75+
}).substring(userRequest.length)
76+
);
7877
}
7978
}
8079

src/plugin.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,12 @@ export default class DocgenPlugin {
140140

141141
// eslint-disable-next-line
142142
// @ts-ignore
143-
module.addDependency(new DocGenDependency(module.request));
143+
module.addDependency(
144+
new DocGenDependency(
145+
module.request,
146+
this.parser.parse(nameForCondition)
147+
)
148+
);
144149
});
145150
};
146151

0 commit comments

Comments
 (0)