Skip to content

Commit c3eae8b

Browse files
committed
Fix flow for webpack5
1 parent 2da54ab commit c3eae8b

2 files changed

Lines changed: 41 additions & 44 deletions

File tree

lib/index.js

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ class BundleTrackerPlugin {
5656
};
5757
this.name = 'BundleTrackerPlugin';
5858

59-
this.output = {};
59+
this.output = {
60+
status: 'compile',
61+
assets: {},
62+
chunks: {},
63+
};
6064
this.outputChunkDir = '';
6165
this.outputTrackerFile = '';
6266
this.outputTrackerDir = '';
@@ -104,11 +108,9 @@ class BundleTrackerPlugin {
104108
}
105109
/**
106110
* Write bundle tracker stats file
107-
*
108-
* @param {Compiler} _compiler
109-
* @param {Partial<Contents>} contents
110111
*/
111-
_writeOutput(_compiler, contents) {
112+
_writeOutput() {
113+
const contents = this.output;
112114
Object.assign(this.contents, contents, {
113115
assets: mergeObjectsAndSortKeys(this.contents.assets, contents.assets),
114116
chunks: mergeObjectsAndSortKeys(this.contents.chunks, contents.chunks),
@@ -141,54 +143,50 @@ class BundleTrackerPlugin {
141143
}
142144
/**
143145
* Handle compile hook
144-
* @param {Compiler} compiler
146+
* @param {Compiler} _compiler
145147
*/
146-
_handleCompile(compiler) {
147-
this._writeOutput(compiler, { status: 'compile' });
148+
_handleCompile(_compiler) {
149+
this.output = { status: 'compile', assets: {}, chunks: {} };
150+
this._writeOutput();
148151
}
149152

150-
_handleAssetEmitted(_compiler, _, stats) {
151-
/** @type {Contents} */
152-
const output = { status: 'done', assets: {}, chunks: {} };
153-
Object.entries(stats.compilation.assets).map(([assetName, _]) => {
154-
const fileInfo = {
155-
name: assetName,
156-
path: getAssetPath(stats.compilation, assetName),
157-
};
153+
_handleAssetEmitted(_compiler, assetName, stats) {
154+
const fileInfo = {
155+
name: assetName,
156+
path: getAssetPath(stats.compilation, assetName),
157+
};
158158

159-
if (this.options.integrity === true) {
160-
fileInfo.integrity = this._computeIntegrity(getSource(stats.compilation, assetName));
161-
}
159+
if (this.options.integrity === true) {
160+
fileInfo.integrity = this._computeIntegrity(getSource(stats.compilation, assetName));
161+
}
162162

163-
if (this.options.publicPath) {
164-
if (this.options.publicPath === 'auto') {
165-
fileInfo.publicPath = 'auto';
166-
} else {
167-
fileInfo.publicPath = this.options.publicPath + assetName;
168-
}
163+
if (this.options.publicPath) {
164+
if (this.options.publicPath === 'auto') {
165+
fileInfo.publicPath = 'auto';
166+
} else {
167+
fileInfo.publicPath = this.options.publicPath + assetName;
169168
}
169+
}
170170

171-
if (this.options.relativePath === true) {
172-
fileInfo.path = path.relative(this.outputChunkDir, fileInfo.path);
173-
}
171+
if (this.options.relativePath === true) {
172+
fileInfo.path = path.relative(this.outputChunkDir, fileInfo.path);
173+
}
174174

175+
// @ts-ignore: TS2339: Property 'assetsInfo' does not exist on type 'Compilation'.
176+
if (stats.compilation.assetsInfo) {
175177
// @ts-ignore: TS2339: Property 'assetsInfo' does not exist on type 'Compilation'.
176-
if (stats.compilation.assetsInfo) {
177-
// @ts-ignore: TS2339: Property 'assetsInfo' does not exist on type 'Compilation'.
178-
fileInfo.sourceFilename = stats.compilation.assetsInfo.get(assetName).sourceFilename;
179-
}
178+
fileInfo.sourceFilename = stats.compilation.assetsInfo.get(assetName).sourceFilename;
179+
}
180180

181-
output.assets[assetName] = fileInfo;
182-
});
183-
this.output = output;
181+
this.output.assets[assetName] = fileInfo;
184182
}
185183

186184
/**
187185
* Handle compile hook
188-
* @param {Compiler} compiler
186+
* @param {Compiler} _compiler
189187
* @param {Stats} stats
190188
*/
191-
_handleDone(compiler, stats) {
189+
_handleDone(_compiler, stats) {
192190
if (stats.hasErrors()) {
193191
const findError = compilation => {
194192
if (compilation.errors.length > 0) {
@@ -197,27 +195,27 @@ class BundleTrackerPlugin {
197195
return compilation.children.find(child => findError(child));
198196
};
199197
const error = findError(stats.compilation);
200-
this._writeOutput(compiler, {
198+
this.output = {
201199
status: 'error',
202200
error: error?.name ?? 'unknown-error',
203201
message: stripAnsi(error['message']),
204-
});
202+
};
203+
this._writeOutput();
205204

206205
return;
207206
}
208207

209208
stats.compilation.chunkGroups.forEach(chunkGroup => {
210209
if (!chunkGroup.isInitial()) return;
211-
212210
this.output.chunks[chunkGroup.name] = chunkGroup.getFiles();
213211
});
214212

215213
if (this.options.logTime === true) {
216214
this.output.startTime = stats.startTime;
217215
this.output.endTime = stats.endTime;
218216
}
219-
220-
this._writeOutput(compiler, this.output);
217+
this.output.status = 'done';
218+
this._writeOutput();
221219
}
222220

223221
/**
@@ -226,7 +224,6 @@ class BundleTrackerPlugin {
226224
*/
227225
apply(compiler) {
228226
this._setParamsFromCompiler(compiler);
229-
230227
compiler.hooks.compile.tap(this.name, this._handleCompile.bind(this, compiler));
231228
compiler.hooks.assetEmitted.tap(this.name, this._handleAssetEmitted.bind(this, compiler));
232229
compiler.hooks.done.tap(this.name, this._handleDone.bind(this, compiler));

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"homepage": "https://github.com/django-webpack/webpack-bundle-tracker",
1313
"bugs": "https://github.com/django-webpack/webpack-bundle-tracker/issues",
1414
"engines": {
15-
"node": ">=22.0.0"
15+
"node": ">=20.0.0"
1616
},
1717
"repository": {
1818
"type": "git",

0 commit comments

Comments
 (0)