2016-11-25 70 views
2

我想在我的React Native包中导入ReactNativePropRegistry。在反应本机中有条件地解析模块

我曾经在React Native 0.38.0之前将它从'react/lib/ReactNativePropRegistry'导入,但它在React Native 0.38.0中更改为'react-native/Libraries/Renderer/src/renderers/native/ReactNativePropRegistry'

我要支持所有的阵营原生版本,包括0.38.0我的包,所以我做了这个

if(semver.gte(reactNativeVersion, '0.38.0-rc.0')) { 
    const ReactNativePropRegistry = require('react-native/Libraries/Renderer/src/renderers/native/ReactNativePropRegistry'); 
} else { 
    const ReactNativePropRegistry = require('react/lib/ReactNativePropRegistry'); 
} 

但似乎打包尝试静态解析模块。因此,即使reactNativeVersion小于0.38.0-rc.0,它也会尝试在此路径上解析模块'react-native/Libraries/Renderer/src/renderers/native/ReactNativePropRegistry',从而导致它引发错误Unable to resolve module ...

有没有办法解决这个问题?

回答

1

你不能做动态导入。然而,由于本土做出反应打包使用node-haste两者作出反应和应对本地使用急速@providesModule声明,您可以通过名称来导入模块,而无需指定位置:

require('ReactNativePropRegistry'); 

我还没有尝试过的这个特定的模块,但理论上它应该工作。

可能很高兴知道RN的一些遥远的未来版本,包装商可能会因包装边界上的providesModule名称而失去对所需模块的支持,因此这可能不是永久性解决方案。

但是,如果您正在使用库,最终只能弃用对0.38以前版本的支持。或者你可以这样做吗?