0
0
Angularframework~10 mins

Bundle size analysis in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Bundle size analysis
Start Angular Build
Build Produces Bundle Files
Run Bundle Analyzer Tool
Analyze Bundle Contents
Identify Large Modules
Optimize Code (Lazy Loading, Tree Shaking)
Rebuild and Re-Analyze
Repeat Until Bundle Size Acceptable
This flow shows how Angular builds bundles, analyzes their size, identifies large parts, optimizes, and repeats to reduce bundle size.
Execution Sample
Angular
ng build --prod --stats-json
npx source-map-explorer dist/main.js
// Analyze output and optimize
Build Angular app with stats, then use source-map-explorer to see bundle size details.
Execution Table
StepActionInput/ToolOutput/Result
1Run Angular production buildng build --prod --stats-jsonGenerates dist folder with bundles and stats.json
2Run bundle analyzernpx source-map-explorer dist/main.jsVisual report showing sizes of modules in main.js
3Review reportReport with module sizesIdentify large modules like Angular core, third-party libs
4Apply optimizationsLazy loading, tree shaking, code splittingSmaller bundle size after rebuild
5Rebuild appng build --prod --stats-jsonNew bundles with reduced size
6Re-run analyzernpx source-map-explorer dist/main.jsUpdated report showing size improvements
7Repeat if neededAnalyze and optimizeBundle size meets performance goals
💡 Stop when bundle size is optimized and meets performance targets
Variable Tracker
VariableStartAfter Step 4After Step 5Final
Bundle Size (MB)10.57.26.86.8
Large Modules Count5333
Lazy Loaded Modules0222
Key Moments - 3 Insights
Why does the bundle size not reduce immediately after the first build?
The first build creates the bundles and stats.json but no optimizations are applied yet. See execution_table step 1 and 2 where analysis happens after build.
How does lazy loading affect the bundle size?
Lazy loading splits code into separate bundles loaded on demand, reducing initial bundle size. This is shown in variable_tracker where Lazy Loaded Modules increase after step 4.
Why run the analyzer multiple times?
Each time you optimize and rebuild, you run the analyzer again to verify improvements. This loop is shown in execution_table steps 6 and 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what tool is used to analyze the bundle size?
Awebpack-dev-server
Bng serve
Csource-map-explorer
Dnpm install
💡 Hint
Check execution_table step 2 for the tool used to analyze bundles.
According to variable_tracker, what happens to the bundle size after applying optimizations?
AIt increases
BIt decreases
CIt stays the same
DIt becomes zero
💡 Hint
Look at Bundle Size (MB) values from Start to After Step 5 in variable_tracker.
At which step in execution_table do you identify large modules?
AStep 3
BStep 1
CStep 5
DStep 7
💡 Hint
Step 3 in execution_table shows reviewing the report to identify large modules.
Concept Snapshot
Angular Bundle Size Analysis:
- Build with 'ng build --prod --stats-json' to get bundles and stats
- Use 'source-map-explorer' to visualize bundle contents
- Identify large modules and optimize with lazy loading and tree shaking
- Rebuild and re-analyze until bundle size is acceptable
- Repeat to keep app fast and small
Full Transcript
Bundle size analysis in Angular starts by building the app with production settings and generating a stats file. Then, a tool like source-map-explorer is run to visualize the sizes of different modules inside the bundle. This helps identify which parts of the code or libraries are large. Developers then apply optimizations such as lazy loading modules and tree shaking unused code. After rebuilding, the analysis is repeated to check if the bundle size has decreased. This process repeats until the bundle size is optimized for better app performance.