How to Use Source Map Explorer in Angular for Bundle Analysis
source-map-explorer in Angular, first install it with npm install -D source-map-explorer. Then build your Angular app with source maps enabled using ng build --prod --source-map and run npx source-map-explorer dist/your-app-name/main.*.js to visualize the bundle contents.Syntax
The basic syntax to use source-map-explorer with Angular is:
ng build --prod --source-map: Builds your Angular app in production mode with source maps enabled.npx source-map-explorer <path-to-js-file>: Runs the explorer on the generated JavaScript bundle file.
You replace <path-to-js-file> with the actual path to your main JavaScript bundle inside the dist folder.
ng build --prod --source-map npx source-map-explorer dist/your-app-name/main.*.js
Example
This example shows how to build an Angular app with source maps and analyze the main bundle using source-map-explorer. It helps you see which libraries or code parts take the most space.
npm install -D source-map-explorer ng build --prod --source-map npx source-map-explorer dist/my-angular-app/main.*.js
Common Pitfalls
1. Not enabling source maps: If you skip --source-map during build, source-map-explorer cannot map the minified code back to original files.
2. Wrong file path: Using an incorrect path or filename for the bundle will cause errors. Use glob patterns like main.*.js to match hashed filenames.
3. Using production build without source maps: Production builds often disable source maps by default, so you must explicitly enable them.
ng build --prod npx source-map-explorer dist/my-angular-app/main.*.js # Wrong: No source maps, error or empty output # Correct: ng build --prod --source-map npx source-map-explorer dist/my-angular-app/main.*.js
Quick Reference
- Install source-map-explorer as a dev dependency.
- Build Angular app with
--source-mapflag. - Run
npx source-map-exploreron the main JS bundle. - Use glob patterns for hashed filenames.
- View interactive bundle size visualization in your browser.