Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(deps): update chokidar to v4 #5374

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ node_modules
examples/**/main.js
examples/client/trusted-types-overlay/app.js
test/fixtures/reload-config/foo.js
test/fixtures/worker-config-dev-server-false/public/worker-bundle.js
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ yarn-error.log

test/fixtures/static-config/public/assets/non-exist.txt
test/fixtures/watch-files-config/public/assets/non-exist.txt
test/fixtures/watch-files-config/public/non-existant/non-exist.txt
test/fixtures/reload-config/main.css
test/fixtures/reload-config-2/main.css
test/fixtures/worker-config-dev-server-false/public
Expand Down
62 changes: 59 additions & 3 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const schema = require("./options.json");
/** @typedef {import("webpack").Stats} Stats */
/** @typedef {import("webpack").MultiStats} MultiStats */
/** @typedef {import("os").NetworkInterfaceInfo} NetworkInterfaceInfo */
/** @typedef {import("chokidar").WatchOptions} WatchOptions */
/** @typedef {import("chokidar").ChokidarOptions & { disableGlobbing?: boolean }} WatchOptions */
/** @typedef {import("chokidar").FSWatcher} FSWatcher */
/** @typedef {import("connect-history-api-fallback").Options} ConnectHistoryApiFallbackOptions */
/** @typedef {import("bonjour-service").Bonjour} Bonjour */
Expand Down Expand Up @@ -3255,9 +3255,65 @@ class Server {
* @param {string | string[]} watchPath
* @param {WatchOptions} [watchOptions]
*/
watchFiles(watchPath, watchOptions) {
watchFiles(watchPath, watchOptions = {}) {
const chokidar = require("chokidar");
const watcher = chokidar.watch(watchPath, watchOptions);

const watchPathArr = Array.isArray(watchPath) ? watchPath : [watchPath];

if (watchOptions.disableGlobbing !== true) {
const isGlob = require("is-glob");
const watchPathGlobs = watchPathArr.filter((p) => isGlob(p));

// No need to do all this work when no globs are used
if (watchPathGlobs.length > 0) {
const globParent = require("glob-parent");
const picomatch = require("picomatch");

watchPathGlobs.forEach((p) => {
watchPathArr[watchPathArr.indexOf(p)] = globParent(p);
});

const matcher = picomatch(watchPathGlobs, {
cwd: watchOptions.cwd,
dot: true,
alexander-akait marked this conversation as resolved.
Show resolved Hide resolved
});

const ignoreFunc = (/** @type {string} */ p) =>
!watchPathArr.includes(p) && !matcher(p);

const ignoredArr = Array.isArray(watchOptions.ignored)
? watchOptions.ignored
: [];

// Nested ternaries are forbidden by eslint so we end up with this
if (watchOptions.ignored && !Array.isArray(watchOptions.ignored)) {
ignoredArr.push(watchOptions.ignored);
}

const ignoredGlobs = [];
for (let i = 0; i < ignoredArr.length; i++) {
const ignored = ignoredArr[i];
if (typeof ignored === "string" && isGlob(ignored)) {
ignoredGlobs.push(ignored);
ignoredArr.splice(i, 1);
}
Fuzzyma marked this conversation as resolved.
Show resolved Hide resolved
}

if (ignoredGlobs.length > 0) {
const ignoreMatcher = picomatch(ignoredGlobs, {
dot: true,
cwd: watchOptions.cwd,
});
ignoredArr.push(ignoreMatcher);
}

ignoredArr.push(ignoreFunc);

watchOptions.ignored = ignoredArr;
Copy link
Member

@alexander-akait alexander-akait Dec 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Fuzzyma Can we simplify this and rewrite it into a more understandable logic, here one array is derived from the second and the second from the third, it is very difficult to read, I mean:

watchOptions.ignored = []
  .concat(watchPathArr.map(item => isGlob(item) ? generateIgnoreMatcher(item) : false))
  .concat(watchOptions.ignored.map(item => {  isGlob(item) ? generateIgnoreMatcher(item) :  item }))
  .filter(Boolean)

This is an approximate pseudocode, but it is easy to understand where everything comes from and how it is formed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to finish this soon and make a release

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do!

As long as chokidar is not handling the issue linked in the first post, this is blocked anyway. As alternative we have to filter out undefined options ourselves before passing them to chokidar. Wdyt?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexander-akait I tried it your way. But there is one detail that might end up being performance sensitive.
I dont want to generate multiple matchers (one for each glob) if one is sufficient for all globs. Therefore I need to filter out the glob strings twice.

I tried to simplify the code further and think the code that I have now is easy enough to understand. I made it so that everything ends up in the ignoreArr.
Also fixed a bug while I was at it.

We need tests for the ignore case though! I am not quite sure how to do the ignore case. I need to take a look.

Also I made the snapshots break because ignore is an array now when passied to chokidar. Need to fix that as well

}
}

const watcher = chokidar.watch(watchPathArr, watchOptions);

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