Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/purge-readdir-prefix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"enhanced-resolve": minor
---

Add a `readdirPrefix` option to `CachedInputFileSystem#purge` that, with `exact: true`, still removes the cached directory listings below `what`.
8 changes: 6 additions & 2 deletions lib/CachedInputFileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -827,13 +827,17 @@ module.exports = class CachedInputFileSystem {

/**
* @param {(string | Buffer | URL | number | (string | URL | Buffer | number)[] | Set<string | URL | Buffer | number>)=} what what to purge
* @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`
* @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
*/
purge(what, options) {
this._statBackend.purge(what, options);
this._lstatBackend.purge(what, options);
if (options !== undefined && options !== null && options.exact === true) {
this._readdirBackend.purge(what, options);
if (options.readdirPrefix === true) {
this._readdirBackend.purge(what);
} else {
this._readdirBackend.purge(what, options);
}
} else {
this._readdirBackend.purgeParent(what);
}
Expand Down
18 changes: 18 additions & 0 deletions test/CachedInputFileSystem.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,24 @@ describe("cachedInputFileSystem CacheBackend", () => {
});
});

it("should prefix-purge readdir but keep stats with readdirPrefix: true", (t, done) => {
fs.readdir("/test/path/sub", (err, r1) => {
assert.strictEqual(r1[0], "0");
fs.stat("/test/path/sub/file", (err, r2) => {
r2.cached = true;
fs.purge("/test/path", { exact: true, readdirPrefix: true });
fs.readdir("/test/path/sub", (err, r3) => {
// the listing below the purged directory is fetched again
assert.strictEqual(r3[0], "1");
fs.stat("/test/path/sub/file", (err, r4) => {
assert.strictEqual(r4.cached, true);
done();
});
});
});
});
});

it("should accept Buffer with exact: true", (t, done) => {
fs.stat("/test/path", (err, r1) => {
r1.cached = true;
Expand Down
3 changes: 2 additions & 1 deletion types/CachedInputFileSystem.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ declare class CachedInputFileSystem {
realpathSync: import("./Resolver").RealPathSync | undefined;
/**
* @param {(string | Buffer | URL | number | (string | URL | Buffer | number)[] | Set<string | URL | Buffer | number>)=} what what to purge
* @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`
* @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
*/
purge(
what?:
Expand All @@ -45,6 +45,7 @@ declare class CachedInputFileSystem {
options?:
| {
exact?: boolean;
readdirPrefix?: boolean;
}
| undefined,
): void;
Expand Down
Loading