-
Notifications
You must be signed in to change notification settings - Fork 654
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: Ionic 2 (dev/prod) environment variables configuration #1205
Comments
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. |
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 Implementation steps
├── src/config/environment.dev.ts
export const ENV = {
PRODUCTION : false,
API_URL : 'dev.local'
}; ├── src/config/environment.prod.ts
export const ENV = {
PRODUCTION : true,
API_URL : 'prod.local'
};
// use the 'environment-dev' as the default import(!)
import { ENV } from '../../config/environment-dev';
@Component({
templateUrl: 'hello-ionic.html'
})
export class HelloIonicPage {
console.log( ENV.API_URL );
}
├── /package.json
"config": {
"ionic_rollup": "./config/rollup.config.js"
}
├── /config/rollup.config.js
var replace = require('rollup-plugin-replace');
var isProd = (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 |
Is there any plans to actually implement this? |
With Steps to implement:
├── /config/webpack.config.js
require('dotenv').config();
...
function getPlugins() {
var plugins = [
new webpack.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(new webpack.optimize.OccurrenceOrderPlugin());
}
return plugins;
}
...
├── /package.json
"config": {
"ionic_webpack": "./config/webpack.config.js"
}
├── /src/customTypings/customTypings.d.ts
declare var API_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 require('dotenv').config({path: '/custom/path/to/your/env/vars'}) |
This solution is working, but I think, rewriting all webpack.config is not good, becase updates... Is there any idea from ionic team? |
I'd really like to see this functionality supported. Is a custom webpack config the only way? |
Okay... After a lot of coffee and head bumping, I've realized that the best way (at least for me) is to change the environments manually and not rely on ionic to change it. That was mainly because ionic-cli does not let you run environment.ts
Then I added an environment.service among my services. environment.service.ts
Then I added the service to the providers of the app.module and used it like any other service. any.component.ts
And finally, in order to change the environment, you have to toggle (comment and uncomment) the first two line of environment.ts. This is a good workaround for now, if you need different values (URLs, API keys, etc.) to be used in development and production. But, I think eventually, ionic and ionic-cli teams will have to work together to integrate something like this internally, that lets us use different sets of environment variables based on a flag that we pass to the command that is running the app. |
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 / PRODUCTION or CUSTOM ENV: ionic build / PRODUCTION ENV (DEFAULT): ionic build / DEVELOPMENT or CUSTOM ENV: eg. http://jonnyreeves.co.uk/2016/simple-webpack-prod-and-dev-config/ |
@slinto You are absolutely correct, there is the possibility of publishing the app with dev variables. But, it is only as dangerous as any other solution. You may build the prod app with the wrong command as well. |
+1 |
Hey, guys, any progress of official implements? |
@dnmd can you confirm that your solution works with a production build? |
with
By the way, isn't it better to listen for the argument |
I like the idea of @slinto
And we have to copy config file at build time to choose the environment. This is managed by a hook based on this post : http://www.kdmooreconsulting.com/blogs/build-a-cordova-hook-to-setup-environment-specific-constants/ But since we do that, we lost the live reload feature! (ionic-app-script watch doesn't copy our right config.js file and we don't know how to do that). If we can set a parameter like @slinto told -- to choose the environment with something like "--env=xxx", and make it available for live reload ... -- it will be really cool! |
Definitely a must. So +1 |
So how would this tie in with the Live Deploy to channel feature currently available https://docs.ionic.io/services/deploy/ ? |
OK, so I needed this kind of feature to be able to have different parameters (api endpoints, google analytics ID etc...) with different environments (dev, preprod, prod...). So I made my first module (feedbacks more than welcome, and please be gentle :p) to be able to do that: gl-ionic2-env-configuration Basically, it loads a In the package documentation, I linked a simple node executable to easily copy the configuration file for a specific environment. Advantages
Hope this will help. |
Any updates to this Ionic team? I would love to see this baked into the |
Inspired by the solution from @tabirkeland I thought I'd post my own version of this. The differences with the script below vs the original solution:
Here's the steps to get it all working:
// Set the `ENV` global variable to be used in the app.
var path = require('path');
var webpack = require('webpack');
var projectRootDir = process.env.IONIC_ROOT_DIR;
var appScriptsDir = process.env.IONIC_APP_SCRIPTS_DIR;
var config = require(path.join(appScriptsDir, 'config', 'webpack.config.js'));
var env = process.env.IONIC_ENV || 'dev';
var envVars;
try {
envVars = require(path.join(projectRootDir, 'env', env + '.json'));
} catch(e) {
envVars = {};
}
config.plugins = config.plugins || [];
config.plugins.push(
new webpack.DefinePlugin({
ENV: Object.assign(envVars, {
environment: JSON.stringify(env)
})
})
);
if(env === 'prod') {
// This helps ensure the builds are consistent if source hasn't changed:
config.plugins.push(new webpack.optimize.OccurrenceOrderPlugin());
}
module.exports = config;
"config": {
"ionic_webpack": "./config/webpack.config.js"
}
{
"enableLogging": true,
"apiServerUrl": "\"http://example.com/api\""
} Now, you can use the declare const ENV;
if(ENV.environment === 'dev') {
// Run without the `--prod` flag.
// Any keys defined in `dev.json` will be available on the `ENV` object.
} else if (ENV.environment === 'prod') {
// Run with the `--prod` flag.
// Any keys defined in `prod.json` will be available on the `ENV` object.
} The script creates an The {
"apiServerUrl": "\"http://example.com/api\""
} It would be great to see this baked into the core |
I implemented the solution from @fiznool the webpack script does not look for My very limited knowlege of webpack stumped me for a bit and I had to add extra quotes in my json so I ended up with |
+1 (what's the correct way to vote on a feature request?) |
@zuperm4n Use the thumbs up emoji on the first post |
@dwieeb can we get an ETA for Ionic Angular 4 please? Weeks, months? |
+1 for this feature request |
For those looking for a solution that supports Works by extending webpack's configuration so should be pretty easy to refactor out later whenever Ionic 4 drops with native Angular CLI support. |
Hi All, Thanks! |
@wilfredonoyola I use this too, thanks! |
All, with Ionic 4 release approaching, I know that ENV vars will be handled. If you are using Ionic 3.9.2, here is what I have been using for months to handle ENV vars. It's been pieced together from this and a few other threads. https://gist.github.com/tabirkeland/a17c67b2f1ea3331d94db34ed7191c34 |
Changing the milestone to Ionic Angular 4. CLI 4.0.0 will likely be released before the upcoming Framework release. CLI 4.0.0 Milestone: https://github.com/ionic-team/ionic-cli/milestone/36 See this issue to learn how to test Ionic Angular 4 in the alpha CLI today: #3019 (warning: it's likely a bit early yet) |
I'm really not sure why this has gotten so complicated. As an example, in Ember.js ( Basically:
This gives you the flexibility of making use of existing system environment variables as being able to easily declare extra project-specific env variables via the In other solutions I've seen above env variables are being declared as JS modules in the source code, which isn't a great idea, and not particularly flexible. Maybe I'm missing something with the added complexity of building via cordova as well as the web, but this seems to me to be already solved elsewhere. EDIT: I see that this is an upcoming feature of Ionic 4 so I'm looking forward to seeing it implemented as part of core 👍 |
Recently tried the solution proposed by @fiznool but failed. When I used the DefinePlugin inside the webpack config in my node_modules everything worked fine. But as soon as I extended the config the build broke with webpack service a 404 for all compiled resources. This error also persisted when I simply copied the entire webpack config out of the node_modules folder. We wanted to use the DefinPlugin to enable feature flags from the console when running |
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 |
Great! As the requestor of this feature, I am so happy it is finally resolved. |
It is fixed if you upgrade aka migrate
…On Mon, Jul 30, 2018, 10:22 PM Amr ElAdawy ***@***.***> wrote:
Great! As the requestor of this feature, I am so happy it is finally
resolved.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#1205 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAP7O-_V4Or7xnbRBPUTEohiuPSAlhooks5uL815gaJpZM4JUhnW>
.
|
For all those still subscribed to this issue, @nphyatt has put in support for this in It runs a Webpack plugin which replaces usages of Let us know what you think! |
This is great news that this has arrived!
|
Could you elaborate on how the documentation is ambiguous? I do think the documentation assumes some knowledge about: environment variables, NodeJS's 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 |
@dwieeb Thank you for your response.
Thank you in advance! |
Thanks @granttink! Good feedback.
Yeah, the files are actually optional, which I think should also be stated. Also, the documentation assumes some knowledge about the format of I added an issue to revise the documentation: ionic-team/ionic-app-scripts#1474. Thank you 👍 |
Thank you @dwieeb, that clarifies things quite a lot. |
Should this issue be closed? Considering the feature is not working correctly? |
To clarify:
|
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. |
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 |
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 |
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: ionic-team/ionic-framework#7413
The text was updated successfully, but these errors were encountered: