From a05d00dba5c6f09b8804aa375ffe3d75b8c18441 Mon Sep 17 00:00:00 2001 From: Mark Molinaro Date: Tue, 22 Jun 2021 19:50:05 +0000 Subject: [PATCH] Allow simple regex to be passed in array --- lib/watchpack.js | 17 ++++++++++++----- test/Watchpack.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/lib/watchpack.js b/lib/watchpack.js index 9f1dca1..8397ec3 100644 --- a/lib/watchpack.js +++ b/lib/watchpack.js @@ -24,18 +24,25 @@ function addWatchersToSet(watchers, set) { } } -const stringToRegexp = ignored => { - const source = globToRegExp(ignored, { globstar: true, extended: true }) - .source; +const stringOrRegexpToRegexp = ignored => { + if (ignored instanceof RegExp) { + if (ignored.flags !== "") { + throw new Error( + `Invalid option for 'ignored', regex in list does not support flags ${ignored}` + ); + } + return ignored.source; + } + const { source } = globToRegExp(ignored, { globstar: true, extended: true }); const matchingStart = source.slice(0, source.length - 1) + "(?:$|\\/)"; return matchingStart; }; const ignoredToRegexp = ignored => { if (Array.isArray(ignored)) { - return new RegExp(ignored.map(i => stringToRegexp(i)).join("|")); + return new RegExp(ignored.map(i => stringOrRegexpToRegexp(i)).join("|")); } else if (typeof ignored === "string") { - return new RegExp(stringToRegexp(ignored)); + return new RegExp(stringOrRegexpToRegexp(ignored)); } else if (ignored instanceof RegExp) { return ignored; } else if (ignored) { diff --git a/test/Watchpack.js b/test/Watchpack.js index ade8021..7cf0a48 100644 --- a/test/Watchpack.js +++ b/test/Watchpack.js @@ -14,6 +14,16 @@ describe("Watchpack", function() { beforeEach(testHelper.before); afterEach(testHelper.after); + it("should throw on invalid ignore options", function() { + try { + var w = new Watchpack({ + ignored: [/\/a$/i] + }); + } catch(e) { + e.message.should.startWith("Invalid"); + } + }); + it("should watch a single file", function(done) { var w = new Watchpack({ aggregateTimeout: 1000 @@ -118,6 +128,32 @@ describe("Watchpack", function() { }); }); + it("should not watch a single ignored file (array with regexp and glob)", function(done) { + var w = new Watchpack({ + aggregateTimeout: 300, + ignored: [/\/a$/, "**/b"] + }); + 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