angular-cli/packages/schematics/angular/migrations/update-11/add-declaration-map-compiler-option.ts
Alan Agius 9025637a93 feat(@schematics/angular): add migration to add declaration maps to libraries
Since version 10.1 we added declaration maps for local library builds, to improve the DX. With this change we add it to existing project.

See: #17920
2020-09-18 14:52:08 -05:00

49 lines
1.4 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 { Rule, Tree } from '@angular-devkit/schematics';
import { JSONFile } from '../../utility/json-file';
import { getWorkspace } from '../../utility/workspace';
import { Builders } from '../../utility/workspace-models';
export default function (): Rule {
return async host => {
const workspace = await getWorkspace(host);
for (const [, project] of workspace.projects) {
for (const [, target] of project.targets) {
if (target.builder !== Builders.NgPackagr) {
continue;
}
if (!target.configurations) {
continue;
}
for (const options of Object.values(target.configurations)) {
addDeclarationMapValue(host, options?.tsConfig, false);
}
addDeclarationMapValue(host, target.options?.tsConfig, true);
}
}
};
}
function addDeclarationMapValue(host: Tree, tsConfigPath: unknown, value: boolean): void {
if (typeof tsConfigPath !== 'string') {
return;
}
const declarationMapPath = ['compilerOptions', 'declarationMap'];
const file = new JSONFile(host, tsConfigPath);
if (file.get(declarationMapPath) === undefined) {
file.modify(declarationMapPath, value);
}
}