配置 transform-inline-environment-variables 用于 react-native 0.60.6

huangapple go评论71阅读模式
英文:

configuring transform-inline-environment-variables for react-native 0.60.6

问题

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

这是我的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。

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

This is my babel.config.js for my react-native project, and I'm trying to configure the transform-inline-environment-variables babel plugin. But whenever I run the app the environment variable is always undefined.

I run react-native start --reset-cache to clear the cache and run the project using the command APP_ENV=dev react-native run-ios.

But the variable is still undefined. What am I doing wrong here? The react-native version is 0.60.6

答案1

得分: 2

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

console.log(process.env.APP_ENV)

而且,一旦连接了模拟器,可以然后运行 start,所以:

  1. yarn run-ios
  2. 关闭 metro bundler 终端窗口,保持模拟器开启
  3. API_ENV=dev yarn start --reset-cache

此问题在此处有详细介绍。

更好的做法是将您的实现替换为react-native-config

英文:

Try logging access directly (rather than destructuring):

console.log(process.env.APP_ENV)

Also, once you have the emulator connected, you can then run start, so:

  1. yarn run-ios
  2. Close the metro bundler terminal window and leave the emulator open
  3. API_ENV=dev yarn start --reset-cache

This issue is covered in detail here.

Better still, replace your implementation with react-native-config.

huangapple
  • 本文由 发表于 2020年1月6日 15:43:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608352.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定