简体   繁体   English

如何在使用create-react-app创建的文件中导入Emscripten生成的.wasm / js文件

[英]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-mimewebpack-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

  1. Copy the .wasm output of the emcc to the react-app/public folder cp stackoverflow.wasm gifsicle-react/public/stackoverflow.wasm 所述的.wasm输出复制emccreact-app/public文件夹cp stackoverflow.wasm gifsicle-react/public/stackoverflow.wasm
  2. Add 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
  3. Replace the relative path of the .wasm file in the .js with an absolute one. 用绝对值替换.js中.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
  4. The generated javascript will try to resolve the path relative to the website directory. 生成的javascript将尝试解析相对于网站目录的路径。 Comment out this line 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.

相关问题 如何将 Next.js 应用于使用 create-react-app 创建的 React 项目? - How can I apply Next.js to a React project created with create-react-app? create-react-app - 如何捆绑 JS 文件 - create-react-app - How to bundle JS files react-i18next 未在使用 create-react-app 创建的 React 应用程序中加载 json 翻译文件 - react-i18next not loading json translation files in React app created with create-react-app 如何配置使用 create-react-app 创建的 Typescript React App 以使用 yarn start(开发模式)将文件发送到特定目录 - How do I configure Typescript React App created with create-react-app to emit the files to particular directory with yarn start (dev mode) 无法导入生成的类型文件 create-react-app - Can't import generated typings file create-react-app 如何在create-react-app创建的应用程序中使用jsx文件(不运行“npm run eject”)? - How to use jsx files in an app created by create-react-app (without run “npm run eject”)? 如何使用 create-react-app 在 Jest 测试中忽略生成的 Relay 查询文件? - How to ignore generated Relay query files in Jest tests using create-react-app? 在 js 文件中使用 sass 变量 create-react-app - Use sass variables into js files create-react-app create-react-app 2.0,如何在 sass/scss 文件中使用绝对路径导入? - create-react-app 2.0, how to use absolute path import in sass/scss files? 如何删除 React App 符号和名称或“使用 create-react-app 创建的网站”? - How can I remove the React App symbol and name or the “Website created using create-react-app”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM