Skip to content
Permalink
Browse files

fix(bazel): do not modify tsconfig.json (#30984)

This is a patch PR for #30877

PR Close #30984
  • Loading branch information...
kyliau authored and AndrewKushnir committed Jun 11, 2019
1 parent 762fc28 commit 49307f0595ae8927620a969186c8c29e03f24809
@@ -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/.
@@ -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
@@ -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),
]);
@@ -212,29 +212,6 @@ describe('ng-add schematic', () => {
expect(builder).toBe('@angular/bazel:build');
});

it('should create a backup for original tsconfig.json', () => {
expect(host.files).toContain('/tsconfig.json');
const original = host.readContent('/tsconfig.json');
host = schematicRunner.runSchematic('ng-add', defaultOptions, host);
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', () => {
host = schematicRunner.runSchematic('ng-add', defaultOptions, host);
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

0 comments on commit 49307f0

Please sign in to comment.
You can’t perform that action at this time.