Analyzing Angular bundle with Source Map Explorer
Tags: angular, performance, sourcemapexplorer
Published: 2021-03-11 20:49:14

Performance matters, you probably already heard that before. Today, in FE applications we add tons of code to get our tasks done. In the meantime, we add modules, dependencies, functions, templates, directives, etc.

It is very important to have a clear picture of our applications so we can make improvements based on that. 

In this post we will see how to include source-map-explorer into our Angular app.

**What is source-map-explorer?**
The source map explorer determines which file each byte in your minified code came from. It shows you an interactive tree-map visualization to help you debug where all the code is coming from. 


Before starting I would like to recommend this [video](https://www.youtube.com/watch?v=-eH2gCGHcGs&t=995s&ab_channel=DemoswithAngular) from Stephen Fluin, angular team member. You will find some very interesting concepts about how to measure and improve performance. Additionally, the video recommends source-map-explorer.

I have created a simple application with some dependencies and modules including. The app only lists the dependencies with repo and npm links.
The goal is to see how the map looks.

*Disclaimer: this is not a real world app, so you will see more benefits of this tool on medium/big applications. Give it a try on your side!*
* ngx-moment
* ngx-date-fns
* angular-calendar
* ngx-mask
* ng-bootstrap

demo app
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j6xawfe128o0d1ijqv9u.gif)

**Install source-map-explorer**
1- `npm install source-map-explorer`
2- on **angular.json**, make sure you have these 2 flags in true
```javascript
"sourceMap": true, // this is to enable the source maps to get a better analysis
"namedChunks": true, // to get identify modules names and not use random numbers for lazy loading modules
```
3- `ng build prod`
4- On package.json, include this on scripts section (**make sure you replace your app name after dist/**)
```javascript
"source-map-explorer": "source-map-explorer dist/your-angular-app-name/**/*.js"
```
5- `npm run source-map-explorer`

Let's see how it looks (yes, moment is heavy)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cs3sus1y5ebsp2s2b3ad.png)

There is a dropdown that allows you to navigate chunks

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7k0wgc2h8ktrh4iyxiku.gif)

And that it's! Let's wrap up with some conclusions

* source-map-explorer is a very helpful tool to understand how your bundle is to then make improvements
* there is another tool called **webpack-bundle-analyzer** (I wrote this [post](https://dev.to/salimchemes/analyzing-angular-bundle-with-webpack-bundle-analyzer-29gp) about it). The reason source-map-explorer is more accurate is because Angular has some features built on top of webpack. So to webpack-bundle-analyzer could misscategorize or missatribute some of the source code impacting the bundle result
* bundle size is the most important thing to be aware in terms on performance. Before adding any new module/dependency, try to think on how this is going to impact on the bundle.


References
* [repo](https://github.com/salimchemes/angular-bundle-analyzer)
* [Stephen Fluin video](https://github.com/salimchemes/angular-bundle-analyzer)
* [npm](https://www.npmjs.com/package/source-map-explorer)

Thanks for reading!