Angular Tree-Shaking

August 12, 2019  less than 1 minute read  

Learn about Angular tree-shaking.

Tree shaking

When a tree is shaked especially in autumn, dead leaves will fall off. In Angular context, tree shaking means shaking off unused code by Angular compiler ngc. In other words, it means removal of dead code.

Angular compiler is capable of executing tree-shaking and excludes dead code from built bundles. Dead code is the code that is not referenced in an Angular app. Tree-shaking reduces the size of JavaScript bundles (built output).

Note that tree shaking is a common term used in JavaScript context and thus available with other JavaScript libraries and frameworks too.

How to Enable Angular Tree Shaking

To enable Angular tree-shaking, use Angular compiler CLI --prod flag. Thus the command to build Angular app with tree-shaking included is

ng build --prod

Angular CLI Build --prod option description:
Angular CLI build options

Support Jun

Thank you for reading!    Support JunSupport Jun

Support Jun on Amazon US

Support Jun on Amazon Canada

If you are preparing for Software Engineer interviews, I suggest Elements of Programming Interviews in Java for algorithm practice. Good luck!

You can also support me by following me on Medium or Twitter.

Feel free to contact me if you have any questions.

Comments