In this lecture, we're going to talk about the compilation strategy and their uses to compile our app.
We've talked about how we need to load anglers code in the main entry file.
It's the first step we must take.
But what exactly are we loading?
Why do we need to load angular first?
Do you answer this question?
We need to talk about compilation compilation is common in every programming language.
It's the process of transforming human readable code into machine code.
This is not always true.
For example, TypeScript is compiled into JavaScript.
Both languages are human readable compilation as a very broad term for transforming code from one language
to another.
So why am I bringing up compilation?
Browsers do not understand the inner mechanics of an angular app.
There are features in angular not understood by the browser.
These features need to be compiled.
A new problem arises with this step in the process.
When do we compile our code?
That's a very important question.
It can impact the performance of an app.
Angular provides two compilation strategies called Just in Time Compilation and Ahead of Time compilation.
Both strategies will compile our code from TypeScript to JavaScript.
It's not a difference of what, but when.
Let's dive into just in time compilation first.
This strategy will compile our code in the browser.
The browser will compile our code when the files are downloaded in the browser.
Let's look at how this works.
First, the server will compile our TypeScript code.
This process happens regardless if we're using just in time or ahead of time compilation.
Once that's finished, the browser will download our code.
This includes the compiler, which is very big.
There's a lot of code that needs to be processed.
The browser doesn't understand features like templates, services and modules.
These are features we will be exploring in a future lecture.
Angular needs to take the time to help the browser understand these features.
After compilation is complete, the browser will finally be able to run our app.
It's a complicated process.
We don't need to understand how it works underneath.
Having a high level overview will suffice.
Let's look at the next strategy, which is ahead of time compilation.
This strategy will compile our code on the server.
This process has been moved from the browser to the server.
All the browser needs to do is download the code.
Here's an overview of what happens.
The ceiling line will compile our TypeScript code into JavaScript after it's finished compiling our
TypeScript code.
It'll move on to compiling the templates, services and modules.
After the compilation is complete, the browser will download the app.
It'll be able to run the app immediately.
The compiler doesn't get downloaded, either.
Both strategies are available in Angular, which should we use?
Let's compare them side by side.
The file size difference between both strategies is significant.
With just in time compilation, the file size is large.
It's because the app needs to be shipped with the compiler.
It's almost half the size of the app itself.
In comparison, the browser doesn't need to download the compiler with ahead of time compilation.
This difference impacts performance.
Not only does the browser need to download the compiler, but it has to run the compiler.
Otherwise, our app won't work, on the other hand.
The app can run in the browser with aoat compilation after it's finished downloading.
It's much easier.
Lastly, errors appear at different times.
We won't spot an error until the app is running in the browser with Git compilation.
A02 compilation will show the error in the command line.
As you can probably tell, there is one superior strategy.
In most cases, we will want to use Aoki compilation.
So why does Angular include Git compilation?
EOG compilation is relatively new in the beginning.
Git was the only option we had somewhere around angular nine.
The team introduced the Aot compiler.
It slowly became the default compiler for production and development builds.
Digit compiler is kept around for those who are still running on the old compiler.
However, it's recommended we always use AOG compilation for the rest of this course.
We're going to be using AOC compilation.
Let's start applying this knowledge in the next lecture.