@@ -14,7 +14,7 @@ export function resolveWithPaths(
14
14
host : ts . CompilerHost ,
15
15
cache ?: ts . ModuleResolutionCache ,
16
16
) {
17
- if ( ! request || ! request . request ) {
17
+ if ( ! request || ! request . request || ! compilerOptions . paths ) {
18
18
callback ( null , request ) ;
19
19
return ;
20
20
}
@@ -26,21 +26,54 @@ export function resolveWithPaths(
26
26
}
27
27
28
28
// 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 ) {
31
31
// can only contain zero or one
32
32
const starIndex = pattern . indexOf ( '*' ) ;
33
33
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
+ }
35
40
} 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
+ }
37
47
} else {
38
48
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
+ }
40
55
}
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
+ }
42
75
43
- if ( ! isPathMapped ) {
76
+ request . request = path . resolve ( compilerOptions . baseUrl , replacement ) ;
44
77
callback ( null , request ) ;
45
78
return ;
46
79
}
0 commit comments