Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(@schematics/angular): add include to type definitions in tsconfig #16939

Merged
merged 1 commit into from Feb 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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