Skip to content

Commit a83228b

Browse files
committed
fix: polling usage in watchFiles
1 parent b446782 commit a83228b

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

lib/Server.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -973,9 +973,12 @@ class Server {
973973
// https://github.com/webpack/watchpack/blob/master/lib/DirectoryWatcher.js#L49
974974
// this isn't an elegant solution, but we'll improve it in the future
975975
// eslint-disable-next-line no-undefined
976-
const usePolling = !!watchOptions.poll || watchOptions.usePolling;
976+
const usePolling = watchOptions.usePolling || Boolean(watchOptions.poll);
977977
const interval =
978-
typeof watchOptions.poll === 'number'
978+
// eslint-disable-next-line no-nested-ternary
979+
typeof watchOptions.interval !== 'undefined'
980+
? watchOptions.interval
981+
: typeof watchOptions.poll === 'number'
979982
? watchOptions.poll
980983
: // eslint-disable-next-line no-undefined
981984
undefined;

test/server/watchFiles-option.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const path = require('path');
44
const fs = require('graceful-fs');
5+
const chokidar = require('chokidar');
56
const testServer = require('../helpers/test-server');
67
const config = require('../fixtures/contentbase-config/webpack.config');
78
const port = require('../ports-map')['watchFiles-option'];
@@ -277,6 +278,8 @@ describe("'watchFiles' option", () => {
277278
describe('should work with options', () => {
278279
const file = path.join(watchDir, 'assets/example.txt');
279280

281+
const chokidarMock = jest.spyOn(chokidar, 'watch');
282+
280283
beforeAll((done) => {
281284
server = testServer.start(
282285
config,
@@ -285,6 +288,7 @@ describe("'watchFiles' option", () => {
285288
paths: file,
286289
options: {
287290
usePolling: true,
291+
interval: 400,
288292
},
289293
},
290294
port,
@@ -298,6 +302,21 @@ describe("'watchFiles' option", () => {
298302
fs.truncateSync(file);
299303
});
300304

305+
it('should pass correct options to chokidar config', () => {
306+
expect(chokidarMock).toHaveBeenCalledWith(file, {
307+
ignoreInitial: true,
308+
persistent: true,
309+
followSymlinks: false,
310+
atomic: false,
311+
alwaysStat: true,
312+
ignorePermissionErrors: true,
313+
// eslint-disable-next-line no-undefined
314+
ignored: undefined,
315+
usePolling: true,
316+
interval: 400,
317+
});
318+
});
319+
301320
it('should reload on file content changed', (done) => {
302321
server.staticWatchers[0].on('change', (changedPath) => {
303322
expect(changedPath).toBe(file);

0 commit comments

Comments
 (0)