[英]How can I import Emscripten generated .wasm/js files in files created with create-react-app
I have a library I can successfully build & run with the Emscripten
generated .html files. 我有一个库,我可以使用
Emscripten
生成的.html文件成功构建和运行。 I'd like to use it with React now, however, I get a MIME type error` which I can't fix. 我现在想将它与React一起使用,但是,我得到一个MIME类型错误,我无法修复。 Here are the steps I take to generate the React app and .wasm files
以下是我生成React应用程序和.wasm文件的步骤
npx create-react-app stackoverflow
DEST=stackoverflow/src/stackoverflow.html
sudo docker run --rm -v $(pwd):/src trzeci/emscripten emmake make
$ docker run --rm -v $(pwd):/src trzeci/emscripten emcc \
... o. files
-s MODULARIZE=1 \
-s ERROR_ON_UNDEFINED_SYMBOLS=0 \
-s EXPORT_NAME=Stackoverflow \
-o ${DEST}
# disable eslint for the generate file
sed -i '1s;^;/* eslint-disable */\n;' ${DEST}
I expect to be able to import the Stackoverflow
object inside of stackoverflow.js
我希望能够导入
Stackoverflow
的内部对象stackoverflow.js
App.js
...
import Stackoverflow from './stackoverflow';
console.log(Stackoverflow());
...
However the dev server fails to load, and the console shows these warnings. 但是,dev服务器无法加载,控制台会显示这些警告。
m streaming compile failed: TypeError: Failed to execute 'compile' on 'WebAssembly': Incorrect response MIME type. Expected 'application/wasm'.
(anonymous) @ stackoverflow.js:1667
Promise.then (async)
createWasm @ stackoverflow.js:1663
Module.asm @ stackoverflow.js:1694
(anonymous) @ stackoverflow.js:5322
./src/App.js @ App.js:5
__webpack_require__ @ bootstrap:781
fn @ bootstrap:149
./src/index.js @ index.css?f3f6:45
__webpack_require__ @ bootstrap:781
fn @ bootstrap:149
0 @ stackoverflow.js:5867
__webpack_require__ @ bootstrap:781
checkDeferredModules @ bootstrap:45
webpackJsonpCallback @ bootstrap:32
(anonymous) @ main.chunk.js:1
stackoverflow.js:1668 falling back to ArrayBuffer instantiation
(anonymous) @ stackoverflow.js:1668
Promise.then (async)
createWasm @ stackoverflow.js:1663
Module.asm @ stackoverflow.js:1694
(anonymous) @ stackoverflow.js:5322
./src/App.js @ App.js:5
__webpack_require__ @ bootstrap:781
fn @ bootstrap:149
./src/index.js @ index.css?f3f6:45
__webpack_require__ @ bootstrap:781
fn @ bootstrap:149
0 @ stackoverflow.js:5867
__webpack_require__ @ bootstrap:781
checkDeferredModules @ bootstrap:45
webpackJsonpCallback @ bootstrap:32
(anonymous) @ main.chunk.js:1
stackoverflow.js:1654 failed to asynchronously prepare wasm: CompileError: AsyncCompile: Wasm decoding failed: expected magic word 00 61 73 6d, found 3c 21 44 4f @+0
The first error reported says that the MIME type is wrong, and this question tells me to update node-mime
and webpack-dev-server
. 报告的第一个错误说MIME类型错误, 这个问题告诉我更新
node-mime
和webpack-dev-server
。 node-mime
isn't present in my node_modules
, but webpack-dev-server
is and this issue, which has apparently solved my problem is available on 3.3.1. node-mime
在我的node_modules
不存在,但是webpack-dev-server
就是这个问题,这显然已经解决了我的问题,可以在3.3.1上找到。
$ npm run eject
And then I change my webpack-dev-server
version to 3.3.1 and install. 然后我将我的
webpack-dev-server
版本更改为3.3.1并安装。
Now I run npm run start
and step through the code, I can confirm I get to the correct dev-server. 现在我运行
npm run start
并逐步执行代码,我可以确认我到达了正确的dev-server。
Yet my error persists. 但我的错误仍然存在。 I feel like I'm missing something obvious here.
我觉得我在这里遗漏了一些明显的东西。
I had to do a couple of things to get this to work, Most of my troubleshooting here was futile (the dev server was not serving the .wasm files at all, it falls back on the .html when it can't find what you want, this was the source of my MIME errors). 我必须做几件事才能让它工作,我的大部分故障排除都是徒劳的(开发服务器根本没有提供.wasm文件,当它无法找到你时,它会回到.html上想要,这是我的MIME错误的来源)。
HOW TO MAKE EMSCRIPTEN JAVASCRIPT WORK WITH create-react-app 如何让EMSCRIPTEN JAVASCRIPT使用create-react-app
emcc
to the react-app/public
folder cp stackoverflow.wasm gifsicle-react/public/stackoverflow.wasm
emcc
到react-app/public
文件夹cp stackoverflow.wasm gifsicle-react/public/stackoverflow.wasm
eslint-disable
to the top of your files (I use this sed command) sed -i.old '1s;^;\\/* eslint-disable *\\/;' stackoverflow.js
eslint-disable
添加到文件顶部(我使用此sed命令) sed -i.old '1s;^;\\/* eslint-disable *\\/;' stackoverflow.js
sed -i.old '1s;^;\\/* eslint-disable *\\/;' stackoverflow.js
.wasm
file in the .js with an absolute one. .wasm
文件的相对路径。 (This is necessary to get files in the /public
directory) sed -i.old "s|stackoverflow.wasm|/stackoverflow.wasm|" stackoverflow.js
/public
目录中的文件所必需的) sed -i.old "s|stackoverflow.wasm|/stackoverflow.wasm|" stackoverflow.js
sed -i.old "s|stackoverflow.wasm|/stackoverflow.wasm|" stackoverflow.js
sed -i.old "s|wasmBinaryFile = locateFile|// wasmBinaryFile = locateFile|" stackoverflow.js
sed -i.old "s|wasmBinaryFile = locateFile|// wasmBinaryFile = locateFile|" stackoverflow.js
sed -i.old "s|wasmBinaryFile = locateFile|// wasmBinaryFile = locateFile|" stackoverflow.js
I ended up writing this blog post which has more detail about how I managed this. 我最后写了这篇博文,其中详细介绍了我如何管理这篇文章。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.