Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular CLI fileReplacements for default/development

I have a rather complex angular.json because I have multiple projects. What I would like to have is separate environment.ts files per project. Replacing the environment file when the build target is production seems to be pretty straightforward:

"configurations": {
  "production": {
    "fileReplacements": [
      {
        "replace": "src/environments/environment.ts",
        "with": "src/private-label/redacted/environments/environment.prod.ts"
      }
    ],
  }
}

What I'm looking for is a way to do the same with the default/development configuration. The angular.json does not contain a configuration node for anything like that that I can find and so I'm not sure if it is possible and/or where to specify fileReplacements when not targeting production.

Of course, if there is a better way to handle environments per project that I'm not seeing I would also be interested in that.

like image 912
Jacob Avatar asked Feb 15 '19 18:02

Jacob


People also ask

What is Angular CLI json used for?

json at the root level of an Angular workspace provides workspace-wide and project-specific configuration defaults for build and development tools provided by the Angular CLI. Path values given in the configuration are relative to the root workspace folder.

What is the use of environment TS in Angular?

A project's src/environments/ folder contains the base configuration file, environment. ts , which provides a default environment. You can add override defaults for additional environments, such as production and staging, in target-specific configuration files.

Where is Angular CLI json located?

The angular-cli. json should be located in the root folder of the project.


1 Answers

I assume with "default/development configuration" you are referring to what is served with the ng serve command.

Option 1: replace environment files:

You can specify the fileReplacements array in the build object as well (this one is used for ng serve).

  "build": {
    "options": {
      [...]
      "fileReplacements": [
        {
          "replace": "src/environments/environment.ts",
          "with": "src/environments/environment.development.ts"
        }
      ]      
    },
    "configurations": {
      // other - non default - configurations 
    }
  }

Option 2: specifiy default configuration:

If you want to serve an already existing configuration withng serve, you can change the serve options:

  "configurations": {
    "youConfigName": {
      // config details here
    }
  },
  [...]
  "serve": {
    "options": {
      "browserTarget": "name-of-your-app:build:youConfigName"
    }
  }

The important point is to set the build configuration target with :yourConfigName.


Both options are configured per project and therefore allow you full control.

like image 197
jowey Avatar answered Sep 30 '22 06:09

jowey