Repository navigation
Expand file tree
/
Copy pathwpwatch.js
More file actions
124 lines (106 loc) · 3.96 KB
/
Copy pathwpwatch.js
File metadata and controls
124 lines (106 loc) · 3.96 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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
'use strict';
const _ = require('lodash');
const BbPromise = require('bluebird');
const webpack = require('webpack');
const tty = require('tty');
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)`);
}
let currentCompileWatch = null;
// This allows us to hold the compile until "webpack:compile:watch" has resolved
const beforeCompile = () =>
new BbPromise(resolve => {
// eslint-disable-next-line promise/catch-or-return
BbPromise.resolve(currentCompileWatch)
// Forwarding the error to the then so we don't display it twice
// (once when it was originally thrown, and once when the promise rejects)
.catch(error => error)
.then(error => {
if (error) {
return null;
}
currentCompileWatch = null;
resolve();
return null;
});
});
const compiler = webpack(this.webpackConfig);
// Determine if we can use hooks or if we should fallback to the plugin api
const hasHooks = compiler.hooks && compiler.hooks.beforeCompile;
const hasPlugins = compiler.plugin;
const canEmit = hasHooks || hasPlugins;
if (hasHooks) {
compiler.hooks.beforeCompile.tapPromise('webpack:compile:watch', beforeCompile);
} else if (hasPlugins) {
compiler.plugin('before-compile', (compilationParams, callback) => {
beforeCompile()
.then(callback) // eslint-disable-line promise/no-callback-in-promise
.catch(_.noop);
});
}
const consoleStats = this.webpackConfig.stats || {
colors: tty.isatty(process.stdout.fd),
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;
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;
const statsOutput = stats.toString(consoleStats);
if (statsOutput) {
this.serverless.cli.consoleLog(stats.toString(consoleStats));
}
}
if (firstRun) {
firstRun = false;
this.serverless.cli.log('Watching for changes...');
callback();
} else if (canEmit && currentCompileWatch === null) {
// eslint-disable-next-line promise/no-promise-in-callback
currentCompileWatch = BbPromise.resolve(this.serverless.pluginManager.spawn('webpack:compile:watch')).then(
() => this.serverless.cli.log('Watching for changes...')
);
}
});
};
return BbPromise.fromCallback(cb => {
startWatch(cb);
});
}
};