olahol / reactpack

:package: build your react apps with one command and one `npm i`.
982 stars 36 forks source link

use DefinePlugin to include environment variables #16

Closed erikhuisman closed 8 years ago

erikhuisman commented 8 years ago

I did something like this to load some environment variables. Might come in handy to people building for test, staging, production.

  if (options.env) {
    var envfile = path.join(process.cwd(), path.dirname(entry), '.env.js')

    try {
      var environments = require(envfile);
      config.plugins.push(new webpack.DefinePlugin(environments))

      config._msgs.push(util.format(
        'Using custom environments (%s).',
        path.relative(process.cwd(), JSON.stringify(environments))
      ))
      config._msgs.push(util.format(
        'Using custom environments found in root of entrypoint (%s).',
        path.relative(process.cwd(), envfile)
      ))
    } catch (e) {
      config._msgs.push(util.format(
        'No custom environments found in root of entrypoint (%s).',
        path.relative(process.cwd(), envfile)
      ))
    }
  }
/ /.env.js
module.exports = {
    DEBUG: JSON.stringify(true),
    API: JSON.stringify('https://test.api.com')
};
console.log(DEBUG, API) // true, 'https://test.api.com'
olahol commented 8 years ago

Thank you @erikhuisman, that's a really nice addition, I've added this in version 0.5.0.