Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run ng build --prod with different environment settings

I have different env settings for each customer with each having it's own {{custName}}/environment.prod.ts file . I want to use their respective prod environment files with ng build --prod command. The problem I'm facing is even after specifiying the environment it's using the default environment.prod.ts

My package.json has script like

{.....
   customer_a : "ng build --prod --environment=custA_prod",
....}

In angular-cli.json I have mentioned the path which goes something like this

  "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts",
        "qa": "environments/environment.qa.ts",
        "custA_prod": "environments/custA/environment.prod.ts"
}

Is it possible to use all the features of prod build but with some different environment settings ?

like image 804
Riya Avatar asked Jun 29 '18 12:06

Riya


People also ask

How does Angular handle multiple environments?

Adding multiple environments: Open your angular project in a preferred IDE. Navigate to src>environments, there you can see two files environment. ts and environment.

What is the difference between environment ts and environment prod ts?

Both files are configuration files (global variables ) with different values. When you build or run your project in development environment “environment. ts” file will be used. Whereas if you run your project in production mode “environment.


2 Answers

After Angular 6+, angular.cli.json has been replaced by angular.json. We should use --configuration as bellow

package.json

"build_dev": "npm run && ng build --configuration=test",

angular.json

...
 "configurations": {
.....
"test": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.test.ts"
                }
              ]
            }
          }
like image 109
Muthukumar Marichamy Avatar answered Oct 11 '22 17:10

Muthukumar Marichamy


mark as production mode on you enviorement file:

environments/custA/environment.prod.ts File:

export const environment = {
  production: true,
  envName: "custA_prod",
  .....
};

Or add main.ts your custom check:

import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { AppModule } from './app/app.module';
import { environment } from './environments/environment';

if (environment.production || environment.envName == 'custA_prod') {
  enableProdMode();
}

platformBrowserDynamic().bootstrapModule(AppModule);
like image 2
Daniel Segura Pérez Avatar answered Oct 11 '22 18:10

Daniel Segura Pérez