mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-16 10:33:43 +08:00
fix(@schematics/angular): add module
in root tsconfig when migrating
This commit is contained in:
parent
e6b59e44ef
commit
dad2de9737
@ -32,7 +32,6 @@ import {
|
||||
addPackageJsonDependency,
|
||||
} from '../../utility/dependencies';
|
||||
import {
|
||||
appendPropertyInAstObject,
|
||||
appendValueInAstArray,
|
||||
findPropertyInAstObject,
|
||||
} from '../../utility/json-utils';
|
||||
@ -734,31 +733,47 @@ function updateRootTsConfig(): Rule {
|
||||
}
|
||||
|
||||
const tsCfgAst = parseJsonAst(buffer.toString(), JsonParseMode.Loose);
|
||||
if (tsCfgAst.kind != 'object') {
|
||||
throw new SchematicsException(
|
||||
'Invalid tsconfig. Was expecting an object'
|
||||
);
|
||||
if (tsCfgAst.kind !== 'object') {
|
||||
throw new SchematicsException('Invalid root tsconfig. Was expecting an object');
|
||||
}
|
||||
|
||||
const compilerOptionsAstNode = findPropertyInAstObject(tsCfgAst, 'compilerOptions');
|
||||
if (!compilerOptionsAstNode || compilerOptionsAstNode.kind != 'object') {
|
||||
throw new SchematicsException('Invalid tsconfig "compilerOptions" property; expected an object.');
|
||||
throw new SchematicsException(
|
||||
'Invalid root tsconfig "compilerOptions" property; expected an object.',
|
||||
);
|
||||
}
|
||||
|
||||
if (findPropertyInAstObject(compilerOptionsAstNode, 'baseUrl')) {
|
||||
if (
|
||||
findPropertyInAstObject(compilerOptionsAstNode, 'baseUrl') &&
|
||||
findPropertyInAstObject(compilerOptionsAstNode, 'module')
|
||||
) {
|
||||
return host;
|
||||
}
|
||||
|
||||
const recorder = host.beginUpdate(tsConfigPath);
|
||||
appendPropertyInAstObject(
|
||||
recorder,
|
||||
compilerOptionsAstNode,
|
||||
'baseUrl',
|
||||
'./',
|
||||
4,
|
||||
);
|
||||
const compilerOptions = compilerOptionsAstNode.value;
|
||||
const { baseUrl = './', module = 'es2015'} = compilerOptions;
|
||||
|
||||
const validBaseUrl = ['./', '', '.'];
|
||||
if (!validBaseUrl.includes(baseUrl as string)) {
|
||||
const formattedBaseUrl = validBaseUrl.map(x => `'${x}'`).join(', ');
|
||||
context.logger.warn(tags.oneLine
|
||||
`Root tsconfig option 'baseUrl' is not one of: ${formattedBaseUrl}.
|
||||
This might cause unexpected behaviour when generating libraries.`,
|
||||
);
|
||||
}
|
||||
|
||||
if (module !== 'es2015') {
|
||||
context.logger.warn(
|
||||
`Root tsconfig option 'module' is not 'es2015'. This might cause unexpected behaviour.`,
|
||||
);
|
||||
}
|
||||
|
||||
compilerOptions.module = module;
|
||||
compilerOptions.baseUrl = baseUrl;
|
||||
|
||||
host.overwrite(tsConfigPath, JSON.stringify(tsCfgAst.value, null, 2));
|
||||
|
||||
host.commitUpdate(recorder);
|
||||
return host;
|
||||
};
|
||||
}
|
||||
|
@ -777,22 +777,33 @@ describe('Migration to v6', () => {
|
||||
|
||||
describe('root ts config', () => {
|
||||
const rootTsConfig = '/tsconfig.json';
|
||||
let compilerOptions: JsonObject;
|
||||
|
||||
beforeEach(() => {
|
||||
tree.create(rootTsConfig, `
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "es2015"
|
||||
"noEmitOnError": true
|
||||
}
|
||||
}
|
||||
`);
|
||||
});
|
||||
|
||||
it('should add baseUrl', () => {
|
||||
tree.create(oldConfigPath, JSON.stringify(baseConfig, null, 2));
|
||||
tree = schematicRunner.runSchematic('migration-01', defaultOptions, tree);
|
||||
const content = tree.readContent(rootTsConfig);
|
||||
const config = JSON.parse(content);
|
||||
expect(config.compilerOptions.baseUrl).toEqual('./');
|
||||
compilerOptions = JSON.parse(content).compilerOptions;
|
||||
});
|
||||
|
||||
it('should add baseUrl', () => {
|
||||
expect(compilerOptions.baseUrl).toEqual('./');
|
||||
});
|
||||
|
||||
it('should add module', () => {
|
||||
expect(compilerOptions.module).toEqual('es2015');
|
||||
});
|
||||
|
||||
it('should not remove existing options', () => {
|
||||
expect(compilerOptions.noEmitOnError).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user