Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular CLI build target vs environment

Tags:

In the Angular CLI, what is the difference between the --target and --environment options when running the build command?

From the documentation:

ng build can specify both a build target (--target=production or --target=development) and an environment file to be used with that build (--environment=dev or --environment=prod). By default, the development build target and environment are used.

However, they never really clarify the distinction between the two.

From what I can gather, the --environment flag controls which environment file (environment.ts vs environment.prod.ts) gets used when doing the build. But then what does --target control?

like image 707
Sergey K Avatar asked Sep 11 '17 17:09

Sergey K


People also ask

What is environment TS in Angular?

A project's src/environments/ folder contains the base configuration file, environment. ts , which provides a default environment. You can add override defaults for additional environments, such as production and staging, in target-specific configuration files. For example: myProject/src/environments.

How does Angular know which environment?

angular-cli knows what environment to use, because you are passing the --environment flag. If no flag is passed, then dev environment is used.


1 Answers

--environment is a key for the apps[0].environments object from .angular-cli.json

It is like a profile for the running environment (for example: local, development server, test server, CI server, stage server, production server and so on). The value of the apps[0].environments object is a file name with all settings for the environment. There you could set up backend endpoint, keys and whatever else you want. Then you could use it inside your code:

import {environment} from '@environments/environment'; const userEndPoint = `${environment.apiRoot}/user/`; 

Every environment could be production (environment.production === true) or non production i.e. development (environment.production === false). This is a target which could be defined also with the next parameter:

--target is a enum of two values: development or production. It is a 'meta' flags, that set other flags:

Flag | --dev | --prod
--- | --- | ---
--aot | false | true
--environment | dev | prod
--output-hashing | media | all
--sourcemaps | true | false
--extract-css | false | true
--named-chunks   | true | false
--build-optimizer | false | true with AOT and Angular 5

--prod also sets the following non-flaggable settings:
- Adds service worker if configured in .angular-cli.json.
- Replaces process.env.NODE_ENV in modules with the production value (this is needed for some libraries, like react).
- Runs UglifyJS on the code.

from https://github.com/angular/angular-cli/wiki/build/1cf783837c392f5fadc7286e1fb28220b9a1b507#--dev-vs---prod-builds

like image 190
asnov Avatar answered Sep 23 '22 16:09

asnov