Angular 9.0.0 and Ivy improvements
One of the biggest updates in the pipeline right now is the upcoming release of Angular 9.0.0. Currently in RC, Angular 9.0 will hopefully ship early next year. But this release includes so much that I thought it required a post to highlight some of the most important updates. Now for Ionic devs, why should they care about this? Well given that most apps currently are built with Angular and Ionic, any improvements to the overall ecosystem should always be welcomed! But given the goals of Angular 9.0, this directly impacts (in a positive way) Ionic be being able to ship faster and smaller apps, in faster manner. For reference, we’ll be looking at the Angular version of the Star Track demo app – built with Ionic 4 and Angular – and checking out the improvements.
Ivy By Default
One of the biggest improvements to Angular coming in 9.0 is that Ivy is enabled by default. Ivy is Angular’s new renderer. A super high level overview is that Ivy enables apps to only require pieces of the render that they actually need, instead of the whole thing. This means that our final output will be smaller, which is always better for performance. It’s important to note, that Ivy won’t make your components/app significantly smaller if you’re using every feature available to Angular. But there are noticeable improvements to the main.js
chunk of an app.
For comparison, let’s look at the build output from Star Track with Ivy enabled and with it disabled. We’ll compare the size of the main.js
file, which is mostly going to be Angular specific code.
# Ivy disabled
$ ng build --prod
...
chunk {10} main-es2015.d03d9cadf1579320f520.js (main) 537 kB [initial] [rendered]
chunk {10} main-es5.d03d9cadf1579320f520.js (main) 628 kB [initial] [rendered]
...
# Ivy enabled
$ ng build --prod
...
chunk {10} main-es2015.bfc9e260b847bc2b02fc.js (main) 465 kB [initial] [rendered]
chunk {10} main-es5.bfc9e260b847bc2b02fc.js (main) 551 kB [initial] [rendered]
..
From 537kb/628kb without Ivy, to 465kb/551kb with it! Note that Star Track is using almost every feature available from Angular, so delta is a bit smaller, but still a welcomed improvement!
Now Ivy has been around for sometime in earlier release, but always as an opt-in feature. With 9.0, however, Ivy is enabled by default and developers need to opt-out manually. If Ivy is so great, why would developers need to opt-out of it? Well, as this is part of a new major version, there are bound to be some breaking changes here and there. Most of these are not change that app developers need to worry themselves with, but something that component authors would need to deal with. If you’re using third-party components/packages that have not updated, you’ll want to opt-out of Ivy for now, and open an issue (or better yet, a pull request) against the repo.
Ionic itself will be ready for Ivy when Angular 9.0 is shipped. I’ve been following along and making the necessary updates needed. We recently have merged a pull request and have been testing a build of Ionic and Angular 9.0.
EntryComponents…BYE BYE BYE
If you’ve been working with Angular for some time now, you know that one of the most frustrating things to deal with are entryComponents
. entryComponents
, for the uninitiated, are components that Angular will load imperatively and not through a template. For Ionic, this means overlay components like Modals and Popovers make heavy use of entryComponent
for figuring out what to actually render. From a users perspective, there’s often a bit of a hiccup when working with Modals/Popovers. The process typically goes:
- Generate a new component
- Splitting it out into its own module
- Attempt to load the component in an overlay
- Get an obscure error about a Module Factory
- Realize you forgot to add setup entryComponents
This was such a common issue that we even modified the component schematic from Angular to include options for auto-adding components to entryComponents
. Thankfully, with 9.0, this is a thing of the past!
With the way components are compiled (thanks to Ivy), entryComponent
s are no longer needed. So Modals and Popovers just become much easier to think about.
CLI Improvements
In addition to improvements coming to the framework itself, Angular’s CLI is also getting some improvements as well. For starters, when serving your app, the app will automatically be built using AOT. This is a great improvement as we no longer have to deal with different environments for our apps. For example; you serve your app with ionic serve
or ng serve
and the app builds fine. There are no errors, the app loads, all is well! But, when you go to deploy and do a production build, an error appears. Why is the error there and why did it show up only during production? Well due to how components are built, AOT often is able to find potential errors before they happen at run time. Say you have a click handler like so:
<ion-content>
<h1>Click the button</h1>
<ion-button (click)="logEvent($event)">Click me</ion-button>
</ion-content>
And the component class looks like so:
@Component({...})
export class MyComponent{
constructor(){...}
logevent(event: any){
console.log(event)
}
}
The small error here is that the click handler is for logEvent
, but the actual method is called logevent
. This would result in a runtime error, which we do not want. Thanks to being able to run AOT when running serve, we can now catch small errors like this and get meaningful feedback before they ever happen.
Another improvement is with differential builds. In previous releases, the Angular CLI would perform two different builds, on for ES5 environments and one for ES2015. This process produced the ES5 builds first, then would go back and rebuild the app for ES2015, which could make your production builds take a long time if the app was rather large. With 9.0, the CLI will now change how this is done. Instead of building the ES5 version, the CLI will do a ES2015 build of your app first. From there, that ES2015 build will go through a much quick build process to produce a ES5 build with the necessary polyfills. This is a small and subtle change, but can make your build time much faster!
Parting Thoughts
Angular 9.0 is still in RC (RC6) as of now and most likely won’t be shipping before the year is over (good news IMO). But with all the improvements we’ve gone over here, Angular 9.0 will be a welcome update that should help every developer be successful! Cheers 🍻