@@ -20,21 +20,26 @@ import { getAllOptions, getTargets, getWorkspace, readJsonFileAsAstObject } from
2020 * Update the tsconfig files for applications
2121 * - Removes enableIvy: true
2222 * - Sets stricter file inclusions
23+ * - Sets module compiler option to esnext or commonjs
2324 */
2425export function updateApplicationTsConfigs ( ) : Rule {
2526 return ( tree , context ) => {
2627 const workspace = getWorkspace ( tree ) ;
28+ const logger = context . logger ;
29+
30+ // Add `module` option in the workspace tsconfig
31+ updateModuleCompilerOption ( tree , '/tsconfig.json' ) ;
2732
2833 for ( const { target } of getTargets ( workspace , 'build' , Builders . Browser ) ) {
29- updateTsConfig ( tree , target , Builders . Browser , context . logger ) ;
34+ updateTsConfig ( tree , target , Builders . Browser , logger ) ;
3035 }
3136
3237 for ( const { target } of getTargets ( workspace , 'server' , Builders . Server ) ) {
33- updateTsConfig ( tree , target , Builders . Server , context . logger ) ;
38+ updateTsConfig ( tree , target , Builders . Server , logger ) ;
3439 }
3540
3641 for ( const { target } of getTargets ( workspace , 'test' , Builders . Karma ) ) {
37- updateTsConfig ( tree , target , Builders . Karma , context . logger ) ;
42+ updateTsConfig ( tree , target , Builders . Karma , logger ) ;
3843 }
3944
4045 return tree ;
@@ -74,14 +79,17 @@ function updateTsConfig(tree: Tree, builderConfig: JsonAstObject, builderName: B
7479 }
7580 }
7681
82+ // Update 'module' compilerOption
83+ updateModuleCompilerOption ( tree , tsConfigPath , builderName ) ;
84+
7785 // Add stricter file inclusions to avoid unused file warning during compilation
7886 if ( builderName !== Builders . Karma ) {
7987 // Note: we need to re-read the tsconfig after very commit because
8088 // otherwise the updates will be out of sync since we are ammending the same node.
8189
8290 // we are already checking that tsconfig exists above!
8391 // tslint:disable-next-line: no-non-null-assertion
84- tsConfigAst = readJsonFileAsAstObject ( tree , tsConfigPath ) ! ;
92+ tsConfigAst = readJsonFileAsAstObject ( tree , tsConfigPath ) ! ;
8593 const include = findPropertyInAstObject ( tsConfigAst , 'include' ) ;
8694
8795 if ( include && include . kind === 'array' ) {
@@ -113,17 +121,51 @@ function updateTsConfig(tree: Tree, builderConfig: JsonAstObject, builderName: B
113121 if ( newFiles . length ) {
114122 recorder = tree . beginUpdate ( tsConfigPath ) ;
115123 // tslint:disable-next-line: no-non-null-assertion
116- tsConfigAst = readJsonFileAsAstObject ( tree , tsConfigPath ) ! ;
124+ tsConfigAst = readJsonFileAsAstObject ( tree , tsConfigPath ) ! ;
117125 insertPropertyInAstObjectInOrder ( recorder , tsConfigAst , 'files' , newFiles , 2 ) ;
118126 tree . commitUpdate ( recorder ) ;
119127 }
120128
121129 recorder = tree . beginUpdate ( tsConfigPath ) ;
122130 // tslint:disable-next-line: no-non-null-assertion
123- tsConfigAst = readJsonFileAsAstObject ( tree , tsConfigPath ) ! ;
131+ tsConfigAst = readJsonFileAsAstObject ( tree , tsConfigPath ) ! ;
124132 removePropertyInAstObject ( recorder , tsConfigAst , 'exclude' ) ;
125133 tree . commitUpdate ( recorder ) ;
126134 }
127135 }
128136 }
129137}
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