Skip to content

Commit 1d27bca

Browse files
feat: add readdirPrefix option to CachedInputFileSystem#purge
With exact: true, readdirPrefix: true still drops the cached readdir results below the purged path, while file stats stay cached.
1 parent 9704b87 commit 1d27bca

4 files changed

Lines changed: 31 additions & 3 deletions

File tree

‎.changeset/purge-readdir-prefix.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"enhanced-resolve": minor
3+
---
4+
5+
Add a `readdirPrefix` option to `CachedInputFileSystem#purge` that, with `exact: true`, still removes the cached directory listings below `what`.

‎lib/CachedInputFileSystem.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -827,13 +827,17 @@ module.exports = class CachedInputFileSystem {
827827

828828
/**
829829
* @param {(string | Buffer | URL | number | (string | URL | Buffer | number)[] | Set<string | URL | Buffer | number>)=} what what to purge
830-
* @param {{ exact?: boolean }=} options options; `exact: true` removes only cache entries whose key matches `what` exactly instead of any entry whose key starts with `what`
830+
* @param {{ exact?: boolean, readdirPrefix?: boolean }=} options options; `exact: true` removes only cache entries whose key matches `what` exactly instead of any entry whose key starts with `what`; with `exact: true`, `readdirPrefix: true` still removes every `readdir` entry whose key starts with `what`, so the listings of directories below a changed directory are dropped while their file stats are kept
831831
*/
832832
purge(what, options) {
833833
this._statBackend.purge(what, options);
834834
this._lstatBackend.purge(what, options);
835835
if (options !== undefined && options !== null && options.exact === true) {
836-
this._readdirBackend.purge(what, options);
836+
if (options.readdirPrefix === true) {
837+
this._readdirBackend.purge(what);
838+
} else {
839+
this._readdirBackend.purge(what, options);
840+
}
837841
} else {
838842
this._readdirBackend.purgeParent(what);
839843
}

‎test/CachedInputFileSystem.test.js‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,24 @@ describe("cachedInputFileSystem CacheBackend", () => {
638638
});
639639
});
640640

641+
it("should prefix-purge readdir but keep stats with readdirPrefix: true", (t, done) => {
642+
fs.readdir("/test/path/sub", (err, r1) => {
643+
assert.strictEqual(r1[0], "0");
644+
fs.stat("/test/path/sub/file", (err, r2) => {
645+
r2.cached = true;
646+
fs.purge("/test/path", { exact: true, readdirPrefix: true });
647+
fs.readdir("/test/path/sub", (err, r3) => {
648+
// the listing below the purged directory is fetched again
649+
assert.strictEqual(r3[0], "1");
650+
fs.stat("/test/path/sub/file", (err, r4) => {
651+
assert.strictEqual(r4.cached, true);
652+
done();
653+
});
654+
});
655+
});
656+
});
657+
});
658+
641659
it("should accept Buffer with exact: true", (t, done) => {
642660
fs.stat("/test/path", (err, r1) => {
643661
r1.cached = true;

‎types/CachedInputFileSystem.d.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ declare class CachedInputFileSystem {
2929
realpathSync: import("./Resolver").RealPathSync | undefined;
3030
/**
3131
* @param {(string | Buffer | URL | number | (string | URL | Buffer | number)[] | Set<string | URL | Buffer | number>)=} what what to purge
32-
* @param {{ exact?: boolean }=} options options; `exact: true` removes only cache entries whose key matches `what` exactly instead of any entry whose key starts with `what`
32+
* @param {{ exact?: boolean, readdirPrefix?: boolean }=} options options; `exact: true` removes only cache entries whose key matches `what` exactly instead of any entry whose key starts with `what`; with `exact: true`, `readdirPrefix: true` still removes every `readdir` entry whose key starts with `what`, so the listings of directories below a changed directory are dropped while their file stats are kept
3333
*/
3434
purge(
3535
what?:
@@ -45,6 +45,7 @@ declare class CachedInputFileSystem {
4545
options?:
4646
| {
4747
exact?: boolean;
48+
readdirPrefix?: boolean;
4849
}
4950
| undefined,
5051
): void;

0 commit comments

Comments
 (0)