Skip to content

Commit 16bfdf0

Browse files
authored
feat(@angular/cli): use environmentSource key for environments (#4705)
Heavily based on @jsanchezgarcia work in #4476. Fix #3857 BREAKING CHANGE: A new environmentSource entry replaces the previous source entry inside environments. To migrate the code follow the example below: Before: ``` "environments": { "source": "environments/environment.ts", "dev": "environments/environment.ts", "prod": "environments/environment.prod.ts" } ``` After: ``` "environmentSource": "environments/environment.ts", "environments": { "dev": "environments/environment.ts", "prod": "environments/environment.prod.ts" } ```
1 parent 862c163 commit 16bfdf0

File tree

5 files changed

+39
-6
lines changed

5 files changed

+39
-6
lines changed

docs/documentation/build.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ By default, the development build target and environment are used.
2222
The mapping used to determine which environment file is used can be found in `.angular-cli.json`:
2323

2424
```json
25+
"environmentSource": "environments/environment.ts",
2526
"environments": {
26-
"source": "environments/environment.ts",
2727
"dev": "environments/environment.ts",
2828
"prod": "environments/environment.prod.ts"
2929
}

packages/@angular/cli/blueprints/ng2/files/angular-cli.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
"styles.<%= styleExt %>"
2323
],
2424
"scripts": [],
25+
"environmentSource": "environments/environment.ts",
2526
"environments": {
26-
"source": "environments/environment.ts",
2727
"dev": "environments/environment.ts",
2828
"prod": "environments/environment.prod.ts"
2929
}

packages/@angular/cli/lib/config/schema.json

+4
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@
147147
},
148148
"additionalProperties": false
149149
},
150+
"environmentSource":{
151+
"description": "Source file for environment config.",
152+
"type": "string"
153+
},
150154
"environments": {
151155
"description": "Name and corresponding file for environment config.",
152156
"type": "object",

packages/@angular/cli/models/webpack-configs/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ const getTestConfig = function (projectRoot, environment, appConfig, testConfig)
103103
// This plugin is responsible for swapping the environment files.
104104
// Since it takes a RegExp as first parameter, we need to escape the path.
105105
// See https://webpack.github.io/docs/list-of-plugins.html#normalmodulereplacementplugin
106-
new RegExp(path.resolve(appRoot, appConfig.environments['source'])
106+
new RegExp(path.resolve(appRoot, appConfig.environmentSource)
107107
.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&')),
108108
path.resolve(appRoot, appConfig.environments[environment])
109109
),

packages/@angular/cli/models/webpack-configs/typescript.ts

+32-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as fs from 'fs';
22
import * as path from 'path';
3+
import { stripIndent } from 'common-tags';
34
import {AotPlugin, AotPluginOptions} from '@ngtools/webpack';
45
import { WebpackConfigOptions } from '../webpack-config';
56

@@ -19,15 +20,43 @@ function _createAotPlugin(wco: WebpackConfigOptions, options: any) {
1920
let hostOverrideFileSystem: any = {};
2021
// process environment file replacement
2122
if (appConfig.environments) {
22-
if (!('source' in appConfig.environments)) {
23-
throw new SilentError(`Environment configuration does not contain "source" entry.`);
23+
if (!appConfig.environmentSource) {
24+
let migrationMessage = '';
25+
if ('source' in appConfig.environments) {
26+
migrationMessage = '\n\n' + stripIndent`
27+
A new environmentSource entry replaces the previous source entry inside environments.
28+
29+
To migrate angular-cli.json follow the example below:
30+
31+
Before:
32+
33+
"environments": {
34+
"source": "environments/environment.ts",
35+
"dev": "environments/environment.ts",
36+
"prod": "environments/environment.prod.ts"
37+
}
38+
39+
40+
After:
41+
42+
"environmentSource": "environments/environment.ts",
43+
"environments": {
44+
"dev": "environments/environment.ts",
45+
"prod": "environments/environment.prod.ts"
46+
}
47+
`;
48+
}
49+
throw new SilentError(
50+
`Environment configuration does not contain "environmentSource" entry.${migrationMessage}`
51+
);
52+
2453
}
2554
if (!(buildOptions.environment in appConfig.environments)) {
2655
throw new SilentError(`Environment "${buildOptions.environment}" does not exist.`);
2756
}
2857

2958
const appRoot = path.resolve(projectRoot, appConfig.root);
30-
const sourcePath = appConfig.environments['source'];
59+
const sourcePath = appConfig.environmentSource;
3160
const envFile = appConfig.environments[buildOptions.environment];
3261
const environmentContent = fs.readFileSync(path.join(appRoot, envFile)).toString();
3362

0 commit comments

Comments
 (0)