Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Bundle Size Analysis in Angular
📖 Scenario: You are working on an Angular project and want to understand how to analyze the size of your app's JavaScript bundles. This helps you find out which parts of your app make the bundle bigger and how to optimize loading speed.
🎯 Goal: Learn how to generate and read a bundle size report in Angular using the Angular CLI. You will create a simple Angular component, configure the build to produce a stats file, and then analyze the bundle size report.
📋 What You'll Learn
Create a new Angular component named bundle-analyzer with a simple template
Add a configuration variable to enable stats JSON generation during build
Run the Angular build command with stats generation enabled
Add a script or command to analyze the generated bundle stats
💡 Why This Matters
🌍 Real World
Bundle size analysis helps developers understand what parts of their Angular app make the app large. This knowledge is useful to optimize loading speed and user experience.
💼 Career
Many frontend developer roles require knowledge of build optimization and bundle size analysis to deliver fast and efficient web applications.
Progress0 / 4 steps
1
Create a simple Angular component
Create a new Angular component named bundle-analyzer with a template that contains a <p> tag showing the text Bundle Size Analysis Component.
Angular
Hint
Use the @Component decorator with selector and template properties. Export a class named BundleAnalyzerComponent.
2
Add build configuration to generate stats JSON
In your Angular project's angular.json file, add or update the build options to include statsJson: true under architect > build > options.
Angular
Hint
Open angular.json and find the build options section. Add "statsJson": true inside the options object.
3
Build the Angular project to generate stats file
Run the Angular build command ng build --configuration production in your terminal to create the production build and generate the stats.json file.
Angular
Hint
Use the Angular CLI command ng build --configuration production to build the app and create the stats file.
4
Analyze the generated bundle stats
Add a script in your package.json named analyze that runs npx webpack-bundle-analyzer dist/your-project/stats.json to open a visual bundle size report. Replace your-project with your actual project folder name.
Angular
Hint
Open package.json and add a new script named analyze that runs npx webpack-bundle-analyzer on your stats.json file.
Practice
(1/5)
1. What is the main purpose of bundle size analysis in Angular applications?
easy
A. To change the app's color scheme
B. To add more features to the Angular app
C. To find which parts of the app make the bundle large
D. To increase the app's loading time
Solution
Step 1: Understand bundle size analysis goal
Bundle size analysis helps identify what parts of the app increase the size of the final build.
Step 2: Match purpose with options
Only To find which parts of the app make the bundle large correctly states the purpose as finding large parts in the bundle.
Final Answer:
To find which parts of the app make the bundle large -> Option C
Quick Check:
Bundle size analysis = find large parts [OK]
Hint: Think: Why check bundle size? To find big parts [OK]
Common Mistakes:
Confusing bundle size analysis with adding features
Thinking it changes app appearance
Assuming it slows down the app
2. Which Angular CLI command generates a JSON file useful for bundle size analysis?
easy
A. ng build --stats-json
B. ng serve --stats-json
C. ng test --stats-json
D. ng lint --stats-json
Solution
Step 1: Recall Angular CLI commands for building
The command to build the app and generate stats is ng build --stats-json.
Step 2: Identify correct option
ng build --stats-json matches the correct command to create the JSON file for bundle analysis.
Final Answer:
ng build --stats-json -> Option A
Quick Check:
Build with stats JSON = ng build --stats-json [OK]
Hint: Build command with --stats-json creates analysis file [OK]
Common Mistakes:
Using ng serve instead of ng build
Confusing test or lint commands with build
Forgetting to add --stats-json flag
3. After running ng build --stats-json, you use source-map-explorer on the generated JSON. What will you see?
medium
A. A list of unused CSS styles
B. A list of errors in your Angular code
C. A report of unit test results
D. A visual map showing which files contribute most to bundle size
Solution
Step 1: Understand what source-map-explorer does
This tool shows a visual breakdown of the bundle, highlighting file sizes.
Step 2: Match output with options
A visual map showing which files contribute most to bundle size correctly describes the visual map of file size contributions.
Final Answer:
A visual map showing which files contribute most to bundle size -> Option D