@@ -32,7 +32,6 @@ import {
32
32
addPackageJsonDependency ,
33
33
} from '../../utility/dependencies' ;
34
34
import {
35
- appendPropertyInAstObject ,
36
35
appendValueInAstArray ,
37
36
findPropertyInAstObject ,
38
37
} from '../../utility/json-utils' ;
@@ -734,31 +733,47 @@ function updateRootTsConfig(): Rule {
734
733
}
735
734
736
735
const tsCfgAst = parseJsonAst ( buffer . toString ( ) , JsonParseMode . Loose ) ;
737
- if ( tsCfgAst . kind != 'object' ) {
738
- throw new SchematicsException (
739
- 'Invalid tsconfig. Was expecting an object'
740
- ) ;
736
+ if ( tsCfgAst . kind !== 'object' ) {
737
+ throw new SchematicsException ( 'Invalid root tsconfig. Was expecting an object' ) ;
741
738
}
742
739
743
740
const compilerOptionsAstNode = findPropertyInAstObject ( tsCfgAst , 'compilerOptions' ) ;
744
741
if ( ! compilerOptionsAstNode || compilerOptionsAstNode . kind != 'object' ) {
745
- throw new SchematicsException ( 'Invalid tsconfig "compilerOptions" property; expected an object.' ) ;
742
+ throw new SchematicsException (
743
+ 'Invalid root tsconfig "compilerOptions" property; expected an object.' ,
744
+ ) ;
746
745
}
747
746
748
- if ( findPropertyInAstObject ( compilerOptionsAstNode , 'baseUrl' ) ) {
747
+ if (
748
+ findPropertyInAstObject ( compilerOptionsAstNode , 'baseUrl' ) &&
749
+ findPropertyInAstObject ( compilerOptionsAstNode , 'module' )
750
+ ) {
749
751
return host ;
750
752
}
751
753
752
- const recorder = host . beginUpdate ( tsConfigPath ) ;
753
- appendPropertyInAstObject (
754
- recorder ,
755
- compilerOptionsAstNode ,
756
- 'baseUrl' ,
757
- './' ,
758
- 4 ,
759
- ) ;
754
+ const compilerOptions = compilerOptionsAstNode . value ;
755
+ const { baseUrl = './' , module = 'es2015' } = compilerOptions ;
756
+
757
+ const validBaseUrl = [ './' , '' , '.' ] ;
758
+ if ( ! validBaseUrl . includes ( baseUrl as string ) ) {
759
+ const formattedBaseUrl = validBaseUrl . map ( x => `'${ x } '` ) . join ( ', ' ) ;
760
+ context . logger . warn ( tags . oneLine
761
+ `Root tsconfig option 'baseUrl' is not one of: ${ formattedBaseUrl } .
762
+ This might cause unexpected behaviour when generating libraries.` ,
763
+ ) ;
764
+ }
765
+
766
+ if ( module !== 'es2015' ) {
767
+ context . logger . warn (
768
+ `Root tsconfig option 'module' is not 'es2015'. This might cause unexpected behaviour.` ,
769
+ ) ;
770
+ }
771
+
772
+ compilerOptions . module = module ;
773
+ compilerOptions . baseUrl = baseUrl ;
774
+
775
+ host . overwrite ( tsConfigPath , JSON . stringify ( tsCfgAst . value , null , 2 ) ) ;
760
776
761
- host . commitUpdate ( recorder ) ;
762
777
return host ;
763
778
} ;
764
779
}
0 commit comments