-
Notifications
You must be signed in to change notification settings - Fork 304
Alias environment specific module (to change API keys, constants in c… #683
Conversation
…ode across environments).
I realize that this is a very simple and base solution -- I am hardcoding in the alias name and path, as it's what I've used in my current project. I do not expect this to be pulled, but rather conceptualized, and serve as a base of how to achieve a request many users are asking for. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do you pass a value to env.IONIC_ENV? Is it automatically dev/prod, depending on if --prod is passed as argument?
@biesbjerg -- Yup, it's either
|
Nice. Seems like a clean solution, and should be possible to use now by extending the webpack config to define the alias like in your PR until official support hopefully arrives. Thank you for sharing! |
Anytime! Thanks for your consideration! |
One drawback using this method is if you are using an interface to describe your config object. For example:
Visual Studio Code does not seem to resolve it and autocomplete keys when typing |
I'm going to give this some thought over the next couple weeks. Thanks for contributing! Thanks, |
It would be great to have this feature baked into app scripts. There have been other solutions also debated in ionic-team/ionic-cli#1205 - anything that provides us with environment-specific config would be a big step forward. One thing that was brought up in the other thread was the need for more environments than just dev/prod - for example, qa. In addition to |
Where to use this module alias? Can we have an example? I tried using this but getting TS error can' find module @app/config. Any help? Thanks in advance. |
Can someone explain this PR to me like I'm five. I'm not familiar with the alias option in webpack. We need to make sure we consider Rollup, too. Thanks, |
@danbucholtz - The alias option let's you alias a path, so if I alias This becomes important when you have multiple environments. It's a better practice to create multiple files that export the same module, but the values differ based on what environment being run. By creating an alias, such as Webpack creates the alias for us, which unfortunately, @riteshwaghela, does not seem to work well in certain IDEs. It works in VS Code, but you don't get a typeahead, which I'd expect because we are setting an alias. However, I can live with the trade-off because I can easily switch configuration values when building. |
@danbucholtz - I don't think this PR is a solid fix. I think it's a good baseline to start from, however, and it served as a good stopgap for people who had the same issues I did. I think there would be a more elegant way to incorporate this into your build scripts; this was quick-and-dirty. But, the need for supporting multiple environment configurations is needed, I think it would be best to address it from the |
What do you all think about something like this? Let's continue the discussion there. Please provide feedback. In the meantime, I'm going to close this PR. Thanks for contributing and spurring the discussion. Thanks, |
@ehorodyski I am using VS code, still this didn't work for me. I added the alias in the webpack.config.js file the same way you did. And then imported the module in my code using |
@riteshwaghela - Did you modify the |
@ehorodyski Thanks for your reply. Yes I have modified my project's package.json to new webpack config file. and in the new webpack.config.js, added the alias like this: |
Hey @riteshwaghela -- try this: package.json "scripts": {
"clean": "ionic-app-scripts clean",
"build": "ionic-app-scripts build -w ./config/webpack.config.js",
"ionic:build": "ionic-app-scripts build -w ./config/webpack.config.js",
"ionic:serve": "ionic-app-scripts serve -w ./config/webpack.config.js"
},
"config": {
"webpack": "./config/webpack.config.js"
}, I noticed, at least in RC6 when I wrote this stop-gap, you needed to modify the scripts portion of Hope this helps! |
For me the following is working: package.json
/src/config/webpack.config.js
Then you can import the config with Config file
|
@bnova-stefan I get @app/config cannot be found. Can you share you code? |
Hi tmvnk. |
You can use @bnova-stefan 's solution, and if you want more than DEV/PROD, use TARGET. replace: with and for your build, ex: ionic serve --target qa If you get @app/config cannot be found, it's not finding your config file. |
Just to expand @bnova-stefan 's excellent solution for folks new to this.
For example,
|
Actually, I spoke a bit too soon as I'm getting an error when building for production:
I assume this is due to the AoT Compiler. |
@vkniazeu yes idd, same result here |
@vkniazeu it's probably complaining about the import inside your config files. Does the error go away if you use a static value instead, e.g: export const ENV = {
LOG_LEVEL: 1
// other properties with values for the prod environment
} |
@fiznool , unfortunately it doesn't. Removing the imports was the first thing I'd tried, but the error persisted regardless. |
@bnova-stefan , does your solution work for you with |
FWIW, the following changes make
This basically overwrites the "--prod" or "--dev" environment flags in serve/build commands, but at least the production build is error free and you have some flexibility with having dev/prod config variables in separate files. |
I suspect that the AoT Compiler is looking for a properly defined (installed?) module/package, when it encounters |
well toggling contents of an index.ts file makes the whole webpack stuff obsolete. then for example you could have a index.ts file for every git branch which you don't touch anymore and you switch env by branch. but I also would like to see a solution for the alias issue. this whole dev/prod stuff with ionic drives me crazy. |
@vkniazeu you can also take a look at http://www.roblouie.com/article/296/ionic-2-environment-variables-the-best-way/ which i used to manage env variables in components. but I this approach doesn't seem to work inside app.module.ts when trying to setup firebase config inside imports. |
Thank you for the tips, @mediastuttgart! |
I found fix to Create dummy config. // src/config/config.ts
export const ENV = {
LOG_LEVEL: undefined
} Define alias {
"compilerOptions": {
...
"baseUrl": "./src",
"paths": {
"@app/config": ["config/config"]
}
}
... Include configs. import {Env} from '@app/config'; This trik deceives ts compiler by |
@alu , I spent quite some time yesterday trying to do something very similar although a slightly different way. I'm realizing this environment config adjustment must occur before ngc and webpack steps. The reason I'm saying this is that I ran into a situation that seemed strange, but with exploring static resolution of values more, it makes sense. The following would work fine with your script and ngc:
However, if you were to use the ENV value directly in creating another value, for example in your components, the value would be statically created at ngc compilation time, and NOT be replaced by webpack:
So, in summary, your workaround works, but for statements that do not get AoT-compiled into static values by ngc. |
I've spent a few hours trying almost all the solutions above, but none of them seem to work with AoT compiler, for setting up different Firebase environments, which occur in app.module.ts in the imports section (and I can't use Angular DI in this section). Not even using @alu fix. Because of this I've decided to take a different approach: I've created a shell wrapper for Ionic CLI. I call it Bionic :) Steps:
config.dev.ts
Tweak the shell however you want. |
A stopgap solution I am currently using which makes use of git: branch = master src/config.ts export API_URL = 'dev.example.com'; branch = release/production src/config.ts export ENV = 'prod.example.com'; branch = qa src/config.ts export ENV = 'qa.example.com'; |
thanks @mediastuttgart nice article |
Short description of what this resolves:
Allows environment-based configuration at the code-level.
Changes proposed in this pull request:
Using a module alias in Webpack, allow users to utilize environment-based configuration in their app code.
I've seen a lot of requests and half-measures researching this today and thought I could contribute to the base idea of how this can be accomplished.
This is a quick proof of what can be done. I am willing to help contribute upon this concept and expand it to a form that works for the Ionic team.