Skip to content

fix(@schematics/angular): add baseUrl in root tsconfig when migrating #11261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions packages/schematics/angular/migrations/update-6/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,60 @@ function updateTsLintConfig(): Rule {
};
}

function updateRootTsConfig(): Rule {
return (host: Tree, context: SchematicContext) => {
const tsConfigPath = '/tsconfig.json';
const buffer = host.read(tsConfigPath);
if (!buffer) {
return;
}

const tsCfgAst = parseJsonAst(buffer.toString(), JsonParseMode.Loose);
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 root tsconfig "compilerOptions" property; expected an object.',
);
}

if (
findPropertyInAstObject(compilerOptionsAstNode, 'baseUrl') &&
findPropertyInAstObject(compilerOptionsAstNode, 'module')
) {
return host;
}

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));

return host;
};
}

export default function (): Rule {
return (host: Tree, context: SchematicContext) => {
if (host.exists('/.angular.json') || host.exists('/angular.json')) {
Expand All @@ -748,6 +802,7 @@ export default function (): Rule {
migrateConfiguration(config, context.logger),
updateSpecTsConfig(config),
updatePackageJson(config),
updateRootTsConfig(),
updateTsLintConfig(),
(host: Tree, context: SchematicContext) => {
context.logger.warn(tags.oneLine`Some configuration options have been changed,
Expand Down
32 changes: 32 additions & 0 deletions packages/schematics/angular/migrations/update-6/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,38 @@ describe('Migration to v6', () => {
});
});

describe('root ts config', () => {
const rootTsConfig = '/tsconfig.json';
let compilerOptions: JsonObject;

beforeEach(() => {
tree.create(rootTsConfig, `
{
"compilerOptions": {
"noEmitOnError": true
}
}
`);

tree.create(oldConfigPath, JSON.stringify(baseConfig, null, 2));
tree = schematicRunner.runSchematic('migration-01', defaultOptions, tree);
const content = tree.readContent(rootTsConfig);
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();
});
});

describe('package.json', () => {
it('should add a dev dependency to @angular-devkit/build-angular', () => {
tree.create(oldConfigPath, JSON.stringify(baseConfig, null, 2));
Expand Down