Skip to content

Commit 7aed240

Browse files
committed
Refactors output handling
1 parent 4a38afb commit 7aed240

1 file changed

Lines changed: 18 additions & 26 deletions

File tree

lib/index.js

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,14 @@ class BundleTrackerPlugin {
4949
/** @type {Options} */
5050
this.options = options;
5151
/** @type {Contents} */
52-
this.contents = {
52+
this.output = {
5353
status: 'initialization',
5454
assets: {},
5555
chunks: {},
5656
};
5757
this.name = 'BundleTrackerPlugin';
5858

59-
this.output = {
60-
status: 'compile',
61-
assets: {},
62-
chunks: {},
63-
};
59+
this.assets = {};
6460
this.outputChunkDir = '';
6561
this.outputTrackerFile = '';
6662
this.outputTrackerDir = '';
@@ -109,19 +105,18 @@ class BundleTrackerPlugin {
109105
/**
110106
* Write bundle tracker stats file
111107
*/
112-
_writeOutput() {
113-
const contents = this.output;
114-
Object.assign(this.contents, contents, {
115-
assets: mergeObjectsAndSortKeys(this.contents.assets, contents.assets),
116-
chunks: mergeObjectsAndSortKeys(this.contents.chunks, contents.chunks),
108+
_writeOutput(contents) {
109+
Object.assign(this.output, contents, {
110+
assets: mergeObjectsAndSortKeys(this.output.assets, contents.assets),
111+
chunks: mergeObjectsAndSortKeys(this.output.chunks, contents.chunks),
117112
});
118113

119114
if (this.options.publicPath) {
120-
this.contents.publicPath = this.options.publicPath;
115+
this.output.publicPath = this.options.publicPath;
121116
}
122117

123118
fs.mkdirSync(this.outputTrackerDir, { recursive: true, mode: 0o755 });
124-
fs.writeFileSync(this.outputTrackerFile, JSON.stringify(this.contents, null, this.options.indent));
119+
fs.writeFileSync(this.outputTrackerFile, JSON.stringify(this.output, null, this.options.indent));
125120
}
126121
/**
127122
* Compute hash for a content
@@ -145,8 +140,7 @@ class BundleTrackerPlugin {
145140
* Handle compile hook
146141
*/
147142
_handleCompile() {
148-
this.output = { status: 'compile', assets: {}, chunks: {} };
149-
this._writeOutput();
143+
this._writeOutput({ status: 'compile' });
150144
}
151145

152146
/**
@@ -188,7 +182,7 @@ class BundleTrackerPlugin {
188182
fileInfo.sourceFilename = this._compilation.assetsInfo.get(assetName).sourceFilename;
189183
}
190184

191-
this.output.assets[assetName] = fileInfo;
185+
this.assets[assetName] = fileInfo;
192186
}
193187

194188
/**
@@ -204,28 +198,26 @@ class BundleTrackerPlugin {
204198
return compilation.children.find(child => findError(child));
205199
};
206200
const error = findError(stats.compilation);
207-
// @ts-ignore: TS2739
208-
this.output = {
201+
this._writeOutput({
209202
status: 'error',
210203
error: error?.name ?? 'unknown-error',
211204
message: stripAnsi(error['message']),
212-
};
213-
this._writeOutput();
214-
205+
});
215206
return;
216207
}
217208

209+
const chunks = {};
218210
stats.compilation.chunkGroups.forEach(chunkGroup => {
219211
if (!chunkGroup.isInitial()) return;
220-
this.output.chunks[chunkGroup.name] = chunkGroup.getFiles();
212+
chunks[chunkGroup.name] = chunkGroup.getFiles();
221213
});
222214

215+
const output = { status: 'done', chunks, assets: this.assets };
223216
if (this.options.logTime === true) {
224-
this.output.startTime = stats.startTime;
225-
this.output.endTime = stats.endTime;
217+
output.startTime = stats.startTime;
218+
output.endTime = stats.endTime;
226219
}
227-
this.output.status = 'done';
228-
this._writeOutput();
220+
this._writeOutput(output);
229221
}
230222

231223
/**

0 commit comments

Comments
 (0)