Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular ng build with different "profiles"

In Maven (Java) there is a possibility to build, for example, a webapp war with different "profiles", a "profile" indicating for example a URL of a web service to put in a configuration file. Thus the "test profile" will indicate a URL different from that of the "production profile".

Is there something similar to profiles for ng build?

like image 985
John Donn Avatar asked Nov 29 '22 22:11

John Donn


1 Answers

For Angular 6+:

Create a file for each profile inside environments folder:

environments/environment.ts
environments/environment.prod1.ts
environments/environment.prod2.ts

And inisde each file put the parameters of corresponding profile:

export const environment = {
  production: true,
  serverUrl: "http://prod1.site.com"
};

You can access the parameters iniside your component/service like this:

import {environment} from '../../environments/environment';

@Injectable()
export class SomeService {
  SERVER_URL: string = environment.serverUrl;

And add the new profiles environment inside angular.json under configurations:

"configurations": {
  "prod1": { ... },
  "prod2": {
    "fileReplacements": [
      {
        "replace": "src/environments/environment.ts",
        "with": "src/environments/environment.prod2.ts"
      }
    ]
  }
}

And finally choose the profile when building the app:

ng build --configuration = prod1
like image 102
Abdennacer Lachiheb Avatar answered Dec 10 '22 02:12

Abdennacer Lachiheb