Sub topic 3
Running and Building an Angular Application
Assume you know basic TypeScript, HTML, CSS, npm, and terminal commands. The Angular CLI provides commands for development, production builds, testing, and project maintenance.
Run and Build
ng serve compiles the application and starts a development server. It watches source files and rebuilds after changes.
ng build creates deployable output in dist/. Use a named configuration when you need different build settings.
ng serve --open
ng build --configuration productionThe serve command is for local development. The build command produces files for hosting. A successful development build does not guarantee a successful production build, because production settings can enable optimization and stricter checks.
Configurations and Tests
Build configurations are defined in angular.json. Shared options apply to every build. A configuration can override those options:
{
"configurations": {
"production": {
"optimization": true,
"outputHashing": "all"
},
"development": {
"optimization": false,
"sourceMap": true
}
}
}Use configurations for development, staging, and production differences. Keep secrets out of environment files and source code. Inject secrets through the deployment system instead.
ng test runs the test target configured for the project. It usually watches files during development. Use a CI-specific configuration when your workspace provides one.
ng test
ng test --watch=falsePackages and Organization
ng add installs a package and runs its setup schematic. ng update updates Angular packages and applies supported migrations.
ng add @angular/material
ng update @angular/core @angular/cliAs an application grows, organize code by feature rather than by artifact type. For example, keep product components, services, and routes under src/app/products/. Generate code inside that feature folder:
ng generate component products/product-list
ng generate service products/productMultiple Projects and CLI Extensions
A workspace can contain several applications and libraries. A library holds reusable code and can be built independently.
ng generate library shared-ui
ng build shared-uiThe workspace configuration identifies each project and its targets. Pass --project when a command is ambiguous.
The CLI is extended through schematics and builders. A schematic generates or modifies files. A builder performs a task such as building, serving, or testing. Packages can provide both.
Exercise
Which command should you use to check a production configuration without starting a server?
Show answer
Use ng build --configuration production.