There are several blog posts that explain why switching from grunt or gulp to building with just plain npm is a good idea for example this one by Cory Hourse or this one by Keith Cirkle. One thing these blog posts do not explain is how I can easily do environment configuration. For example a common requirement is to have different REST API locations. During development the server might be running on localhost:8080
, but on production it should be accessed through a relative URL such as /api
or /rest/api
and the port or protocol are different for development and production.
There are several solutions for this. For example grunt supports template strings like <% %>
and <%= %>
and there are grunt or gulp plugins like in this question about grunt-ng-config. These solutions are specific to Angular (which I am using), but I am not necessary looking for an AngularJS specific solution.
I also know of the angular-environment
plugin, but as far as I can see this does configuration at run time and I am looking for something that can do this at build time.
So what I am looking for is something that allows me to configure my application at build time either by replacing some template strings or by generating a Javascript file with some variables that I can read at run time.
One requirement is that it should be OS independent. So I do not want to use UNIX specific tools such as sed
to rewrite a file. And due to different variable expansion (e.g. %
vs. $
) a solution should not rely on environment variables.
Is there an existing solution or a best-practice for this?