Creating a Production Build
The production build compiles your Angular application into optimized static files ready for deployment.
Building for Production
ng build --configuration production
Or simply:
ng build
The default configuration is production when no --configuration flag is passed.
What the Production Build Does
- Ahead-of-Time (AOT) compilation — Templates are compiled to JavaScript at build time, not in the browser
- Tree shaking — Unused code is eliminated from the final bundle
- Minification — Variable names are shortened and whitespace removed
- Bundling — All JavaScript is combined into a small number of files
- Hashing — Filenames include content hashes for cache busting
Output Structure
dist/my-app/
├── browser/
│ ├── index.html
│ ├── main.3f8a2b1c.js
│ ├── polyfills.7d4e5f6a.js
│ ├── runtime.1a2b3c4d.js
│ ├── styles.9e8d7c6b.css
│ ├── favicon.ico
│ └── assets/
│ └── ...
└── server/ # Only if SSR is enabled
└── server.mjs
Understanding the Bundles
| Bundle | Purpose |
|---|---|
main.*.js |
Your application code |
polyfills.*.js |
Browser compatibility (Zone.js, etc.) |
runtime.*.js |
Webpack runtime — loads other chunks |
styles.*.css |
Global styles |
Build Output Size
A minimal Angular app produces:
main.js— ~150-200 KB (gzipped: ~50-60 KB)polyfills.js— ~30 KB (gzipped: ~10 KB)runtime.js— ~2 KB
The CLI warns when bundles exceed configured budgets.
Build Budgets and Performance Enforcement
Build budgets are size thresholds defined in angular.json that fail the build when exceeded. They prevent bundle size regressions from reaching production.
Configuring Budgets
{
"architect": {
"build": {
"configurations": {
"production": {
"budgets": [
{
"type": "initial",
"maximumWarning": "500kb",
"maximumError": "1mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "2kb",
"maximumError": "4kb"
},
{
"type": "any",
"maximumWarning": "250kb",
"maximumError": "500kb"
}
]
}
}
}
}
}
Budget Types
| Type | What It Measures |
|---|---|
initial |
Total size of all initial bundles |
any |
Any single bundle file |
anyComponentStyle |
CSS for a single component |
anyScript |
Any single JS file |
all |
Total of all files |
When Budgets Are Exceeded
- Warning — Build succeeds, prints a yellow warning
- Error — Build fails with a non-zero exit code
Analyzing Bundle Size
# Build with stats JSON
ng build --stats-json
# Analyze with webpack-bundle-analyzer
npx webpack-bundle-analyzer dist/my-app/browser/stats.json
This produces an interactive treemap showing which modules contribute most to bundle size.
Deployment
The dist/my-app/browser/ folder contains static files. Deploy to:
- Netlify/Vercel — Drag and drop or connect a Git repo
- GitHub Pages —
npx angular-cli-ghpages --dir=dist/my-app/browser - Nginx — Copy files to the web root
- AWS S3 + CloudFront — Upload to S3, serve via CloudFront
- Firebase Hosting —
firebase deploy --only hosting
All these serve the static files and route all requests to index.html for client-side routing.
Quiz
1. What does AOT compilation do in a production build?
2. What happens when a bundle exceeds the maximumError budget?
3. Which tool helps analyze Angular bundle composition?
Flashcards
Question
How do you create a production build?
Click to reveal answer
Answer
ng build --configuration production — outputs to dist/<project>/browser/.
Question
What is AOT compilation?
Click to reveal answer
Answer
Ahead-of-Time compilation — templates are compiled to JavaScript at build time, not in the browser.
Question
What are build budgets?
Click to reveal answer
Answer
Size thresholds in angular.json that warn or fail the build when bundles exceed limits.
Revision Notes
Key Takeaways
- 1. ng build produces optimized static files in dist/
- 2. AOT compilation moves template compilation to build time
- 3. Build budgets enforce size limits and fail the build when exceeded
- 4. The browser/ folder is what you deploy to static hosts
Interview Tips
- • Explain the difference between JIT and AOT compilation
- • Be ready to describe tree shaking and how it reduces bundle size
- • Mention build budgets as a CI/CD quality gate
Cheat Sheet
Production Build Cheat Sheet
Build:
ng build --configuration production
Output: dist/<project>/browser/
Key optimizations:
- AOT compilation
- Tree shaking
- Minification
- Content hashing
Budget types:
initial— total initial bundle sizeany— any single fileanyComponentStyle— per-component CSS
Analyze bundles:
ng build --stats-json
npx webpack-bundle-analyzer dist/my-app/browser/stats.json