Skip to content

Commit 7fa443a

Browse files
committed
refactor: normalize all watch options together
1 parent b4a0cfd commit 7fa443a

File tree

3 files changed

+346
-47
lines changed

3 files changed

+346
-47
lines changed

lib/Server.js

+46-43
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
});
@@ -2150,35 +2180,8 @@ class Server {
21502180
}
21512181

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

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

0 commit comments

Comments
 (0)