-
Notifications
You must be signed in to change notification settings - Fork 417
/
Copy pathwpwatch.js
88 lines (77 loc) · 2.79 KB
/
wpwatch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
'use strict';
const _ = require('lodash');
const BbPromise = require('bluebird');
const webpack = require('webpack');
module.exports = {
wpwatch() {
if (this.options['webpack-no-watch']) {
// If we do not watch we will just run an ordinary compile
this.serverless.cli.log('Watch disabled by option.');
return this.serverless.pluginManager.spawn('webpack:compile');
}
this.serverless.cli.log('Bundling with Webpack...');
const watchOptions = {};
const usePolling = this.options['webpack-use-polling'];
if (usePolling) {
watchOptions.poll = _.isInteger(usePolling) ? usePolling : 3000;
this.serverless.cli.log(`Enabled polling (${watchOptions.poll} ms)`);
}
const compiler = webpack(this.webpackConfig);
const consoleStats = this.webpackConfig.stats || {
colors: true,
hash: false,
version: false,
chunks: false,
children: false
};
// This starts the watch and waits for the immediate compile that follows to end or fail.
let lastHash = null;
const startWatch = (callback) => {
let firstRun = true;
const watcher = compiler.watch(watchOptions, (err, stats) => {
if (err) {
if (firstRun) {
firstRun = false;
return callback(err);
}
throw err;
}
process.env.SLS_DEBUG && this.serverless.cli.log(`Webpack watch invoke: HASH NEW=${stats.hash} CUR=${lastHash}`);
// If the file hash did not change there were no effective code changes detected
// (comment changes do not change the compile hash and do not account for a rebuild!)
// See here: https://webpack.js.org/api/node/#watching (note below watching)
if (stats && stats.hash === lastHash) {
if (firstRun) {
firstRun = false;
callback();
}
return;
}
if (stats) {
lastHash = stats.hash;
this.serverless.cli.consoleLog(stats.toString(consoleStats));
}
if (firstRun) {
firstRun = false;
this.serverless.cli.log('Watching for changes...');
callback();
} else {
// We will close the watcher while the compile event is triggered and resume afterwards to prevent race conditions.
watcher.close(() => {
return this.serverless.pluginManager.spawn('webpack:compile:watch')
.then(() => {
// Resume watching after we triggered the compile:watch event
return BbPromise.fromCallback(cb => {
startWatch(cb);
})
.then(() => this.serverless.cli.log('Watching for changes...'));
});
});
}
});
};
return BbPromise.fromCallback(cb => {
startWatch(cb);
});
},
};