Skip to content

Commit 705c95b

Browse files
alan-agius4clydin
authored andcommitted
fix(@schematics/angular): add module in root tsconfig when migrating
1 parent 3b3389d commit 705c95b

File tree

2 files changed

+47
-21
lines changed

2 files changed

+47
-21
lines changed

packages/schematics/angular/migrations/update-6/index.ts

+31-16
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import {
3232
addPackageJsonDependency,
3333
} from '../../utility/dependencies';
3434
import {
35-
appendPropertyInAstObject,
3635
appendValueInAstArray,
3736
findPropertyInAstObject,
3837
} from '../../utility/json-utils';
@@ -734,31 +733,47 @@ function updateRootTsConfig(): Rule {
734733
}
735734

736735
const tsCfgAst = parseJsonAst(buffer.toString(), JsonParseMode.Loose);
737-
if (tsCfgAst.kind != 'object') {
738-
throw new SchematicsException(
739-
'Invalid tsconfig. Was expecting an object'
740-
);
736+
if (tsCfgAst.kind !== 'object') {
737+
throw new SchematicsException('Invalid root tsconfig. Was expecting an object');
741738
}
742739

743740
const compilerOptionsAstNode = findPropertyInAstObject(tsCfgAst, 'compilerOptions');
744741
if (!compilerOptionsAstNode || compilerOptionsAstNode.kind != 'object') {
745-
throw new SchematicsException('Invalid tsconfig "compilerOptions" property; expected an object.');
742+
throw new SchematicsException(
743+
'Invalid root tsconfig "compilerOptions" property; expected an object.',
744+
);
746745
}
747746

748-
if (findPropertyInAstObject(compilerOptionsAstNode, 'baseUrl')) {
747+
if (
748+
findPropertyInAstObject(compilerOptionsAstNode, 'baseUrl') &&
749+
findPropertyInAstObject(compilerOptionsAstNode, 'module')
750+
) {
749751
return host;
750752
}
751753

752-
const recorder = host.beginUpdate(tsConfigPath);
753-
appendPropertyInAstObject(
754-
recorder,
755-
compilerOptionsAstNode,
756-
'baseUrl',
757-
'./',
758-
4,
759-
);
754+
const compilerOptions = compilerOptionsAstNode.value;
755+
const { baseUrl = './', module = 'es2015'} = compilerOptions;
756+
757+
const validBaseUrl = ['./', '', '.'];
758+
if (!validBaseUrl.includes(baseUrl as string)) {
759+
const formattedBaseUrl = validBaseUrl.map(x => `'${x}'`).join(', ');
760+
context.logger.warn(tags.oneLine
761+
`Root tsconfig option 'baseUrl' is not one of: ${formattedBaseUrl}.
762+
This might cause unexpected behaviour when generating libraries.`,
763+
);
764+
}
765+
766+
if (module !== 'es2015') {
767+
context.logger.warn(
768+
`Root tsconfig option 'module' is not 'es2015'. This might cause unexpected behaviour.`,
769+
);
770+
}
771+
772+
compilerOptions.module = module;
773+
compilerOptions.baseUrl = baseUrl;
774+
775+
host.overwrite(tsConfigPath, JSON.stringify(tsCfgAst.value, null, 2));
760776

761-
host.commitUpdate(recorder);
762777
return host;
763778
};
764779
}

packages/schematics/angular/migrations/update-6/index_spec.ts

+16-5
Original file line numberDiff line numberDiff line change
@@ -777,22 +777,33 @@ describe('Migration to v6', () => {
777777

778778
describe('root ts config', () => {
779779
const rootTsConfig = '/tsconfig.json';
780+
let compilerOptions: JsonObject;
781+
780782
beforeEach(() => {
781783
tree.create(rootTsConfig, `
782784
{
783785
"compilerOptions": {
784-
"module": "es2015"
786+
"noEmitOnError": true
785787
}
786788
}
787789
`);
788-
});
789790

790-
it('should add baseUrl', () => {
791791
tree.create(oldConfigPath, JSON.stringify(baseConfig, null, 2));
792792
tree = schematicRunner.runSchematic('migration-01', defaultOptions, tree);
793793
const content = tree.readContent(rootTsConfig);
794-
const config = JSON.parse(content);
795-
expect(config.compilerOptions.baseUrl).toEqual('./');
794+
compilerOptions = JSON.parse(content).compilerOptions;
795+
});
796+
797+
it('should add baseUrl', () => {
798+
expect(compilerOptions.baseUrl).toEqual('./');
799+
});
800+
801+
it('should add module', () => {
802+
expect(compilerOptions.module).toEqual('es2015');
803+
});
804+
805+
it('should not remove existing options', () => {
806+
expect(compilerOptions.noEmitOnError).toBeDefined();
796807
});
797808
});
798809

0 commit comments

Comments
 (0)