Skip to content

Commit

Permalink
Allow function in watchOptions.ignored
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Molinaro committed Nov 12, 2021
1 parent c651129 commit 08f6a1a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -49,6 +49,7 @@ var wp = new Watchpack({
// ignored: "string" - a glob pattern for files or folders that should not be watched
// ignored: ["string", "string"] - multiple glob patterns that should be ignored
// ignored: /regexp/ - a regular expression for files or folders that should not be watched
// ignored: (entry) => boolean - an arbirary function which must return truthy to ignore an entry
// All subdirectories are ignored too
});

Expand Down
6 changes: 4 additions & 2 deletions lib/watchpack.js
Expand Up @@ -31,13 +31,15 @@ const stringToRegexp = ignored => {
return matchingStart;
};

const ignoredToRegexp = ignored => {
const ignoredToRegexpLike = ignored => {
if (Array.isArray(ignored)) {
return new RegExp(ignored.map(i => stringToRegexp(i)).join("|"));
} else if (typeof ignored === "string") {
return new RegExp(stringToRegexp(ignored));
} else if (ignored instanceof RegExp) {
return ignored;
} else if (ignored instanceof Function) {
return { test: ignored };
} else if (ignored) {
throw new Error(`Invalid option for 'ignored': ${ignored}`);
} else {
Expand All @@ -48,7 +50,7 @@ const ignoredToRegexp = ignored => {
const normalizeOptions = options => {
return {
followSymlinks: !!options.followSymlinks,
ignored: ignoredToRegexp(options.ignored),
ignored: ignoredToRegexpLike(options.ignored),
poll: options.poll
};
};
Expand Down
26 changes: 26 additions & 0 deletions test/Watchpack.js
Expand Up @@ -118,6 +118,32 @@ describe("Watchpack", function() {
});
});

it("should not watch a single ignored file (function)", function(done) {
var w = new Watchpack({
aggregateTimeout: 300,
ignored: (entry) => entry.includes("a")
});
var changeEvents = 0;
var aggregatedEvents = 0;
w.on("change", () => {
changeEvents++;
});
w.on("aggregated", () => {
aggregatedEvents++;
});
w.watch([path.join(fixtures, "a")], []);
testHelper.tick(() => {
testHelper.file("a");
testHelper.tick(1000, () => {
changeEvents.should.be.eql(0);
aggregatedEvents.should.be.eql(0);
testHelper.getNumberOfWatchers().should.be.eql(0);
w.close();
done();
});
});
});

it("should watch multiple files", function(done) {
var w = new Watchpack({
aggregateTimeout: 1000
Expand Down

0 comments on commit 08f6a1a

Please sign in to comment.