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:
Keen Yee Liau 2019-06-05 09:47:26 -07:00 committed by Andrew Kushnir
parent 699283c4ee
commit b0866769b0
4 changed files with 5 additions and 91 deletions

View File

@ -1,11 +1,11 @@
# Building with Bazel
This guide explains how to build and test Angular apps with Bazel.
This guide explains how to build and test Angular apps with Bazel.
<div class="alert is-helpful">
This guide assumes you are already familiar with developing and building Angular applications using the [CLI](cli).
This guide assumes you are already familiar with developing and building Angular applications using the [CLI](cli).
It describes features which are part of Angular Labs, and are not considered a stable, supported API.
@ -33,7 +33,7 @@ then create the new application with
ng new --collection=@angular/bazel
```
Now when you use Angular CLI build commands such as `ng build` and `ng serve`,
Now when you use Angular CLI build commands such as `ng build` and `ng serve`,
Bazel is used behind the scenes.
Outputs from Bazel appear in the `dist/bin` folder.
@ -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:
- `/angular.json.bak` replaces `/angular.json`
- `/tsconfig.json.bak` replaces `/tsconfig.json`
## Advanced configuration
@ -63,7 +62,7 @@ You can manually adjust the Bazel configuration to:
* customize the build steps
* parallellize the build for scale and incrementality
Create the initial Bazel configuration files by running the following command:
Create the initial Bazel configuration files by running the following command:
```sh
ng build --leaveBazelFilesOnDisk
@ -120,4 +119,4 @@ Rules are used in `BUILD.bazel` files, which are markers for the packages in you
In the `BUILD.bazel` file, each rule must first be imported, using the `load` statement. Then the rule is called with some attributes, and the result of calling the rule is that you've declared to Bazel how it can derive some outputs given some inputs and dependencies. Then later, when you run a `bazel` command line, Bazel loads all the rules you've declared to determine an absolute ordering of what needs to be run. Note that only the rules needed to produce the requested output will actually be executed.
A list of common rules for frontend development is documented in the README at https://github.com/bazelbuild/rules_nodejs/.
A list of common rules for frontend development is documented in the README at https://github.com/bazelbuild/rules_nodejs/.

View File

@ -41,7 +41,6 @@ function testBazel() {
function testNonBazel() {
# Replace angular.json that uses Bazel builder with the default generated by CLI
mv ./angular.json.bak ./angular.json
mv ./tsconfig.json.bak ./tsconfig.json
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}
ng build --progress=false

View File

@ -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
* upgrades the version of rxjs in package.json if necessary.
@ -388,10 +329,8 @@ export default function(options: Schema): Rule {
addDevDependenciesToPackageJson(options),
addPostinstallToGenerateNgSummaries(),
backupAngularJson(),
backupTsconfigJson(),
updateAngularJsonToUseBazelBuilder(options),
updateGitignore(),
updateTsconfigJson(),
upgradeRxjs(),
installNodeModules(options),
]);

View File

@ -228,29 +228,6 @@ describe('ng-add schematic', () => {
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', () => {
const cases = [
// version|upgrade