@@ -5,7 +5,7 @@ use std::{
55
66use crate :: PathUtil ;
77use serde:: Deserialize ;
8- use typescript_tsconfig_json:: { CompilerOptions , TsConfigJson } ;
8+ use typescript_tsconfig_json:: { CompilerOptionsPathsMap , ExtendsField } ;
99
1010#[ derive( Debug , Deserialize ) ]
1111#[ serde( rename_all = "camelCase" ) ]
@@ -14,19 +14,35 @@ pub struct TsConfig {
1414 #[ serde( skip) ]
1515 path : PathBuf ,
1616
17- // Base url for compiler option paths.
18- #[ serde( skip) ]
19- paths_base : PathBuf ,
17+ #[ serde( default ) ]
18+ pub extends : Option < ExtendsField > ,
2019
21- /// The deserialized `tsconfig.json`.
22- pub data : TsConfigJson ,
20+ # [ serde ( default ) ]
21+ pub compiler_options : CompilerOptions ,
2322
2423 /// Bubbled up project references with a reference to their tsconfig.
2524 #[ serde( default ) ]
2625 pub references : Vec < ProjectReference > ,
2726}
2827
28+ /// Compiler Options
29+ ///
30+ /// <https://www.typescriptlang.org/tsconfig#compilerOptions>
31+ #[ derive( Debug , Default , Deserialize ) ]
32+ #[ serde( rename_all = "camelCase" ) ]
33+ pub struct CompilerOptions {
34+ base_url : Option < PathBuf > ,
35+
36+ /// Path aliases
37+ paths : Option < CompilerOptionsPathsMap > ,
38+
39+ /// The actual base for where path aliases are resolved from.
40+ #[ serde( skip) ]
41+ paths_base : PathBuf ,
42+ }
43+
2944/// Project Reference
45+ ///
3046/// <https://www.typescriptlang.org/docs/handbook/project-references.html>
3147#[ derive( Debug , Deserialize ) ]
3248pub struct ProjectReference {
@@ -42,32 +58,16 @@ pub struct ProjectReference {
4258impl TsConfig {
4359 pub fn parse ( path : & Path , json : & mut str ) -> Result < Self , serde_json:: Error > {
4460 _ = json_strip_comments:: strip ( json) ;
45-
46- let data: TsConfigJson = serde_json:: from_str ( json) ?;
47-
48- let mut tsconfig = Self {
49- path : path. to_path_buf ( ) ,
50- paths_base : PathBuf :: new ( ) ,
51- references : data. references . as_ref ( ) . map_or_else ( Vec :: new, |refs| {
52- refs. iter ( )
53- . map ( |pr| ProjectReference { path : pr. path . clone ( ) , tsconfig : None } )
54- . collect ( )
55- } ) ,
56- data,
57- } ;
61+ let mut tsconfig: Self = serde_json:: from_str ( json) ?;
62+ tsconfig. path = path. to_path_buf ( ) ;
5863 let directory = tsconfig. directory ( ) . to_path_buf ( ) ;
59-
60- if let Some ( compiler_options) = & mut tsconfig. data . compiler_options {
61- if let Some ( base_url) = & compiler_options. base_url {
62- compiler_options. base_url = Some ( directory. normalize_with ( base_url) ) ;
63- }
64-
65- if compiler_options. paths . is_some ( ) {
66- tsconfig. paths_base =
67- compiler_options. base_url . as_ref ( ) . map_or ( directory, Clone :: clone) ;
68- }
64+ if let Some ( base_url) = tsconfig. compiler_options . base_url {
65+ tsconfig. compiler_options . base_url = Some ( directory. normalize_with ( base_url) ) ;
66+ }
67+ if tsconfig. compiler_options . paths . is_some ( ) {
68+ tsconfig. compiler_options . paths_base =
69+ tsconfig. compiler_options . base_url . as_ref ( ) . map_or ( directory, Clone :: clone) ;
6970 }
70-
7171 Ok ( tsconfig)
7272 }
7373
@@ -82,29 +82,23 @@ impl TsConfig {
8282 }
8383
8484 fn base_path ( & self ) -> & Path {
85- self . data
86- . compiler_options
85+ self . compiler_options
86+ . base_url
8787 . as_ref ( )
88- . and_then ( |opt| opt. base_url . as_ref ( ) )
8988 . map_or_else ( || self . directory ( ) , |path| path. as_ref ( ) )
9089 }
9190
9291 pub fn extend_tsconfig ( & mut self , tsconfig : & Self ) {
93- if let Some ( their_options) = & tsconfig. data . compiler_options {
94- let my_options = self . data . compiler_options . get_or_insert ( CompilerOptions :: default ( ) ) ;
95-
96- if my_options. paths . is_none ( ) {
97- self . paths_base = my_options
98- . base_url
99- . as_ref ( )
100- . map_or_else ( || tsconfig. paths_base . clone ( ) , Clone :: clone) ;
101-
102- my_options. paths = their_options. paths . clone ( ) ;
103- }
104-
105- if my_options. base_url . is_none ( ) {
106- my_options. base_url = their_options. base_url . clone ( ) ;
107- }
92+ let compiler_options = & mut self . compiler_options ;
93+ if compiler_options. paths . is_none ( ) {
94+ compiler_options. paths_base = compiler_options
95+ . base_url
96+ . as_ref ( )
97+ . map_or_else ( || tsconfig. compiler_options . paths_base . clone ( ) , Clone :: clone) ;
98+ compiler_options. paths = tsconfig. compiler_options . paths . clone ( ) ;
99+ }
100+ if compiler_options. base_url . is_none ( ) {
101+ compiler_options. base_url = tsconfig. compiler_options . base_url . clone ( ) ;
108102 }
109103 }
110104
@@ -131,15 +125,12 @@ impl TsConfig {
131125 }
132126
133127 let base_url_iter = self
134- . data
135128 . compiler_options
129+ . base_url
136130 . as_ref ( )
137- . and_then ( |opt| opt. base_url . as_ref ( ) )
138131 . map_or_else ( Vec :: new, |base_url| vec ! [ base_url. normalize_with( specifier) ] ) ;
139132
140- let Some ( paths_map) =
141- self . data . compiler_options . as_ref ( ) . and_then ( |opt| opt. paths . as_ref ( ) )
142- else {
133+ let Some ( paths_map) = & self . compiler_options . paths else {
143134 return base_url_iter;
144135 } ;
145136
@@ -178,6 +169,10 @@ impl TsConfig {
178169 Clone :: clone,
179170 ) ;
180171
181- paths. into_iter ( ) . map ( |p| self . paths_base . normalize_with ( p) ) . chain ( base_url_iter) . collect ( )
172+ paths
173+ . into_iter ( )
174+ . map ( |p| self . compiler_options . paths_base . normalize_with ( p) )
175+ . chain ( base_url_iter)
176+ . collect ( )
182177 }
183178}
0 commit comments