|
1 | 1 | # Application Environments
|
2 | 2 |
|
3 |
| -## Configuring available environments |
| 3 | +In Angular CLI you can configure the build system to replace existing files for your intended |
| 4 | +environment. |
4 | 5 |
|
5 |
| -`.angular-cli.json` contains an **environments** section. By default, this looks like: |
| 6 | +## Configuring available file replacements |
6 | 7 |
|
7 |
| -``` json |
8 |
| -"environments": { |
9 |
| - "dev": "environments/environment.ts", |
10 |
| - "prod": "environments/environment.prod.ts" |
11 |
| -} |
12 |
| -``` |
13 |
| - |
14 |
| -You can add additional environments as required. To add a **staging** environment, your configuration would look like: |
| 8 | +`angular.json` contains an **fileReplacements** section within the production configuration of the |
| 9 | +build target. By default, this looks like: |
15 | 10 |
|
16 | 11 | ``` json
|
17 |
| -"environments": { |
18 |
| - "dev": "environments/environment.ts", |
19 |
| - "staging": "environments/environment.staging.ts", |
20 |
| - "prod": "environments/environment.prod.ts" |
| 12 | +"configurations": { |
| 13 | + "production": { |
| 14 | + "fileReplacements": [ |
| 15 | + { |
| 16 | + "replace": "src/environments/environment.ts", |
| 17 | + "with": "src/environments/environment.prod.ts" |
| 18 | + } |
| 19 | + ], |
| 20 | + ... |
| 21 | +``` |
| 22 | + |
| 23 | +This means that when you use your production configuration (via `ng build --prod` or |
| 24 | +`ng build --configuration=production`), the `src/environments/environment.ts` file will be replaced |
| 25 | +with `src/environments/environment.prod.ts`. |
| 26 | + |
| 27 | +This is useful for using different code or variables when creating a new build. |
| 28 | +By default no file is replaced in the build. |
| 29 | + |
| 30 | +You can add additional configurations as required. |
| 31 | +To add a **staging** environment, create a copy of `src/environments/environment.ts` called `src/environments/environment.staging.ts`, then add a `staging` configuration to `angular.jsob`: |
| 32 | + |
| 33 | +```json |
| 34 | +"configurations": { |
| 35 | + "production": { ... }, |
| 36 | + "staging": { |
| 37 | + "fileReplacements": [ |
| 38 | + { |
| 39 | + "replace": "src/environments/environment.ts", |
| 40 | + "with": "src/environments/environment.staging.ts" |
| 41 | + } |
| 42 | + ] |
| 43 | + } |
21 | 44 | }
|
22 | 45 | ```
|
23 | 46 |
|
24 |
| -## Adding environment-specific files |
25 |
| - |
26 |
| -The environment-specific files are set out as shown below: |
27 |
| - |
28 |
| -``` |
29 |
| -└── src |
30 |
| - └── environments |
31 |
| - ├── environment.prod.ts |
32 |
| - └── environment.ts |
33 |
| -``` |
34 |
| - |
35 |
| -If you wanted to add another environment for **staging**, your file structure would become: |
36 |
| - |
37 |
| -``` |
38 |
| -└── src |
39 |
| - └── environments |
40 |
| - ├── environment.prod.ts |
41 |
| - ├── environment.staging.ts |
42 |
| - └── environment.ts |
| 47 | +You can add more configuration options to this environment as well. |
| 48 | +Any option that your build supports can be overriden in a configuration. |
| 49 | + |
| 50 | +To build using the staging configuration, run `ng build --configuration=staging`. |
| 51 | + |
| 52 | +To serve using the staging configuration, you must edit the `serve` target to use the `staging` |
| 53 | +build configuration: |
| 54 | +```json |
| 55 | +"serve": { |
| 56 | + "builder": "@angular-devkit/build-angular:dev-server", |
| 57 | + "options": { |
| 58 | + "browserTarget": "your-project-name:build" |
| 59 | + }, |
| 60 | + "configurations": { |
| 61 | + "production": { |
| 62 | + "browserTarget": "your-project-name:build:production" |
| 63 | + }, |
| 64 | + "staging": { |
| 65 | + "browserTarget": "your-project-name:build:staging" |
| 66 | + } |
| 67 | + } |
| 68 | +}, |
43 | 69 | ```
|
44 | 70 |
|
45 |
| -## Amending environment-specific files |
| 71 | +## Changing environment-specific files |
46 | 72 |
|
47 | 73 | `environment.ts` contains the default settings. If you take a look at this file, it should look like:
|
48 | 74 |
|
@@ -103,20 +129,6 @@ export class AppComponent {
|
103 | 129 | }
|
104 | 130 | ```
|
105 | 131 |
|
106 |
| -## Environment-specific builds |
107 |
| - |
108 |
| -Running: |
109 |
| - |
110 |
| -``` |
111 |
| -ng build |
112 |
| -``` |
113 |
| - |
114 |
| -Will use the defaults found in `environment.ts` |
115 |
| - |
116 |
| -Running: |
117 |
| - |
118 |
| -``` |
119 |
| -ng build --env=staging |
120 |
| -``` |
| 132 | +You will always import the original environments file. |
| 133 | +This way the build system can replace the original in each configuration. |
121 | 134 |
|
122 |
| -Will use the values from `environment.staging.ts` |
|
0 commit comments