The problem: Passing env variables (configurations) from cli
Team decided against adding this functionality in angular cli
and possibility to define angular.json
as .ts file (where you could use env variables during build) is also not given.
Possible options/workarounds:
Using @ngx-env/builder
Passing environment variables with NG_APP_ prefix, similar how it is done in react. To me this looked like the easiest solution, but I haven't used this one personally.
Need to use custom builder from this lib "builder": "@ngx-env/builder:browser"
Custom script to write environment files
Use fs to do the fileReplacement dynamically with a set-env.js
script and you can have your particular client configurations in that script that you read there and write the values into the environment.ts
file.
Slightly different version, using dotenv as well
Custom webpack builder
Using @angular-builders/custom-webpack
and webpack DefinePlugin and spawn ng serve as a child process. That way you can pass an environment variable that determines which client configuration to use
Pass environment variables with angular-server-side-configuration
https://github.com/kyubisation/angular-server-side-configuration
Replace environment files for clients in your deployment tools
Load a specific file that you are replacing in any way during your deployment process and load them via APP_INITIALIZER in your app module:
{
provide: APP_INITIALIZER,
useFactory: async () => {
try {
// Dynamically import the client-specific environment
const clientEnvironment = await import(
`../config/${clientConfig.ts}`
).then((mod) => mod.environment);
return { ...clientEnvironment };
} catch (error) {
console.log(error);
// If the client-specific environment file doesn't exist, use the default environment
return {};
}
},
Using window as a workaround
Have an env.js file for each client - similar to the solution above where your deployment process would replace the env.js
file with the specific client configuration.
Have a angular.ts file and transform it to angular.json via script
In the angular.ts
file you could then import the desired client configurations based on an environment variable you pass, e.g. CLIENT_ENV=<client-name><env>
. This .ts
file you then need to transform to angular.json
before your build.
Wrap up
Hopefully this can give you some inspiration on how to solve this issue with angular cli.
Since you wrote that you have api keys in those client environment files as well, just be aware that all those values are readable by anyone accessing your application and that these keys should not contain sensitive information.