|
1 | | -/* eslint-disable no-param-reassign, no-underscore-dangle */ |
2 | | - |
3 | 1 | import path from "path"; |
4 | 2 | import createDebug from "debug"; |
5 | 3 | import { Compiler, WebpackPluginInstance } from "webpack"; |
6 | 4 | import ts from "typescript"; |
7 | 5 | import * as docGen from "react-docgen-typescript"; |
8 | 6 | import { matcher } from "micromatch"; |
9 | 7 |
|
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 | | - |
14 | 8 | import { LoaderOptions } from "./types"; |
15 | 9 | import DocGenDependency from "./dependency"; |
16 | 10 | import { |
@@ -89,63 +83,45 @@ export default class DocgenPlugin implements WebpackPluginInstance { |
89 | 83 | const isExcluded = matchGlob(exclude); |
90 | 84 | const isIncluded = matchGlob(include); |
91 | 85 |
|
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}` |
134 | 99 | ); |
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 | + }); |
149 | 125 | } |
150 | 126 |
|
151 | 127 | getOptions(): { |
|
0 commit comments