66 * found in the LICENSE file at https://angular.io/license
77 */
88import {
9+ JsonAstObject ,
910 JsonParseMode ,
1011 isJsonObject ,
1112 join ,
@@ -17,6 +18,7 @@ import { Rule, Tree } from '@angular-devkit/schematics';
1718import {
1819 findPropertyInAstObject ,
1920 insertPropertyInAstObjectInOrder ,
21+ removePropertyInAstObject ,
2022} from '../../utility/json-utils' ;
2123
2224// tslint:disable-next-line:max-line-length
@@ -40,24 +42,12 @@ not IE 9-11 # For IE 9-11 support, remove 'not'.`;
4042export function updateES5Projects ( ) : Rule {
4143 return ( host : Tree ) => {
4244 const tsConfigPath = '/tsconfig.json' ;
43- const buffer = host . read ( tsConfigPath ) ;
44- if ( ! buffer ) {
45- return host ;
46- }
47-
48- const tsCfgAst = parseJsonAst ( buffer . toString ( ) , JsonParseMode . Loose ) ;
49-
50- if ( tsCfgAst . kind !== 'object' ) {
51- return host ;
52- }
53-
54- const compilerOptions = findPropertyInAstObject ( tsCfgAst , 'compilerOptions' ) ;
55- if ( ! compilerOptions || compilerOptions . kind !== 'object' ) {
45+ const compilerOptions = getCompilerOptionsAstObject ( host , tsConfigPath ) ;
46+ if ( ! compilerOptions ) {
5647 return host ;
5748 }
5849
5950 const recorder = host . beginUpdate ( tsConfigPath ) ;
60-
6151 const scriptTarget = findPropertyInAstObject ( compilerOptions , 'target' ) ;
6252 if ( ! scriptTarget ) {
6353 insertPropertyInAstObjectInOrder ( recorder , compilerOptions , 'target' , 'es2015' , 4 ) ;
@@ -78,11 +68,11 @@ export function updateES5Projects(): Rule {
7868
7969 host . commitUpdate ( recorder ) ;
8070
81- return updateBrowserlist ;
71+ return updateProjects ;
8272 } ;
8373}
8474
85- function updateBrowserlist ( ) : Rule {
75+ function updateProjects ( ) : Rule {
8676 return ( tree ) => {
8777 const angularConfigContent = tree . read ( 'angular.json' ) || tree . read ( '.angular.json' ) ;
8878
@@ -110,6 +100,34 @@ function updateBrowserlist(): Rule {
110100 continue ;
111101 }
112102
103+ // Older projects app and spec ts configs had script and module set in them.
104+ const tsConfigs = [ ] ;
105+ const architect = project . architect ;
106+ if ( isJsonObject ( architect )
107+ && isJsonObject ( architect . build )
108+ && isJsonObject ( architect . build . options )
109+ && typeof architect . build . options . tsConfig === 'string' ) {
110+ tsConfigs . push ( architect . build . options . tsConfig ) ;
111+ }
112+
113+ if ( isJsonObject ( architect )
114+ && isJsonObject ( architect . test )
115+ && isJsonObject ( architect . test . options )
116+ && typeof architect . test . options . tsConfig === 'string' ) {
117+ tsConfigs . push ( architect . test . options . tsConfig ) ;
118+ }
119+
120+ for ( const tsConfig of tsConfigs ) {
121+ const compilerOptions = getCompilerOptionsAstObject ( tree , tsConfig ) ;
122+ if ( ! compilerOptions ) {
123+ continue ;
124+ }
125+ const recorder = tree . beginUpdate ( tsConfig ) ;
126+ removePropertyInAstObject ( recorder , compilerOptions , 'target' ) ;
127+ removePropertyInAstObject ( recorder , compilerOptions , 'module' ) ;
128+ tree . commitUpdate ( recorder ) ;
129+ }
130+
113131 const browserslistPath = join ( normalize ( project . root ) , 'browserslist' ) ;
114132 if ( typeof project . sourceRoot === 'string' ) {
115133 // Move the CLI 7 style browserlist to root if it's there.
@@ -143,3 +161,23 @@ function updateBrowserlist(): Rule {
143161 return tree ;
144162 } ;
145163}
164+
165+ function getCompilerOptionsAstObject ( host : Tree , tsConfigPath : string ) : JsonAstObject | undefined {
166+ const buffer = host . read ( tsConfigPath ) ;
167+ if ( ! buffer ) {
168+ return ;
169+ }
170+
171+ const tsCfgAst = parseJsonAst ( buffer . toString ( ) , JsonParseMode . Loose ) ;
172+
173+ if ( tsCfgAst . kind !== 'object' ) {
174+ return ;
175+ }
176+
177+ const compilerOptions = findPropertyInAstObject ( tsCfgAst , 'compilerOptions' ) ;
178+ if ( ! compilerOptions || compilerOptions . kind !== 'object' ) {
179+ return ;
180+ }
181+
182+ return compilerOptions ;
183+ }
0 commit comments