@@ -6,7 +6,6 @@ import { matcher } from "micromatch";
66import * as webpack from "webpack" ;
77
88import { LoaderOptions } from "./types" ;
9- import DocGenDependency from "./dependency" ;
109import {
1110 generateDocgenCodeBlock ,
1211 GeneratorOptions ,
@@ -130,74 +129,89 @@ export default class DocgenPlugin implements webpack.WebpackPluginInstance {
130129 const webpackVersion = compiler . webpack . version ;
131130 const isWebpack5 = parseInt ( webpackVersion . split ( "." ) [ 0 ] , 10 ) >= 5 ;
132131
133- compiler . hooks . compilation . tap ( pluginName , ( compilation ) => {
134- if ( isWebpack5 ) {
135- compilation . dependencyTemplates . set (
132+ compiler . hooks . compilation . tap (
133+ pluginName ,
134+ ( compilation : webpack . Compilation ) => {
135+ if ( isWebpack5 ) {
136+ // Since this file is needed only for webpack 5, load it only then
137+ // to simplify the implementation of the file.
138+ //
136139 // eslint-disable-next-line
137- // @ts -ignore: Webpack 4 type
138- DocGenDependency ,
139- // eslint-disable-next-line
140- // @ts -ignore: Webpack 4 type
141- new DocGenDependency . Template ( )
142- ) ;
143- }
144-
145- compilation . hooks . seal . tap ( pluginName , ( ) => {
146- const modulesToProcess : [ string , webpack . Module ] [ ] = [ ] ;
147-
148- // 1. Aggregate modules to process
149- compilation . modules . forEach ( ( module : webpack . Module ) => {
150- const nameForCondition = module . nameForCondition ( ) || "" ;
151-
152- if ( isExcluded ( nameForCondition ) ) {
153- debugExclude (
154- `Module not matched in "exclude": ${ nameForCondition } `
155- ) ;
156- return ;
157- }
158-
159- if ( ! isIncluded ( nameForCondition ) ) {
160- debugExclude (
161- `Module not matched in "include": ${ nameForCondition } `
162- ) ;
163- return ;
164- }
165-
166- modulesToProcess . push ( [ nameForCondition , module ] ) ;
167- } ) ;
168-
169- // 2. Create a ts program with the modules
170- const tsProgram = ts . createProgram (
171- modulesToProcess . map ( ( [ name ] ) => name ) ,
172- compilerOptions
173- ) ;
174-
175- // 3. Process and parse each module and add the type information
176- // as a dependency
177- modulesToProcess . forEach ( ( [ name , module ] ) => {
178- if ( isWebpack5 ) {
179- module . addDependency (
140+ const { DocGenDependency } = require ( "./dependency" ) ;
141+
142+ compilation . dependencyTemplates . set (
143+ // eslint-disable-next-line
144+ // @ts -ignore: Webpack 4 type
145+ DocGenDependency ,
146+ // eslint-disable-next-line
147+ // @ts -ignore: Webpack 4 type
148+ new DocGenDependency . Template ( )
149+ ) ;
150+ }
151+
152+ compilation . hooks . seal . tap ( pluginName , ( ) => {
153+ const modulesToProcess : [ string , webpack . Module ] [ ] = [ ] ;
154+
155+ // 1. Aggregate modules to process
156+ compilation . modules . forEach ( ( module : webpack . Module ) => {
157+ const nameForCondition = module . nameForCondition ( ) || "" ;
158+
159+ if ( isExcluded ( nameForCondition ) ) {
160+ debugExclude (
161+ `Module not matched in "exclude": ${ nameForCondition } `
162+ ) ;
163+ return ;
164+ }
165+
166+ if ( ! isIncluded ( nameForCondition ) ) {
167+ debugExclude (
168+ `Module not matched in "include": ${ nameForCondition } `
169+ ) ;
170+ return ;
171+ }
172+
173+ modulesToProcess . push ( [ nameForCondition , module ] ) ;
174+ } ) ;
175+
176+ // 2. Create a ts program with the modules
177+ const tsProgram = ts . createProgram (
178+ modulesToProcess . map ( ( [ name ] ) => name ) ,
179+ compilerOptions
180+ ) ;
181+
182+ // 3. Process and parse each module and add the type information
183+ // as a dependency
184+ modulesToProcess . forEach ( ( [ name , module ] ) => {
185+ if ( isWebpack5 ) {
186+ // Since this file is needed only for webpack 5, load it only then
187+ // to simplify the implementation of the file.
188+ //
180189 // eslint-disable-next-line
181- // @ts -ignore: Webpack 4 type
182- new DocGenDependency (
183- generateDocgenCodeBlock ( {
184- filename : name ,
185- source : name ,
186- componentDocs : docGenParser . parseWithProgramProvider (
187- name ,
188- ( ) => tsProgram
189- ) ,
190- ...generateOptions ,
191- } )
192- )
193- ) ;
194- } else {
195- // Assume webpack 4 or earlier
196- processModule ( docGenParser , module , tsProgram , generateOptions ) ;
197- }
190+ const { DocGenDependency } = require ( "./dependency" ) ;
191+
192+ module . addDependency (
193+ // eslint-disable-next-line
194+ // @ts -ignore: Webpack 4 type
195+ new DocGenDependency (
196+ generateDocgenCodeBlock ( {
197+ filename : name ,
198+ source : name ,
199+ componentDocs : docGenParser . parseWithProgramProvider (
200+ name ,
201+ ( ) => tsProgram
202+ ) ,
203+ ...generateOptions ,
204+ } )
205+ )
206+ ) ;
207+ } else {
208+ // Assume webpack 4 or earlier
209+ processModule ( docGenParser , module , tsProgram , generateOptions ) ;
210+ }
211+ } ) ;
198212 } ) ;
199- } ) ;
200- } ) ;
213+ }
214+ ) ;
201215 }
202216
203217 getOptions ( ) : {
0 commit comments