为react-native 0.60.6配置transform-inline-environment-variables

sha*_*oof 5 plugins babeljs react-native

module.exports = function () {
  return {
    "presets": ['module:metro-react-native-babel-preset'],
    "plugins": [
      ["transform-inline-environment-variables", {
        "include": [
          "APP_ENV"
        ]
      }]
    ]
  }
};
Run Code Online (Sandbox Code Playgroud)

这是我的react-native项目的babel.config.js,我正在尝试配置transform-inline-environment-variables babel插件。但每当我运行应用程序时,环境变量总是未定义。

我运行react-native start --reset-cache以清除缓存并使用命令运行项目APP_ENV=dev react-native run-ios

但该变量仍然未定义。我在这里做错了什么?React-native版本是0.60.6

Eli*_*ant 2

尝试直接记录访问(而不是解构):

console.log(process.env.APP_ENV)
Run Code Online (Sandbox Code Playgroud)

另外,一旦连接了模拟器,您就可以运行start因此:

  1. yarn run-ios
  2. 关闭 Metro Bunler 终端窗口并保持模拟器打开
  3. API_ENV=dev yarn start --reset-cache

此处详细介绍了这个问题。

更好的是,用react-native-config替换你的实现。