Skip to content

Commit e391a04

Browse files
authored
fix: using on-the-fly constructed object like cache key doesn't work (#159)
1 parent 687169b commit e391a04

File tree

1 file changed

+39
-29
lines changed

1 file changed

+39
-29
lines changed

src/utils/path-cache.ts

+39-29
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { join } from 'path';
1212
export class PathCache {
1313
useCache: boolean;
1414
existsCache: Map<string, boolean>;
15-
absoluteCache: Map<{ basePath: string; aliasPath: string }, string>;
15+
absoluteCache: Map<string, string>;
1616
fileExtensions: string[];
1717

1818
constructor(useCache: boolean, fileExtensions?: string[]) {
@@ -34,20 +34,6 @@ export class PathCache {
3434
}
3535
}
3636

37-
/**
38-
* exists checks if file exists.
39-
* @param path the filepath to check.
40-
* @returns {boolean} result of check.
41-
*/
42-
private exists(path: string): boolean {
43-
return (
44-
existsSync(path) ||
45-
this.fileExtensions.some((extension) =>
46-
existsSync(`${path}.${extension}`)
47-
)
48-
);
49-
}
50-
5137
/**
5238
* existsResolvedAlias checks if file exists, uses cache when possible.
5339
* @param {string} path the filepath to check.
@@ -64,6 +50,34 @@ export class PathCache {
6450
}
6551
}
6652

53+
/**
54+
* getAbsoluteAliasPath finds the absolute alias path, uses cache when possible.
55+
* @param {string} basePath the basepath of the alias.
56+
* @param {string} aliasPath the aliaspath of the alias.
57+
* @returns {string} the absolute alias path.
58+
*/
59+
public getAbsoluteAliasPath(basePath: string, aliasPath: string): string {
60+
const request = { basePath, aliasPath };
61+
if (!this.useCache) return this.getAAP(request);
62+
if (this.absoluteCache.has(this.getCacheKey(request))) {
63+
return this.absoluteCache.get(this.getCacheKey(request));
64+
} else {
65+
const result = this.getAAP(request);
66+
this.absoluteCache.set(this.getCacheKey(request), result);
67+
return result;
68+
}
69+
}
70+
71+
private getCacheKey({
72+
basePath,
73+
aliasPath
74+
}: {
75+
basePath: string;
76+
aliasPath: string;
77+
}): string {
78+
return `${basePath}___${aliasPath}`;
79+
}
80+
6781
/**
6882
* getAAP finds the absolute alias path.
6983
* @param {string} basePath the basepath of the alias.
@@ -99,20 +113,16 @@ export class PathCache {
99113
}
100114

101115
/**
102-
* getAbsoluteAliasPath finds the absolute alias path, uses cache when possible.
103-
* @param {string} basePath the basepath of the alias.
104-
* @param {string} aliasPath the aliaspath of the alias.
105-
* @returns {string} the absolute alias path.
116+
* exists checks if file exists.
117+
* @param path the filepath to check.
118+
* @returns {boolean} result of check.
106119
*/
107-
public getAbsoluteAliasPath(basePath: string, aliasPath: string): string {
108-
const request = { basePath, aliasPath };
109-
if (!this.useCache) return this.getAAP(request);
110-
if (this.absoluteCache.has(request)) {
111-
return this.absoluteCache.get(request);
112-
} else {
113-
const result = this.getAAP(request);
114-
this.absoluteCache.set(request, result);
115-
return result;
116-
}
120+
private exists(path: string): boolean {
121+
return (
122+
existsSync(path) ||
123+
this.fileExtensions.some((extension) =>
124+
existsSync(`${path}.${extension}`)
125+
)
126+
);
117127
}
118128
}

0 commit comments

Comments
 (0)