Skip to content

Commit a34faf6

Browse files
committed
fix: bug fixing and make code more readable
1 parent 3dd2b4b commit a34faf6

File tree

1 file changed

+24
-31
lines changed

1 file changed

+24
-31
lines changed

lib/Server.js

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3259,15 +3259,18 @@ class Server {
32593259
const chokidar = require("chokidar");
32603260

32613261
const watchPathArr = Array.isArray(watchPath) ? watchPath : [watchPath];
3262+
const ignoredArr = Array.isArray(watchOptions.ignored)
3263+
? watchOptions.ignored
3264+
: [];
32623265

32633266
if (watchOptions.disableGlobbing !== true) {
3267+
const picomatch = require("picomatch");
32643268
const isGlob = require("is-glob");
32653269
const watchPathGlobs = watchPathArr.filter((p) => isGlob(p));
32663270

3267-
// No need to do all this work when no globs are used
3271+
// No need to do all this work when no globs are used in watcher
32683272
if (watchPathGlobs.length > 0) {
32693273
const globParent = require("glob-parent");
3270-
const picomatch = require("picomatch");
32713274

32723275
watchPathGlobs.forEach((p) => {
32733276
watchPathArr[watchPathArr.indexOf(p)] = globParent(p);
@@ -3278,39 +3281,29 @@ class Server {
32783281
dot: true,
32793282
});
32803283

3281-
const ignoreFunc = (/** @type {string} */ p) =>
3282-
!watchPathArr.includes(p) && !matcher(p);
3283-
3284-
const ignoredArr = Array.isArray(watchOptions.ignored)
3285-
? watchOptions.ignored
3286-
: [];
3287-
3288-
// Nested ternaries are forbidden by eslint so we end up with this
3289-
if (watchOptions.ignored && !Array.isArray(watchOptions.ignored)) {
3290-
ignoredArr.push(watchOptions.ignored);
3291-
}
3292-
3293-
const ignoredGlobs = [];
3294-
for (let i = 0; i < ignoredArr.length; i++) {
3295-
const ignored = ignoredArr[i];
3296-
if (typeof ignored === "string" && isGlob(ignored)) {
3297-
ignoredGlobs.push(ignored);
3298-
ignoredArr.splice(i, 1);
3299-
}
3300-
}
3284+
// Ignore all paths that don't match any of the globs
3285+
ignoredArr.push((p) => !watchPathArr.includes(p) && !matcher(p));
3286+
}
33013287

3302-
if (ignoredGlobs.length > 0) {
3303-
const ignoreMatcher = picomatch(ignoredGlobs, {
3304-
dot: true,
3305-
cwd: watchOptions.cwd,
3306-
});
3307-
ignoredArr.push(ignoreMatcher);
3308-
}
3288+
// Double filter to satisfy typescript. Otherwise nasty casting is required
3289+
const ignoredGlobs = ignoredArr
3290+
.filter((s) => typeof s === "string")
3291+
.filter((s) => isGlob(s));
33093292

3310-
ignoredArr.push(ignoreFunc);
3293+
// No need to do all this work when no globs are used in ignored
3294+
if (ignoredGlobs.length > 0) {
3295+
const matcher = picomatch(ignoredGlobs, {
3296+
cwd: watchOptions.cwd,
3297+
dot: true,
3298+
});
33113299

3312-
watchOptions.ignored = ignoredArr;
3300+
ignoredArr.push(matcher);
33133301
}
3302+
3303+
// Filter out all the glob strings
3304+
watchOptions.ignored = ignoredArr.filter(
3305+
(s) => typeof s === "string" && isGlob(s),
3306+
);
33143307
}
33153308

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

0 commit comments

Comments
 (0)