You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Can we raise the priority for this issue? This is actually very important. Our app has different configs for prod and devel. Without this, it requires ugly hand rolled variable updates... Just make the release really cumbersome.
Reacted by Gerald Tremblay, Roland Groza, Yury, Sean Hill, yuhj86, Vitaly Ivanov, wissebarkhof, astopo, Zeno, Juan Herrera and 66 more
Basically what happens is that the import paths are replaced based on the process.env.IONIC_ENV variable during compile time. This will use the environment.dev.ts during development and environment.prod.ts when in production.
Implementation steps
install the rollup-plugin-replace npm install --save-dev rollup-plugin-replace
create two files; environment.dev.ts and environment.prod.ts within e.g. the src/config directory.
import the environment-dev as the default within your pages / services etc.
// use the 'environment-dev' as the default import(!)import{ENV}from'../../config/environment-dev';
@Component({templateUrl: 'hello-ionic.html'})exportclassHelloIonicPage{console.log(ENV.API_URL);}
create your rollup.config.js within the /config directory. Require the plugin, and add the replace snippet below to your plugins section. One can use node_modules\@ionic\app-scripts\config\rollup.config.js as a template.
├──/config/rollup.config.jsvarreplace=require('rollup-plugin-replace');varisProd=(process.env.IONIC_ENV==='prod');
...
plugins: [replace({exclude: 'node_modules/**',// use the /config/environment-dev as the default import(!), no stub needed.// note we only replace the "last" part of the import statement so relative paths are maintained'/config/environment-dev' : (isProd ? '/config/environment-prod' : '/config/environment-dev'),})...]
Hope this helps someone! Of course in an ideal situation a file replacement would take place, similar to the Angular CLI
Reacted by Tomáš Stankovič, Harsha Bonthu, Gevorg Harutyunyan, Jerome Velociter, Taylor Birkeland, Roland Groza, Gerald Tremblay, Nick, Gaetan SENN, Toni Mitrevski and 37 more
Reacted by Raúl García Pérez, huangzk, Allan Clempe, Paul G Mathew, Pedro Monte Mor and teddy
Reacted by Pedro Monte Mor and Alisson Gomes
Reacted by Nick, João Vitor Paes de Barros do Carmo, Sereyboth Van, Raúl García Pérez, Michael Aldridge, Rodolfo Silva, Eyal Chloe Rosen, huangzk, Fabio Souza, Jeff Fischer and 2 more
Create .env file with env variables defined in the root directory of your app. Recommended:
add .env to .gitignore.
Create and modify webpack.config.js from original found here:
├──/config/webpack.config.jsrequire('dotenv').config();
...
functiongetPlugins(){varplugins=[newwebpack.DefinePlugin({'API_URL': JSON.stringify(process.env.API_URL)// Add any more variables here that you wish to define})];if(process.env.IONIC_ENV==='prod'){// This helps ensure the builds are consistent if source hasn't changed:plugins.push(newwebpack.optimize.OccurrenceOrderPlugin());}returnplugins;}...
Define custom webpack config file in package.json.
Create custom typings file defining the constants that are in your .env file so that webpack can populate them and you are able to reference them in your application.
├──/src/customTypings/customTypings.d.tsdeclarevarAPI_URL: string;// Add any more variables here that you wish to define
You should now be able to reference the global variable(s) in your app.
Furthermore, you can create multiple .env files for different environments and require them in webpack.config.js like so:
@jgw96 Can we get this added to a milestone? We should be able to specify different environment configuration e.g. different API endpoints for dev and production. As @slinto mentioned creating a modified webpack.config is not ideal as updates will cause issues :/
Reacted by Tom Schreck, Alexey Pavlov, Gustavo Lira e Silva, sami karakoese, Loc Nguyen, Besi, Steve Schwarz, Tim Waite and Mohammad Shamma
hmm, the manual comment/uncomment of right ENV is dangerous, because is there a big risk with publishing application with development environment to store, especially if your team is about 1+ people :) I think about right way as:
ionic serve / DEVELOPMENT ENV (DEFAULT): $ ionic serve
ionic serve / PRODUCTION or CUSTOM ENV: $ ionic serve --env=prod, $ ionic serve --env=test, $ ionic serve --prod
ionic build / PRODUCTION ENV (DEFAULT): $ ionic build
ionic build / DEVELOPMENT or CUSTOM ENV: $ ionic build --dev $ ionic build --env=test $ ionic build --env=dev
This is great news that this has arrived!
However:
The docs link provided on the app-scripts readme.md is extremely ambiguous and not very helpful whatsoever to someone wanting to implement in their existing/new projects.
Is there an example/starter project where this is being used correctly?
How does one specify the environment (prod/dev/etc) to the CLI?
Could you elaborate on how the documentation is ambiguous?
I do think the documentation assumes some knowledge about: environment variables, NodeJS's process.env, dev builds vs prod builds. But I also think this is fine. I suspect people who understand the concept of environments have likely come across the aforementioned concepts as well, but maybe I'm mistaken. In any case, I will look at PRs to app-scripts for improvements. 🙂
There's no example/starter, but if someone makes one I'd be happy to link to it.
The environment is either dev (default) or prod via --prod.
No matter whether --prod or --dev is used when running ionic serve/build the process.env.IONIC_ENV always specifies "dev" for me.
The doc mentions ".env.dev" and ".env.prod" files in root of application.
a. What is the full filename? anything.env.dev.ts? or did you mean env.dev.ts?
b. What is the root of the application? ./src (where index.html resides) or ./ (where package.json resides)?
c. is there an example what should the contents of these files should contain in order to alter environment variables?
IONIC_ENV not being set is a bug, probably something that was overlooked. I believe both it and NODE_ENV should be set to prod/production respectively if --prod is used.
Yeah, the files are actually optional, which I think should also be stated. Also, the documentation assumes some knowledge about the format of .env files. Also, we can make it clear that this is the webpack plugin we're using.
This issue will remain closed as it is now an available feature. There are minor issues with it, which should be tracked separately. I would prefer new issues for them. I will update this issue when they are fixed.
Activity
itrethan commentedon Oct 22, 2016
Can we raise the priority for this issue? This is actually very important. Our app has different configs for prod and devel. Without this, it requires ugly hand rolled variable updates... Just make the release really cumbersome.
dnmd commentedon Oct 24, 2016
Currently our implementation uses the rollup-plugin-replace, and a slightly modified rollup config.
Basically what happens is that the import paths are replaced based on the
process.env.IONIC_ENVvariable during compile time. This will use theenvironment.dev.tsduring development andenvironment.prod.tswhen in production.Implementation steps
install the rollup-plugin-replace
npm install --save-dev rollup-plugin-replacecreate two files;
environment.dev.tsandenvironment.prod.tswithin e.g. thesrc/configdirectory.environment-devas the default within your pages / services etc./package.jsonrollup.config.jswithin the/configdirectory. Require the plugin, and add thereplacesnippet below to your plugins section. One can usenode_modules\@ionic\app-scripts\config\rollup.config.jsas a template.Hope this helps someone! Of course in an ideal situation a file replacement would take place, similar to the Angular CLI
tabirkeland commentedon Nov 1, 2016
With
"@ionic/app-scripts": "0.0.39"now defaulting to webpack for bundling, I am doing something like this.Steps to implement:
Make sure you have installed the latest app-scripts
npm install @ionic/app-scripts@latestand have the latesttsconfig.json.Install dotenv.
Create
.envfile with env variables defined in the root directory of your app. Recommended:add
.envto.gitignore.Create and modify
webpack.config.jsfrom original found here:package.json..envfile so that webpack can populate them and you are able to reference them in your application.You should now be able to reference the global variable(s) in your app.
Furthermore, you can create multiple
.envfiles for different environments and require them inwebpack.config.jslike so:tomasstankovic commentedon Nov 8, 2016
This solution is working, but I think, rewriting all webpack.config is not good, becase updates... Is there any idea from ionic team?
rossholdway commentedon Nov 11, 2016
@jgw96 Can we get this added to a milestone? We should be able to specify different environment configuration e.g. different API endpoints for dev and production. As @slinto mentioned creating a modified webpack.config is not ideal as updates will cause issues :/
locnguyen commentedon Nov 11, 2016
I'd really like to see this functionality supported. Is a custom webpack config the only way?
tomasstankovic commentedon Nov 11, 2016
hmm, the manual comment/uncomment of right ENV is dangerous, because is there a big risk with publishing application with development environment to store, especially if your team is about 1+ people :) I think about right way as:
ionic serve / DEVELOPMENT ENV (DEFAULT):
$ ionic serveionic serve / PRODUCTION or CUSTOM ENV:
$ ionic serve --env=prod,$ ionic serve --env=test,$ ionic serve --prodionic build / PRODUCTION ENV (DEFAULT):
$ ionic buildionic build / DEVELOPMENT or CUSTOM ENV:
$ ionic build --dev$ ionic build --env=test$ ionic build --env=deveg. http://jonnyreeves.co.uk/2016/simple-webpack-prod-and-dev-config/
allaanz commentedon Nov 14, 2016
+1
zjhiphop commentedon Nov 20, 2016
Hey, guys, any progress of official implements?
105 remaining items
imhoffd commentedon Jul 30, 2018
Hi all! 👋 As many of you may have seen, we announced Ionic 4 beta on our blog! 🎉
I'm closing this issue as it has been addressed with Ionic 4. If I'm mistaken, please let me know.
Here is some documentation on the Angular CLI's concept of application environments: https://github.com/angular/angular-cli/blob/master/docs/documentation/stories/application-environments.md
amreladawy commentedon Jul 31, 2018
Great! As the requestor of this feature, I am so happy it is finally resolved.
joefeser commentedon Jul 31, 2018
imhoffd commentedon Aug 24, 2018
For all those still subscribed to this issue, @nphyatt has put in support for this in
@ionic/app-scriptsto support environments for Ionic 3!It runs a Webpack plugin which replaces usages of
process.env.VARwith string literals, using actual environment variables during the build to do so. See https://github.com/ionic-team/ionic-app-scripts#environments for docs and ionic-team/ionic-app-scripts#1471 for the PR. 🎉Let us know what you think!
granttink commentedon Sep 5, 2018
This is great news that this has arrived!
However:
imhoffd commentedon Sep 5, 2018
Could you elaborate on how the documentation is ambiguous?
I do think the documentation assumes some knowledge about: environment variables, NodeJS's
process.env, dev builds vs prod builds. But I also think this is fine. I suspect people who understand the concept of environments have likely come across the aforementioned concepts as well, but maybe I'm mistaken. In any case, I will look at PRs to app-scripts for improvements. 🙂There's no example/starter, but if someone makes one I'd be happy to link to it.
The environment is either dev (default) or prod via
--prod.granttink commentedon Sep 5, 2018
@dwieeb Thank you for your response.
a. What is the full filename? anything.env.dev.ts? or did you mean env.dev.ts?
b. What is the root of the application? ./src (where index.html resides) or ./ (where package.json resides)?
c. is there an example what should the contents of these files should contain in order to alter environment variables?
Thank you in advance!
imhoffd commentedon Sep 5, 2018
Thanks @granttink! Good feedback.
IONIC_ENVnot being set is a bug, probably something that was overlooked. I believe both it andNODE_ENVshould be set toprod/productionrespectively if--prodis used.Yeah, the files are actually optional, which I think should also be stated. Also, the documentation assumes some knowledge about the format of
.envfiles. Also, we can make it clear that this is the webpack plugin we're using.I added an issue to revise the documentation: ionic-team/ionic-app-scripts#1474. Thank you 👍
granttink commentedon Sep 5, 2018
Thank you @dwieeb, that clarifies things quite a lot.
granttink commentedon Sep 12, 2018
Should this issue be closed? Considering the feature is not working correctly?
granttink commentedon Sep 12, 2018
To clarify:
imhoffd commentedon Sep 12, 2018
This issue will remain closed as it is now an available feature. There are minor issues with it, which should be tracked separately. I would prefer new issues for them. I will update this issue when they are fixed.
kyleabens commentedon Oct 4, 2018
Any update or work around on this issue? I’m trying to build my app with the —prod flag now and it’s still not switching out the variables properly
md-hamed commentedon Dec 13, 2018
For anyone who is still stuck on this; I simply needed to have different configurations for different environments. Check the following gist for my implementation: https://gist.github.com/md-hamed/9212a32b37e72b1f8fd51136b1e1dfdc