1
0
mirror of https://github.com/angular/angular-cli.git synced 2025-05-16 10:33:43 +08:00
angular-cli/packages/schematics/angular/migrations/update-10/update-module-and-target-compiler-options.ts
Alan Agius 8b96e52d83 fix(@schematics/angular): remove solution style tsconfig from new projects
Following the issues highlighted in https://docs.google.com/document/d/1eB6cGCG_2ircfS5GzpDC9dBgikeYYcMxghVH5sDESHw/edit?usp=sharing and discussions held with the TypeScript team. The best course of action is to rollback this feature.

In future, it is not excluded that solution style tsconfigs are re-introduced..

Closes  and closes 
2020-08-12 19:26:12 +01:00

153 lines
4.9 KiB
TypeScript

/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* 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 { dirname, join, normalize } from '@angular-devkit/core';
import { Rule, Tree } from '@angular-devkit/schematics';
import { JSONFile } from '../../utility/json-file';
import { getWorkspace } from '../../utility/workspace';
import { Builders } from '../../utility/workspace-models';
interface ModuleAndTargetReplamenent {
oldModule?: string;
newModule?: string | false;
oldTarget?: string;
newTarget?: string;
}
export default function (): Rule {
return async (host, { logger }) => {
// Workspace level tsconfig
try {
updateModuleAndTarget(host, 'tsconfig.json', {
oldModule: 'esnext',
newModule: 'es2020',
});
} catch (error) {
logger.warn(
`Unable to update 'tsconfig.json' module option from 'esnext' to 'es2020': ${
error.message || error
}`,
);
}
const workspace = await getWorkspace(host);
// Find all tsconfig which are refereces used by builders
for (const [, project] of workspace.projects) {
for (const [, target] of project.targets) {
// E2E builder doesn't reference a tsconfig but it uses one found in the root folder.
if (target.builder === Builders.Protractor && typeof target.options?.protractorConfig === 'string') {
const tsConfigPath = join(dirname(normalize(target.options.protractorConfig)), 'tsconfig.json');
try {
updateModuleAndTarget(host, tsConfigPath, {
oldTarget: 'es5',
newTarget: 'es2018',
});
} catch (error) {
logger.warn(
`Unable to update '${tsConfigPath}' target option from 'es5' to 'es2018': ${
error.message || error
}`,
);
}
continue;
}
// Update all other known CLI builders that use a tsconfig
const tsConfigs = [
target.options || {},
...Object.values(target.configurations || {}),
]
.filter(opt => typeof opt?.tsConfig === 'string')
.map(opt => (opt as { tsConfig: string }).tsConfig);
const uniqueTsConfigs = [...new Set(tsConfigs)];
if (uniqueTsConfigs.length < 1) {
continue;
}
switch (target.builder as Builders) {
case Builders.Server:
uniqueTsConfigs.forEach(p => {
try {
updateModuleAndTarget(host, p, {
oldModule: 'commonjs',
// False will remove the module
// NB: For server we no longer use commonjs because it is bundled using webpack which has it's own module system.
// This ensures that lazy-loaded works on the server.
newModule: false,
});
} catch (error) {
logger.warn(
`Unable to remove '${p}' module option (was 'commonjs'): ${
error.message || error
}`,
);
}
try {
updateModuleAndTarget(host, p, {
newTarget: 'es2016',
});
} catch (error) {
logger.warn(
`Unable to update '${p}' target option to 'es2016': ${
error.message || error
}`,
);
}
});
break;
case Builders.Karma:
case Builders.Browser:
case Builders.NgPackagr:
uniqueTsConfigs.forEach(p => {
try {
updateModuleAndTarget(host, p, {
oldModule: 'esnext',
newModule: 'es2020',
});
} catch (error) {
logger.warn(
`Unable to update '${p}' module option from 'esnext' to 'es2020': ${
error.message || error
}`,
);
}
});
break;
}
}
}
};
}
function updateModuleAndTarget(host: Tree, tsConfigPath: string, replacements: ModuleAndTargetReplamenent) {
const json = new JSONFile(host, tsConfigPath);
const { oldTarget, newTarget, newModule, oldModule } = replacements;
if (newTarget) {
const target = json.get(['compilerOptions', 'target']);
if ((typeof target === 'string' && (!oldTarget || oldTarget === target.toLowerCase())) || !target) {
json.modify(['compilerOptions', 'target'], newTarget);
}
}
if (newModule === false) {
json.remove(['compilerOptions', 'module']);
} else if (newModule) {
const module = json.get(['compilerOptions', 'module']);
if (typeof module === 'string' && oldModule === module.toLowerCase()) {
json.modify(['compilerOptions', 'module'], newModule);
}
}
}