From 77f07c6d1303853fe4be516b5d07c673fcfb756a Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Tue, 11 Feb 2020 19:33:41 +0100 Subject: [PATCH] fix(@schematics/angular): add include to type definitions in tsconfig Fixes #16923 (cherry picked from commit bf04333ed454db89b0bc683ca513e465ed7032ef) --- .../update-9/update-app-tsconfigs.ts | 34 ++++++++++++++----- .../update-9/update-app-tsconfigs_spec.ts | 3 +- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/packages/schematics/angular/migrations/update-9/update-app-tsconfigs.ts b/packages/schematics/angular/migrations/update-9/update-app-tsconfigs.ts index 05d454f612ee..df9fecc1ff7c 100644 --- a/packages/schematics/angular/migrations/update-9/update-app-tsconfigs.ts +++ b/packages/schematics/angular/migrations/update-9/update-app-tsconfigs.ts @@ -5,7 +5,7 @@ * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ -import { JsonAstObject, logging } from '@angular-devkit/core'; +import { JsonAstObject, join, logging, normalize } from '@angular-devkit/core'; import { Rule, Tree, UpdateRecorder } from '@angular-devkit/schematics'; import { dirname, relative } from 'path'; import { @@ -36,23 +36,29 @@ export function updateApplicationTsConfigs(): Rule { // Add `module` option in the workspace tsconfig updateModuleCompilerOption(tree, '/tsconfig.json'); - for (const { target } of getTargets(workspace, 'build', Builders.Browser)) { - updateTsConfig(tree, target, Builders.Browser, logger); + for (const { target, project } of getTargets(workspace, 'build', Builders.Browser)) { + updateTsConfig(tree, target, project, Builders.Browser, logger); } - for (const { target } of getTargets(workspace, 'server', Builders.Server)) { - updateTsConfig(tree, target, Builders.Server, logger); + for (const { target, project } of getTargets(workspace, 'server', Builders.Server)) { + updateTsConfig(tree, target, project, Builders.Server, logger); } - for (const { target } of getTargets(workspace, 'test', Builders.Karma)) { - updateTsConfig(tree, target, Builders.Karma, logger); + for (const { target, project } of getTargets(workspace, 'test', Builders.Karma)) { + updateTsConfig(tree, target, project, Builders.Karma, logger); } return tree; }; } -function updateTsConfig(tree: Tree, builderConfig: JsonAstObject, builderName: Builders, logger: logging.LoggerApi) { +function updateTsConfig( + tree: Tree, + builderConfig: JsonAstObject, + project: JsonAstObject, + builderName: Builders, + logger: logging.LoggerApi, +) { const options = getAllOptions(builderConfig); for (const option of options) { let recorder: UpdateRecorder; @@ -108,6 +114,18 @@ function updateTsConfig(tree: Tree, builderConfig: JsonAstObject, builderName: B recorder.insertLeft(start.offset, tsInclude.text.replace('.ts', '.d.ts')); tree.commitUpdate(recorder); } + } else { + // Includes are not present, add includes to dts files + // By default when 'include' nor 'files' fields are used TypeScript + // will include all ts files. + const srcRootAst = findPropertyInAstObject(project, 'sourceRoot'); + const include = srcRootAst?.kind === 'string' + ? join(normalize(srcRootAst.value), '**/*.d.ts') + : '**/*.d.ts'; + + recorder = tree.beginUpdate(tsConfigPath); + insertPropertyInAstObjectInOrder(recorder, tsConfigAst, 'include', [include], 2); + tree.commitUpdate(recorder); } const files = findPropertyInAstObject(tsConfigAst, 'files'); diff --git a/packages/schematics/angular/migrations/update-9/update-app-tsconfigs_spec.ts b/packages/schematics/angular/migrations/update-9/update-app-tsconfigs_spec.ts index 840b7c3492ca..cd27a12af0af 100644 --- a/packages/schematics/angular/migrations/update-9/update-app-tsconfigs_spec.ts +++ b/packages/schematics/angular/migrations/update-9/update-app-tsconfigs_spec.ts @@ -60,9 +60,10 @@ describe('Migration to version 9', () => { it('should update apps tsConfig with stricter files inclusions', async () => { overrideJsonFile(tree, 'tsconfig.app.json', defaultTsConfigOptions); const tree2 = await schematicRunner.runSchematicAsync('workspace-version-9', {}, tree.branch()).toPromise(); - const { exclude, files } = JSON.parse(tree2.readContent('tsconfig.app.json')); + const { exclude, files, include } = JSON.parse(tree2.readContent('tsconfig.app.json')); expect(exclude).toBeUndefined(); expect(files).toEqual(['src/main.ts', 'src/polyfills.ts']); + expect(include).toEqual(['src/**/*.d.ts']); }); it('should resolve paths correctly even if they are using windows separators', async () => {