Skip to content

Commit e1b50e2

Browse files
committed
refactor: normalize all watch options together
1 parent 36daa58 commit e1b50e2

File tree

3 files changed

+346
-47
lines changed

3 files changed

+346
-47
lines changed

lib/Server.js

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -444,16 +444,49 @@ class Server {
444444

445445
const compilerOptions = this.getCompilerOptions();
446446
// TODO remove `{}` after drop webpack v4 support
447-
const watchOptions = compilerOptions.watchOptions || {};
448-
const defaultWatchOptions = {
449-
ignoreInitial: true,
450-
persistent: true,
451-
followSymlinks: false,
452-
atomic: false,
453-
alwaysStat: true,
454-
ignorePermissionErrors: true,
455-
...watchOptions,
447+
const compilerWatchOptions = compilerOptions.watchOptions || {};
448+
449+
const getDefaultWatchOptions = (watchOptions = {}) => {
450+
// duplicate the same massaging of options that watchpack performs
451+
// https://github.com/webpack/watchpack/blob/master/lib/DirectoryWatcher.js#L49
452+
// this isn't an elegant solution, but we'll improve it in the future
453+
const usePolling =
454+
typeof watchOptions.usePolling !== "undefined"
455+
? watchOptions.usePolling
456+
: Boolean(watchOptions.poll);
457+
458+
const interval =
459+
// eslint-disable-next-line no-nested-ternary
460+
typeof watchOptions.interval !== "undefined"
461+
? watchOptions.interval
462+
: typeof watchOptions.poll === "number"
463+
? watchOptions.poll
464+
: // eslint-disable-next-line no-undefined
465+
undefined;
466+
467+
const finalWatchOptions = {
468+
ignoreInitial: true,
469+
persistent: true,
470+
followSymlinks: false,
471+
atomic: false,
472+
alwaysStat: true,
473+
ignorePermissionErrors: true,
474+
...compilerWatchOptions,
475+
...watchOptions,
476+
ignored: watchOptions.ignored,
477+
usePolling,
478+
interval,
479+
};
480+
481+
if (finalWatchOptions.poll) {
482+
delete finalWatchOptions.poll;
483+
}
484+
485+
return finalWatchOptions;
456486
};
487+
488+
const defaultWatchOptions = getDefaultWatchOptions();
489+
457490
const defaultOptionsForStatic = {
458491
directory: path.join(process.cwd(), "public"),
459492
staticOptions: {},
@@ -965,8 +998,7 @@ class Server {
965998
staticOption.watch = defaultOptionsForStatic.watch;
966999
} else if (typeof staticOption.watch === "object") {
9671000
staticOption.watch = {
968-
...defaultOptionsForStatic.watch,
969-
...staticOption.watch,
1001+
...getDefaultWatchOptions(staticOption.watch),
9701002
};
9711003
}
9721004

@@ -990,8 +1022,7 @@ class Server {
9901022
{
9911023
paths: options.watchFiles.paths,
9921024
options: {
993-
...defaultWatchOptions,
994-
...(options.watchFiles.options || {}),
1025+
...getDefaultWatchOptions(options.watchFiles.options || {}),
9951026
},
9961027
},
9971028
];
@@ -1004,8 +1035,7 @@ class Server {
10041035
return {
10051036
paths: item.paths,
10061037
options: {
1007-
...defaultWatchOptions,
1008-
...(item.options || {}),
1038+
...getDefaultWatchOptions(item.options || {}),
10091039
},
10101040
};
10111041
});
@@ -2149,35 +2179,8 @@ class Server {
21492179
}
21502180

21512181
watchFiles(watchPath, watchOptions) {
2152-
// duplicate the same massaging of options that watchpack performs
2153-
// https://github.com/webpack/watchpack/blob/master/lib/DirectoryWatcher.js#L49
2154-
// this isn't an elegant solution, but we'll improve it in the future
2155-
const usePolling =
2156-
typeof watchOptions.usePolling !== "undefined"
2157-
? watchOptions.usePolling
2158-
: Boolean(watchOptions.poll);
2159-
const interval =
2160-
// eslint-disable-next-line no-nested-ternary
2161-
typeof watchOptions.interval !== "undefined"
2162-
? watchOptions.interval
2163-
: typeof watchOptions.poll === "number"
2164-
? watchOptions.poll
2165-
: // eslint-disable-next-line no-undefined
2166-
undefined;
2167-
2168-
const finalWatchOptions = {
2169-
...watchOptions,
2170-
ignored: watchOptions.ignored,
2171-
usePolling,
2172-
interval,
2173-
};
2174-
2175-
if (finalWatchOptions.poll) {
2176-
delete finalWatchOptions.poll;
2177-
}
2178-
21792182
const chokidar = require("chokidar");
2180-
const watcher = chokidar.watch(watchPath, finalWatchOptions);
2183+
const watcher = chokidar.watch(watchPath, watchOptions);
21812184

21822185
// disabling refreshing on changing the content
21832186
if (this.options.liveReload) {

0 commit comments

Comments
 (0)