1+ use indexmap:: IndexMap ;
2+ use rustc_hash:: FxHasher ;
3+ use serde:: Deserialize ;
14use std:: {
25 hash:: BuildHasherDefault ,
36 path:: { Path , PathBuf } ,
47 sync:: Arc ,
58} ;
69
7- use indexmap:: IndexMap ;
8- use rustc_hash:: FxHasher ;
9- use serde:: Deserialize ;
10-
1110use crate :: PathUtil ;
1211
1312pub type CompilerOptionsPathsMap = IndexMap < String , Vec < String > , BuildHasherDefault < FxHasher > > ;
@@ -19,6 +18,8 @@ pub enum ExtendsField {
1918 Multiple ( Vec < String > ) ,
2019}
2120
21+ const TEMPLATE_VARIABLE : & str = "${configDir}" ;
22+
2223#[ derive( Debug , Deserialize ) ]
2324#[ serde( rename_all = "camelCase" ) ]
2425pub struct TsConfig {
@@ -85,8 +86,11 @@ impl TsConfig {
8586 tsconfig. root = root;
8687 tsconfig. path = path. to_path_buf ( ) ;
8788 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+ }
9094 }
9195 if tsconfig. compiler_options . paths . is_some ( ) {
9296 tsconfig. compiler_options . paths_base =
@@ -106,6 +110,16 @@ impl TsConfig {
106110 }
107111 }
108112 }
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+ }
109123 }
110124 self
111125 }
@@ -221,9 +235,12 @@ impl TsConfig {
221235 ///
222236 /// See <https://github.com/microsoft/TypeScript/pull/58042>
223237 fn substitute_template_variable ( directory : & Path , path : & mut String ) {
224- const TEMPLATE_VARIABLE : & str = "${configDir}/" ;
225238 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+ }
227244 }
228245 }
229246}
0 commit comments