Feature request: Ionic 2 (dev/prod) environment variables configuration
From @amreladawy on July 24, 2016 6:58
Short description of the problem:
It is a feature request. Just like Angular2, I hope that Ionic can provide 2 files (environment.dev.ts and environment.prod.ts) that would contain variables with different values corresponding to production and development environment. During the build, the appropriate file to be copied and bundled within the app artifact.
Which Ionic Version? 2.x
https://forum.ionicframework.com/t/ionic-2-environment-variables-config-setup/58147/1
https://stackoverflow.com/questions/36004810/how-to-config-different-development-environment-in-angular-2-app
Copied from original issue: driftyco/ionic#7413
Currently using the implementation here and so far so good, works like a charm. Sadly I could not extend it because IONIC_ENV only supports dev and prod and I could not for the love of me find a way to get webpack to work with IONIC_ENV and NODE_ENV together even using webpacks EnvironmentPlugin did not seem to work so I am stuck with just dev and prod for now and its not that bad really.
We have 5 environments and urgently need this. If there is no solution I am willing to code one. Can anybody give me pointers where to hook into?
@nottinhill Since you have 5 environments and there isn't an official way to do this, there are several working solutions in the comments above that you can use until there official support.
We are currently using a merge of gits .gitattributes use own feature and some of the examples stated above to manage multiple environments. You could do that too.
@dani-inside how would you pass the nodejs argument in this case? I tried "npm run ionic:serve --prod" but that doesn't seem to work. And what about "ionic serve"?
@marcorinck That's how we use the nodejs arguments:
- ionic serve --qa
- ionic build android --qa
- ionic serve --production
- ionic build android --production
Please note that the "--prod" argument is reserved by Ionic, that's why we used "production" for the custom environment. According to the Ionic documentation the "--prod" flag will minify your app’s code as Ionic’s source and also remove any debugging capabilities from the APK. This is generally used when deploying an app to the Google Play Store or Apple App Store.
@dani-inside thanks for the hint, yes, now the argument is being passed through.
Problem is, when the rollup replace plugin is doing an actual replacement the build doesn't seem to work anymore. No error is logged into the console, but the dev server doesn't serve any app files, I get HTTP 404 even for main.css and main.js
I use ionic 3.4.0 and have used the included rollup.config.js and just added the replace plugin. I had to add "var argv = require('yargs').argv;" though to parse the arguments.
I'd prefer to use your environment solution because I need environment specific variables before bootstrapping the angular app which doesn't work with many other proposed solutions.
@marcorinck I was having the same issue as you when using rollup replace plugin. Somewhere along the way, main.js and main.css are not completed.
But using @fiznool webpack implementation did the trick just fine. Try that instead!
You can use webpack implementation but you should use webpack-merge which merges the webpack config from app-scripts and yours. This approach will not touch the default webpack config then we can keep it up to date.
Source: http://disq.us/p/1i75llk
@nottinhill A bit late for you but I had the same need for multiple environments.
I solved it using webpack-merge, webpack's NormalModuleReplacementPlugin and deepmerge.
Full installation instructions at https://github.com/pbowyer/ionic-cascading-config/blob/master/INSTALL.md and a working project repository at https://github.com/pbowyer/ionic-cascading-config.
I could not get command line flags to work with the latest Ionic like @dani-inside uses - they worked for ionic serve but not for ionic cordova build. As a result I've had to define my own npm scripts, one for each environment. If anyone can tell me how I'd like to learn.
For anyone needing a cross-platform solution for windows too - one small change to the solution from @pbowyer
install cross-env:
npm install -S cross-env
and then change the scripts config in package.json to
"cordova:build:dev": "cross-env NODE_ENV=dev ionic cordova build android",
"cordova:build:stage": "cross-env NODE_ENV=stage ionic cordova build android",
"cordova:build:prod": "cross-env NODE_ENV=prod ionic cordova build android --prod",
"ionic:serve:dev": "cross-env NODE_ENV=dev npm run ionic:serve",
"ionic:serve:stage": "cross-env NODE_ENV=stage npm run ionic:serve",
"ionic:serve:prod": "cross-env NODE_ENV=prod npm run ionic:serve"
@wearetelescopic already incorporated :)
https://github.com/pbowyer/ionic-cascading-config/commit/e8555a794e932c8ccef1bc1b6b25af2a81682d5d
Here is my easy to use solution
- Easy to import:
import { ENV } from '@app/env' - Can be used anywhere, even in your
app.module.ts
Thanks, @gshigeto, your solution worked! It was surprising that it worked just like that, out of the box, without installing any plugins.
Because several environment variables are the same for all my environments, I used class inheritance to make it work easily. For example:
environment.model.ts:
export interface Environment {
readonly BASE_PATH: string;
readonly DEFAULT_BROWSER_PARAMS: ThemeableBrowserOptions;
readonly HEADER_BG_COLOR: string;
readonly PATH_01: string;
readonly PATH_02: string;
readonly PATH_03: string;
readonly SOCKET_URI: string;
readonly STATUS_BAR_BG_COLOR: string;
readonly TOOLBAR_BG_COLOR: string;
readonly TOOLBAR_COLOR: string;
}
environment.base.ts:
export abstract class EnvironmentBase implements Environment {
// public abstract to be defined in subclass
public abstract readonly BASE_PATH: string;
// protected abstract to be defined in subclass and used only here
// (it isn't in the interface)
protected abstract readonly SOCKET_PORT: number;
// shared environment variables
public readonly STATUS_BAR_BG_COLOR = '#4a8bfc';
public readonly TOOLBAR_BG_COLOR = '#189be3';
public readonly TOOLBAR_COLOR = '#ffffff';
// shared computed environment variables
// (BE CAREFUL: use "this" in a get function,
// otherwise you can get unexpected undefined values
// if the value is defined in subclass:
//
// public readonly HEADER_BG_COLOR = this.STATUS_BAR_BG_COLOR; // this works
// public readonly PATH_01 = this.BASE_PATH + '/my/path/02/'; // this doesn't work
//
// To avoid these kind of problems, its better to use "this" inside getters always)
public get HEADER_BG_COLOR() { return this.STATUS_BAR_BG_COLOR };
public get PATH_01() { return this.BASE_PATH + '/my/path/01/' };
public get PATH_02() { return this.BASE_PATH + '/my/path/02/' };
public get PATH_03() { return this.BASE_PATH + '/my/path/03/' };
public get SOCKET_URI() { return `${this.BASE_PATH}:${this.SOCKET_PORT}` };
// the constant can be an object with other constants too
public get DEFAULT_BROWSER_PARAMS() {
let params: ThemeableBrowserOptions = {
statusbar: {
color: this.STATUS_BAR_BG_COLOR,
},
toolbar: {
height: 44,
color: this.TOOLBAR_BG_COLOR,
},
title: {
color: this.TOOLBAR_COLOR,
showPageTitle: true
},
backButton: {
image: 'ic_arrow_back',
imagePressed: 'ic_arrow_back_pressed',
align: 'left',
},
forwardButton: {
image: 'ic_arrow_forward',
imagePressed: 'ic_arrow_forward_pressed',
align: 'left',
},
closeButton: {
image: 'ic_close',
imagePressed: 'ic_close_pressed',
align: 'left',
},
backButtonCanClose: true,
};
return params;
};
}
environment.ts:
class EnvironmentImpl extends EnvironmentBase {
public readonly BASE_PATH = 'https://localhost';
public readonly SOCKET_PORT = 3000;
}
export const ENV: Environment = new EnvironmentImpl();
environment.stage.ts:
class EnvironmentImpl extends EnvironmentBase {
public readonly BASE_PATH = 'https://my-fake-site.com';
public readonly SOCKET_PORT = 3000;
}
export const ENV: Environment = new EnvironmentImpl();
environment.prod.ts:
class EnvironmentImpl extends EnvironmentBase {
public readonly BASE_PATH = 'https://my-site.com';
public readonly SOCKET_PORT = 3000;
}
export const ENV: Environment = new EnvironmentImpl();
Then I just import the ENV constant and use it like:
console.log('path01: ' + ENV.PATH_01);
console.log('socket uri: ' + ENV.SOCKET_URI);
Thanks, @gshigeto great solution. I have one question in your documentation you mentioned
For any other configuration, just add another file src/environments/environment.*.ts which will then be used with build flags. It is that easy!
I have added an extra configuration enviroment file environment.acc.ts but I'm unable to use it, how do I set a build flag.
I have tried these command lines already:
- npm run ionic:serve --env=acc
- npm run ionic:serve --acc
- npm run ionic:serve:acc (and in the package.json added a script like "ionic:serve": "IONIC_ENV=acc ionic-app-scripts serve")
- export IONIC_ENV=acc && npm run ionic:serve
But IONIC_ENV is always set to dev except when running as:
- npm run ionic:serve --prod
@gshigeto having the same issue as @Heerschop
Any suggestions? Thank yoU!
@Heerschop @jamesdixon I made it work using NODE_ENV instead of IONIC_ENV in the files /config/optimization.config.js and /config/webpack.config.js:
"@app/env": path.resolve('./src/environments/environment' + (process.env.NODE_ENV === 'dev' ? '' : '.' + process.env.NODE_ENV) + '.ts')
This also allows me to use ionic prod flag (for optimization) with other environments other than production (like stage).
In your package.json scripts you can add something like:
"ionic:serve:dev": "cross-env NODE_ENV=dev npm run ionic:serve",
"ionic:serve:stage": "cross-env NODE_ENV=stage npm run ionic:serve",
"ionic:serve:prod": "cross-env NODE_ENV=prod npm run ionic:serve",
"ionic:build:dev": "cross-env NODE_ENV=dev ionic cordova build",
"ionic:build:stage": "cross-env NODE_ENV=stage ionic cordova build --prod",
"ionic:build:prod": "cross-env NODE_ENV=prod ionic cordova build --prod",
"ionic:run:dev": "cross-env NODE_ENV=dev ionic cordova run",
"ionic:run:stage": "cross-env NODE_ENV=stage ionic cordova run --prod",
"ionic:run:prod": "cross-env NODE_ENV=prod ionic cordova run --prod",
(I've used cross-env because I'm on Windows: npm install --save-dev cross-env) (following the posts from @pbowyer and @wearetelescopic)
Then you can just execute commands like:
npm run ionic:serve:dev
npm run ionic:build:stage android
npm run ionic:run:prod android
and so on...
@jamesdixon thanks for the response i have solved it in a similar way by using APP_ENV. We also had a problem on a windows machine. Now I know how to fix that :-)
@gshigeto I'm still curious if we are missing something.
@lucasbasquerotto nice solution! Thank you 👍
@Heerschop the Ionic CLI currently only supports the use of dev and prod. I was in the process of making a pull request to support other environments by changing the IONIC_ENV but got pretty busy. I got ahead of myself and put that in the README
Using @lucasbasquerotto solution of using NODE_ENV or even creating your own argument such as ENV will work as long as you change it in your webpack.config.js and optimization.config.js to use that variable. This is a great solution for the time being!
Thanks, everyone!