Skip to content

Commit 4a38afb

Browse files
committed
Refactor compilation logic and fix webpack4 flow
1 parent c3eae8b commit 4a38afb

1 file changed

Lines changed: 26 additions & 13 deletions

File tree

lib/index.js

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -143,21 +143,31 @@ class BundleTrackerPlugin {
143143
}
144144
/**
145145
* Handle compile hook
146-
* @param {Compiler} _compiler
147146
*/
148-
_handleCompile(_compiler) {
147+
_handleCompile() {
149148
this.output = { status: 'compile', assets: {}, chunks: {} };
150149
this._writeOutput();
151150
}
152151

153-
_handleAssetEmitted(_compiler, assetName, stats) {
152+
/**
153+
* Hook to inject the webpack compilation object
154+
*/
155+
_handleThisCompilation(compilation) {
156+
this._compilation = compilation;
157+
}
158+
159+
/**
160+
* Hook to handle each compiled asset and set their
161+
* info into the output object
162+
*/
163+
_handleAssetEmitted(assetName) {
154164
const fileInfo = {
155165
name: assetName,
156-
path: getAssetPath(stats.compilation, assetName),
166+
path: getAssetPath(this._compilation, assetName),
157167
};
158168

159169
if (this.options.integrity === true) {
160-
fileInfo.integrity = this._computeIntegrity(getSource(stats.compilation, assetName));
170+
fileInfo.integrity = this._computeIntegrity(getSource(this._compilation, assetName));
161171
}
162172

163173
if (this.options.publicPath) {
@@ -173,20 +183,19 @@ class BundleTrackerPlugin {
173183
}
174184

175185
// @ts-ignore: TS2339: Property 'assetsInfo' does not exist on type 'Compilation'.
176-
if (stats.compilation.assetsInfo) {
186+
if (this._compilation.assetsInfo) {
177187
// @ts-ignore: TS2339: Property 'assetsInfo' does not exist on type 'Compilation'.
178-
fileInfo.sourceFilename = stats.compilation.assetsInfo.get(assetName).sourceFilename;
188+
fileInfo.sourceFilename = this._compilation.assetsInfo.get(assetName).sourceFilename;
179189
}
180190

181191
this.output.assets[assetName] = fileInfo;
182192
}
183193

184194
/**
185-
* Handle compile hook
186-
* @param {Compiler} _compiler
195+
* Handle done hook and write output file
187196
* @param {Stats} stats
188197
*/
189-
_handleDone(_compiler, stats) {
198+
_handleDone(stats) {
190199
if (stats.hasErrors()) {
191200
const findError = compilation => {
192201
if (compilation.errors.length > 0) {
@@ -195,6 +204,7 @@ class BundleTrackerPlugin {
195204
return compilation.children.find(child => findError(child));
196205
};
197206
const error = findError(stats.compilation);
207+
// @ts-ignore: TS2739
198208
this.output = {
199209
status: 'error',
200210
error: error?.name ?? 'unknown-error',
@@ -224,9 +234,12 @@ class BundleTrackerPlugin {
224234
*/
225235
apply(compiler) {
226236
this._setParamsFromCompiler(compiler);
227-
compiler.hooks.compile.tap(this.name, this._handleCompile.bind(this, compiler));
228-
compiler.hooks.assetEmitted.tap(this.name, this._handleAssetEmitted.bind(this, compiler));
229-
compiler.hooks.done.tap(this.name, this._handleDone.bind(this, compiler));
237+
compiler.hooks.compile.tap(this.name, this._handleCompile.bind(this));
238+
// The thisCompilation hook is required here because webpack4 does not properly
239+
// inject the stats object with the compilation data into the assetEmitted hook
240+
compiler.hooks.thisCompilation.tap(this.name, this._handleThisCompilation.bind(this));
241+
compiler.hooks.assetEmitted.tap(this.name, this._handleAssetEmitted.bind(this));
242+
compiler.hooks.done.tap(this.name, this._handleDone.bind(this));
230243
}
231244
}
232245

0 commit comments

Comments
 (0)