Skip to content

Instantly share code, notes, and snippets.

@mars mars/development.js
Last active May 11, 2017

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

diegoaguilar commented Mar 13, 2016

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

@emaillenin

This comment has been minimized.

Copy link

emaillenin commented May 23, 2016

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

@jt3k

This comment has been minimized.

Copy link

jt3k commented Jul 19, 2016

why use JSON.stringify ?

@luchillo17

This comment has been minimized.

Copy link

luchillo17 commented Feb 20, 2017

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
You can’t perform that action at this time.