Skip to content

Commit 9e36127

Browse files
authored
chore: update chokidar to v4 (#5657)
* chore: update chokidar to v4 * chore: only prevent interval be undefined * feat: support for glob support * feat: enhance watchFiles option to support ignored patterns with glob strings * feat: add tests for watchFiles option with ignored glob array support * fix: windows support
1 parent d163488 commit 9e36127

8 files changed

Lines changed: 877 additions & 186 deletions

File tree

lib/Server.js

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const schema = require("./options.json");
1717
/** @typedef {import("webpack").Stats} Stats */
1818
/** @typedef {import("webpack").MultiStats} MultiStats */
1919
/** @typedef {import("os").NetworkInterfaceInfo} NetworkInterfaceInfo */
20-
/** @typedef {import("chokidar").WatchOptions} WatchOptions */
20+
/** @typedef {import("chokidar").ChokidarOptions} WatchOptions */
2121
/** @typedef {import("chokidar").FSWatcher} FSWatcher */
2222
/** @typedef {import("connect-history-api-fallback").Options} ConnectHistoryApiFallbackOptions */
2323
/** @typedef {import("bonjour-service").Bonjour} Bonjour */
@@ -833,7 +833,7 @@ class Server {
833833

834834
const usePolling = getPolling();
835835
const interval = getInterval();
836-
const { poll, ...rest } = watchOptions;
836+
const { poll: _poll, interval: _interval, ...rest } = watchOptions;
837837

838838
return {
839839
ignoreInitial: true,
@@ -844,9 +844,7 @@ class Server {
844844
ignorePermissionErrors: true,
845845
// Respect options from compiler watchOptions
846846
usePolling,
847-
interval,
848-
ignored: watchOptions.ignored,
849-
// TODO: we respect these options for all watch options and allow developers to pass them to chokidar, but chokidar doesn't have these options maybe we need revisit that in future
847+
...(interval !== undefined ? { interval } : {}),
850848
...rest,
851849
};
852850
};
@@ -3188,10 +3186,42 @@ class Server {
31883186
* @param {string | string[]} watchPath watch path
31893187
* @param {WatchOptions=} watchOptions watch options
31903188
*/
3191-
watchFiles(watchPath, watchOptions) {
3189+
watchFiles(watchPath, watchOptions = {}) {
31923190
const chokidar = require("chokidar");
3191+
const path = require("node:path");
3192+
const { globSync, isDynamicPattern } = require("tinyglobby");
3193+
3194+
const isWin = path.sep === "\\";
3195+
const toPosix = (/** @type {string} */ filePath) =>
3196+
isWin ? filePath.split(path.sep).join("/") : filePath;
3197+
const toNative = (/** @type {string} */ filePath) =>
3198+
isWin ? filePath.split("/").join(path.sep) : filePath;
3199+
3200+
const cwd = watchOptions.cwd ? toPosix(watchOptions.cwd) : undefined;
3201+
3202+
const expand = (/** @type {string} */ item) => {
3203+
const posix = toPosix(item);
3204+
return isDynamicPattern(posix)
3205+
? globSync(posix, { cwd, absolute: true }).map(toNative)
3206+
: item;
3207+
};
3208+
3209+
const resolveGlobs = (/** @type {string | string[]} */ input) =>
3210+
(Array.isArray(input) ? input : [input]).flatMap((item) =>
3211+
typeof item === "string" ? expand(item) : item,
3212+
);
3213+
3214+
const resolvedPaths = resolveGlobs(watchPath);
3215+
3216+
if (typeof watchOptions.ignored === "string") {
3217+
watchOptions.ignored = resolveGlobs(watchOptions.ignored);
3218+
} else if (Array.isArray(watchOptions.ignored)) {
3219+
watchOptions.ignored = watchOptions.ignored.flatMap((item) =>
3220+
typeof item === "string" ? expand(item) : item,
3221+
);
3222+
}
31933223

3194-
const watcher = chokidar.watch(watchPath, watchOptions);
3224+
const watcher = chokidar.watch(resolvedPaths, watchOptions);
31953225

31963226
// disabling refreshing on changing the content
31973227
if (this.options.liveReload) {

package-lock.json

Lines changed: 103 additions & 53 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"@types/ws": "^8.18.1",
5454
"ansi-html-community": "^0.0.8",
5555
"bonjour-service": "^1.3.0",
56-
"chokidar": "^3.6.0",
56+
"chokidar": "^4.0.3",
5757
"compression": "^1.8.1",
5858
"connect-history-api-fallback": "^2.0.0",
5959
"express": "^5.2.1",
@@ -66,6 +66,7 @@
6666
"schema-utils": "^4.3.3",
6767
"selfsigned": "^5.5.0",
6868
"serve-index": "^1.9.2",
69+
"tinyglobby": "^0.2.15",
6970
"webpack-dev-middleware": "^8.0.2",
7071
"ws": "^8.20.0"
7172
},
@@ -85,6 +86,7 @@
8586
"@types/graceful-fs": "^4.1.9",
8687
"@types/node": "^24.0.14",
8788
"@types/node-forge": "^1.3.1",
89+
"@types/picomatch": "^4.0.2",
8890
"@types/trusted-types": "^2.0.7",
8991
"acorn": "^8.14.0",
9092
"babel-jest": "^30.0.4",

0 commit comments

Comments
 (0)