Skip to content

Commit

Permalink
Use --webpack-use-polling to switch to fs polling mode on watch (#225)
Browse files Browse the repository at this point in the history
* Use --webpack-use-polling to switch to fs pooling mode on watch

* Added unit tests
  • Loading branch information
HyperBrain authored Sep 18, 2017
1 parent 80783a0 commit fe45384
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,10 @@ $ serverless invoke local --function <function-name> --path event.json --watch
Everytime the sources are changed, the function will be executed again with the
changed sources. The command will watch until the process is terminated.

If you have your sources located on a file system that does not offer events,
you can enable polling with the `--webpack-use-polling=<time in ms>` option.
If you omit the value, it defaults to 3000 ms.

All options that are supported by invoke local can be used as usual:

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

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

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

#### Custom paths

If you do not use the default path and override it in your Webpack configuration,
Expand Down
9 changes: 8 additions & 1 deletion lib/run.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const _ = require('lodash');
const BbPromise = require('bluebird');
const webpack = require('webpack');

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

const compiler = webpack(this.webpackConfig);
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)`);
}

compiler.watch({}, (err /*, stats */) => {
compiler.watch(watchOptions, (err /*, stats */) => {
if (err) {
throw err;
}
Expand Down
10 changes: 9 additions & 1 deletion lib/wpwatch.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
'use strict';

const _ = require('lodash');
const BbPromise = require('bluebird');
const webpack = require('webpack');

module.exports = {
wpwatch() {
this.serverless.cli.log('Watching 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);
compiler.watch({}, (err, stats) => {
compiler.watch(watchOptions, (err, stats) => {
if (err) {
throw err;
}
Expand Down
20 changes: 20 additions & 0 deletions tests/run.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,25 @@ describe('run', () => {
expect(chdirStub).to.have.been.calledOnce;
expect(chdirStub).to.have.been.calledWithExactly('originalPath');
});

it('should turn on polling and set the default poll interval', () => {
module.isWatching = false;
const watch = module.watch.bind(module);
webpackMock.compilerMock.watch = sandbox.stub().yields(null, {});
module.options['webpack-use-polling'] = true;

watch();
expect(webpackMock.compilerMock.watch).to.have.been.calledWith({ poll: 3000 });
});

it('should turn on polling and set the specified poll interval', () => {
module.isWatching = false;
const watch = module.watch.bind(module);
webpackMock.compilerMock.watch = sandbox.stub().yields(null, {});
const interval = module.options['webpack-use-polling'] = _.now() % 10000;

watch();
expect(webpackMock.compilerMock.watch).to.have.been.calledWith({ poll: interval });
});
});
});

0 comments on commit fe45384

Please # to comment.