Providing build configuration

Hi there,

I have been working some time with ionic 1 and I recently started a new project using ionic 2. It is awesome so far, but there is a problem I already had with ionic 1 which I hadn’t resolved as I would like to, and this time I thought I would be able to resolve with the App Scripts system but I really don’t see how to do it.

The basic problem I have is that I need to have a configuration file which changes depending on the environment, this is something I was used to do automatically with gradle on Android and it was always super useful. Let me give you an example so that is easy to understand, imagine I have the following configuration file named “config.json”:

{
    "server_url": "http://backend.dev"
}

When my application starts I read this file (similar to language files for translation) and I will use this different configuration values at runtime, in this case the url I will use to call my API. The straightforward solution is to update this file everytime I make a new build, but this is of course not a good idea. Now what I would like to do, is have different files to use in different environments:

android.json          <--- Debug android file
ios.json              <--- Debug ios file
android-release.json  <--- Release android file
local.json            <--- Config used for ionic serve
...

What I would like to do is before starting any build, replace the “config.json” file with one of these files. The solution I found in ionic 1 was to implement cordova hook scripts to do this in before_prepare, the problem with that is that this wouldn’t be called from “ionic serve” command, so I would have to replicate the code in a gulp task as well.

Now in order to replicate this in ionic 2 I was hoping to have a better way of doing this, simply having a script executed before every build where I can check environment (platform, debug, etc.) and place the correct file, but so far I haven’t found a good solution.

Any ideas?

PS: here’s the documentation of the gradle feature I was mentioning: https://developer.android.com/studio/build/build-variants.html

I have been investigating further into this, and I think I can confirm it is currently impossible to do what I am mentioning without duplicating code/files. I created my own app-script after looking into the source code of both ionic-cli and ionic-app-scripts. And I found the key problem here (line 59):

https://github.com/driftyco/ionic-cli/blob/master/lib/ionic/build.js

The ionic-app-scripts build command is called like this:

npmScripts.runIonicScript('build');

which means, all the command line arguments are lost. So, if I called “ionic build android”, this 3rd argument (“android”) will not reach the app scripts, so it is not possible (as far as I know) to get this information. There is a difference with the serve command, which is called like this:

npmScripts.runIonicScript('serve', rawCliArguments.slice(1));

so this command is correctly maintaining the original information.

As a conclusion I think I have to make both cordova hooks and ionic scripts (which substitute gulp), and we are in the same situation as before.

Is there any possibility to get this fixed? I think it should be as easy as changing that line to:

npmScripts.runIonicScript('build', rawCliArguments.slice(1));

This problem can also be solved if “ionic serve” internally calls “cordova serve”, since “ionic build” is already calling “cordova build”, but I can see that is more problematic.