Skip to content

Commit

Permalink
fix: support ignore globs, enable dotfiles
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuzzyma committed Dec 13, 2024
1 parent 9492a00 commit 184588d
Showing 1 changed file with 53 additions and 19 deletions.
72 changes: 53 additions & 19 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").ChokidarOptions} 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 @@ -3257,30 +3257,64 @@ class Server {
*/
watchFiles(watchPath, watchOptions = {}) {
const chokidar = require("chokidar");
const isGlob = require("is-glob");

const watchPathArr = Array.isArray(watchPath) ? watchPath : [watchPath];
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");
if (watchOptions.disableGlobbing !== true) {
const isGlob = require("is-glob");
const watchPathGlobs = watchPathArr.filter((p) => isGlob(p));

watchPathGlobs.forEach((p) => {
watchPathArr[watchPathArr.indexOf(p)] = globParent(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");

const matcher = picomatch(watchPathGlobs);
const ignoreFunc = (/** @type {string} */ p) =>
!watchPathArr.includes(p) && !matcher(p);
watchPathGlobs.forEach((p) => {
watchPathArr[watchPathArr.indexOf(p)] = globParent(p);
});

if (Array.isArray(watchOptions.ignored)) {
watchOptions.ignored.push(ignoreFunc);
} else {
watchOptions.ignored = watchOptions.ignored
? [watchOptions.ignored, ignoreFunc]
: ignoreFunc;
const matcher = picomatch(watchPathGlobs, {
cwd: watchOptions.cwd,
dot: true,
});
const ignoreFunc = (/** @type {string} */ p) =>
!watchPathArr.includes(p) && !matcher(p);

if (Array.isArray(watchOptions.ignored)) {
const ignoredGlobs = [];
for (let i = 0; i < watchOptions.ignored.length; i++) {
const ignored = watchOptions.ignored[i];
if (typeof ignored === "string" && isGlob(ignored)) {
ignoredGlobs.push(ignored);
watchOptions.ignored.splice(i, 1);
}
}

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

watchOptions.ignored.push(ignoreFunc);
} else {
if (
watchOptions.ignored &&
typeof watchOptions.ignored === "string" &&
isGlob(watchOptions.ignored)
) {
watchOptions.ignored = picomatch(watchOptions.ignored, {
dot: true,
cwd: watchOptions.cwd,
});
}

watchOptions.ignored = watchOptions.ignored
? [watchOptions.ignored, ignoreFunc]
: ignoreFunc;
}
}
}

Expand Down

0 comments on commit 184588d

Please sign in to comment.