8

I have an Angular app which uses environment.ts file to set stuff like urls of servers.

I know that I can use build configurations specified in angular.json to replace files at build time, e.g. replace environment.ts with environment.prod.ts.

I would like something similar with ng serve, e.g. launching

ng serve --configuration=production

and having environment.prod.ts replacing environment.ts.

I this possible? Are there better ways to drive with command lines parameters which settings are used in ng serve?

1 Answer 1

19

The one you say, ng serve --configuration=production actually replaces environment.ts with environment.prod.ts. It is already defined in Angular.

In case you want to have another replacements (i.e., more files replaced) depending on configuration, go to you angular.json and follow the structure:

  1. Create a new configuration name under build:configurations
  2. Fill in the fileReplacements array with your files to be replaced
  3. Under serve:configurations create a new tag, and reference to the one you just created.
  4. Launch serve, like: ng serve --c=configuration-name (note that --c is the same as --configuration)

Example:

{
  . . .
  "projects": {
    "your-project-name": {
      . . .
      "architect": {
        "build": {
          . . .
          "options": { . . . },
          "configurations": {
            "production": { . . .  },
            "configuration-you-want-to-create": {
              "fileReplacements": [
                {
                  "replace": "src/path-to-file.original",
                  "with": "src/path-to-file.tobereplaced"
                },
                {
                  "replace": "src/other-path-to-file.original",
                  "with": "src/other-path-to-file.tobereplaced"
                }
              ]
            }
          }
        },
        "serve": {
          . . .
          "options": {. . .},
          "configurations": {
            "production": { . . .},
            "configuration-you-want-to-create": {
              "browserTarget": "your-project-name:build:configuration-you-want-to-create"
            }
          }
        },
        . . .
      }
    }},
  . . .
}
Sign up to request clarification or add additional context in comments.

5 Comments

Great answer! 👏
one quick question here: When using fileReplacement, what are the rules to follow? I mean on serve, in my case, i get some schema validation failing, and I cant seems to find any real life example (except that environement one). Nor can I find any infos on what is allowed and what is not here...
@Minus Not really sure what your issue is. Angular docs are scarce on fille replacements (angular.io/guide/…). Maybe sombre bracket nesting issue? Any other thant that, not info on name conventions (if thats your issue, try with only letters).
@Sergio, the issue was actually filename's extention missing... The validation error message lead me off tracks... and took me too much time to figure out! :(
Great answer I've seen about this question and It worked. Thanks.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.