@@ -12,7 +12,7 @@ import { join } from 'path';
12
12
export class PathCache {
13
13
useCache : boolean ;
14
14
existsCache : Map < string , boolean > ;
15
- absoluteCache : Map < { basePath : string ; aliasPath : string } , string > ;
15
+ absoluteCache : Map < string , string > ;
16
16
fileExtensions : string [ ] ;
17
17
18
18
constructor ( useCache : boolean , fileExtensions ?: string [ ] ) {
@@ -34,20 +34,6 @@ export class PathCache {
34
34
}
35
35
}
36
36
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
-
51
37
/**
52
38
* existsResolvedAlias checks if file exists, uses cache when possible.
53
39
* @param {string } path the filepath to check.
@@ -64,6 +50,34 @@ export class PathCache {
64
50
}
65
51
}
66
52
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
+
67
81
/**
68
82
* getAAP finds the absolute alias path.
69
83
* @param {string } basePath the basepath of the alias.
@@ -99,20 +113,16 @@ export class PathCache {
99
113
}
100
114
101
115
/**
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.
106
119
*/
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
+ ) ;
117
127
}
118
128
}
0 commit comments