Skip to content

Commit

Permalink
fix: ignore empty strings in an array
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Aug 13, 2024
1 parent 38e2071 commit c07ada7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
16 changes: 10 additions & 6 deletions lib/watchpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, "/"));
Expand Down
22 changes: 22 additions & 0 deletions test/Watchpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit c07ada7

Please sign in to comment.