Skip to content

Commit 5629ac3

Browse files
committed
chore(deps): update chokidar to v4
In order to replace chokidars removed glob functionality, we have to use some trickery. - `glob-parent` is used to get the common ancenstor dir that needs to be watched. - `picomatch` is used as ignore-filter to make sure that only files/dirs are watched that match the given glob - `is-glob` is used to only use glob logic when needed In total this adds 4 direct or transitive dependencies. However, the update to chokidar v4 removes 12. So its a net positive I also added a test to ensure that creation of nested directories is detected properly.
1 parent b1e549f commit 5629ac3

File tree

8 files changed

+332
-62
lines changed

8 files changed

+332
-62
lines changed

Diff for: .eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ node_modules
66
examples/**/main.js
77
examples/client/trusted-types-overlay/app.js
88
test/fixtures/reload-config/foo.js
9+
test/fixtures/worker-config-dev-server-false/public/worker-bundle.js

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ yarn-error.log
1818

1919
test/fixtures/static-config/public/assets/non-exist.txt
2020
test/fixtures/watch-files-config/public/assets/non-exist.txt
21+
test/fixtures/watch-files-config/public/non-existant/non-exist.txt
2122
test/fixtures/reload-config/main.css
2223
test/fixtures/reload-config-2/main.css
2324
test/fixtures/worker-config-dev-server-false/public

Diff for: lib/Server.js

+30-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const schema = require("./options.json");
1818
/** @typedef {import("webpack").Stats} Stats */
1919
/** @typedef {import("webpack").MultiStats} MultiStats */
2020
/** @typedef {import("os").NetworkInterfaceInfo} NetworkInterfaceInfo */
21-
/** @typedef {import("chokidar").WatchOptions} WatchOptions */
21+
/** @typedef {import("chokidar").ChokidarOptions} WatchOptions */
2222
/** @typedef {import("chokidar").FSWatcher} FSWatcher */
2323
/** @typedef {import("connect-history-api-fallback").Options} ConnectHistoryApiFallbackOptions */
2424
/** @typedef {import("bonjour-service").Bonjour} Bonjour */
@@ -3254,9 +3254,36 @@ class Server {
32543254
* @param {string | string[]} watchPath
32553255
* @param {WatchOptions} [watchOptions]
32563256
*/
3257-
watchFiles(watchPath, watchOptions) {
3257+
watchFiles(watchPath, watchOptions = {}) {
32583258
const chokidar = require("chokidar");
3259-
const watcher = chokidar.watch(watchPath, watchOptions);
3259+
const isGlob = require("is-glob");
3260+
3261+
const watchPathArr = Array.isArray(watchPath) ? watchPath : [watchPath];
3262+
const watchPathGlobs = watchPathArr.filter((p) => isGlob(p));
3263+
3264+
// No need to do all this work when no globs are used
3265+
if (watchPathGlobs.length > 0) {
3266+
const globParent = require("glob-parent");
3267+
const picomatch = require("picomatch");
3268+
3269+
watchPathGlobs.forEach((p) => {
3270+
watchPathArr[watchPathArr.indexOf(p)] = globParent(p);
3271+
});
3272+
3273+
const matcher = picomatch(watchPathGlobs);
3274+
const ignoreFunc = (/** @type {string} */ p) =>
3275+
!watchPathArr.includes(p) && !matcher(p);
3276+
3277+
if (Array.isArray(watchOptions.ignored)) {
3278+
watchOptions.ignored.push(ignoreFunc);
3279+
} else {
3280+
watchOptions.ignored = watchOptions.ignored
3281+
? [watchOptions.ignored, ignoreFunc]
3282+
: ignoreFunc;
3283+
}
3284+
}
3285+
3286+
const watcher = chokidar.watch(watchPathArr, watchOptions);
32603287

32613288
// disabling refreshing on changing the content
32623289
if (this.options.liveReload) {

0 commit comments

Comments
 (0)