AI Hints
Home›Angular Tutorial›Building for Production, @defer and Deploying
Angular · Lesson 109 of 120

Building for Production

Ready for real users.

⏱ 2 min read▶ Video at 2:10:45🎓 Free
Watch this lesson in the Angular courseWatch on YouTube ↗Full Angular course

Ng serve is made for development. It's quick to rebuild, and easy to debug. Real users need something different. Small files, that load fast.

  • ng serve: for development
  • Users need speed
  • A production build

Development or production?

A development build keeps names readable, and adds extra checks, to help you debug. A production build removes unused code, shrinks names, and compresses everything. The files are much smaller.

Development: ng serve

  • readable codeextra checksbigger files

Production: ng build

  • unused code removedminifiedsmall files

ng build

This is a small shop page. Ng build makes a production build, by default. Run it. The whole app is in one main file, of about two hundred and thirty kilobytes. Compressed for the network, it's about sixty four.

app.tstypescript
import { Component, signal } from '@angular/core';
import { CurrencyPipe } from '@angular/common';
import { BOOKS } from './books';

@Component({
  selector: 'app-root',
  imports: [CurrencyPipe],
  template: `
    <h1>Page Turners</h1>
    @for (b of books(); track b.id) {
      <p>{{ b.title }} · {{ b.price | currency: 'GBP' }}</p>
    }
  `,
})
export class App {
  books = signal(BOOKS);
}
Output
Initial chunk files | Names | Raw size | Estimated transfer size
main-RRB52YE2.js | main | 231.19 kB | 63.71 kB
styles-RFJL2J33.css | styles | 790 bytes | 790 bytes
| Initial total | 231.99 kB | 64.50 kB
Application bundle generation complete. [0.940 seconds]
Also uses books.ts
export interface Book {
  id: number;
  title: string;
  author: string;
  price: number;
  stock: number;
}

export const BOOKS: Book[] = [
  { id: 1, title: 'Dune', author: 'Frank Herbert', price: 9.99, stock: 4 },
  { id: 2, title: 'The Hobbit', author: 'J.R.R. Tolkien', price: 8.99, stock: 0 },
  { id: 3, title: 'Circe', author: 'Madeline Miller', price: 10.99, stock: 2 },
  { id: 4, title: 'Emma', author: 'Jane Austen', price: 6.99, stock: 7 },
  { id: 5, title: 'Persuasion', author: 'Jane Austen', price: 7.49, stock: 1 },
];
Also uses _mode
prod

What the build does

It compiles TypeScript, and templates, into JavaScript. It removes code that's never used, which is called tree shaking. It minifies, by shortening names, and removing spaces. And it adds a hash to each file name, so browsers never use an old copy.

keyvalue
CompileTS + templates → JS
Tree shakingdrop unused code
Minifyshort names, no spaces
main-FQWW5NZR.jsa hash in the name

Summary

Ng build makes small, fast production files, in the dist folder. Next, deferring parts of the page.

Key takeaways
  • ng build
  • dist/shop
  • Minified + hashed
Next up: @defer — load parts later