mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-17 02:54:21 +08:00
refactor(@schematics/angular): use new workspace helpers in update-app-tsconfigs migration
This commit is contained in:
parent
741b7313e9
commit
6ff04473ef
@ -5,18 +5,13 @@
|
||||
* 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, join, logging, normalize } from '@angular-devkit/core';
|
||||
import { join, logging, normalize, workspaces } from '@angular-devkit/core';
|
||||
import { Rule, Tree } from '@angular-devkit/schematics';
|
||||
import { dirname, relative } from 'path';
|
||||
import { JSONFile } from '../../utility/json-file';
|
||||
import { findPropertyInAstObject } from '../../utility/json-utils';
|
||||
import { allTargetOptions, allWorkspaceTargets, getWorkspace } from '../../utility/workspace';
|
||||
import { Builders } from '../../utility/workspace-models';
|
||||
import {
|
||||
forwardSlashPath,
|
||||
getAllOptions,
|
||||
getTargets,
|
||||
getWorkspace,
|
||||
} from './utils';
|
||||
import { forwardSlashPath } from './utils';
|
||||
|
||||
/**
|
||||
* Update the tsconfig files for applications
|
||||
@ -25,45 +20,52 @@ import {
|
||||
* - Sets module compiler option to esnext or commonjs
|
||||
*/
|
||||
export function updateApplicationTsConfigs(): Rule {
|
||||
return (tree, { logger }) => {
|
||||
const workspace = getWorkspace(tree);
|
||||
return async (tree, { logger }) => {
|
||||
const workspace = await getWorkspace(tree);
|
||||
|
||||
// Add `module` option in the workspace tsconfig
|
||||
updateModuleCompilerOption(tree, '/tsconfig.json');
|
||||
|
||||
for (const { target, project } of getTargets(workspace, 'build', Builders.Browser)) {
|
||||
updateTsConfig(tree, target, project, Builders.Browser, logger);
|
||||
}
|
||||
for (const [targetName, target, , project] of allWorkspaceTargets(workspace)) {
|
||||
switch (targetName) {
|
||||
case 'build':
|
||||
if (target.builder !== Builders.Browser) {
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
case 'server':
|
||||
if (target.builder !== Builders.Server) {
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
case 'test':
|
||||
if (target.builder !== Builders.Karma) {
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
continue;
|
||||
}
|
||||
|
||||
for (const { target, project } of getTargets(workspace, 'server', Builders.Server)) {
|
||||
updateTsConfig(tree, target, project, Builders.Server, logger);
|
||||
}
|
||||
|
||||
for (const { target, project } of getTargets(workspace, 'test', Builders.Karma)) {
|
||||
updateTsConfig(tree, target, project, Builders.Karma, logger);
|
||||
updateTsConfig(tree, target, project.sourceRoot, logger);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function updateTsConfig(
|
||||
tree: Tree,
|
||||
builderConfig: JsonAstObject,
|
||||
project: JsonAstObject,
|
||||
builderName: Builders,
|
||||
builderConfig: workspaces.TargetDefinition,
|
||||
projectSourceRoot: string | undefined,
|
||||
logger: logging.LoggerApi,
|
||||
) {
|
||||
const options = getAllOptions(builderConfig);
|
||||
for (const option of options) {
|
||||
const tsConfigOption = findPropertyInAstObject(option, 'tsConfig');
|
||||
|
||||
if (!tsConfigOption || tsConfigOption.kind !== 'string') {
|
||||
for (const [, options] of allTargetOptions(builderConfig)) {
|
||||
const tsConfigPath = options.tsConfig;
|
||||
if (!tsConfigPath || typeof tsConfigPath !== 'string') {
|
||||
continue;
|
||||
}
|
||||
|
||||
const tsConfigPath = tsConfigOption.value;
|
||||
|
||||
// Update 'module' compilerOption
|
||||
updateModuleCompilerOption(tree, tsConfigPath, builderName);
|
||||
updateModuleCompilerOption(tree, tsConfigPath, builderConfig.builder);
|
||||
|
||||
let tsConfigJson;
|
||||
try {
|
||||
@ -88,7 +90,7 @@ function updateTsConfig(
|
||||
}
|
||||
|
||||
// Add stricter file inclusions to avoid unused file warning during compilation
|
||||
if (builderName !== Builders.Karma) {
|
||||
if (builderConfig.builder !== Builders.Karma) {
|
||||
|
||||
const include = tsConfigJson.get(['include']);
|
||||
if (include && Array.isArray(include)) {
|
||||
@ -101,9 +103,8 @@ function updateTsConfig(
|
||||
// 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')
|
||||
const include = projectSourceRoot !== undefined
|
||||
? join(normalize(projectSourceRoot), '**/*.d.ts')
|
||||
: '**/*.d.ts';
|
||||
|
||||
tsConfigJson.modify(['include'], [include]);
|
||||
@ -114,16 +115,16 @@ function updateTsConfig(
|
||||
const newFiles: string[] = [];
|
||||
const tsConfigDir = dirname(forwardSlashPath(tsConfigPath));
|
||||
|
||||
const mainOption = findPropertyInAstObject(option, 'main');
|
||||
if (mainOption && mainOption.kind === 'string') {
|
||||
const mainOption = options.main;
|
||||
if (mainOption && typeof mainOption === 'string') {
|
||||
newFiles.push(
|
||||
forwardSlashPath(relative(tsConfigDir, forwardSlashPath(mainOption.value))));
|
||||
forwardSlashPath(relative(tsConfigDir, forwardSlashPath(mainOption))));
|
||||
}
|
||||
|
||||
const polyfillsOption = findPropertyInAstObject(option, 'polyfills');
|
||||
if (polyfillsOption && polyfillsOption.kind === 'string') {
|
||||
const polyfillsOption = options.polyfills;
|
||||
if (polyfillsOption && typeof polyfillsOption === 'string') {
|
||||
newFiles.push(
|
||||
forwardSlashPath(relative(tsConfigDir, forwardSlashPath(polyfillsOption.value))));
|
||||
forwardSlashPath(relative(tsConfigDir, forwardSlashPath(polyfillsOption))));
|
||||
}
|
||||
|
||||
if (newFiles.length) {
|
||||
@ -136,7 +137,7 @@ function updateTsConfig(
|
||||
}
|
||||
}
|
||||
|
||||
function updateModuleCompilerOption(tree: Tree, tsConfigPath: string, builderName?: Builders) {
|
||||
function updateModuleCompilerOption(tree: Tree, tsConfigPath: string, builderName?: string) {
|
||||
let tsConfigJson;
|
||||
try {
|
||||
tsConfigJson = new JSONFile(tree, tsConfigPath);
|
||||
|
@ -93,10 +93,10 @@ export async function createDefaultPath(tree: Tree, projectName: string): Promis
|
||||
|
||||
export function* allWorkspaceTargets(
|
||||
workspace: workspaces.WorkspaceDefinition,
|
||||
): Iterable<[string, workspaces.TargetDefinition]> {
|
||||
for (const [, project] of workspace.projects) {
|
||||
for (const targetEntry of project.targets) {
|
||||
yield targetEntry;
|
||||
): Iterable<[string, workspaces.TargetDefinition, string, workspaces.ProjectDefinition]> {
|
||||
for (const [projectName, project] of workspace.projects) {
|
||||
for (const [targetName, target] of project.targets) {
|
||||
yield [targetName, target, projectName, project] ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user