Angular: Import different scripts depending on the environment

Viewed 484

I am developing a web application in Angular (version 2+). This application must work in two environments: test and production. I need to use an external script. When imported this way, everything works:

File angular.json:

"scripts": [
              "src/assets/my_folder/dev/my_script.js"
           ]

In my_folder are the dev and prod folders. The application works perfectly in the Development environment, but I have no idea how to do a "dynamic variation" of this path with the path "src/assets/my_folder/prod/my_script.js" when the application is actually deployed in the production environment.

Is there some sort of "best practice" in Angular for doing this thing?

1 Answers

You can use angular.json to configure various environment

"configurations": {
    "dev": {
      "scripts": [
          "src/assets/my_folder/dev/my_script.js"
      ]   
    },
    "prod": {
      "scripts": [
          "src/assets/my_folder/prod/my_script.js"
      ]  
    }
}

Alternate build configurations

Related