How build a specific configuration according to target?


I would like to know if there is possible with Ionic to build a specific configuration according target…
i.e. When I use “ionic serve”, backend API of localhost server is used , but when I use “ionic cordova run”, I would like that backend API of my real server is used. If there is a way to do it, I’m very interested…

Idea is to have a snapshot of configuration used by the project, and when is requested, possibility to switch to another snapshot of configuration.
In C/C++ projects, this functionnality is visible by “Development/Release” environment.

Thank you,

There around definitely ways to achieve this, I wrote a tutorial on this, have a look:

Thank you a lot for your tutorial.

I follow it but in my side, it doesn’t work. It seems that “–prod” is not considered by App, when I start in “ionic serve --prod” environment, the dev is loaded.

Any idea?

Note: I has been to use InjectionToken instead OpaqueToken in my project (I use Ionic 3/Angular 4)

Yep, you’re right about the token change .

Regarding the --prod flag, evidently you can only use that during the build task now, it no longer works during serve.

I suppose this mostly makes sense as you’d really only use prod when going to production, in which case you would build and deploy to a phone.

If you do need to use environment variables while using serve, you can use NODE_ENV instead. Check the Wrap Up section of my tutorial, and the git project.

Short version is you can just create a task in your package.json that sets NODE_ENV to whatever value you want:

"scripts": {
    "clean": "ionic-app-scripts clean",
    "build": "ionic-app-scripts build",
    "ionic:build": "ionic-app-scripts build",
    "ionic:serve": "ionic-app-scripts serve",
    "ionic:serve:qa": "NODE_ENV=qa ionic-app-scripts serve" // THIS IS THE ONE I CREATED

Then in your environment variables module just check for process.env.NODE_ENV instead of process.env.IONIC_ENV.

Since NODE_ENV is a separate environment variable on your compueter it doesn’t matter what options ionic accepts. So this should definitely work and will also allow you to set more than 2 environments if needed.

So, I’m tried your git project and it doesn’t work on my side.

But after analyzing, I think to have find the problem: I use Windows system.

I found a solution that works on my side, I shall to set NODE_ENV environment by the Windows way. So, to load (regarding your project) qa variables, I shall execute the following commands:

set NODE_ENV=qa
ionic serve

I will create a batch file to shortcut commands.

So thank you so much to give me a good way to have a solution :slight_smile:

No problem, sorry I didn’t mention my config was for is. Keep in mind you can make an npm trash that does that for you, sets the variable and then does serve. That way you just have to run ‘npm run my-task’.

Just shorter to type each time than manually resetting the environment variable

Thank you for you advise but using batch, I think it is more simple and smaller. For instance, I have created the command iserve with only --prod, --dev or --fix as argument in charge to execute ionic serve --lab in the good environment.