Skip to content

Commit fe45384

Browse files
authored
Use --webpack-use-polling to switch to fs polling mode on watch (#225)
* Use --webpack-use-polling to switch to fs pooling mode on watch * Added unit tests
1 parent 80783a0 commit fe45384

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,10 @@ $ serverless invoke local --function <function-name> --path event.json --watch
270270
Everytime the sources are changed, the function will be executed again with the
271271
changed sources. The command will watch until the process is terminated.
272272

273+
If you have your sources located on a file system that does not offer events,
274+
you can enable polling with the `--webpack-use-polling=<time in ms>` option.
275+
If you omit the value, it defaults to 3000 ms.
276+
273277
All options that are supported by invoke local can be used as usual:
274278

275279
- `--function` or `-f` (required) is the name of the function to run
@@ -300,6 +304,11 @@ In comparison to `serverless offline`, the `start` command will fire an `init` a
300304

301305
You can find an example setup in the [`examples`][link-examples] folder.
302306

307+
If you have your sources located on a file system that does not offer events,
308+
e.g. a mounted volume in a Docker container, you can enable polling with the
309+
`--webpack-use-polling=<time in ms>` option. If you omit the value, it defaults
310+
to 3000 ms.
311+
303312
#### Custom paths
304313

305314
If you do not use the default path and override it in your Webpack configuration,

lib/run.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
const _ = require('lodash');
34
const BbPromise = require('bluebird');
45
const webpack = require('webpack');
56

@@ -9,8 +10,14 @@ module.exports = {
910
this.serverless.cli.log(`Watch function ${functionName}...`);
1011

1112
const compiler = webpack(this.webpackConfig);
13+
const watchOptions = {};
14+
const usePolling = this.options['webpack-use-polling'];
15+
if (usePolling) {
16+
watchOptions.poll = _.isInteger(usePolling) ? usePolling : 3000;
17+
this.serverless.cli.log(`Enabled polling (${watchOptions.poll} ms)`);
18+
}
1219

13-
compiler.watch({}, (err /*, stats */) => {
20+
compiler.watch(watchOptions, (err /*, stats */) => {
1421
if (err) {
1522
throw err;
1623
}

lib/wpwatch.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
'use strict';
22

3+
const _ = require('lodash');
34
const BbPromise = require('bluebird');
45
const webpack = require('webpack');
56

67
module.exports = {
78
wpwatch() {
89
this.serverless.cli.log('Watching with Webpack...');
910

11+
const watchOptions = {};
12+
const usePolling = this.options['webpack-use-polling'];
13+
if (usePolling) {
14+
watchOptions.poll = _.isInteger(usePolling) ? usePolling : 3000;
15+
this.serverless.cli.log(`Enabled polling (${watchOptions.poll} ms)`);
16+
}
17+
1018
const compiler = webpack(this.webpackConfig);
11-
compiler.watch({}, (err, stats) => {
19+
compiler.watch(watchOptions, (err, stats) => {
1220
if (err) {
1321
throw err;
1422
}

tests/run.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,25 @@ describe('run', () => {
108108
expect(chdirStub).to.have.been.calledOnce;
109109
expect(chdirStub).to.have.been.calledWithExactly('originalPath');
110110
});
111+
112+
it('should turn on polling and set the default poll interval', () => {
113+
module.isWatching = false;
114+
const watch = module.watch.bind(module);
115+
webpackMock.compilerMock.watch = sandbox.stub().yields(null, {});
116+
module.options['webpack-use-polling'] = true;
117+
118+
watch();
119+
expect(webpackMock.compilerMock.watch).to.have.been.calledWith({ poll: 3000 });
120+
});
121+
122+
it('should turn on polling and set the specified poll interval', () => {
123+
module.isWatching = false;
124+
const watch = module.watch.bind(module);
125+
webpackMock.compilerMock.watch = sandbox.stub().yields(null, {});
126+
const interval = module.options['webpack-use-polling'] = _.now() % 10000;
127+
128+
watch();
129+
expect(webpackMock.compilerMock.watch).to.have.been.calledWith({ poll: interval });
130+
});
111131
});
112132
});

0 commit comments

Comments
 (0)