@@ -20,7 +20,7 @@ export function resolveWithPaths(
2020 host : ts . CompilerHost ,
2121 cache ?: ts . ModuleResolutionCache ,
2222) {
23- if ( ! request || ! request . request ) {
23+ if ( ! request || ! request . request || ! compilerOptions . paths ) {
2424 callback ( null , request ) ;
2525 return ;
2626 }
@@ -32,21 +32,54 @@ export function resolveWithPaths(
3232 }
3333
3434 // check if any path mapping rules are relevant
35- const isPathMapped = compilerOptions . paths && Object . keys ( compilerOptions . paths )
36- . some ( pattern => {
35+ const pathMapOptions = [ ] ;
36+ for ( const pattern in compilerOptions . paths ) {
3737 // can only contain zero or one
3838 const starIndex = pattern . indexOf ( '*' ) ;
3939 if ( starIndex === - 1 ) {
40- return pattern === request . request ;
40+ if ( pattern === request . request ) {
41+ pathMapOptions . push ( {
42+ partial : '' ,
43+ potentials : compilerOptions . paths [ pattern ]
44+ } ) ;
45+ }
4146 } else if ( starIndex === pattern . length - 1 ) {
42- return request . request . startsWith ( pattern . slice ( 0 , - 1 ) ) ;
47+ if ( request . request . startsWith ( pattern . slice ( 0 , - 1 ) ) ) {
48+ pathMapOptions . push ( {
49+ partial : request . request . slice ( pattern . length - 1 ) ,
50+ potentials : compilerOptions . paths [ pattern ]
51+ } ) ;
52+ }
4353 } else {
4454 const [ prefix , suffix ] = pattern . split ( '*' ) ;
45- return request . request . startsWith ( prefix ) && request . request . endsWith ( suffix ) ;
55+ if ( request . request . startsWith ( prefix ) && request . request . endsWith ( suffix ) ) {
56+ pathMapOptions . push ( {
57+ partial : request . request . slice ( prefix . length ) . slice ( 0 , - suffix . length ) ,
58+ potentials : compilerOptions . paths [ pattern ]
59+ } ) ;
60+ }
4661 }
47- } ) ;
62+ }
63+
64+ if ( pathMapOptions . length === 0 ) {
65+ callback ( null , request ) ;
66+ return ;
67+ }
68+
69+ if ( pathMapOptions . length === 1 && pathMapOptions [ 0 ] . potentials . length === 1 ) {
70+ const onlyPotential = pathMapOptions [ 0 ] . potentials [ 0 ] ;
71+ let replacement ;
72+ const starIndex = onlyPotential . indexOf ( '*' ) ;
73+ if ( starIndex === - 1 ) {
74+ replacement = onlyPotential ;
75+ } else if ( starIndex === onlyPotential . length - 1 ) {
76+ replacement = onlyPotential . slice ( 0 , - 1 ) + pathMapOptions [ 0 ] . partial ;
77+ } else {
78+ const [ prefix , suffix ] = onlyPotential . split ( '*' ) ;
79+ replacement = prefix + pathMapOptions [ 0 ] . partial + suffix ;
80+ }
4881
49- if ( ! isPathMapped ) {
82+ request . request = path . resolve ( compilerOptions . baseUrl , replacement ) ;
5083 callback ( null , request ) ;
5184 return ;
5285 }
0 commit comments