@@ -14,7 +14,7 @@ export function resolveWithPaths(
1414 host : ts . CompilerHost ,
1515 cache ?: ts . ModuleResolutionCache ,
1616) {
17- if ( ! request || ! request . request ) {
17+ if ( ! request || ! request . request || ! compilerOptions . paths ) {
1818 callback ( null , request ) ;
1919 return ;
2020 }
@@ -26,21 +26,54 @@ export function resolveWithPaths(
2626 }
2727
2828 // check if any path mapping rules are relevant
29- const isPathMapped = compilerOptions . paths && Object . keys ( compilerOptions . paths )
30- . some ( pattern => {
29+ const pathMapOptions = [ ] ;
30+ for ( const pattern in compilerOptions . paths ) {
3131 // can only contain zero or one
3232 const starIndex = pattern . indexOf ( '*' ) ;
3333 if ( starIndex === - 1 ) {
34- return pattern === request . request ;
34+ if ( pattern === request . request ) {
35+ pathMapOptions . push ( {
36+ partial : '' ,
37+ potentials : compilerOptions . paths [ pattern ]
38+ } ) ;
39+ }
3540 } else if ( starIndex === pattern . length - 1 ) {
36- return request . request . startsWith ( pattern . slice ( 0 , - 1 ) ) ;
41+ if ( request . request . startsWith ( pattern . slice ( 0 , - 1 ) ) ) {
42+ pathMapOptions . push ( {
43+ partial : request . request . slice ( pattern . length - 1 ) ,
44+ potentials : compilerOptions . paths [ pattern ]
45+ } ) ;
46+ }
3747 } else {
3848 const [ prefix , suffix ] = pattern . split ( '*' ) ;
39- return request . request . startsWith ( prefix ) && request . request . endsWith ( suffix ) ;
49+ if ( request . request . startsWith ( prefix ) && request . request . endsWith ( suffix ) ) {
50+ pathMapOptions . push ( {
51+ partial : request . request . slice ( prefix . length ) . slice ( 0 , - suffix . length ) ,
52+ potentials : compilerOptions . paths [ pattern ]
53+ } ) ;
54+ }
4055 }
41- } ) ;
56+ }
57+
58+ if ( pathMapOptions . length === 0 ) {
59+ callback ( null , request ) ;
60+ return ;
61+ }
62+
63+ if ( pathMapOptions . length === 1 && pathMapOptions [ 0 ] . potentials . length === 1 ) {
64+ const onlyPotential = pathMapOptions [ 0 ] . potentials [ 0 ] ;
65+ let replacement ;
66+ const starIndex = onlyPotential . indexOf ( '*' ) ;
67+ if ( starIndex === - 1 ) {
68+ replacement = onlyPotential ;
69+ } else if ( starIndex === onlyPotential . length - 1 ) {
70+ replacement = onlyPotential . slice ( 0 , - 1 ) + pathMapOptions [ 0 ] . partial ;
71+ } else {
72+ const [ prefix , suffix ] = onlyPotential . split ( '*' ) ;
73+ replacement = prefix + pathMapOptions [ 0 ] . partial + suffix ;
74+ }
4275
43- if ( ! isPathMapped ) {
76+ request . request = path . resolve ( compilerOptions . baseUrl , replacement ) ;
4477 callback ( null , request ) ;
4578 return ;
4679 }
0 commit comments