Environment Variables in an isomorphic JS app: Webpack find & replace?

后端 未结 4 940
没有蜡笔的小新
没有蜡笔的小新 2020-12-29 08:32

I\'m using webpack to bundle an isomorphic JS app (based on this example) so that the browser runs the same code as the server. Everything is running smoothly except I have

4条回答
  •  梦毁少年i
    2020-12-29 09:04

    Note that using the DefinePlugin as suggested in the accepted answer is potentially a dangerous action as it completely exposes process.env. As Tobias commented above there's actually a plugin EnvironmentPlugin that does exactly this with an added whitelisting ability, using DefinePlugin internally.

    In your webpack.config.js:

    {
      plugins: [
        new webpack.EnvironmentPlugin([
          'NODE_ENV',
          'WHITELISTED_ENVIRONMENT_VARIABLE'
        ])
      ]
    }
    

提交回复
热议问题