Skip to content

Commit

Permalink
refactor: normalize all watch options together
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Nov 15, 2021
1 parent b4a0cfd commit 7fa443a
Show file tree
Hide file tree
Showing 3 changed files with 346 additions and 47 deletions.
89 changes: 46 additions & 43 deletions lib/Server.js
Expand Up @@ -444,16 +444,49 @@ class Server {

const compilerOptions = this.getCompilerOptions();
// TODO remove `{}` after drop webpack v4 support
const watchOptions = compilerOptions.watchOptions || {};
const defaultWatchOptions = {
ignoreInitial: true,
persistent: true,
followSymlinks: false,
atomic: false,
alwaysStat: true,
ignorePermissionErrors: true,
...watchOptions,
const compilerWatchOptions = compilerOptions.watchOptions || {};

const getDefaultWatchOptions = (watchOptions = {}) => {
// duplicate the same massaging of options that watchpack performs
// https://github.com/webpack/watchpack/blob/master/lib/DirectoryWatcher.js#L49
// this isn't an elegant solution, but we'll improve it in the future
const usePolling =
typeof watchOptions.usePolling !== "undefined"
? watchOptions.usePolling
: Boolean(watchOptions.poll);

const interval =
// eslint-disable-next-line no-nested-ternary
typeof watchOptions.interval !== "undefined"
? watchOptions.interval
: typeof watchOptions.poll === "number"
? watchOptions.poll
: // eslint-disable-next-line no-undefined
undefined;

const finalWatchOptions = {
ignoreInitial: true,
persistent: true,
followSymlinks: false,
atomic: false,
alwaysStat: true,
ignorePermissionErrors: true,
...compilerWatchOptions,
...watchOptions,
ignored: watchOptions.ignored,
usePolling,
interval,
};

if (finalWatchOptions.poll) {
delete finalWatchOptions.poll;
}

return finalWatchOptions;
};

const defaultWatchOptions = getDefaultWatchOptions();

const defaultOptionsForStatic = {
directory: path.join(process.cwd(), "public"),
staticOptions: {},
Expand Down Expand Up @@ -965,8 +998,7 @@ class Server {
staticOption.watch = defaultOptionsForStatic.watch;
} else if (typeof staticOption.watch === "object") {
staticOption.watch = {
...defaultOptionsForStatic.watch,
...staticOption.watch,
...getDefaultWatchOptions(staticOption.watch),
};
}

Expand All @@ -990,8 +1022,7 @@ class Server {
{
paths: options.watchFiles.paths,
options: {
...defaultWatchOptions,
...(options.watchFiles.options || {}),
...getDefaultWatchOptions(options.watchFiles.options || {}),
},
},
];
Expand All @@ -1004,8 +1035,7 @@ class Server {
return {
paths: item.paths,
options: {
...defaultWatchOptions,
...(item.options || {}),
...getDefaultWatchOptions(item.options || {}),
},
};
});
Expand Down Expand Up @@ -2150,35 +2180,8 @@ class Server {
}

watchFiles(watchPath, watchOptions) {
// duplicate the same massaging of options that watchpack performs
// https://github.com/webpack/watchpack/blob/master/lib/DirectoryWatcher.js#L49
// this isn't an elegant solution, but we'll improve it in the future
const usePolling =
typeof watchOptions.usePolling !== "undefined"
? watchOptions.usePolling
: Boolean(watchOptions.poll);
const interval =
// eslint-disable-next-line no-nested-ternary
typeof watchOptions.interval !== "undefined"
? watchOptions.interval
: typeof watchOptions.poll === "number"
? watchOptions.poll
: // eslint-disable-next-line no-undefined
undefined;

const finalWatchOptions = {
...watchOptions,
ignored: watchOptions.ignored,
usePolling,
interval,
};

if (finalWatchOptions.poll) {
delete finalWatchOptions.poll;
}

const chokidar = require("chokidar");
const watcher = chokidar.watch(watchPath, finalWatchOptions);
const watcher = chokidar.watch(watchPath, watchOptions);

// disabling refreshing on changing the content
if (this.options.liveReload) {
Expand Down

0 comments on commit 7fa443a

Please sign in to comment.