Optimize your Angular 2 Application with Tree Shaking

This post was written just after Angular's version 2.0 release. In that era the Angular CLI project was just getting started. Today they provide a complete build package so you should go over to their site if you want to get started right away.

Interested in learning more about migrating to the latest version of Angular? Follow the link here.

Disclaimer: As commenter Ben Elliot pointed out, the UglifyJs plugin used in the Webpack build is not able to remove unused Typescript classes because those, when transpiled, are implemented using IIFE. The bundle size mentioned in this post is not really reflecting tree shaking but minification. To be able to properly apply tree shaking we need to use a different module loader like Rollup. The repository referenced in this post has been updated to use Rollup instead of Webpack 2 with a resulting bundle file size of 522 KB. A new blog post will be added in the future explaining the new workflow.

Angular 2 is massive framework for creating modern web applications. Just to create a "hello world" application, we might end up with a bundle of more than 2.5 MB. If you have a 50 mbps internet connection, that might not seem problematic but most people don't have that privilege. What's more, Angular 2 has now morphed into a multi-platform framework with the goal of creating mobile applications from the same codebase as your web application. Downloading 2.5 MB in an unreliable 3G connection with high latency is a big deal.

Fortunately, we have some techniques to significantly reduce the size of our bundles. We have been using many of them for quite some time-- like minification, concatenation and compression. There are also some newer ones like lazy loading, tree shaking and Ahead of Time (AoT) compilation. To orchestrate these techniques, our tool of choice has been Webpack, mostly because it's easy to use and it's a more mature technology compared to its closest competitor, SystemJS.

The goal for this post is to show you how to create a build system for Angular 2 using Webpack, while applying most of the optimization techniques listed above. The reason to apply most and not all the techniques is due to an incompatibility of Typescript versions that the Angular compiler and Webpack 2 expects. The compiler requires Typescript 1.9 while Webpack 2, in order to be able to perform tree shaking, expects Typescript 2.x. This issue might get fixed in the next release candidate of Angular 2 (RC6) but for now, we are going to focus on tree shaking and will leave the integration with the Angular compiler for a future blog post.

Application Structure

Before we start talking about the nuances of Webpack required to create a build system, let's see the folder structure of our "Hello World" application.

Those are the libraries that our simple application might need at some point, and two of those libraries are not directly called by our code but need to be globally available in the browser. Those dependencies are zone.js and reflect-metadata. It's important to keep that in mind when we start talking about the chunks that Webpack is going to create.

Next, we are going to focus on the development dependencies of our project:

Notice that we are using the 2.x branch for all the libraries shown above. This is required in order to be able to perform tree shaking as we are going to see next.

Why Typescript 2 is Needed

Webpack 2 is able to understand the import syntax without the use of loaders and that's why it is capable of performing tree shaking. But that's pretty much the only thing from ES2015 Webpack 2 is able to understand, for any other new syntax we do need the help of a transpiler, in this case Typescript.

What is Tree Shaking? Tree shaking is the ability to remove any code that we are not actually using in our application from the final bundle. It's one of the most effective techniques to reduce the footprint of an application.

Tree shaking is one of the last things that is performed by our build system and this happens after Typescript has finished transpiling our code. The problem is that with Typescript 1.x it was not possible to preserve the import syntax after doing the transpilation to ES5. Luckily, Typescript 2 solves that and it's now possible to have a configuration file as follows:

The first two lines of the compilerOptions are key for tree shaking. We are telling Typescript to transpile our code to ES5 ("target": "es5") while preserving the import keyword for our modules ("module": "es2015"). This combination of values was not allowed in Typescript 1.x.

Configuring Webpack

The Webpack configuration is pretty standard. We are using the loader awesome-typescript-loader to perform the transpilation and applying the UglifyJsPlugin when creating a production bundle. Below, we are showing just the relevant parts of the configuration file. To see the file in detail, check the source code.

We need to keep our application and library code (Angular 2) in the same chunk.

We need to minify the files.

By default, Webpack 2 is going to perform tree shaking by not exporting the modules that are not needed by our application in the bundle file. But one thing is to not export, for example, a function, and another is to actually remove that function from the bundle. Webpack will be in charge of removing any reference of unused code in our bundle file, but is not going to remove the unused code from the bundle. It is the minifier, in this case the UglifyJsPlugin, that is smart enough to remove code (variables, functions, classes, etc.) that is not actually being used inside the bundle.

In short, when performing tree shaking, Webpack removes the links and UglifyJsPlugin removes the code.

The Results

It's time to see the results of applying tree shaking to our bundles. We are going to perform the build in development mode first to have a file size reference when tree shaking is not performed.

Our build system is creating two chunks, app and globals. The app chunk contains the code of our application and Angular 2 itself, while globals chunk contains the Javascript files that we need to have globally available: zone.js and reflect-metadata. Because our application does not explicitly depend upon those two packages, the global chunk can't be optimized by the tree shaker, it can only be optimized by the minifier.

Let's perform now a production build that applies tree shaking and minification:

While both chunks decreased substantially in size, we can see that the app chunk is the one that shrank the most. It is now around 30% its original size!

Conclusion

Having an application with a footprint of almost 1 MB is not yet good enough. There's nothing much we can do about the globals chunk, but for the app chunk we could potentially apply the AoT compiler to obtain even better results. We can save that topic for another blog post. For now, when using Webpack as a build system, tree shaking is a good mechanism for reducing the size of an Angular 2 application.