如何在Expo(react-native)App中加载WASM

问题描述 投票:0回答:1

如何将 wasm 文件加载到 expo React Native 应用程序中?我正在使用

"expo": "~49.0.8"

这是我尝试过的一切: 我尝试使用

Asset
useAssets
中的
expo-assets

我尝试使用

fetch
中的
fetch('../assets/my-wasm-file.wasm')
加载它。

我也尝试简单地导入它,例如:

import wasm from "my-node-module-package-that-has-a-wasm-file/path/to/wasm-file.wasm"
,但这也不起作用。

我最后尝试的是这个包

https://github.com/cawfree/react-native-webassembly
。 我尝试了提到的
import HelloWorld from './hello-world.wasm';
方法而不是
axios
方法。 axios 方法似乎让我更接近目标,但我收到一个错误,指出
hermes
存在问题。我尝试在
ios/Podfile
中禁用 Hermes,然后运行
pod install
但没有成功。

react-native expo webassembly react-native-hermes
1个回答
0
投票

我也遇到了这个问题,想为下一个人留下答案。

“Expo 的简单性是以灵活性为代价的。Expo 没有提供一种简单的方法来导入用更强大的静态编译语言(如 Rust、C++ 或 Golang)编写的外部本机库。”

问题是 expo 包装了 React Native 并允许我们编写跨平台工作的代码,为此需要简化它可以使用的内容和位置。

截至目前,您无法在 expo 中导入和使用 .wasm 文件,或者该文件未记录。

在不弹出的情况下使其工作的方法是:

  1. 编译 Rust -> wasm VIA
    wasm-pack
  2. wasm-pack -> 生成包含所有逻辑的 .wasm 文件
  3. brew install binaryen
  4. wasm2js pkg/your_wasm_file_bg.wasm -o your_wasm_file.js
  5. 将 your_wasm_file.js 文件复制到 expo/react-native 存储库并按预期使用

对这个存储库大喊大叫,我建议您深入研究,因为这就是我找到解决方案的方法: https://github.com/conduition/expo-wasm-demo

对于一个简单的简单示例,请查看: https://github.com/iou-experiments/app/blob/a6d38e5f151681c12fe0250288446582ae7a7211/app/(tabs)/wasm.tsx

© www.soinside.com 2019 - 2024. All rights reserved.