Skip to content

Commit afae476

Browse files
alan-agius4vikerman
authored andcommitted
fix(@schematics/angular): replace '**/*.ts file inclusion with **/*.d.ts
This fixes warnings such as the below; ``` WARNING in /test-update/src/environments/environment.prod.ts is part of the TypeScript compilation but it's unused. Add only entry points to the 'files' or 'include' properties in your tsconfig. ``` When the previous generated add was in VE.
1 parent 98bc458 commit afae476

File tree

2 files changed

+54
-18
lines changed

2 files changed

+54
-18
lines changed

packages/schematics/angular/migrations/update-9/update-app-tsconfigs.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,33 +74,38 @@ function updateTsConfig(tree: Tree, builderConfig: JsonAstObject, builderName: B
7474
}
7575

7676
// Add stricter file inclusions to avoid unused file warning during compilation
77+
const rootInSrc = tsConfigPath.includes('src/');
78+
const rootSrc = rootInSrc ? '' : 'src/';
7779
if (builderName !== Builders.Karma) {
7880
// Note: we need to re-read the tsconfig after very commit because
7981
// otherwise the updates will be out of sync since we are ammending the same node.
8082
tsConfigAst = readJsonFileAsAstObject(tree, tsConfigPath);
8183
const files = findPropertyInAstObject(tsConfigAst, 'files');
8284
const include = findPropertyInAstObject(tsConfigAst, 'include');
8385

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 = readJsonFileAsAstObject(tree, tsConfigPath);
97-
recorder = tree.beginUpdate(tsConfigPath);
98-
insertPropertyInAstObjectInOrder(recorder, tsConfigAst, 'include', [`${rootSrc}**/*.d.ts`], 2);
99-
tree.commitUpdate(recorder);
86+
recorder = tree.beginUpdate(tsConfigPath);
87+
if (include && include.kind === 'array') {
88+
const tsInclude = include.elements.find(({ value }) => typeof value === 'string' && value.endsWith('**/*.ts'));
89+
if (tsInclude) {
90+
const { start, end } = tsInclude;
91+
recorder.remove(start.offset, end.offset - start.offset);
92+
// Replace ts includes with d.ts
93+
recorder.insertLeft(start.offset, tsInclude.text.replace('.ts', '.d.ts'));
10094
}
95+
} else {
96+
insertPropertyInAstObjectInOrder(recorder, tsConfigAst, 'include', [`${rootSrc}**/*.d.ts`], 2);
97+
}
98+
99+
tree.commitUpdate(recorder);
101100

101+
if (!files) {
102102
tsConfigAst = readJsonFileAsAstObject(tree, tsConfigPath);
103103
recorder = tree.beginUpdate(tsConfigPath);
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);
104109
removePropertyInAstObject(recorder, tsConfigAst, 'exclude');
105110
tree.commitUpdate(recorder);
106111
}

packages/schematics/angular/migrations/update-9/update-app-tsconfigs_spec.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe('Migration to version 9', () => {
6464
expect(include).toEqual(['src/**/*.d.ts']);
6565
});
6666

67-
it('should not update apps tsConfig when tsconfig has include', async () => {
67+
it('should update apps tsConfig when tsconfig has include', async () => {
6868
const tsConfigContent = {
6969
...defaultTsConfigOptions,
7070
include: ['foo.ts'],
@@ -74,10 +74,41 @@ describe('Migration to version 9', () => {
7474

7575
const tree2 = await schematicRunner.runSchematicAsync('migration-09', {}, tree.branch()).toPromise();
7676
const { files, include } = JSON.parse(tree2.readContent('tsconfig.app.json'));
77-
expect(files).toEqual(undefined);
77+
expect(files).toEqual(['src/main.ts', 'src/polyfills.ts']);
7878
expect(include).toEqual(['foo.ts']);
7979
});
8080

81+
it(`should update include '**/*.ts' in apps tsConfig to '**/*.d.ts'`, async () => {
82+
const tsConfigContent = {
83+
...defaultTsConfigOptions,
84+
include: ['src/**/*.ts'],
85+
exclude: ['test.ts'],
86+
};
87+
88+
overrideJsonFile(tree, 'tsconfig.app.json', tsConfigContent);
89+
90+
const tree2 = await schematicRunner.runSchematicAsync('migration-09', {}, tree.branch()).toPromise();
91+
const { files, include, exclude } = JSON.parse(tree2.readContent('tsconfig.app.json'));
92+
expect(files).toEqual(['src/main.ts', 'src/polyfills.ts']);
93+
expect(include).toEqual(['src/**/*.d.ts']);
94+
expect(exclude).toBeUndefined();
95+
});
96+
97+
it(`should update include '**/*.ts' in apps tsConfig to '**/*.d.ts' when includes contains multiple elements`, async () => {
98+
const tsConfigContent = {
99+
...defaultTsConfigOptions,
100+
include: ['foo.ts', 'src/**/*.ts'],
101+
};
102+
103+
overrideJsonFile(tree, 'tsconfig.app.json', tsConfigContent);
104+
105+
const tree2 = await schematicRunner.runSchematicAsync('migration-09', {}, tree.branch()).toPromise();
106+
const { files, include, exclude } = JSON.parse(tree2.readContent('tsconfig.app.json'));
107+
expect(files).toEqual(['src/main.ts', 'src/polyfills.ts']);
108+
expect(include).toEqual(['foo.ts', 'src/**/*.d.ts']);
109+
expect(exclude).toBeUndefined();
110+
});
111+
81112
it(`should remove angularCompilerOptions when enableIvy is true and it's the only option`, async () => {
82113
overrideJsonFile(tree, 'tsconfig.app.json', defaultTsConfigOptions);
83114
const tree2 = await schematicRunner.runSchematicAsync('migration-09', {}, tree.branch()).toPromise();

0 commit comments

Comments
 (0)