Sub topic 8
Reading angular.json
This chapter assumes you can read JSON, have created a workspace with ng new, and know that ng build and ng serve exist. angular.json sits at the workspace root. The CLI reads it to learn which projects exist, where their sources live, and how to build, serve and test each one.
The overall shape
The file has a few top-level keys. The most important is projects, an object with one entry per application or library. Each project has its own settings and a set of targets.
Project settings
Inside a project, a few keys describe where things are:
projectTypeisapplicationorlibrary.rootis the project's folder relative to the workspace root. A single-app workspace uses an empty string.sourceRootis where the source code lives, usuallysrc.
Targets under architect
The architect object holds the project's targets. A target is a named task that you run with a command such as ng build or ng test. Each target has these parts:
builderis the package and builder that does the work.optionsare the settings used every time the target runs.configurationsare named sets of overrides.defaultConfigurationis the set applied when you don't pass--configuration.
Here is a trimmed build target:
{
"projects": {
"my-app": {
"projectType": "application",
"root": "",
"sourceRoot": "src",
"architect": {
"build": {
"builder": "@angular/build:application",
"options": {
"browser": "src/main.ts",
"tsConfig": "tsconfig.app.json",
"styles": ["src/styles.css"]
},
"configurations": {
"production": { "outputHashing": "all" },
"development": { "optimization": false, "sourceMap": true }
},
"defaultConfiguration": "production"
}
}
}
}
}The browser option names the entry point, src/main.ts. The tsConfig option points to the TypeScript configuration used for this build. The styles array lists global stylesheets.
How configurations merge
When you run ng build --configuration development, the CLI starts with options and then applies the development entries on top. A key in the configuration replaces the same key in options. Keys it doesn't mention keep their base values.
With the file above, a development build keeps browser, tsConfig and styles. It then adds optimization: false and sourceMap: true. A plain ng build uses production, because that is the default configuration.
Common errors
- Editing the wrong level. A setting placed in
optionsapplies to every build. If you want it only in production, put it inconfigurations.production. - Misspelled option names. The CLI validates options against the builder's schema and fails with an error about an unknown or invalid property.
- Wrong project name. In a workspace with several projects,
ng build my-liblooks up that exact key underprojects. - Invalid JSON. A trailing comma breaks the whole file, and every CLI command then fails.
Exercise
In the angular.json above, you run ng build --configuration development. Which values apply for optimization, sourceMap and outputHashing?
Show answer
optimization is false and sourceMap is true, both set by the development configuration. outputHashing is not set in options or in development, so the builder's own default applies. The production value all is not used.
Exercise
You want source maps in every build of the app, whichever configuration is used. Where do you add "sourceMap": true?
Show answer
Add it to the options object of the build target. Entries in options apply to all configurations, and a configuration can still override them.