|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | +import { JsonAstObject, JsonParseMode, parseJsonAst } from '@angular-devkit/core'; |
| 9 | +import { Rule, Tree, UpdateRecorder } from '@angular-devkit/schematics'; |
| 10 | +import { |
| 11 | + findPropertyInAstObject, |
| 12 | + insertPropertyInAstObjectInOrder, |
| 13 | + removePropertyInAstObject, |
| 14 | +} from '../../utility/json-utils'; |
| 15 | +import { Builders } from '../../utility/workspace-models'; |
| 16 | +import { getAllOptions, getTargets, getWorkspace } from './utils'; |
| 17 | + |
| 18 | + |
| 19 | +/** |
| 20 | + * Update the tsconfig files for applications |
| 21 | + * - Removes enableIvy: true |
| 22 | + * - Sets stricter file inclusions |
| 23 | + */ |
| 24 | +export function updateApplicationTsConfigs(): Rule { |
| 25 | + return (tree: Tree) => { |
| 26 | + const workspace = getWorkspace(tree); |
| 27 | + |
| 28 | + for (const { target } of getTargets(workspace, 'build', Builders.Browser)) { |
| 29 | + updateTsConfig(tree, target, Builders.Browser); |
| 30 | + } |
| 31 | + |
| 32 | + for (const { target } of getTargets(workspace, 'server', Builders.Server)) { |
| 33 | + updateTsConfig(tree, target, Builders.Server); |
| 34 | + } |
| 35 | + |
| 36 | + for (const { target } of getTargets(workspace, 'test', Builders.Karma)) { |
| 37 | + updateTsConfig(tree, target, Builders.Karma); |
| 38 | + } |
| 39 | + |
| 40 | + return tree; |
| 41 | + }; |
| 42 | +} |
| 43 | + |
| 44 | +function updateTsConfig(tree: Tree, builderConfig: JsonAstObject, builderName: Builders) { |
| 45 | + const options = getAllOptions(builderConfig); |
| 46 | + for (const option of options) { |
| 47 | + let recorder: UpdateRecorder; |
| 48 | + const tsConfigOption = findPropertyInAstObject(option, 'tsConfig'); |
| 49 | + |
| 50 | + if (!tsConfigOption || tsConfigOption.kind !== 'string') { |
| 51 | + continue; |
| 52 | + } |
| 53 | + |
| 54 | + const tsConfigPath = tsConfigOption.value; |
| 55 | + let tsConfigAst = getTsConfigAst(tree, tsConfigPath); |
| 56 | + if (!tsConfigAst) { |
| 57 | + continue; |
| 58 | + } |
| 59 | + |
| 60 | + // Remove 'enableIvy: true' since this is the default in version 9. |
| 61 | + const angularCompilerOptions = findPropertyInAstObject(tsConfigAst, 'angularCompilerOptions'); |
| 62 | + if (angularCompilerOptions && angularCompilerOptions.kind === 'object') { |
| 63 | + const enableIvy = findPropertyInAstObject(angularCompilerOptions, 'enableIvy'); |
| 64 | + if (enableIvy && enableIvy.kind === 'true') { |
| 65 | + recorder = tree.beginUpdate(tsConfigPath); |
| 66 | + if (angularCompilerOptions.properties.length === 1) { |
| 67 | + // remove entire 'angularCompilerOptions' |
| 68 | + removePropertyInAstObject(recorder, tsConfigAst, 'angularCompilerOptions'); |
| 69 | + } else { |
| 70 | + removePropertyInAstObject(recorder, angularCompilerOptions, 'enableIvy'); |
| 71 | + } |
| 72 | + tree.commitUpdate(recorder); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // Add stricter file inclusions to avoid unused file warning during compilation |
| 77 | + if (builderName !== Builders.Karma) { |
| 78 | + // Note: we need to re-read the tsconfig after very commit because |
| 79 | + // otherwise the updates will be out of sync since we are ammending the same node. |
| 80 | + tsConfigAst = getTsConfigAst(tree, tsConfigPath) as JsonAstObject; |
| 81 | + const files = findPropertyInAstObject(tsConfigAst, 'files'); |
| 82 | + const include = findPropertyInAstObject(tsConfigAst, 'include'); |
| 83 | + |
| 84 | + if (!files && !include) { |
| 85 | + const rootInSrc = tsConfigPath.includes('src/'); |
| 86 | + const rootSrc = rootInSrc ? '' : 'src/'; |
| 87 | + const files = builderName === Builders.Server |
| 88 | + ? [`${rootSrc}main.server.ts`] |
| 89 | + : [`${rootSrc}main.ts`, `${rootSrc}polyfills.ts`]; |
| 90 | + |
| 91 | + recorder = tree.beginUpdate(tsConfigPath); |
| 92 | + insertPropertyInAstObjectInOrder(recorder, tsConfigAst, 'files', files, 2); |
| 93 | + tree.commitUpdate(recorder); |
| 94 | + |
| 95 | + if (builderName === Builders.Browser) { |
| 96 | + tsConfigAst = getTsConfigAst(tree, tsConfigPath) as JsonAstObject; |
| 97 | + recorder = tree.beginUpdate(tsConfigPath); |
| 98 | + insertPropertyInAstObjectInOrder(recorder, tsConfigAst, 'include', [`${rootSrc}**/*.d.ts`], 2); |
| 99 | + tree.commitUpdate(recorder); |
| 100 | + } |
| 101 | + |
| 102 | + tsConfigAst = getTsConfigAst(tree, tsConfigPath) as JsonAstObject; |
| 103 | + recorder = tree.beginUpdate(tsConfigPath); |
| 104 | + removePropertyInAstObject(recorder, tsConfigAst, 'exclude'); |
| 105 | + tree.commitUpdate(recorder); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +function getTsConfigAst(tree: Tree, path: string): JsonAstObject | undefined { |
| 112 | + const configBuffer = tree.read(path); |
| 113 | + if (!configBuffer) { |
| 114 | + return undefined; |
| 115 | + } |
| 116 | + |
| 117 | + const content = configBuffer.toString(); |
| 118 | + const tsConfigAst = parseJsonAst(content, JsonParseMode.Loose); |
| 119 | + if (!tsConfigAst || tsConfigAst.kind !== 'object') { |
| 120 | + return undefined; |
| 121 | + } |
| 122 | + |
| 123 | + return tsConfigAst; |
| 124 | +} |
0 commit comments