What’s in your Angular Bundle?

Angular Background

If you’ve ever built an Angular Application before, keeping your build lean and quick is key. With a labyrinth of configuration options and an endless sea of third-party libraries to use, it’s very easy to increase the time it takes to build your application if left unchecked.  Even more so if you work on a large team where developers are working in parallel.  So how can we get ahead of this potential pitfall so your build remains performant?   

One great way is to use the webpack bundle analyzer tool.  This tool takes in a stats file generated from the Angular CLI and provides an interactive FoamTree.  The benefit here is to quickly identify problem areas where unnecessary files may be included in your application’s distribution.  Let’s take a look at how to do this. 

Step 1

First we need to install the webpack-bundle-analyzer package and save it to our package.json.  We can do this with the below command.

npm install webpack-bundle-analyze --save-dev

Step 2

Now that the analyzer tool is installed, we need to generate a build of our application with the stats file.  Run the below to generate a production build with the stats file.

ng build --prod --stats-json
Example Output

Step 3

With the generated build, you can check your dist folder and see the generated stats.json file that was created.  That’s the file we are going to hand to the analyzer to visualize for us.

npx webpack-bundle-analyzer dist/stats.json

This will boot up a local server on port 8888 with our FoamTree.  When you hit the site it may feel a bit overwhelming with how many files are included in your bundle, but you’ll certainly find some weasels in there that could give your application a little boost. It’s that easy folks! More Angular tips and tricks to come; stay tuned!

About Rōnin Consulting – Rōnin Consulting provides software engineering and systems integration services for healthcare, financial services, distribution, technology, and other business lines. Services include custom software development and architecture, cloud and hybrid implementations, business analysis, data analysis, and project management for a range of clients from the Fortune 500 to rapidly evolving startups. For more information, please visit our website.

Brian Weiss is a UI/UX Architect hellbent on designing and developing, meaningful, human-focused applications. He's also hellbent on finding the best chocolate chip cookie this world has to offer. When he's not developing software or eating cookies, he's probably watching anime and collecting Funko Pops.

Contact Us

Give us a call or fill in the form below and we will contact you. We aim to answer all inquiries within 24 hours on business days.