Skip to content
intermediate Phase 1 · Scaffolding Your First Angular App

Building for Production

Produce an optimized build output ready for any static host.

35m
0 problems
Topic Progress 0%

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

  1. Ahead-of-Time (AOT) compilation — Templates are compiled to JavaScript at build time, not in the browser
  2. Tree shaking — Unused code is eliminated from the final bundle
  3. Minification — Variable names are shortened and whitespace removed
  4. Bundling — All JavaScript is combined into a small number of files
  5. 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?

Question 1 options

2. What happens when a bundle exceeds the maximumError budget?

Question 2 options

3. Which tool helps analyze Angular bundle composition?

Question 3 options

Flashcards

Question

How do you create a production build?

Answer

ng build --configuration production — outputs to dist/<project>/browser/.

Question

What is AOT compilation?

Answer

Ahead-of-Time compilation — templates are compiled to JavaScript at build time, not in the browser.

Question

What are build budgets?

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 size
  • any — any single file
  • anyComponentStyle — per-component CSS

Analyze bundles:

ng build --stats-json
npx webpack-bundle-analyzer dist/my-app/browser/stats.json

Continue Learning