Skip to content

Commit

Permalink
fix(@schematics/angular): add include to type definitions in tsconfig
Browse files Browse the repository at this point in the history
Fixes #16923

(cherry picked from commit bf04333)
  • Loading branch information
alan-agius4 authored and dgp1130 committed Feb 12, 2020
1 parent 261209c commit 77f07c6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit 77f07c6

Please sign in to comment.