mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-21 22:34: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
|
* 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
|
* 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 { Rule, Tree } from '@angular-devkit/schematics';
|
||||||
import { dirname, relative } from 'path';
|
import { dirname, relative } from 'path';
|
||||||
import { JSONFile } from '../../utility/json-file';
|
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 { Builders } from '../../utility/workspace-models';
|
||||||
import {
|
import { forwardSlashPath } from './utils';
|
||||||
forwardSlashPath,
|
|
||||||
getAllOptions,
|
|
||||||
getTargets,
|
|
||||||
getWorkspace,
|
|
||||||
} from './utils';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update the tsconfig files for applications
|
* Update the tsconfig files for applications
|
||||||
@ -25,45 +20,52 @@ import {
|
|||||||
* - Sets module compiler option to esnext or commonjs
|
* - Sets module compiler option to esnext or commonjs
|
||||||
*/
|
*/
|
||||||
export function updateApplicationTsConfigs(): Rule {
|
export function updateApplicationTsConfigs(): Rule {
|
||||||
return (tree, { logger }) => {
|
return async (tree, { logger }) => {
|
||||||
const workspace = getWorkspace(tree);
|
const workspace = await getWorkspace(tree);
|
||||||
|
|
||||||
// Add `module` option in the workspace tsconfig
|
// Add `module` option in the workspace tsconfig
|
||||||
updateModuleCompilerOption(tree, '/tsconfig.json');
|
updateModuleCompilerOption(tree, '/tsconfig.json');
|
||||||
|
|
||||||
for (const { target, project } of getTargets(workspace, 'build', Builders.Browser)) {
|
for (const [targetName, target, , project] of allWorkspaceTargets(workspace)) {
|
||||||
updateTsConfig(tree, target, project, Builders.Browser, logger);
|
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.sourceRoot, logger);
|
||||||
updateTsConfig(tree, target, project, Builders.Server, logger);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const { target, project } of getTargets(workspace, 'test', Builders.Karma)) {
|
|
||||||
updateTsConfig(tree, target, project, Builders.Karma, logger);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateTsConfig(
|
function updateTsConfig(
|
||||||
tree: Tree,
|
tree: Tree,
|
||||||
builderConfig: JsonAstObject,
|
builderConfig: workspaces.TargetDefinition,
|
||||||
project: JsonAstObject,
|
projectSourceRoot: string | undefined,
|
||||||
builderName: Builders,
|
|
||||||
logger: logging.LoggerApi,
|
logger: logging.LoggerApi,
|
||||||
) {
|
) {
|
||||||
const options = getAllOptions(builderConfig);
|
for (const [, options] of allTargetOptions(builderConfig)) {
|
||||||
for (const option of options) {
|
const tsConfigPath = options.tsConfig;
|
||||||
const tsConfigOption = findPropertyInAstObject(option, 'tsConfig');
|
if (!tsConfigPath || typeof tsConfigPath !== 'string') {
|
||||||
|
|
||||||
if (!tsConfigOption || tsConfigOption.kind !== 'string') {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const tsConfigPath = tsConfigOption.value;
|
|
||||||
|
|
||||||
// Update 'module' compilerOption
|
// Update 'module' compilerOption
|
||||||
updateModuleCompilerOption(tree, tsConfigPath, builderName);
|
updateModuleCompilerOption(tree, tsConfigPath, builderConfig.builder);
|
||||||
|
|
||||||
let tsConfigJson;
|
let tsConfigJson;
|
||||||
try {
|
try {
|
||||||
@ -88,7 +90,7 @@ function updateTsConfig(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add stricter file inclusions to avoid unused file warning during compilation
|
// 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']);
|
const include = tsConfigJson.get(['include']);
|
||||||
if (include && Array.isArray(include)) {
|
if (include && Array.isArray(include)) {
|
||||||
@ -101,9 +103,8 @@ function updateTsConfig(
|
|||||||
// Includes are not present, add includes to dts files
|
// Includes are not present, add includes to dts files
|
||||||
// By default when 'include' nor 'files' fields are used TypeScript
|
// By default when 'include' nor 'files' fields are used TypeScript
|
||||||
// will include all ts files.
|
// will include all ts files.
|
||||||
const srcRootAst = findPropertyInAstObject(project, 'sourceRoot');
|
const include = projectSourceRoot !== undefined
|
||||||
const include = srcRootAst?.kind === 'string'
|
? join(normalize(projectSourceRoot), '**/*.d.ts')
|
||||||
? join(normalize(srcRootAst.value), '**/*.d.ts')
|
|
||||||
: '**/*.d.ts';
|
: '**/*.d.ts';
|
||||||
|
|
||||||
tsConfigJson.modify(['include'], [include]);
|
tsConfigJson.modify(['include'], [include]);
|
||||||
@ -114,16 +115,16 @@ function updateTsConfig(
|
|||||||
const newFiles: string[] = [];
|
const newFiles: string[] = [];
|
||||||
const tsConfigDir = dirname(forwardSlashPath(tsConfigPath));
|
const tsConfigDir = dirname(forwardSlashPath(tsConfigPath));
|
||||||
|
|
||||||
const mainOption = findPropertyInAstObject(option, 'main');
|
const mainOption = options.main;
|
||||||
if (mainOption && mainOption.kind === 'string') {
|
if (mainOption && typeof mainOption === 'string') {
|
||||||
newFiles.push(
|
newFiles.push(
|
||||||
forwardSlashPath(relative(tsConfigDir, forwardSlashPath(mainOption.value))));
|
forwardSlashPath(relative(tsConfigDir, forwardSlashPath(mainOption))));
|
||||||
}
|
}
|
||||||
|
|
||||||
const polyfillsOption = findPropertyInAstObject(option, 'polyfills');
|
const polyfillsOption = options.polyfills;
|
||||||
if (polyfillsOption && polyfillsOption.kind === 'string') {
|
if (polyfillsOption && typeof polyfillsOption === 'string') {
|
||||||
newFiles.push(
|
newFiles.push(
|
||||||
forwardSlashPath(relative(tsConfigDir, forwardSlashPath(polyfillsOption.value))));
|
forwardSlashPath(relative(tsConfigDir, forwardSlashPath(polyfillsOption))));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (newFiles.length) {
|
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;
|
let tsConfigJson;
|
||||||
try {
|
try {
|
||||||
tsConfigJson = new JSONFile(tree, tsConfigPath);
|
tsConfigJson = new JSONFile(tree, tsConfigPath);
|
||||||
|
@ -93,10 +93,10 @@ export async function createDefaultPath(tree: Tree, projectName: string): Promis
|
|||||||
|
|
||||||
export function* allWorkspaceTargets(
|
export function* allWorkspaceTargets(
|
||||||
workspace: workspaces.WorkspaceDefinition,
|
workspace: workspaces.WorkspaceDefinition,
|
||||||
): Iterable<[string, workspaces.TargetDefinition]> {
|
): Iterable<[string, workspaces.TargetDefinition, string, workspaces.ProjectDefinition]> {
|
||||||
for (const [, project] of workspace.projects) {
|
for (const [projectName, project] of workspace.projects) {
|
||||||
for (const targetEntry of project.targets) {
|
for (const [targetName, target] of project.targets) {
|
||||||
yield targetEntry;
|
yield [targetName, target, projectName, project] ;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user