@@ -73,6 +73,7 @@ export default class Preset {
7373
7474 this . hasDotEnv = hasDotEnv || false ;
7575 this . alias = alias || { } ;
76+ this . aliasedPathCache = { } ;
7677 this . defaultAliases = alias || { } ;
7778 this . ignoredExtensions = ignoredExtensions || [ 'js' , 'jsx' , 'json' ] ;
7879
@@ -84,13 +85,21 @@ export default class Preset {
8485
8586 setAdditionalAliases = ( aliases : Object ) => {
8687 this . alias = { ...this . defaultAliases , ...aliases } ;
88+ this . aliasedPathCache = { } ;
8789 } ;
8890
91+ aliasedPathCache = { } ;
8992 /**
9093 * Checks if there is an alias given for the path, if there is it will return
9194 * the altered path, otherwise it will just return the known path.
9295 */
9396 getAliasedPath ( path : string ) : string {
97+ if ( this . aliasedPathCache [ path ] === null ) {
98+ return path ;
99+ } else if ( this . aliasedPathCache [ path ] ) {
100+ return this . aliasedPathCache [ path ] ;
101+ }
102+
94103 const aliases = Object . keys ( this . alias ) ;
95104
96105 const exactAliases = aliases . filter ( a => a . endsWith ( '$' ) ) ;
@@ -105,10 +114,11 @@ export default class Preset {
105114 } ) ;
106115
107116 if ( exactFoundAlias ) {
117+ this . aliasedPathCache [ path ] = this . alias [ exactFoundAlias ] ;
108118 return this . alias [ exactFoundAlias ] ;
109119 }
110120
111- const pathParts = path . split ( '/' ) ; // eslint-disable-line prefer-const
121+ const pathParts = path . split ( '/' ) ;
112122
113123 // Find matching aliases
114124 const foundAlias = orderBy ( aliases , a => - a . split ( '/' ) . length ) . find ( a => {
@@ -117,10 +127,14 @@ export default class Preset {
117127 } ) ;
118128
119129 if ( foundAlias ) {
130+ const replacedPath = path . replace ( foundAlias , this . alias [ foundAlias ] ) ;
131+ this . aliasedPathCache [ path ] = replacedPath ;
120132 // if an alias is found we will replace the path with the alias
121- return path . replace ( foundAlias , this . alias [ foundAlias ] ) ;
133+ return replacedPath ;
122134 }
123135
136+ this . aliasedPathCache [ path ] = null ;
137+
124138 return path ;
125139 }
126140
0 commit comments