Skip to content

Commit c657f22

Browse files
committed
docs: update application environments
1 parent 518ff63 commit c657f22

File tree

1 file changed

+61
-50
lines changed

1 file changed

+61
-50
lines changed

docs/documentation/stories/application-environments.md

Lines changed: 61 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,73 @@
11
# Application Environments
22

3-
## Configuring available environments
3+
In Angular CLI you can configure the build system to replace existing files for your intended
4+
environment.
45

5-
`.angular-cli.json` contains an **environments** section. By default, this looks like:
6+
## Configuring available file replacements
67

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:
1510

1611
``` 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+
This means that when you use your production configuration (via `ng build --prod` or
23+
`ng build --configuration=production`), the `src/environments/environment.ts` file will be replaced
24+
with `src/environments/environment.prod.ts`.
25+
26+
This is useful for using different code or variables when creating a new build.
27+
By default no file is replaced in the build.
28+
29+
You can add additional configurations as required.
30+
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`:
31+
32+
```json
33+
"configurations": {
34+
"production": { ... },
35+
"staging": {
36+
"fileReplacements": [
37+
{
38+
"replace": "src/environments/environment.ts",
39+
"with": "src/environments/environment.staging.ts"
40+
}
41+
]
42+
}
2143
}
2244
```
2345

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
46+
You can add more configuration options to this environment as well.
47+
Any option that your build supports can be overriden in a configuration.
48+
49+
To build using the staging configuration, run `ng build --configuration=staging`.
50+
51+
To serve using the staging configuration, you must edit the `serve` target to use the `staging`
52+
build configuration:
53+
```json
54+
"serve": {
55+
"builder": "@angular-devkit/build-angular:dev-server",
56+
"options": {
57+
"browserTarget": "your-project-name:build"
58+
},
59+
"configurations": {
60+
"production": {
61+
"browserTarget": "your-project-name:build:production"
62+
},
63+
"staging": {
64+
"browserTarget": "your-project-name:build:staging"
65+
}
66+
}
67+
},
4368
```
4469

45-
## Amending environment-specific files
70+
## Changing environment-specific files
4671

4772
`environment.ts` contains the default settings. If you take a look at this file, it should look like:
4873

@@ -103,20 +128,6 @@ export class AppComponent {
103128
}
104129
```
105130

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-
```
131+
You will always import the original environments file.
132+
This way the build system can replace the original in each configuration.
121133

122-
Will use the values from `environment.staging.ts`

0 commit comments

Comments
 (0)