Angular-cli: Improvement: externalizing environment from Angular CLI build process

Created on 4 Oct 2016  路  4Comments  路  Source: angular/angular-cli

There is a great article: http://www.jvandemo.com/how-to-configure-your-angularjs-application-using-environment-variables/ about externalizing the environment file from the build. The deployment phase can be responsible for providing the app with the correct environment variables. "Build once - Deploy anywhere".

What I did in my own project to get this to work was to externalize my environment parameters the same way as the article had it. Then I imported those variables in the environment.dev.ts file like so:

export const environment = {
production: false,
environment: window['__env'].environment,
build: window['__env'].build,
siteName: window['__env'].siteName,
};

I also referred to my real environment file in index.html via a javascript import.

I'm submitting a ... (check one with "x")

[ ] bug report => search github for a similar issue or PR before submitting
[ x] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior
No bug

Expected behavior
No bug

Minimal reproduction of the problem with instructions
Environment files are compiled with the app during build phase for Angular CLI. It should [ideally] be outside.

What is the motivation / use case for changing the behavior?
Build once - Deploy anywhere.

  • Angular version: 2.0.1
  • Language: [TypeScript 2.0.2]

Most helpful comment

Totally understand. That implementation gets the job done for me today. The current solution forces me to build per environment and when you get to more than 5 environments, that turns into a headache.

Looking forward to what you guys come up with! Good work!!

All 4 comments

using window = using dom = not cross platform solution.
Either way I agree that the current environment variable is still lacking, I prefer something like dotenv solution (create-react-app uses this, just FYI)

Using window is not advisable for the reasons @antonybudianto put out.

While I agree there is room for improvement, I don't agree with this approach. It was one of the approaches we considered initially and decided to go with the current one.

Totally understand. That implementation gets the job done for me today. The current solution forces me to build per environment and when you get to more than 5 environments, that turns into a headache.

Looking forward to what you guys come up with! Good work!!

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

_This action has been performed automatically by a bot._

Was this page helpful?
0 / 5 - 0 ratings