fix(ProfilingPlugin): complete after the writeStream had finished flushing the data to the filesystem

Fixes a race-condition where `events.json` might not yet be available immediately after compilation.
This commit is contained in:
Bazyli Brzóska 2018-03-05 22:46:45 +01:00
parent ae2ae4e504
commit 883088e521
1 changed files with 9 additions and 5 deletions

View File

@ -73,17 +73,18 @@ class Profiler {
/** /**
* @param {string} outputPath The location where to write the log. * @param {string} outputPath The location where to write the log.
* @returns {{trace: ?, counter: number, profiler: Profiler}} The trace object * @returns {{trace: ?, counter: number, profiler: Profiler, fsStream: WriteStream}} The trace object
*/ */
function createTrace(outputPath) { function createTrace(outputPath) {
const trace = new Trace({ const trace = new Trace({
noStream: true noStream: true
}); });
const profiler = new Profiler(inspector); const profiler = new Profiler(inspector);
const fsStream = fs.createWriteStream(outputPath);
let counter = 0; let counter = 0;
trace.pipe(fs.createWriteStream(outputPath)); trace.pipe(fsStream);
// These are critical events that need to be inserted so that tools like // These are critical events that need to be inserted so that tools like
// chrome dev tools can load the profile. // chrome dev tools can load the profile.
trace.instantEvent({ trace.instantEvent({
@ -119,7 +120,8 @@ function createTrace(outputPath) {
return { return {
trace, trace,
counter, counter,
profiler profiler,
fsStream
}; };
} }
@ -169,16 +171,17 @@ class ProfilingPlugin {
); );
// We need to write out the CPU profile when we are all done. // We need to write out the CPU profile when we are all done.
compiler.hooks.done.tap( compiler.hooks.done.tapAsync(
{ {
name: pluginName, name: pluginName,
stage: Infinity stage: Infinity
}, },
() => { (stats, callback) => {
tracer.profiler.stopProfiling().then(parsedResults => { tracer.profiler.stopProfiling().then(parsedResults => {
if (parsedResults === undefined) { if (parsedResults === undefined) {
tracer.profiler.destroy(); tracer.profiler.destroy();
tracer.trace.flush(); tracer.trace.flush();
tracer.fsStream.end(callback);
return; return;
} }
@ -226,6 +229,7 @@ class ProfilingPlugin {
tracer.profiler.destroy(); tracer.profiler.destroy();
tracer.trace.flush(); tracer.trace.flush();
tracer.fsStream.end(callback);
}); });
} }
); );