使用stream-browserify和expo

Har*_*ith 6 babeljs react-native expo

stream不能与expo一起使用,因为它是Node.js标准包.但是,stream-browserify在这些情况下,包可以用作替代方案.

为了使模块解决这个而不是本机Node包,我试图babel-plugin-require-rewrite使用expo.

我将此添加到babel.config.js:

module.exports = function(api) {
  api.cache(true);
  return {
    presets: ['babel-preset-expo'],
    plugins: [
      ["rewrite-require", { aliases: {
        "stream": "stream-browserify"
      }}]
    ]
  };
};
Run Code Online (Sandbox Code Playgroud)

不幸的是,它不受捆绑商的尊重.尝试时出现此错误:

The package at "node_modules\qr-image\lib\qr.js" attempted to import the Node standard library module "stream". It failed because React Native does not include the Node standard library. Read more at https://docs.expo.io/versions/latest/introduction/faq.html#can-i-use-nodejs-packages-with-expo
Run Code Online (Sandbox Code Playgroud)

是否有可能在世博会上做这项工作?

Sta*_*rov 2

您不需要修改 babel 配置即可stream-browserify在源中使用。您可以导入stream-browserify到您的App.js. 我在GitHub上创建了一个简单的示例。

应用程序.js

const Stream = require('stream-browserify');
Run Code Online (Sandbox Code Playgroud)

包.json

  "dependencies": {
    "buffer": "^5.2.1",
    "events": "^3.0.0",
    "stream-browserify": "^2.0.2",
    "readable-stream": {
      "version": "2.3.6",
      "dependencies": {
        "core-util-is": "github:mjmasn/core-util-is"
      }
    }
    ...
  }
Run Code Online (Sandbox Code Playgroud)

stream-browserify有依赖关系readable-stream,有自己的依赖关系和使用节点环境。要解决这个问题,您必须添加这些节点包。你可以在这里阅读有关core-util-isfork 的内容。