Skip to content

Commit 95eb37b

Browse files
committed
refactor: Create a single tsProgram to match old logic
1 parent 5b1c571 commit 95eb37b

1 file changed

Lines changed: 24 additions & 7 deletions

File tree

src/plugin.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Compiler, WebpackPluginInstance } from "webpack";
44
import ts from "typescript";
55
import * as docGen from "react-docgen-typescript";
66
import { matcher } from "micromatch";
7+
import * as webpack from "webpack";
78

89
import { LoaderOptions } from "./types";
910
import DocGenDependency from "./dependency";
@@ -90,7 +91,10 @@ export default class DocgenPlugin implements WebpackPluginInstance {
9091
);
9192

9293
compilation.hooks.seal.tap(pluginName, () => {
93-
for (const module of compilation.modules) {
94+
const modulesToProcess: [string, webpack.Module][] = [];
95+
96+
// 1. Aggregate modules to process
97+
compilation.modules.forEach((module: webpack.Module) => {
9498
const nameForCondition = module.nameForCondition() || "";
9599

96100
if (isExcluded(nameForCondition)) {
@@ -107,19 +111,32 @@ export default class DocgenPlugin implements WebpackPluginInstance {
107111
return;
108112
}
109113

110-
const componentDocs = docGenParser.parse(nameForCondition);
114+
modulesToProcess.push([nameForCondition, module]);
115+
});
116+
117+
// 2. Create a ts program with the modules
118+
const tsProgram = ts.createProgram(
119+
modulesToProcess.map(([name]) => name),
120+
compilerOptions
121+
);
111122

123+
// 3. Process and parse each module and add the type information
124+
// as a dependency
125+
modulesToProcess.forEach(([name, module]) =>
112126
module.addDependency(
113127
new DocGenDependency(
114128
generateDocgenCodeBlock({
115-
filename: nameForCondition,
116-
source: nameForCondition,
117-
componentDocs,
129+
filename: name,
130+
source: name,
131+
componentDocs: docGenParser.parseWithProgramProvider(
132+
name,
133+
() => tsProgram
134+
),
118135
...generateOptions,
119136
})
120137
)
121-
);
122-
}
138+
)
139+
);
123140
});
124141
});
125142
}

0 commit comments

Comments
 (0)