2

我有一个命中 API的React on Rails应用程序。我想将 API 端点配置为localhost用于开发,并将我部署的应用程序的 URL 配置为用于生产。

客户端/package.json

 "scripts": {
    "build:production": "NODE_ENV=production webpack --config webpack.config.js",
 },

客户端/webpack.config.js

const devBuild = process.env.NODE_ENV !== 'production';

const config = {
  entry: [
    'es5-shim/es5-shim',
    'es5-shim/es5-sham',
    'babel-polyfill',
    './app/bundles/Main/startup/registration',
  ],
  output: {
    filename: 'webpack-bundle.js',
    path: __dirname + '/../app/assets/webpack',
  },
  resolve: {
    extensions: ['.js', '.jsx'],
  },
  plugins: [
    new webpack.EnvironmentPlugin({ NODE_ENV: 'development' }),
  ]
}

我看到它process.env.NODE_ENV在 config/webpack.config.js 中可用(这里用于将源映射开发工具添加到模块导出中),但我想要一种方法来查看我的 React 代码中某个地方的环境是什么client/。如果有的话,我有什么选择?

4

1 回答 1

3

我刚刚process.env.NODE_ENV在一个 Redux 操作文件中成功使用。我正在使用如下所示的插件定义在 webpack 中导出 NODE_ENV webpack.config.js

plugins: [
    ...
    new webpack.DefinePlugin({
        'process.env': {
            NODE_ENV: JSON.stringify('ENV_VALUE')
        }
    })
]
于 2017-06-09T16:09:37.697 回答