1
+ use indexmap:: IndexMap ;
2
+ use rustc_hash:: FxHasher ;
3
+ use serde:: Deserialize ;
1
4
use std:: {
2
5
hash:: BuildHasherDefault ,
3
6
path:: { Path , PathBuf } ,
4
7
sync:: Arc ,
5
8
} ;
6
9
7
- use indexmap:: IndexMap ;
8
- use rustc_hash:: FxHasher ;
9
- use serde:: Deserialize ;
10
-
11
10
use crate :: PathUtil ;
12
11
13
12
pub type CompilerOptionsPathsMap = IndexMap < String , Vec < String > , BuildHasherDefault < FxHasher > > ;
@@ -19,6 +18,8 @@ pub enum ExtendsField {
19
18
Multiple ( Vec < String > ) ,
20
19
}
21
20
21
+ const TEMPLATE_VARIABLE : & str = "${configDir}" ;
22
+
22
23
#[ derive( Debug , Deserialize ) ]
23
24
#[ serde( rename_all = "camelCase" ) ]
24
25
pub struct TsConfig {
@@ -85,8 +86,11 @@ impl TsConfig {
85
86
tsconfig. root = root;
86
87
tsconfig. path = path. to_path_buf ( ) ;
87
88
let directory = tsconfig. directory ( ) . to_path_buf ( ) ;
88
- if let Some ( base_url) = tsconfig. compiler_options . base_url {
89
- tsconfig. compiler_options . base_url = Some ( directory. normalize_with ( base_url) ) ;
89
+ if let Some ( base_url) = & tsconfig. compiler_options . base_url {
90
+ // keep the `${configDir}` template variable in the baseUrl
91
+ if !base_url. starts_with ( TEMPLATE_VARIABLE ) {
92
+ tsconfig. compiler_options . base_url = Some ( directory. normalize_with ( base_url) ) ;
93
+ }
90
94
}
91
95
if tsconfig. compiler_options . paths . is_some ( ) {
92
96
tsconfig. compiler_options . paths_base =
@@ -106,6 +110,16 @@ impl TsConfig {
106
110
}
107
111
}
108
112
}
113
+
114
+ let mut p = self . compiler_options . paths_base . to_string_lossy ( ) . to_string ( ) ;
115
+ Self :: substitute_template_variable ( & dir, & mut p) ;
116
+ self . compiler_options . paths_base = p. into ( ) ;
117
+
118
+ if let Some ( base_url) = self . compiler_options . base_url . as_mut ( ) {
119
+ let mut p = base_url. to_string_lossy ( ) . to_string ( ) ;
120
+ Self :: substitute_template_variable ( & dir, & mut p) ;
121
+ * base_url = p. into ( ) ;
122
+ }
109
123
}
110
124
self
111
125
}
@@ -221,9 +235,12 @@ impl TsConfig {
221
235
///
222
236
/// See <https://github.com/microsoft/TypeScript/pull/58042>
223
237
fn substitute_template_variable ( directory : & Path , path : & mut String ) {
224
- const TEMPLATE_VARIABLE : & str = "${configDir}/" ;
225
238
if let Some ( stripped_path) = path. strip_prefix ( TEMPLATE_VARIABLE ) {
226
- * path = directory. join ( stripped_path) . to_string_lossy ( ) . to_string ( ) ;
239
+ if let Some ( unleashed_path) = stripped_path. strip_prefix ( "/" ) {
240
+ * path = directory. join ( unleashed_path) . to_string_lossy ( ) . to_string ( ) ;
241
+ } else {
242
+ * path = directory. join ( stripped_path) . to_string_lossy ( ) . to_string ( ) ;
243
+ }
227
244
}
228
245
}
229
246
}
0 commit comments