2

I am currently working on a strategy to handle multiple environments as easy as possible in Angular CLI.

For this I follow this approach: enter image description here

My goal is to have the base URLs for different REST endpoints just once and all the environments should have them (extending or something).

What is the best way to implement this?

3
  • 1
    maybe write out the example in a code example instead of a picture. Commented Apr 12, 2019 at 8:21
  • 2
    As you presented on the picture, you could use angular environment config`s: angular.io/guide/build Commented Apr 12, 2019 at 8:28
  • notificationsBaseUrl and accountsBaseUrl are better to be used in the respective service.ts files. Commented Apr 12, 2019 at 8:41

2 Answers 2

1

You can use multiple environment files, here is how you can do it and when you build you have to specify the configuration (examples: prod, test, uat)

  • you have to add all your configurations in angular.json file

    "configurations": {
        "production": {
          "fileReplacements": [
            {
              "replace": "src/environments/environment.ts",
              "with": "src/environments/environment.prod.ts"
            }
          ],
          "optimization": true,
          "outputHashing": "all",
          "sourceMap": false,
          "extractCss": true,
          "namedChunks": false,
          "aot": true,
          "extractLicenses": true,
          "vendorChunk": false,
          "buildOptimizer": true,
          "budgets": [
            {
              "type": "initial",
              "maximumWarning": "2mb",
              "maximumError": "5mb"
            }
          ]
        },
        "develop": {
          "fileReplacements": [
            {
              "replace": "src/environments/environment.ts",
              "with": "src/environments/environment.develop.ts"
            }
          ]
        },
        "release": {
          "fileReplacements": [
            {
              "replace": "src/environments/environment.ts",
              "with": "src/environments/environment.release.ts"
            }
          ]
        },
        "uat": {
          "fileReplacements": [
            {
              "replace": "src/environments/environment.ts",
              "with": "src/environments/environment.uat.ts"
            }
          ]
        },
      }
    

add yours urls for each file, and when you build or serve your app locally, you have to specify the configuration

ng build --configuration=uat // for uat env

ng serve --configuration=uat 

or

ng build --prod // for production env

import it that way, and it will pick the right one based on the chosen configuration

import { environment } from 'src/environments/environment';
Sign up to request clarification or add additional context in comments.

Comments

0

If you are using webpack, you can create an environment.ts file with all environment specific data as properties in an object....

    {
        environment: 'dev',
        baseurl: 'http://myBaseUrl'
    }

create a separate file for each environment (prod, qa, etc...) with the same object and properties. import the plain environment.ts file when you need environment information....

Then in each webpack config use the NormalModuleReplacementPlugin...

    new webpack.NormalModuleReplacementPlugin(/\.\/environment\./, './environment.qa.ts')

You are basically telling webpack wherever I import environment replace it with environment with environment.qa (or whichever version)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.