Skip to content

Commit 5b1c571

Browse files
committed
refactor: Write docs at seal
1 parent bcfc489 commit 5b1c571

1 file changed

Lines changed: 38 additions & 62 deletions

File tree

src/plugin.ts

Lines changed: 38 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
/* eslint-disable no-param-reassign, no-underscore-dangle */
2-
31
import path from "path";
42
import createDebug from "debug";
53
import { Compiler, WebpackPluginInstance } from "webpack";
64
import ts from "typescript";
75
import * as docGen from "react-docgen-typescript";
86
import { matcher } from "micromatch";
97

10-
// eslint-disable-next-line
11-
// @ts-ignore: What's the right way to refer to this one?
12-
import JavascriptParser from "webpack/lib/javascript/JavascriptParser.js";
13-
148
import { LoaderOptions } from "./types";
159
import DocGenDependency from "./dependency";
1610
import {
@@ -89,63 +83,45 @@ export default class DocgenPlugin implements WebpackPluginInstance {
8983
const isExcluded = matchGlob(exclude);
9084
const isIncluded = matchGlob(include);
9185

92-
compiler.hooks.compilation.tap(
93-
pluginName,
94-
(compilation, { normalModuleFactory }) => {
95-
compilation.dependencyTemplates.set(
96-
// eslint-disable-next-line
97-
// @ts-ignore TODO: Figure out why this isn't allowed
98-
DocGenDependency,
99-
new DocGenDependency.Template()
100-
);
101-
102-
const handler = (parser: JavascriptParser) => {
103-
parser.hooks.program.tap(pluginName, () => {
104-
// eslint-disable-next-line
105-
// @ts-ignore
106-
const { module } = parser.state;
107-
const nameForCondition = module.nameForCondition();
108-
109-
if (isExcluded(nameForCondition)) {
110-
debugExclude(
111-
`Module not matched in "exclude": ${nameForCondition}`
112-
);
113-
return;
114-
}
115-
116-
if (!isIncluded(nameForCondition)) {
117-
debugExclude(
118-
`Module not matched in "include": ${nameForCondition}`
119-
);
120-
return;
121-
}
122-
123-
const componentDocs = docGenParser.parse(nameForCondition);
124-
125-
module.addDependency(
126-
new DocGenDependency(
127-
generateDocgenCodeBlock({
128-
filename: nameForCondition,
129-
source: nameForCondition,
130-
componentDocs,
131-
...generateOptions,
132-
}).substring(module.userRequest.length)
133-
)
86+
compiler.hooks.compilation.tap(pluginName, (compilation) => {
87+
compilation.dependencyTemplates.set(
88+
DocGenDependency,
89+
new DocGenDependency.Template()
90+
);
91+
92+
compilation.hooks.seal.tap(pluginName, () => {
93+
for (const module of compilation.modules) {
94+
const nameForCondition = module.nameForCondition() || "";
95+
96+
if (isExcluded(nameForCondition)) {
97+
debugExclude(
98+
`Module not matched in "exclude": ${nameForCondition}`
13499
);
135-
});
136-
};
137-
138-
normalModuleFactory.hooks.parser
139-
.for("javascript/auto")
140-
.tap(pluginName, handler);
141-
normalModuleFactory.hooks.parser
142-
.for("javascript/dynamic")
143-
.tap(pluginName, handler);
144-
normalModuleFactory.hooks.parser
145-
.for("javascript/esm")
146-
.tap(pluginName, handler);
147-
}
148-
);
100+
return;
101+
}
102+
103+
if (!isIncluded(nameForCondition)) {
104+
debugExclude(
105+
`Module not matched in "include": ${nameForCondition}`
106+
);
107+
return;
108+
}
109+
110+
const componentDocs = docGenParser.parse(nameForCondition);
111+
112+
module.addDependency(
113+
new DocGenDependency(
114+
generateDocgenCodeBlock({
115+
filename: nameForCondition,
116+
source: nameForCondition,
117+
componentDocs,
118+
...generateOptions,
119+
})
120+
)
121+
);
122+
}
123+
});
124+
});
149125
}
150126

151127
getOptions(): {

0 commit comments

Comments
 (0)