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
Hey all, I'm going to be re-opening this issue to attempt to solve this in the Ionic CLI. I think this issue alone shows how important this is 👍
When built with --prod option, enableProdMode() is called. There is no "Angular is running in the development mode...."
So doesn't it mean that the mechanism is there already? Can anyone please explain how Ionic does the magic, detecting whether --prod is set?
@shawnlan , if I don't make mistake,
- --prod is an ionic command parameter to optimize the build process (uglify, angular aot, sourcemap, ...)
- enableProdMode() is an angular method and must be called by yourself in the code (inside a if(isProdEnv()) to enable optimization inside angular process.
- --release is a cordova parameter, a bit like --prod and will enable some optimisation in cordova native part, make signatures, ...
But finally, the --prod parameter will not allow you to defined multiple environment if you have test, qa, dev, prod, pre-prod, pilot, or others... For example path to server with db can be different on each environment. In our compagny, we have 5 differents environments and each have different server address, different log level, but actually we can not choose easilly between them.
So the initial question was, how can we configure variables depending on your environment (dev machine 1, dev machine 2, QA, pre-prod, prod...) and keeping functionnality of enableProdMode(), --prod and --release ? The angular-cli propose to use file called environment.xxx.ts that you can choose with --env=xxx. And one variable inside this file will enable the enableProdMode() call. But this file choose independent of --prod and --release.
@movrack I understand. To me I just need to distinguish production and development.
The thing is, when you use --prod with ionic cli, enableProdMode() is called somehow, not by myself. That's why I wonder, maybe, there is a way to detect whether --prod is set from JS, and the Ionic team is using it. It's just not documented. Again, I don't know.
Not sure if Cordova has a similar mechanism so that you can detect --release from JS.
@jgw96 Could yo give us a status/feedback on this problematic from ionic team point of view?
@gshigeto @lucasbasquerotto updated to "@ionic/app-scripts": "^3.0.0-201710101530" This breaks the solution offered using 'app/env'. Does anyone have an example to start using the new exports??
module.exports = {
dev: devConfig,
prod: prodConfig
}
Here is the error
nahollmo37714n:drinkpublic.com gap4$ npm run ionic:build:dev
> [email protected] ionic:build:dev /Users/gap4/Documents/WEB/drinkpublic.com
> cross-env NODE_ENV=dev ionic build
[INFO] Running app-scripts build:
[23:31:33] build dev started ...
[23:31:33] clean started ...
[23:31:33] clean finished in 10 ms
[23:31:33] copy started ...
[23:31:33] deeplinks started ...
[23:31:33] deeplinks finished in 117 ms
[23:31:33] transpile started ...
There was an error in config file "/Users/gap4/Documents/WEB/drinkpublic.com/config/webpack.config.js". Using defaults instead.
TypeError: Cannot set property 'alias' of undefined
at module.exports (/Users/gap4/Documents/WEB/drinkpublic.com/config/webpack.config.js:5:34)
at Object.fillConfigDefaults (/Users/gap4/Documents/WEB/drinkpublic.com/node_modules/@ionic/app-scripts/dist/util/config.js:322:30)
at Object.getWebpackConfig (/Users/gap4/Documents/WEB/drinkpublic.com/node_modules/@ionic/app-scripts/dist/webpack.js:192:44)
at Object.buildJsSourceMaps (/Users/gap4/Documents/WEB/drinkpublic.com/node_modules/@ionic/app-scripts/dist/bundle.js:26:35)
at /Users/gap4/Documents/WEB/drinkpublic.com/node_modules/@ionic/app-scripts/dist/transpile.js:117:51
at transpileWorker (/Users/gap4/Documents/WEB/drinkpublic.com/node_modules/@ionic/app-scripts/dist/transpile.js:107:12)
at Object.transpile (/Users/gap4/Documents/WEB/drinkpublic.com/node_modules/@ionic/app-scripts/dist/transpile.js:64:12)
at /Users/gap4/Documents/WEB/drinkpublic.com/node_modules/@ionic/app-scripts/dist/build.js:106:82
[23:31:36] transpile finished in 2.99 s
[23:31:36] preprocess started ...
[23:31:36] preprocess finished in 1 ms
[23:31:36] webpack started ...
There was an error in config file "/Users/gap4/Documents/WEB/drinkpublic.com/config/webpack.config.js". Using defaults instead.
TypeError: Cannot set property 'alias' of undefined
at module.exports (/Users/gap4/Documents/WEB/drinkpublic.com/config/webpack.config.js:5:34)
at Object.fillConfigDefaults (/Users/gap4/Documents/WEB/drinkpublic.com/node_modules/@ionic/app-scripts/dist/util/config.js:322:30)
at getWebpackConfig (/Users/gap4/Documents/WEB/drinkpublic.com/node_modules/@ionic/app-scripts/dist/webpack.js:192:44)
at webpackWorker (/Users/gap4/Documents/WEB/drinkpublic.com/node_modules/@ionic/app-scripts/dist/webpack.js:64:25)
at Object.webpack (/Users/gap4/Documents/WEB/drinkpublic.com/node_modules/@ionic/app-scripts/dist/webpack.js:29:12)
at bundleWorker (/Users/gap4/Documents/WEB/drinkpublic.com/node_modules/@ionic/app-scripts/dist/bundle.js:13:22)
at Object.bundle (/Users/gap4/Documents/WEB/drinkpublic.com/node_modules/@ionic/app-scripts/dist/bundle.js:6:12)
at /Users/gap4/Documents/WEB/drinkpublic.com/node_modules/@ionic/app-scripts/dist/build.js:113:25
[23:31:36] copy finished in 3.28 s
Error: ./src/app/app.module.ts
Module not found: Error: Can't resolve '@app/env' in '/Users/gap4/Documents/WEB/drinkpublic.com/src/app'
@ajonp With newer versions of app-scripts it seems they don't have the optimization.config.ts anymore (that I think was used with IONIC_ENV=prod) and both dev and prod are in the webpack.config.js. I changed it to the following:
var path = require('path');
var useDefaultConfig = require('@ionic/app-scripts/config/webpack.config.js');
module.exports = function () {
var env = process.env.NODE_ENV || 'dev';
var envFile = './src/environments/environment.' + env + '.ts';
var alias = { "@app/env": path.resolve(envFile) };
useDefaultConfig.dev.resolve.alias = alias;
useDefaultConfig.prod.resolve.alias = alias;
return useDefaultConfig;
};
PS: I use the dev environment file with the name environment.dev.ts, if yours is environment.ts you can change the above accordingly, but the main change to be done is setting both the alias above in the same file.
@ajonp I updated my repo a couple of days ago to reflect the recent changes in ionic app scripts.
@gshigeto @lucasbasquerotto No matter what when I try to npm run cross-env DP_ENV=dev ionic-app-scripts build with --prod flag it still takes the environment for prod even though I am saying copy from environment.dev.ts. I really need a compiled prod build, but using the dev env variables.
var path = require('path');
var useDefaultConfig = require('@ionic/app-scripts/config/webpack.config.js');
module.exports = function () {
var env = process.env.DP_ENV || 'dev';
console.log(`using DP_ENV:${env}`)
var envFile = './src/environments/environment' + (env === 'prod' ? '' : '.' + env) + '.ts';
var alias = { "@app/env": path.resolve(envFile) };
useDefaultConfig.dev.resolve.alias = alias;
useDefaultConfig.prod.resolve.alias = alias;
return useDefaultConfig;
};
package.json
"scripts": {
"clean": "ionic-app-scripts clean",
"build": "ionic-app-scripts build",
"lint": "ionic-app-scripts lint",
"ionic:build": "ionic-app-scripts build",
"ionic:serve": "ionic-app-scripts serve",
"ionic:build:dev": "cross-env DP_ENV=dev ionic-app-scripts build",
"ionic:build:stage": "cross-env DP_ENV=stage ionic-app-scripts build",
"ionic:build:prod": "cross-env DP_ENV=prod ionic-app-scripts build"
},
"config": {
"ionic_webpack": "./config/webpack.config.js"
},
Ionic
nahollmo37714n:drinkpublic.com gap4$ ionic info
cli packages: (/Users/gap4/Documents/WEB/drinkpublic.com/node_modules)
@ionic/cli-utils : 1.13.0
ionic (Ionic CLI) : 3.13.0
global packages:
cordova (Cordova CLI) : 7.0.1
local packages:
@ionic/app-scripts : 3.0.0
Cordova Platforms : none
Ionic Framework : ionic-angular 3.7.1-201710060319
System:
Android SDK Tools : 26.0.2
ios-deploy : 1.9.1
Node : v6.11.4
npm : 5.4.2
OS : macOS Sierra
Xcode : Xcode 9.0 Build version 9A235
Misc:
backend : pro
I see it getting picked up while running the webpack
[16:01:41] ionic-app-scripts 3.0.0
[16:01:41] build prod started ...
[16:01:41] clean started ...
[16:01:41] clean finished in 5 ms
[16:01:41] copy started ...
[16:01:41] deeplinks started ...
[16:01:41] deeplinks finished in 125 ms
[16:01:41] ngc started ...
Warning: Can't resolve all parameters for AngularFirestore in /Users/gap4/Documents/WEB/drinkpublic.com/node_modules/angularfire2/firestore/index.d.ts: ([object Object], ?). This will become an error in Angular v5.x
Warning: Can't resolve all parameters for AngularFirestore in /Users/gap4/Documents/WEB/drinkpublic.com/node_modules/angularfire2/firestore/index.d.ts: ([object Object], ?). This will become an error in Angular v5.x
[16:01:50] ngc finished in 9.18 s
[16:01:50] preprocess started ...
[16:01:50] copy finished in 9.38 s
[16:01:50] preprocess finished in 83 ms
[16:01:50] webpack started ...
using DP_ENV:dev
@ajonp I think it is weird such a behaviour as you mentioned, if I understood correctly. Try these 2 cases and see which environment they are using:
In scripts inside package.json:
"ionic:build:dev": "cross-env DP_ENV=dev ionic-app-scripts build",
"ionic:build:dev:prod": "cross-env DP_ENV=dev ionic-app-scripts build --prod",
And run it like:
npm run ionic:build:dev android (or ios) for dev environment without ionic prod flag
npm run ionic:build:dev:prod android (or ios) for dev environment with ionic prod flag
And see if both have the same environment (both should have dev environment).
Call console.log('ENV', ENV) in one of your ts files to see the environment.
This way will be easier to know what is wrong.
@lucasbasquerotto thank you for reminding me to output using console.log('ENV', ENV)
It showed I was using the dev file, it is something with firebase wanting to hold on for some reason.
Thanks again! I would upvote you and @gshigeto if we were on SO. However, I am already probably misusing github for this issue 💯
Well @lucasbasquerotto the saga continues. When I console.log(ENV) I see my dev environment. However when I run AngularFireModule.initializeApp(ENV.firebase), it still seems to use the production variables.
@ajonp I don't know why that would happen. Do you use lazy load modules? (although even in this case it should work)
Try to put this code in your app.module.ts:
import { ENV } from '@app/env';
console.log('ENV', ENV, ENV.firebase);
If ENV.firebase is an object (rather than a primitive value), be careful not to be using the wrong reference. The problem might be there. You can call console.log('ENV', ENV, ENV.firebase); in your environment.dev.ts and environment.ts (and even call console.log(ENV.firebase.someprop) to see the values inside ENV.firebase, because they could change in runtime).
Or you can try to use only primitive values in your ENV object. I do that but more because I store them in a json file because I use a hook to set config.xml variables (like FACEBOOK_APP_ID and FCM_SENDER_ID) dynamically, according to my environment variables, but there is also the benefit to avoid pitfalls due to using references inside the ENV object.
So try some logs and see how you create the ENV.firebase value because the problem might be there.
Thanks again @lucasbasquerotto this solution is now working. @gshigeto solution works great except with the AOT compiler. The repo attached makes it much simpler to work anywhere. Maybe @danbucholtz or @mhartington could take a look and see if it would fit for the overall project needs?
https://github.com/ajonp/angularfire2-env-issue/blob/master/README.md
is there an official way of configuring this? I could not find it in the official document so all above is just a work around?
@sandangel I don't think there is an official way, but the above works pretty much like I want it to, defining the environment variables based on a flag passed when the app is build. Aside from the initial setup (that is done only once), when I add new variables I only have to define it in the interface and in the environment files. Even adding a new environment is very easy, just adding a file for it and defining the values of the variables.
I had end up with integrating ionic with angular cli project. So I can use environment variables and also use angular 5, @nrwl workspace without losing cordova support. You can take a look at https://github.com/ngx-rocket, it’s great. Everything work fine in both angular v4 and v5 except some warning message. Since angular cli support schematics from v1.5, I think ionic team should consider using angular/cli too to take advantages of super fast angular bazel build tool. but i guess I should open another feature request about this.
Hi @lucasbasquerotto @gshigeto @ajonp @sandangel I'm coming to this looking for the best way of building a three way environment approach (prod, dev and staging) into an app using the latest version of Ionic (3.8.0). I'm a bit confused as to what the best approach is now because it seems all three of you do it slightly differently?
@richardshergold I think integrate with angular cli is the best way because with angular 5, you can now do server side rendering with ionnic.
@richardshergold Because there isn't an official approach, my advice is to use the one that you think is the best for you, and the simplest to maintain. In my case I've used @gshigeto approach, it worked very well for almost all cases, just changed it a bit because it didn't worked with AOT when you pass environment variables inside a NgModule. So I made a few changes and you can see this demo that @ajonp created to understand the approach:
https://github.com/ajonp/angularfire2-env-issue
The key files are listed in the README of the repository above, it uses cross-env to define the environment (npm install --save-dev cross-env). Other than the files listed there, give a look to tsconfig.json and you can download the sources and execute it locally to understand how it works. You can add to package.json, inside scripts:
"ionic:run:dev": "cross-env MY_ENV=dev ionic cordova run"
And just execute in browser for simplicity:
npm run ionic:run:dev browser
The environment files are json files inside the env directory in the project root folder (you can see them in the demo linked above).
If you want to create a staging environment, just create a file env/environment.staging.json (just like the other json files), add the folowing to the scripts in package.json:
"ionic:run:staging": "cross-env MY_ENV=staging ionic cordova run",
"ionic:run:staging:prod": "cross-env MY_ENV=staging ionic cordova run --prod",
And execute with:
npm run ionic:run:staging browser (without optimizations)
npm run ionic:run:staging:prod browser (with optimizations)
You can add any number of environments that you want, just like with staging.