fix(bazel): do not modify tsconfig.json (#30877)
Before this change, user's tsconfig.json is cloned and some options controlled by Bazel are removed otherwise Bazel would complain about: ``` WARNING: your tsconfig.json file specifies options which are overridden by Bazel: - compilerOptions.target and compilerOptions.module are controlled by downstream dependencies, such as ts_devserver - compilerOptions.typeRoots is always set to the @types subdirectory of the node_modules attribute - compilerOptions.rootDir and compilerOptions.baseUrl are always the workspace root directory ``` Since the warning has been removed in rules_typescript/8d8d398, there's no need to clone and backup tsconfig.json PR Close #30877
This commit is contained in:
parent
699283c4ee
commit
b0866769b0
|
@ -45,7 +45,6 @@ Outputs from Bazel appear in the `dist/bin` folder.
|
||||||
If you need to opt-out from using Bazel, you can restore the backup files:
|
If you need to opt-out from using Bazel, you can restore the backup files:
|
||||||
|
|
||||||
- `/angular.json.bak` replaces `/angular.json`
|
- `/angular.json.bak` replaces `/angular.json`
|
||||||
- `/tsconfig.json.bak` replaces `/tsconfig.json`
|
|
||||||
|
|
||||||
## Advanced configuration
|
## Advanced configuration
|
||||||
|
|
||||||
|
|
|
@ -41,7 +41,6 @@ function testBazel() {
|
||||||
function testNonBazel() {
|
function testNonBazel() {
|
||||||
# Replace angular.json that uses Bazel builder with the default generated by CLI
|
# Replace angular.json that uses Bazel builder with the default generated by CLI
|
||||||
mv ./angular.json.bak ./angular.json
|
mv ./angular.json.bak ./angular.json
|
||||||
mv ./tsconfig.json.bak ./tsconfig.json
|
|
||||||
rm -rf dist src/main.dev.ts src/main.prod.ts
|
rm -rf dist src/main.dev.ts src/main.prod.ts
|
||||||
yarn webdriver-manager update --gecko=false --standalone=false ${CI_CHROMEDRIVER_VERSION_ARG:---versions.chrome 2.45}
|
yarn webdriver-manager update --gecko=false --standalone=false ${CI_CHROMEDRIVER_VERSION_ARG:---versions.chrome 2.45}
|
||||||
ng build --progress=false
|
ng build --progress=false
|
||||||
|
|
|
@ -222,65 +222,6 @@ function backupAngularJson(): Rule {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a backup for the original tsconfig.json file in case user wants to
|
|
||||||
* eject Bazel and revert to the original workflow.
|
|
||||||
*/
|
|
||||||
function backupTsconfigJson(): Rule {
|
|
||||||
return (host: Tree, context: SchematicContext) => {
|
|
||||||
const tsconfigPath = 'tsconfig.json';
|
|
||||||
if (!host.exists(tsconfigPath)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
host.create(
|
|
||||||
`${tsconfigPath}.bak`, '// This is a backup file of the original tsconfig.json. ' +
|
|
||||||
'This file is needed in case you want to revert to the workflow without Bazel.\n\n' +
|
|
||||||
host.read(tsconfigPath));
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Bazel controls the compilation options of tsc, so many options in
|
|
||||||
* tsconfig.json generated by the default CLI schematics are not applicable.
|
|
||||||
* This function updates the tsconfig.json to remove Bazel-controlled
|
|
||||||
* parameters. This prevents Bazel from printing out warnings about overriden
|
|
||||||
* settings.
|
|
||||||
*/
|
|
||||||
function updateTsconfigJson(): Rule {
|
|
||||||
return (host: Tree, context: SchematicContext) => {
|
|
||||||
const tsconfigPath = 'tsconfig.json';
|
|
||||||
if (!host.exists(tsconfigPath)) {
|
|
||||||
return host;
|
|
||||||
}
|
|
||||||
const contentRaw = host.read(tsconfigPath) !.toString();
|
|
||||||
if (!contentRaw) {
|
|
||||||
return host;
|
|
||||||
}
|
|
||||||
const content = contentRaw.toString();
|
|
||||||
const ast = parseJsonAst(content);
|
|
||||||
if (!isJsonAstObject(ast)) {
|
|
||||||
return host;
|
|
||||||
}
|
|
||||||
const compilerOptions = findPropertyInAstObject(ast, 'compilerOptions');
|
|
||||||
if (!isJsonAstObject(compilerOptions)) {
|
|
||||||
return host;
|
|
||||||
}
|
|
||||||
const recorder = host.beginUpdate(tsconfigPath);
|
|
||||||
// target and module are controlled by downstream dependencies, such as
|
|
||||||
// ts_devserver
|
|
||||||
removeKeyValueInAstObject(recorder, content, compilerOptions, 'target');
|
|
||||||
removeKeyValueInAstObject(recorder, content, compilerOptions, 'module');
|
|
||||||
// typeRoots is always set to the @types subdirectory of the node_modules
|
|
||||||
// attribute
|
|
||||||
removeKeyValueInAstObject(recorder, content, compilerOptions, 'typeRoots');
|
|
||||||
// rootDir and baseUrl are always the workspace root directory
|
|
||||||
removeKeyValueInAstObject(recorder, content, compilerOptions, 'rootDir');
|
|
||||||
removeKeyValueInAstObject(recorder, content, compilerOptions, 'baseUrl');
|
|
||||||
host.commitUpdate(recorder);
|
|
||||||
return host;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @angular/bazel requires minimum version of rxjs to be 6.4.0. This function
|
* @angular/bazel requires minimum version of rxjs to be 6.4.0. This function
|
||||||
* upgrades the version of rxjs in package.json if necessary.
|
* upgrades the version of rxjs in package.json if necessary.
|
||||||
|
@ -388,10 +329,8 @@ export default function(options: Schema): Rule {
|
||||||
addDevDependenciesToPackageJson(options),
|
addDevDependenciesToPackageJson(options),
|
||||||
addPostinstallToGenerateNgSummaries(),
|
addPostinstallToGenerateNgSummaries(),
|
||||||
backupAngularJson(),
|
backupAngularJson(),
|
||||||
backupTsconfigJson(),
|
|
||||||
updateAngularJsonToUseBazelBuilder(options),
|
updateAngularJsonToUseBazelBuilder(options),
|
||||||
updateGitignore(),
|
updateGitignore(),
|
||||||
updateTsconfigJson(),
|
|
||||||
upgradeRxjs(),
|
upgradeRxjs(),
|
||||||
installNodeModules(options),
|
installNodeModules(options),
|
||||||
]);
|
]);
|
||||||
|
|
|
@ -228,29 +228,6 @@ describe('ng-add schematic', () => {
|
||||||
expect(builder).toBe('@angular/bazel:build');
|
expect(builder).toBe('@angular/bazel:build');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should create a backup for original tsconfig.json', async() => {
|
|
||||||
expect(host.files).toContain('/tsconfig.json');
|
|
||||||
const original = host.readContent('/tsconfig.json');
|
|
||||||
host = await schematicRunner.runSchematicAsync('ng-add', defaultOptions, host).toPromise();
|
|
||||||
expect(host.files).toContain('/tsconfig.json.bak');
|
|
||||||
const content = host.readContent('/tsconfig.json.bak');
|
|
||||||
expect(content.startsWith('// This is a backup file')).toBe(true);
|
|
||||||
expect(content).toMatch(original);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should remove Bazel-controlled options from tsconfig.json', async() => {
|
|
||||||
host = await schematicRunner.runSchematicAsync('ng-add', defaultOptions, host).toPromise();
|
|
||||||
expect(host.files).toContain('/tsconfig.json');
|
|
||||||
const content = host.readContent('/tsconfig.json');
|
|
||||||
expect(() => JSON.parse(content)).not.toThrow();
|
|
||||||
expect(JSON.parse(content)).toEqual({
|
|
||||||
compileOnSave: false,
|
|
||||||
compilerOptions: {
|
|
||||||
outDir: './dist/out-tsc',
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('rxjs', () => {
|
describe('rxjs', () => {
|
||||||
const cases = [
|
const cases = [
|
||||||
// version|upgrade
|
// version|upgrade
|
||||||
|
|
Loading…
Reference in New Issue