@@ -20,7 +20,7 @@ export function resolveWithPaths(
20
20
host : ts . CompilerHost ,
21
21
cache ?: ts . ModuleResolutionCache ,
22
22
) {
23
- if ( ! request || ! request . request ) {
23
+ if ( ! request || ! request . request || ! compilerOptions . paths ) {
24
24
callback ( null , request ) ;
25
25
return ;
26
26
}
@@ -32,21 +32,54 @@ export function resolveWithPaths(
32
32
}
33
33
34
34
// 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 ) {
37
37
// can only contain zero or one
38
38
const starIndex = pattern . indexOf ( '*' ) ;
39
39
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
+ }
41
46
} 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
+ }
43
53
} else {
44
54
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
+ }
46
61
}
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
+ }
48
81
49
- if ( ! isPathMapped ) {
82
+ request . request = path . resolve ( compilerOptions . baseUrl , replacement ) ;
50
83
callback ( null , request ) ;
51
84
return ;
52
85
}
0 commit comments