Repository navigation
Expand file tree
/
Copy pathwpwatch.js
More file actions
62 lines (52 loc) · 1.61 KB
/
Copy pathwpwatch.js
File metadata and controls
62 lines (52 loc) · 1.61 KB
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
'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.
const startWatch = (callback) => {
let firstRun = true;
compiler.watch(watchOptions, (err, stats) => {
if (err) {
if (firstRun) {
firstRun = false;
return callback(err);
}
throw err;
}
if (stats) {
this.serverless.cli.consoleLog(stats.toString(consoleStats));
}
this.serverless.cli.log('Watching for changes...');
if (firstRun) {
firstRun = false;
callback();
}
});
};
return BbPromise.fromCallback(cb => {
startWatch(cb);
});
},
};