Skip to content

Instantly share code, notes, and snippets.

@mars
Last active May 11, 2017 17:22
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mars/b5ec07d14415fb5a0bbd to your computer and use it in GitHub Desktop.
Save mars/b5ec07d14415fb5a0bbd to your computer and use it in GitHub Desktop.
Webpack config to support environment variables in JS source
module.exports = {
WEBPACK_ENV: JSON.stringify('development'),
FOO: JSON.stringify('per-environment foo value')
};
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)
]
};
@diegoaguilar
Copy link

@mars Can you extend more on the use of this?

@emaillenin
Copy link

@mars How do we use the config values in our app code?

@jt3k
Copy link

jt3k commented Jul 19, 2016

why use JSON.stringify ?

@luchillo17
Copy link

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