@@ -20,21 +20,26 @@ import { getAllOptions, getTargets, getWorkspace, readJsonFileAsAstObject } from
20
20
* Update the tsconfig files for applications
21
21
* - Removes enableIvy: true
22
22
* - Sets stricter file inclusions
23
+ * - Sets module compiler option to esnext or commonjs
23
24
*/
24
25
export function updateApplicationTsConfigs ( ) : Rule {
25
26
return ( tree , context ) => {
26
27
const workspace = getWorkspace ( tree ) ;
28
+ const logger = context . logger ;
29
+
30
+ // Add `module` option in the workspace tsconfig
31
+ updateModuleCompilerOption ( tree , '/tsconfig.json' ) ;
27
32
28
33
for ( const { target } of getTargets ( workspace , 'build' , Builders . Browser ) ) {
29
- updateTsConfig ( tree , target , Builders . Browser , context . logger ) ;
34
+ updateTsConfig ( tree , target , Builders . Browser , logger ) ;
30
35
}
31
36
32
37
for ( const { target } of getTargets ( workspace , 'server' , Builders . Server ) ) {
33
- updateTsConfig ( tree , target , Builders . Server , context . logger ) ;
38
+ updateTsConfig ( tree , target , Builders . Server , logger ) ;
34
39
}
35
40
36
41
for ( const { target } of getTargets ( workspace , 'test' , Builders . Karma ) ) {
37
- updateTsConfig ( tree , target , Builders . Karma , context . logger ) ;
42
+ updateTsConfig ( tree , target , Builders . Karma , logger ) ;
38
43
}
39
44
40
45
return tree ;
@@ -74,14 +79,17 @@ function updateTsConfig(tree: Tree, builderConfig: JsonAstObject, builderName: B
74
79
}
75
80
}
76
81
82
+ // Update 'module' compilerOption
83
+ updateModuleCompilerOption ( tree , tsConfigPath , builderName ) ;
84
+
77
85
// Add stricter file inclusions to avoid unused file warning during compilation
78
86
if ( builderName !== Builders . Karma ) {
79
87
// Note: we need to re-read the tsconfig after very commit because
80
88
// otherwise the updates will be out of sync since we are ammending the same node.
81
89
82
90
// we are already checking that tsconfig exists above!
83
91
// tslint:disable-next-line: no-non-null-assertion
84
- tsConfigAst = readJsonFileAsAstObject ( tree , tsConfigPath ) ! ;
92
+ tsConfigAst = readJsonFileAsAstObject ( tree , tsConfigPath ) ! ;
85
93
const include = findPropertyInAstObject ( tsConfigAst , 'include' ) ;
86
94
87
95
if ( include && include . kind === 'array' ) {
@@ -113,17 +121,51 @@ function updateTsConfig(tree: Tree, builderConfig: JsonAstObject, builderName: B
113
121
if ( newFiles . length ) {
114
122
recorder = tree . beginUpdate ( tsConfigPath ) ;
115
123
// tslint:disable-next-line: no-non-null-assertion
116
- tsConfigAst = readJsonFileAsAstObject ( tree , tsConfigPath ) ! ;
124
+ tsConfigAst = readJsonFileAsAstObject ( tree , tsConfigPath ) ! ;
117
125
insertPropertyInAstObjectInOrder ( recorder , tsConfigAst , 'files' , newFiles , 2 ) ;
118
126
tree . commitUpdate ( recorder ) ;
119
127
}
120
128
121
129
recorder = tree . beginUpdate ( tsConfigPath ) ;
122
130
// tslint:disable-next-line: no-non-null-assertion
123
- tsConfigAst = readJsonFileAsAstObject ( tree , tsConfigPath ) ! ;
131
+ tsConfigAst = readJsonFileAsAstObject ( tree , tsConfigPath ) ! ;
124
132
removePropertyInAstObject ( recorder , tsConfigAst , 'exclude' ) ;
125
133
tree . commitUpdate ( recorder ) ;
126
134
}
127
135
}
128
136
}
129
137
}
138
+
139
+ function updateModuleCompilerOption ( tree : Tree , tsConfigPath : string , builderName ?: Builders ) {
140
+ const tsConfigAst = readJsonFileAsAstObject ( tree , tsConfigPath ) ;
141
+
142
+ if ( ! tsConfigAst ) {
143
+ return ;
144
+ }
145
+
146
+ const compilerOptions = findPropertyInAstObject ( tsConfigAst , 'compilerOptions' ) ;
147
+ if ( ! compilerOptions || compilerOptions . kind !== 'object' ) {
148
+ return ;
149
+ }
150
+
151
+ const configExtends = findPropertyInAstObject ( tsConfigAst , 'extends' ) ;
152
+ const isExtendedConfig = configExtends && configExtends . kind === 'string' ;
153
+ const recorder = tree . beginUpdate ( tsConfigPath ) ;
154
+
155
+ // Server tsconfig should have a module of commonjs
156
+ const moduleType = builderName === Builders . Server ? 'commonjs' : 'esnext' ;
157
+ if ( isExtendedConfig && builderName !== Builders . Server ) {
158
+ removePropertyInAstObject ( recorder , compilerOptions , 'module' ) ;
159
+ } else {
160
+ const scriptModule = findPropertyInAstObject ( compilerOptions , 'module' ) ;
161
+ if ( ! scriptModule ) {
162
+ insertPropertyInAstObjectInOrder ( recorder , compilerOptions , 'module' , moduleType , 4 ) ;
163
+ } else if ( scriptModule . value !== moduleType ) {
164
+ const { start, end } = scriptModule ;
165
+ recorder . remove ( start . offset , end . offset - start . offset ) ;
166
+ recorder . insertLeft ( start . offset , `"${ moduleType } "` ) ;
167
+ }
168
+ }
169
+
170
+ tree . commitUpdate ( recorder ) ;
171
+ }
0 commit comments