Skip to content

Commit 18c7e98

Browse files
committed
feat: allow chokidar options
1 parent fd8c54a commit 18c7e98

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

lib/Server.js

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -445,13 +445,22 @@ class Server {
445445
const compilerOptions = this.getCompilerOptions();
446446
// TODO remove `{}` after drop webpack v4 support
447447
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,
456+
};
448457
const defaultOptionsForStatic = {
449458
directory: path.join(process.cwd(), "public"),
450459
staticOptions: {},
451460
publicPath: ["/"],
452461
serveIndex: { icons: true },
453462
// Respect options from compiler watchOptions
454-
watch: watchOptions,
463+
watch: defaultWatchOptions,
455464
};
456465

457466
if (typeof options.allowedHosts === "undefined") {
@@ -954,6 +963,11 @@ class Server {
954963
// ensure that watch is an object if true
955964
if (staticOption.watch === true) {
956965
staticOption.watch = defaultOptionsForStatic.watch;
966+
} else if (typeof staticOption.watch === "object") {
967+
staticOption.watch = {
968+
...defaultOptionsForStatic.watch,
969+
...staticOption.watch,
970+
};
957971
}
958972

959973
// ensure that serveIndex is an object if true
@@ -964,7 +978,9 @@ class Server {
964978
}
965979

966980
if (typeof options.watchFiles === "string") {
967-
options.watchFiles = [{ paths: options.watchFiles, options: {} }];
981+
options.watchFiles = [
982+
{ paths: options.watchFiles, options: defaultWatchOptions },
983+
];
968984
} else if (
969985
typeof options.watchFiles === "object" &&
970986
options.watchFiles !== null &&
@@ -973,16 +989,25 @@ class Server {
973989
options.watchFiles = [
974990
{
975991
paths: options.watchFiles.paths,
976-
options: options.watchFiles.options || {},
992+
options: {
993+
...defaultWatchOptions,
994+
...(options.watchFiles.options || {}),
995+
},
977996
},
978997
];
979998
} else if (Array.isArray(options.watchFiles)) {
980999
options.watchFiles = options.watchFiles.map((item) => {
9811000
if (typeof item === "string") {
982-
return { paths: item, options: {} };
1001+
return { paths: item, options: defaultWatchOptions };
9831002
}
9841003

985-
return { paths: item.paths, options: item.options || {} };
1004+
return {
1005+
paths: item.paths,
1006+
options: {
1007+
...defaultWatchOptions,
1008+
...(item.options || {}),
1009+
},
1010+
};
9861011
});
9871012
} else {
9881013
options.watchFiles = [];
@@ -2128,7 +2153,6 @@ class Server {
21282153
// duplicate the same massaging of options that watchpack performs
21292154
// https://github.com/webpack/watchpack/blob/master/lib/DirectoryWatcher.js#L49
21302155
// this isn't an elegant solution, but we'll improve it in the future
2131-
// eslint-disable-next-line no-undefined
21322156
const usePolling =
21332157
typeof watchOptions.usePolling !== "undefined"
21342158
? watchOptions.usePolling
@@ -2143,12 +2167,7 @@ class Server {
21432167
undefined;
21442168

21452169
const finalWatchOptions = {
2146-
ignoreInitial: true,
2147-
persistent: true,
2148-
followSymlinks: false,
2149-
atomic: false,
2150-
alwaysStat: true,
2151-
ignorePermissionErrors: true,
2170+
...watchOptions,
21522171
ignored: watchOptions.ignored,
21532172
usePolling,
21542173
interval,

0 commit comments

Comments
 (0)