-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Best way to pass variables through Webpack? #386
Comments
Not sure if this is the best way, but here is how I do it in both
Then you can just use it in the code
For development I use dotenv to set the environment variable. |
Perfect. Thanks! |
@rbao is correct. @rbao can you make a wiki page to talk briefly about this https://github.com/AngularClass/angular2-webpack-starter/wiki |
Don't forget to restart Webpack after adding the environment variables or the system won't recognize them. |
I did all those things mentioned above, but when running npm start i get a process object that contains an EMPTY env object |
I do it like this:
and then in JS:
|
@marek1 I noticed the same, there is a strange behaviour: When I de When I do |
This strange behavior becomes clear when you look at the packed webpack file. Basically process does not get added to the global scope what so ever. Webpack does a search and replace for anything that is process.env and fills it in with that object specifically everywhere. So essentially it is a new instance of that config object every time. I think this was to not pollute the global space, but for minify purposes seems like a waste. Is there a option to make it part of the global namespace? Example: new webpack.DefinePlugin({
'__PROCESS__': {
'ENV': 'production',
}
}) client code (source mapped): if(__PROCESS__.ENV === 'production'){
//do something
} Real Packed Code: client code (packed): if(({"ENV":"production"}).ENV === 'production'){
//do something
} It is not what you assumed it was window.__PROCESS__ = {"ENV":"production"}; |
Also this is talked about in some detail here webpack/webpack#868 To get this working a little nicer for consuming your config objects I wrote this: function packinize(obj){
_.each(obj, (v, i) => {
if(_.isString(v))
obj[i] = JSON.stringify(v)
else if(_.isObject(v) && !_.isFunction(v))
packinize(v)
})
return obj
}
const configPlugin = new webpack.DefinePlugin({
__CONFIG__: packinize(frontendConfig)
}) |
So, how could I do if const module = __LANG__ ? require('./' + __LANG__) : require('./zh-cn') |
Why not just this? new webpack.DefinePlugin({
'process.env': process.env
}) |
Is @jianghai 's method possible? None of these instructions are clear with the use of TypeScript. |
@HendrikRoth please don't ever do this, as you've just exposed the environment of the process that you built with to your bundle... And subsequently the internet. Many sensitive values are potentially contained therein, unless |
@al-the-x so it means the webpack.DefinePlugin rewrites the process.env as a global variable in a webpack project?
../config/dev.env:
so can we change the process.env in |
In the example from above to which I objected, consider code like this: // some/project/file.js
if (process.env.NODE_ENV === 'production') {
doSomethingForProduction();
} The plugin // some/project/file.js
if ('production' === 'production') {
doSomethingForProduction();
} The config // some/project/file.js
if (({ /* all of process.env as key: value */ }.NODE_ENV === 'production') {
doSomethingForProduction();
} The suggestion you made @gezichenshan, would be much safer, but why not just put those definitions in the const _ = require('lodash');
const KEYS = [
'NODE_ENV',
'BASE_URL',
'SOMETHING_ELSE',
// . . . so many others
];
module.exports = _.chain(KEYS)
.map(key => `process.env.${key}`)
.map(path => [ path, _.get(process.env, path) ])
.fromPairs()
.value(); But likely that's not practical, either, since that would require a large number of environment variables passed to the startup script. At that point, another external configuration file would be better. Probably one that switches values dependent on... |
JSON not JSI probably should've caught this right away, but I wasn't wrapping the values in JSON.strinify() and was throwing console errors that listed the key, but said it wasn't defined. Which makes sense, but took me a few minutes to figure out why. Hope this helps others in the future. Originally I was using the below Then updated it to this to make it work properly |
Simple question, but I'm a little lost with the way
webpack.DefinePlugin
interacts with the application.For my
http
calls, I'd like to have a variable that is dynamic based on theprocess.env.ENV
. What's the best way to define a variable in both my webpack.config.js and webpack.prod.config.js files? And what's the best way to make them global variables in the application?The text was updated successfully, but these errors were encountered: