@@ -10,20 +10,17 @@ const validate = {
10
10
return false ;
11
11
}
12
12
13
- for ( const key in x ) {
14
- const value =
15
- // @ts -expect-error: here "key" can be used to index object
16
- x [ key ] ;
17
- if (
18
- ! Array . isArray ( value ) &&
13
+ const paths = x as Record < string , unknown > ;
14
+
15
+ const isValid = Object . values ( paths ) . every ( value => {
16
+ return (
17
+ Array . isArray ( value ) &&
19
18
value . length > 0 &&
20
- ! value . every ( validate . string )
21
- ) {
22
- return false ;
23
- }
24
- }
19
+ value . every ( validate . string )
20
+ ) ;
21
+ } ) ;
25
22
26
- return true ;
23
+ return isValid ;
27
24
} ,
28
25
} ;
29
26
@@ -59,35 +56,54 @@ export const resolveAliasedImport = ({
59
56
return null ;
60
57
}
61
58
62
- const paths = config . config ?. compilerOptions ?. paths as void | TsconfigPaths ;
63
- const baseUrl = config . config ?. compilerOptions ?. baseUrl as void | string ;
59
+ const paths : unknown = config . config ?. compilerOptions ?. paths ;
60
+
61
+ const potentialBaseUrl : unknown = config . config ?. compilerOptions ?. baseUrl ;
62
+
64
63
const configLocation = path . dirname ( config . filepath ) ;
65
64
66
- if ( ! validate . string ( baseUrl ) ) {
67
- return null ;
68
- }
69
- if ( ! validate . tsconfigPaths ( paths ) ) {
70
- return null ;
71
- }
65
+ if ( validate . tsconfigPaths ( paths ) ) {
66
+ const baseUrl = validate . string ( potentialBaseUrl )
67
+ ? potentialBaseUrl
68
+ : '.' ;
72
69
73
- for ( const alias in paths ) {
74
- const aliasRe = new RegExp ( alias . replace ( '*' , '(.+)' ) , '' ) ;
70
+ for ( const alias in paths ) {
71
+ const aliasRe = new RegExp ( alias . replace ( '*' , '(.+)' ) , '' ) ;
75
72
76
- const aliasMatch = importFilepath . match ( aliasRe ) ;
73
+ const aliasMatch = importFilepath . match ( aliasRe ) ;
77
74
78
- if ( aliasMatch == null ) continue ;
75
+ if ( aliasMatch == null ) continue ;
79
76
80
- for ( const potentialAliasLocation of paths [ alias ] ) {
81
- const resolvedFileLocation = path . resolve (
82
- configLocation ,
83
- baseUrl ,
84
- potentialAliasLocation
85
- // "./utils/*" -> "./utils/style.module.css"
86
- . replace ( '*' , aliasMatch [ 1 ] ) ,
87
- ) ;
77
+ for ( const potentialAliasLocation of paths [ alias ] ) {
78
+ const resolvedFileLocation = path . resolve (
79
+ configLocation ,
80
+ baseUrl ,
81
+ potentialAliasLocation
82
+ // "./utils/*" -> "./utils/style.module.css"
83
+ . replace ( '*' , aliasMatch [ 1 ] ) ,
84
+ ) ;
85
+
86
+ if ( ! fs . existsSync ( resolvedFileLocation ) ) continue ;
88
87
89
- if ( ! fs . existsSync ( resolvedFileLocation ) ) continue ;
88
+ return resolvedFileLocation ;
89
+ }
90
+ }
91
+ }
90
92
93
+ // if paths is defined, but no paths match
94
+ // then baseUrl will not fallback to "."
95
+ // if not using paths to find an alias, baseUrl must be defined
96
+ // so here we only try and resolve the file if baseUrl is explcitly set and valid
97
+ // i.e. if no baseUrl is provided
98
+ // then no imports relative to baseUrl on its own are allowed, only relative to paths
99
+ if ( validate . string ( potentialBaseUrl ) ) {
100
+ const resolvedFileLocation = path . resolve (
101
+ configLocation ,
102
+ potentialBaseUrl ,
103
+ importFilepath ,
104
+ ) ;
105
+
106
+ if ( fs . existsSync ( resolvedFileLocation ) ) {
91
107
return resolvedFileLocation ;
92
108
}
93
109
}
0 commit comments