@@ -4,6 +4,7 @@ import { Compiler, WebpackPluginInstance } from "webpack";
44import ts from "typescript" ;
55import * as docGen from "react-docgen-typescript" ;
66import { matcher } from "micromatch" ;
7+ import * as webpack from "webpack" ;
78
89import { LoaderOptions } from "./types" ;
910import 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