|
7 | 7 | */
|
8 | 8 | import { JsonAstObject } from '@angular-devkit/core';
|
9 | 9 | import { Rule, Tree, UpdateRecorder } from '@angular-devkit/schematics';
|
| 10 | +import { posix } from 'path'; |
10 | 11 | import {
|
11 | 12 | findPropertyInAstObject,
|
12 | 13 | insertPropertyInAstObjectInOrder,
|
@@ -74,38 +75,47 @@ function updateTsConfig(tree: Tree, builderConfig: JsonAstObject, builderName: B
|
74 | 75 | }
|
75 | 76 |
|
76 | 77 | // Add stricter file inclusions to avoid unused file warning during compilation
|
77 |
| - const rootInSrc = tsConfigPath.includes('src/'); |
78 |
| - const rootSrc = rootInSrc ? '' : 'src/'; |
79 | 78 | if (builderName !== Builders.Karma) {
|
80 | 79 | // Note: we need to re-read the tsconfig after very commit because
|
81 | 80 | // otherwise the updates will be out of sync since we are ammending the same node.
|
82 | 81 | tsConfigAst = readJsonFileAsAstObject(tree, tsConfigPath);
|
83 |
| - const files = findPropertyInAstObject(tsConfigAst, 'files'); |
84 | 82 | const include = findPropertyInAstObject(tsConfigAst, 'include');
|
85 | 83 |
|
86 |
| - recorder = tree.beginUpdate(tsConfigPath); |
87 | 84 | if (include && include.kind === 'array') {
|
88 | 85 | const tsInclude = include.elements.find(({ value }) => typeof value === 'string' && value.endsWith('**/*.ts'));
|
89 | 86 | if (tsInclude) {
|
90 | 87 | const { start, end } = tsInclude;
|
| 88 | + recorder = tree.beginUpdate(tsConfigPath); |
91 | 89 | recorder.remove(start.offset, end.offset - start.offset);
|
92 | 90 | // Replace ts includes with d.ts
|
93 | 91 | recorder.insertLeft(start.offset, tsInclude.text.replace('.ts', '.d.ts'));
|
| 92 | + tree.commitUpdate(recorder); |
94 | 93 | }
|
95 |
| - } else { |
96 |
| - insertPropertyInAstObjectInOrder(recorder, tsConfigAst, 'include', [`${rootSrc}**/*.d.ts`], 2); |
97 | 94 | }
|
98 | 95 |
|
99 |
| - tree.commitUpdate(recorder); |
100 |
| - |
| 96 | + const files = findPropertyInAstObject(tsConfigAst, 'files'); |
101 | 97 | if (!files) {
|
102 |
| - tsConfigAst = readJsonFileAsAstObject(tree, tsConfigPath); |
103 |
| - recorder = tree.beginUpdate(tsConfigPath); |
| 98 | + const newFiles: string[] = []; |
| 99 | + |
| 100 | + const mainOption = findPropertyInAstObject(option, 'main'); |
| 101 | + if (mainOption && mainOption.kind === 'string') { |
| 102 | + newFiles.push(posix.relative(posix.dirname(tsConfigPath), mainOption.value)); |
| 103 | + } |
104 | 104 |
|
105 |
| - const files = builderName === Builders.Server |
106 |
| - ? [`${rootSrc}main.server.ts`] |
107 |
| - : [`${rootSrc}main.ts`, `${rootSrc}polyfills.ts`]; |
108 |
| - insertPropertyInAstObjectInOrder(recorder, tsConfigAst, 'files', files, 2); |
| 105 | + const polyfillsOption = findPropertyInAstObject(option, 'polyfills'); |
| 106 | + if (polyfillsOption && polyfillsOption.kind === 'string') { |
| 107 | + newFiles.push(posix.relative(posix.dirname(tsConfigPath), polyfillsOption.value)); |
| 108 | + } |
| 109 | + |
| 110 | + if (newFiles.length) { |
| 111 | + recorder = tree.beginUpdate(tsConfigPath); |
| 112 | + tsConfigAst = readJsonFileAsAstObject(tree, tsConfigPath); |
| 113 | + insertPropertyInAstObjectInOrder(recorder, tsConfigAst, 'files', newFiles, 2); |
| 114 | + tree.commitUpdate(recorder); |
| 115 | + } |
| 116 | + |
| 117 | + recorder = tree.beginUpdate(tsConfigPath); |
| 118 | + tsConfigAst = readJsonFileAsAstObject(tree, tsConfigPath); |
109 | 119 | removePropertyInAstObject(recorder, tsConfigAst, 'exclude');
|
110 | 120 | tree.commitUpdate(recorder);
|
111 | 121 | }
|
|
0 commit comments