Assuming that you are using Angular CLI, which comes with webpack. Closest thing I can think of is to define environment in `angular-cli.json config
-Create a public enum with your environments / profiles under folder app
export enum Env {
Development,
Acceptance,
Production
}
This is absolutely unnecessary but helps while working on configurations. We will use this as a profile differentiator
-Create a new environment configuration file called environment.acc.ts
and environment.dev.ts
under src\environments
folder with appropriate configurations
environment.dev.ts
-
import { Env } from 'app/env.enum';
export const environment = {
production: false,
env: Env.Development
};
environment.acc.ts
-
import { Env } from 'app/env.enum';
export const environment = {
production: false,
env: Env.Acceptance
};
environment.prod.ts
-
import { Env } from 'app/env.enum';
export const environment = {
production: true,
env: Env.Production
};
-Update angular-cli.json
with the provided configurations.
{
"project": {
"version": "1.1.2",
"name": "angular-app"
},
"apps": [
{
.
. //////config removed for sanity//////
.
"environmentSource": "environments/environment.ts",
"environments": {
"dev": "environments/environment.dev.ts",
"prod": "environments/environment.prod.ts",
"acc": "environments/environment.acc.ts",
}
}
]
.
.
.
. //////config removed for sanity//////
}
Now you have environments accesible using environment.env
variable throughout your app.
You can write a config that changes according to environment.
Example -
import { environment } from './environments/environment';
export class Config {
public static get getConfigurationEndpoints() {
if (environment.env.Production) {
return 'https://xyz.azurewebsites.net/api';
}
else if (environment.env.Acceptance) {
return 'https://xyzacc.azurewebsites.net/api';
}
else {
return 'http://localhost:8555/api';
}
}
}
Now, simply build / serve the app with build parameters like given below
ng serve --environment "acc"
ng serve --environment "prod"
Note that this is not the only way of achieving it. However, for our kind of application we preferred this sort of configuration transform method.