From 08f6a1a6fb25252aae5536d87835143cde221ae8 Mon Sep 17 00:00:00 2001 From: Mark Molinaro Date: Fri, 12 Nov 2021 19:57:02 +0000 Subject: [PATCH] Allow function in watchOptions.ignored --- README.md | 1 + lib/watchpack.js | 6 ++++-- test/Watchpack.js | 26 ++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 64a7686..19ada68 100644 --- a/README.md +++ b/README.md @@ -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 }); diff --git a/lib/watchpack.js b/lib/watchpack.js index 9f1dca1..e4ea83a 100644 --- a/lib/watchpack.js +++ b/lib/watchpack.js @@ -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 { @@ -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 }; }; diff --git a/test/Watchpack.js b/test/Watchpack.js index ade8021..0312175 100644 --- a/test/Watchpack.js +++ b/test/Watchpack.js @@ -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