Enhancements in Angular Compiler
Transitioning from Angular 4 to 5 brought about significant improvements in the compiler. The Angular compiler underwent a complete rewrite to enhance its speed and efficiency. Previously, Angular applications utilized Just-in-Time (JIT) compilation, which meant the app was compiled at runtime in the browser before execution. With the upgrades in Angular 5, the shift towards Ahead-of-Time (AOT) compilation was accelerated, resulting in faster app performance by reducing the amount of compilation needed during runtime. AOT compilation now comes enabled by default in all production builds starting from version 1.5 of the Angular CLI.
When preparing an application for deployment, executing the following command is pivotal:
ng build --prod
Several optimizations take place, including the generation of a production version, minification, bundling assets, filename hashing, tree shaking, AOT compilation, among others (toggle functionalities with flags like aot=false). Essentially, the --prod flag produces an optimized bundle of the application through AOT compilation utilizing the ngc (Angular compiler), resulting in streamlined code tailored for the browser (Yes, it pre-compiles templates).
Evolution of TypeScript Compiler
The TypeScript compiler, tsc, is responsible for transpiling TypeScript files and implementing TypeScript features, such as static types, into pure JavaScript devoid of TypeScript-specific keywords and expressions.
The TypeScript compiler encompasses two primary functions: transpilation and type checking. It transforms TypeScript code into JavaScript by:
- Removing all type annotations.
- Compiling new JavaScript features for older versions.
- Converting TypeScript-specific features into standard JavaScript.
During invocation, the compiler references configurations outlined in tsconfig.json (Detailed compiler options with default values can be found here).
While the TypeScript compiler operates like most compilers, there's a notable distinction – it continues emitting JavaScript code even when it encounters errors by default. To counter this, set the noEmitOnError
configuration to true in the tsconfig.json file.
Important Note: tsc and ngc serve distinct purposes, and it's not a matter of choosing one over the other. For further clarity, refer to this insightful answer.
This response was formulated based on information from the following resources:
Cloe, M. (2018). "Angular 5 Projects: Learn to Build Single Page Web Applications Using 70+ Projects".
Dewey, B., Grossnicklaus, K., Japikse, P. (2017). "Building Web Applications with Visual Studio 2017: Using .NET Core and Modern JavaScript Frameworks".
Freeman, A. (2019). "Essential TypeScript: From Beginner to Pro".
Ghiya, P. (2018). "TypeScript Microservices".
Iskandar, A., Chivukulu, S. (2019). "Web Development with Angular and Bootstrap - Third Edition".
Hennessy, K., Arora, C. (2018). "Angular 6 by Example".
Jansen, R., Wolf, I., Vane, V. (2016). "TypeScript: Modern JavaScript Development".
Mohammed, Z. (2019). "Angular Projects".
Seshadri, S. (2018). "Angular: Up and Running".
Wilken, J. (2018). "Angular in Action".