Skip to content

Commit f4f0759

Browse files
author
Frank Schmid
committed
Fixed race condition when triggering watch event
1 parent 7474d3c commit f4f0759

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

lib/wpwatch.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ module.exports = {
3131
};
3232

3333
// This starts the watch and waits for the immediate compile that follows to end or fail.
34+
let lastHash = null;
3435
const startWatch = (callback) => {
3536
let firstRun = true;
36-
compiler.watch(watchOptions, (err, stats) => {
37+
const watcher = compiler.watch(watchOptions, (err, stats) => {
3738
if (err) {
3839
if (firstRun) {
3940
firstRun = false;
@@ -42,17 +43,39 @@ module.exports = {
4243
throw err;
4344
}
4445

46+
process.env.SLS_DEBUG && this.serverless.cli.log(`Webpack watch invoke: HASH NEW=${stats.hash} CUR=${lastHash}`);
47+
48+
// If the file hash did not change there were no effective code changes detected (comment only changed do not change the compile hash!)
49+
// See here: https://webpack.js.org/api/node/#watching (note below watching)
50+
if (stats && stats.hash === lastHash) {
51+
if (firstRun) {
52+
firstRun = false;
53+
callback();
54+
}
55+
return;
56+
}
57+
4558
if (stats) {
59+
lastHash = stats.hash;
4660
this.serverless.cli.consoleLog(stats.toString(consoleStats));
4761
}
4862

49-
this.serverless.cli.log('Watching for changes...');
50-
5163
if (firstRun) {
5264
firstRun = false;
65+
this.serverless.cli.log('Watching for changes...');
5366
callback();
54-
}else{
55-
this.serverless.pluginManager.spawn('webpack:compile:watch');
67+
} else {
68+
// We will close the watcher while the compile event is triggered and resume afterwards to prevent race conditions.
69+
watcher.close(() => {
70+
return this.serverless.pluginManager.spawn('webpack:compile:watch')
71+
.then(() => {
72+
// Resume watching after we triggered the compile:watch event
73+
return BbPromise.fromCallback(cb => {
74+
startWatch(cb);
75+
})
76+
.then(() => this.serverless.cli.log('Watching for changes...'));
77+
});
78+
});
5679
}
5780
});
5881
};

package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)