6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
import {
9
- JsonAstObject ,
10
9
JsonParseMode ,
11
10
isJsonObject ,
12
11
join ,
@@ -36,40 +35,10 @@ not dead
36
35
not IE 9-11 # For IE 9-11 support, remove 'not'.` ;
37
36
38
37
export function updateES5Projects ( ) : Rule {
39
- return ( host : Tree ) => {
40
- const tsConfigPath = '/tsconfig.json' ;
41
- const compilerOptions = getCompilerOptionsAstObject ( host , tsConfigPath ) ;
42
- if ( ! compilerOptions ) {
43
- return host ;
44
- }
45
-
46
- const recorder = host . beginUpdate ( tsConfigPath ) ;
47
- const scriptTarget = findPropertyInAstObject ( compilerOptions , 'target' ) ;
48
- if ( ! scriptTarget ) {
49
- insertPropertyInAstObjectInOrder ( recorder , compilerOptions , 'target' , 'es2015' , 4 ) ;
50
- } else if ( scriptTarget . value !== 'es2015' ) {
51
- const { start, end } = scriptTarget ;
52
- recorder . remove ( start . offset , end . offset - start . offset ) ;
53
- recorder . insertLeft ( start . offset , '"es2015"' ) ;
54
- }
55
-
56
- const scriptModule = findPropertyInAstObject ( compilerOptions , 'module' ) ;
57
- if ( ! scriptModule ) {
58
- insertPropertyInAstObjectInOrder ( recorder , compilerOptions , 'module' , 'esnext' , 4 ) ;
59
- } else if ( scriptModule . value !== 'esnext' ) {
60
- const { start, end } = scriptModule ;
61
- recorder . remove ( start . offset , end . offset - start . offset ) ;
62
- recorder . insertLeft ( start . offset , '"esnext"' ) ;
63
- }
64
-
65
- host . commitUpdate ( recorder ) ;
66
-
67
- return updateProjects ;
68
- } ;
69
- }
38
+ return ( tree : Tree ) => {
39
+ // update workspace tsconfig
40
+ updateTsConfig ( tree , '/tsconfig.json' ) ;
70
41
71
- function updateProjects ( ) : Rule {
72
- return ( tree ) => {
73
42
const angularConfigContent = tree . read ( 'angular.json' ) || tree . read ( '.angular.json' ) ;
74
43
75
44
if ( ! angularConfigContent ) {
@@ -106,28 +75,16 @@ function updateProjects(): Rule {
106
75
continue ;
107
76
}
108
77
109
- const tsConfigs = [ ] ;
110
78
const buildOptionsConfig = architect . build . options ;
111
79
if ( isJsonObject ( buildOptionsConfig ) && typeof buildOptionsConfig . tsConfig === 'string' ) {
112
- tsConfigs . push ( buildOptionsConfig . tsConfig ) ;
80
+ updateTsConfig ( tree , buildOptionsConfig . tsConfig ) ;
113
81
}
114
82
115
83
const testConfig = architect . test ;
116
84
if ( isJsonObject ( testConfig )
117
85
&& isJsonObject ( testConfig . options )
118
86
&& typeof testConfig . options . tsConfig === 'string' ) {
119
- tsConfigs . push ( testConfig . options . tsConfig ) ;
120
- }
121
-
122
- for ( const tsConfig of tsConfigs ) {
123
- const compilerOptions = getCompilerOptionsAstObject ( tree , tsConfig ) ;
124
- if ( ! compilerOptions ) {
125
- continue ;
126
- }
127
- const recorder = tree . beginUpdate ( tsConfig ) ;
128
- removePropertyInAstObject ( recorder , compilerOptions , 'target' ) ;
129
- removePropertyInAstObject ( recorder , compilerOptions , 'module' ) ;
130
- tree . commitUpdate ( recorder ) ;
87
+ updateTsConfig ( tree , testConfig . options . tsConfig ) ;
131
88
}
132
89
133
90
const browserslistPath = join ( normalize ( project . root ) , 'browserslist' ) ;
@@ -159,8 +116,8 @@ function updateProjects(): Rule {
159
116
} ;
160
117
}
161
118
162
- function getCompilerOptionsAstObject ( host : Tree , tsConfigPath : string ) : JsonAstObject | undefined {
163
- const buffer = host . read ( tsConfigPath ) ;
119
+ function updateTsConfig ( tree : Tree , tsConfigPath : string ) : void {
120
+ const buffer = tree . read ( tsConfigPath ) ;
164
121
if ( ! buffer ) {
165
122
return ;
166
123
}
@@ -171,10 +128,37 @@ function getCompilerOptionsAstObject(host: Tree, tsConfigPath: string): JsonAstO
171
128
return ;
172
129
}
173
130
131
+ const configExtends = findPropertyInAstObject ( tsCfgAst , 'extends' ) ;
132
+ const isExtendedConfig = configExtends && configExtends . kind === 'string' ;
133
+
174
134
const compilerOptions = findPropertyInAstObject ( tsCfgAst , 'compilerOptions' ) ;
175
135
if ( ! compilerOptions || compilerOptions . kind !== 'object' ) {
176
136
return ;
177
137
}
178
138
179
- return compilerOptions ;
139
+ const recorder = tree . beginUpdate ( tsConfigPath ) ;
140
+
141
+ if ( isExtendedConfig ) {
142
+ removePropertyInAstObject ( recorder , compilerOptions , 'target' ) ;
143
+ removePropertyInAstObject ( recorder , compilerOptions , 'module' ) ;
144
+ } else {
145
+ const scriptTarget = findPropertyInAstObject ( compilerOptions , 'target' ) ;
146
+ if ( ! scriptTarget ) {
147
+ insertPropertyInAstObjectInOrder ( recorder , compilerOptions , 'target' , 'es2015' , 4 ) ;
148
+ } else if ( scriptTarget . value !== 'es2015' ) {
149
+ const { start, end } = scriptTarget ;
150
+ recorder . remove ( start . offset , end . offset - start . offset ) ;
151
+ recorder . insertLeft ( start . offset , '"es2015"' ) ;
152
+ }
153
+ const scriptModule = findPropertyInAstObject ( compilerOptions , 'module' ) ;
154
+ if ( ! scriptModule ) {
155
+ insertPropertyInAstObjectInOrder ( recorder , compilerOptions , 'module' , 'esnext' , 4 ) ;
156
+ } else if ( scriptModule . value !== 'esnext' ) {
157
+ const { start, end } = scriptModule ;
158
+ recorder . remove ( start . offset , end . offset - start . offset ) ;
159
+ recorder . insertLeft ( start . offset , '"esnext"' ) ;
160
+ }
161
+ }
162
+
163
+ tree . commitUpdate ( recorder ) ;
180
164
}
0 commit comments