展示和发布渠道...如何识别开发人员/产品和真实渠道

时间:2018-09-03 23:14:53

标签: reactjs react-native expo

我在发布发布渠道时遇到了一些麻烦。 我不是React Native和Expo的专家,这使事情变得更加轻松:

如果我在本地开发环境中工作,则不会设置频道(这很有意义)。

但是最终版本或“生产”也将没有频道设置, 这让我不清楚应该如何认识“生产”和“发展”。

然后,如果我想添加一个频道(例如“登台”),则会添加新的复杂性级别,而该频道将改为有一个频道...

锦上添花的是,在我的部署系统(Circle)中,我必须在通道中进行“开发”(否则NODE_ENV将为“生产”)

有人知道如何正确使用频道吗? :)

基本上,我没有找到比这个更好的解决方案了:

import { Constants } from 'expo'
const ENV= {production:{},staging:{},development:{}}

// Having fun with channels
const channel = Constants.manifest.releaseChannel;
if (channel === null || channel === undefined || channel === '') {
  if (process.env.NODE_ENV === 'production') {
    return ENV.production;
  }
  if (process.env.NODE_ENV === 'development') {
    return ENV.development;
  }
}
if (channel === 'staging') {
  return ENV.staging;
}
if (channel === 'development') {
  return ENV.development;
}
return ENV.production;

非常感谢您!

2 个答案:

答案 0 :(得分:2)

我认为您错过了发布渠道的用途。

使用exp build构建您的应用程序时,它将绑定到一个发布渠道(默认为default)。 稍后,如果您想进行OTA更新,则可以运行exp publish,它将在发布渠道上发布您的代码(同样,默认为default)。

当您向用户交付独立版本时,您不想通过OTA给他们提供未经测试的代码等,因此您希望用户将发布渠道设置为ex。 prod

这与NODE_ENV是完全分开的,我对捆绑它们没有任何意义。

答案 1 :(得分:0)

值得注意的是,process.env.NODE_ENV === 'production'构造仅在捆绑包构建过程中才有意义-捆绑器将在NODE_ENV变量设置为特定环境的情况下运行。

production环境中,这意味着捆绑程序的内置minifier将用其实际值(process.env.NODE_ENV fe)替换所有出现的production,这是默认情况下配置minifier的方式在metro捆绑器中。 F.e:

if (process.env.NODE_ENV !== 'production') {
   ... some dev-only code, like 'assert(something)' or 'console.log(some debug info)'
}

将成为

if ('production' !== 'production') {
   ... some dev-only code, like 'assert(something)' or 'console.log(some debug info)'
}

,然后将其进一步缩小为

if (false) {
   ... some dev-only code, like 'assert(something)' or 'console.log(some debug info)'
}

然后将其从结果代码中完全消除,因为if语句中的代码将永远不会执行。

例如,您的代码在开发人员模式下将被视为:

if (channel === null || channel === undefined || channel === '') {
  if ('development' === 'production') {
    return ENV.production;
  }
  if ('development' === 'development') {
    return ENV.development;
  }
}

... further simplified to:

if (channel === null || channel === undefined || channel === '') {
    return ENV.development;
}

并在生产模式下:

if (channel === null || channel === undefined || channel === '') {
  if ('production' === 'production') {
    return ENV.production;
  }
  if ('production' === 'development') {
    return ENV.development;
  }
}

... further simplified to:

if (channel === null || channel === undefined || channel === '') {
    return ENV.production;
}

您应该记住,react-native代码在设备上的本机应用程序内运行,而不是在某些Node实例内运行,因此它对严格特定于Node的生态系统/环境变量一无所知(尽管您可以胡闹-通过process.env等库通过自定义值对dot-env进行补丁),这是npm的某些Node utils / packages不能在react-native应用程序中使用的原因之一-是,因为它们是节点特定的(fs fe)。

这与在提供给客户端浏览器的前端代码中使用process.env.NODE_ENV变量相同-浏览器中没有process实例,除非您使用一个缩小器并将其配置为用某些特定值替换process.env.NODE_ENV出现的位置,您的代码将简单地引发未定义的变量访问错误。 Metro捆绑程序可以为您处理此特定用例,但我认为还是值得一提。