Last active
May 11, 2017 17:22
-
-
Save mars/b5ec07d14415fb5a0bbd to your computer and use it in GitHub Desktop.
Webpack config to support environment variables in JS source
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
WEBPACK_ENV: JSON.stringify('development'), | |
FOO: JSON.stringify('per-environment foo value') | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var nodeEnv = process.env.NODE_ENV || 'development'; | |
var envConfig; | |
try { | |
envConfig = require('./config/'+nodeEnv); | |
console.log('"'+nodeEnv+'" environment'); | |
console.dir(envConfig); | |
} catch(error) { | |
console.error('The NODE_ENV must correspond to a module defined in the config directory. Value was', nodeEnv); | |
throw error; | |
} | |
module.exports = { | |
plugins: [ | |
new Webpack.DefinePlugin(envConfig) | |
] | |
}; |
I guess to ensure it takes all values as string and not as variables, but that's just a guess, only author will know for sure.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
why use
JSON.stringify
?