From c07ada70873356d187180545b96fc6de531d2723 Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Tue, 13 Aug 2024 16:11:11 +0300 Subject: [PATCH] fix: ignore empty strings in an array --- lib/watchpack.js | 16 ++++++++++------ test/Watchpack.js | 22 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/lib/watchpack.js b/lib/watchpack.js index 79cf07c..0b567fc 100644 --- a/lib/watchpack.js +++ b/lib/watchpack.js @@ -23,24 +23,28 @@ function addWatchersToSet(watchers, set) { } const stringToRegexp = ignored => { + if (ignored.length === 0) { + return; + } const source = globToRegExp(ignored, { globstar: true, extended: true }) .source; - const matchingStart = source.slice(0, source.length - 1) + "(?:$|\\/)"; - return matchingStart; + return source.slice(0, source.length - 1) + "(?:$|\\/)"; }; const ignoredToFunction = ignored => { if (Array.isArray(ignored)) { - if (ignored.length === 0) { + const stringRegexps = ignored.map(i => stringToRegexp(i)).filter(Boolean); + if (stringRegexps.length === 0) { return () => false; } - const regexp = new RegExp(ignored.map(i => stringToRegexp(i)).join("|")); + const regexp = new RegExp(stringRegexps.join("|")); return x => regexp.test(x.replace(/\\/g, "/")); } else if (typeof ignored === "string") { - if (ignored.length === 0) { + const stringRegexp = stringToRegexp(ignored); + if (!stringRegexp) { return () => false; } - const regexp = new RegExp(stringToRegexp(ignored)); + const regexp = new RegExp(stringRegexp); return x => regexp.test(x.replace(/\\/g, "/")); } else if (ignored instanceof RegExp) { return x => ignored.test(x.replace(/\\/g, "/")); diff --git a/test/Watchpack.js b/test/Watchpack.js index a527756..1ea13fa 100644 --- a/test/Watchpack.js +++ b/test/Watchpack.js @@ -291,6 +291,28 @@ describe("Watchpack", function() { }); }); + it("should watch a file when ignore is an array with empty string", function(done) { + var w = new Watchpack({ + aggregateTimeout: 1000, + ignored: ["", ""] + }); + var changeEvents = 0; + w.on("change", function(file) { + file.should.be.eql(path.join(fixtures, "a")); + changeEvents++; + }); + w.on("aggregated", function(changes) { + Array.from(changes).should.be.eql([path.join(fixtures, "a")]); + changeEvents.should.be.greaterThan(0); + w.close(); + done(); + }); + w.watch([path.join(fixtures, "a")], []); + testHelper.tick(function() { + testHelper.file("a"); + }); + }); + it("should watch a file when ignore is empty string", function(done) { var w = new Watchpack({ aggregateTimeout: 1000,