-
Notifications
You must be signed in to change notification settings - Fork 84
Converting an Angular CLI workspace
This doc shows how to manually add a working Bazel setup to an existing application created by Angular CLI.
In the future, we expect that a command like
ng add @angular/bazel
will do these steps for you, and you'll only need to interact with theng
command forbuild
,test
,serve
, etc
Install Bazel: https://docs.bazel.build/versions/master/install.html
Install ibazel (watch mode for Bazel): yarn global add @bazel/ibazel
or npm install -g @bazel/ibazel
Instructions below assume your project uses yarn.
Known issue on Mac: "Too many open files" error, will be fixed in next release.
Copy these files to your workspace folder (next to angular-cli.json
) from https://github.com/alexeagle/canonical-angularcli-app/tree/master
.bazelrc
WORKSPACE
BUILD.bazel
src/BUILD.bazel
e2e/BUILD.bazel
e2e/protractor.on-prepare.js
yarn add -D typescript@3.1 @bazel/typescript @bazel/ibazel
Update your application bootstrap to use AOT style. Import from @angular/platform-browser
rather than @angular/platform-browser-dynamic
. (Angular CLI does this transformation behind the scenes for prod builds. Under Bazel we always do AOT so we can be more explicit and less magical)
Note, we are going to import from generated code. After the first build, you can adjust the
paths
in yourtsconfig.json
to point your editor to the generated code to make the red squiggles go away. When Angular ships the new Ivy compiler, we'll stop importing generated code.
Angular CLI transforms your html file to add script tags. Under Bazel we prefer to be more explicit.
Just add after your bootstrap element <app-root>
:
<script src="/zone.min.js"></script>
<script src="/bundle.min.js"></script>
In every *.spec.ts
file, add:
import {BrowserDynamicTestingModule, platformBrowserDynamicTesting} from '@angular/platform-browser-dynamic/testing';
try {
TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
} catch {
// Ignore exceptions when calling it multiple times.
}
If you don't plan to use Angular CLI anymore, you can delete some files that are no longer referenced:
e2e/protractor-conf.js
src/karma.conf.js
src/test.ts
https://github.com/alexeagle/angular-bazel-example/tree/cli is a demo of how you'll be able to use the ng
command soon. In the meantime you should be able to run these commands:
- Development server, watching sources
ibazel run src:devserver
(instead ofng serve
) - Run unit tests:
bazel test //src/...
(instead ofng test
) - Build the production bundle:
bazel build src:bundle
(instead ofng build --prod
- note that under Bazel we never do JIT so all bundles are for production) - Run protractor tests:
bazel test e2e:all
(instead ofng e2e
)
Many commands are unchanged, such as ng lint
, ng generate
, etc.
To see the outputs, we recommend running:
ln -s $(bazel info bazel-bin) dist
so that you get a dist
folder matching the output from ng
commands.
- Turn this into a schematic that works with
ng add
- Eliminate the need to edit sources if possible, especially adding the TestBed initializer