是否可以在React Native中填充Node的fs.readFileSync()? [英] Is it possible to shim Node's fs.readFileSync() in React Native?

查看:198
本文介绍了是否可以在React Native中填充Node的fs.readFileSync()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将为NodeJS编写的许多程序包移植到React Native.

I want to port a number of packages written for NodeJS to React Native.

为此,我使用了流行的Ignite样板创建了一个RN项目,然后使用了 ReactNativify 方法和shim Node API对象大多重用现有的浏览器化垫片.

For this purpose I created a RN project using the popular Ignite boilerplate, then used the ReactNativify method and shim Node API objects mostly reusing existing browserify shims.

(有关详细信息和一些有用的技巧,请参见

(For details and some useful tips see Can we use nodejs code inside react native application?)

在编译后,某些Node对象仍被空的模拟替换,例如fs.在.babelrc中完成,如下所示:

Some Node objects are still replaced with empty mocks after transpilation, such as fs. Done in .babelrc as follows:

    ["module-resolver", {
      "alias": {
        "fs": "./config/mock",
        "sodium-universal": "libsodium"

        // etcetera
      }
    }]

要移植的软件包在其传递依赖项中包含许多对fs.readFileSync的调用.

The packages to port contain a number of calls to fs.readFileSync in its transitive dependencies.

例如其中之一, hypercore-protocol ,有以下代码行:

For instance in one of them, hypercore-protocol, there is this line of code:

module.exports = protobuf(fs.readFileSync(path.join(__dirname, 'schema.proto'), 'utf-8'))

这是一个问题,因为Android和iOS不支持同步文件传输.那行对我来说好像是un-shim-able

And here is a problem, because Android and iOS do not support synchronous file transfer. That line looks like it is un-shim-able to me

现在,尽管存在fs的垫片:反应本机级别-fs 它不实现同步文件系统方法.

Now, while there exists a shims for fs: react-native-level-fs it does not implement the synchronous filesystem methods.

然后是浏览器转换,例如'browserify fs,例如 brfs . readFileSync()静态资产衬里" (及其替代物 bfrs-babel babel-plugin-static-fs ).

Then there are browserify transforms, like brfs, the 'browserify fs.readFileSync() static asset inliner' (and its alternatives bfrs-babel and babel-plugin-static-fs).

但是我不知道如何将它们包括在内,它们是否甚至可以在RN中使用?

But I don't know how to include them, and if they will even work in RN?

所以我看到了四种方法:

So I see four approaches going forward:

  1. 找到一种方法将react-native-level-fsbrfs合并为可用的填充垫片
  2. 编写一个具有所有方法的全新fs填充程序
  3. 如果不可能实现同步fs(我认为是),则以某种方式覆盖遍历可传递依赖树中调用同步方法的所有函数,并用本地代码库中的js代码段替换它们
  4. 如果出现3.的次数过多,则确定该软件包无法移植到React Native
  1. Find a way to merge react-native-level-fs and brfs into a usable shim replacement
  2. Write a completely new fs shim, that has all the methods
  3. If synchronous fs impossible (I think it is), then somehow override all occurrences of functions throughout the transitive dependency tree that invoke sync methods, and replace them by js snippets in local codebase
  4. If there are too many occurrences of 3. then decide the package cannot be ported to React Native

我希望1.或3.成为可行的解决方案.有人可以建议吗?

I am hoping for 1. and alternatively 3. to be working solutions. Can anyone advise?

出于完整性考虑.我正处于生命的这个阶段:

For sake of completeness. I am in this stage of life:

System
  platform           linux                                                                                                
  arch               x64                                                                                                  
  cpu                4 cores   Intel(R) Core(TM) i7-4500U CPU @ 1.80GHz                                                        

JavaScript
  node               7.10.1       /usr/local/bin/node  
  npm                4.2.0        /usr/local/bin/npm   
  yarn               0.24.6       /usr/bin/yarn        

React Native
  react-native-cli   2.0.1       
  app rn version     0.45.1      

Ignite
  ignite             2.0.0        /usr/local/bin/ignite  

Android
  java               1.8.0_111    /usr/bin/java  
  android home       -            undefined 

推荐答案

否. Node的fs.readFileSync没有合理的选择.

No. There is no reasonable alternative for Node's fs.readFileSync.

尽管从技术上讲,可以编写阻塞异步文件操作的readFileSync填充程序,但不建议在异步系统中强制执行同步行为(但是,只有很少的情况下,您可以摆脱它)一次性初始化代码中的同步方法.

Though technically it is possible to write a readFileSync shim that blocks on an asynchronous file operation, it is inadvisable to force synchronous behavior in an asynchronous system (but you may be able to get away with it, when only having few synchronous methods in one-time initialization code).

因此,选项3或4是唯一可行的选择.

So option 3 or 4 are the only viable alternatives.

在我的案例中,Node依赖关系太多,因此我放弃了browserifying/shimming并选择了4.但是...

In my case there were too many Node dependencies, so I ditched browserifying / shimming and opted for 4. But ...

这并不意味着所有东西都必定会丢失.我现在正在调查将NodeJS编译为Android中的本机库

That does not mean all is necessarily lost. I am now investigating Compiling NodeJS as native library in Android

( Realm.io可以在Android胖客户端应用程序(CQRS风格)中桥接本机NodeJS + React Native ).

这篇关于是否可以在React Native中填充Node的fs.readFileSync()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆