加载 wasm 与 react-ts 和 vitejs。

huangapple go评论131阅读模式
英文:

loading wasm with react-ts and vitejs

问题

尝试切换到 Vite.js 而不是 Webpack 和 Create React App,但在处理 wasm 文件时遇到了问题。

代码部分不需要翻译。如果需要更多帮助,请提出具体问题。

英文:

Trying to leave webpack and create-react-app for vitejs. but are having problem with a wasm file.

import init from "swaplab2/dist/swaplab2.wasm?init";
import { setSwaplab2 } from "swaplab2";

export function TestPage(): React.ReactElement {
    const [swaplab2Loaded, setSwaplab2Loaded] = useState(false);

    useEffect(() => {
        //https://vitejs.dev/guide/features.html#webassembly
        if (!swaplab2Loaded) {
            init({})
                .then((instance) => {
                    setSwaplab2(instance);
                    setSwaplab2Loaded(true);
                })
        }
    });

but get an error like

TypeError: WebAssembly.instantiate(): Import #0 module="env" error: module is not an object or function

If I add something like

          init({ env: import.meta.env })

I instead get

TypeError: WebAssembly.instantiate(): Import #72 module="wasi_snapshot_preview1" error: module is not an object or function

Feels like I am on the wrong path here, and would appreciate a pointer

答案1

得分: 0

Swaplab2没有使用独立开关进行编译,而是使用emscripten ABI,而不是WASI。你可以在这里找到更多信息:https://github.com/emscripten-core/emscripten/wiki/WebAssembly-Standalone

通过vitejs很容易解决这个问题。

import swaplab2Url from "swaplab2/dist/swaplab2.wasm?url";
import { Swaplab2Module, setSwaplab2 } from "swaplab2";

export function TestPage(): React.ReactElement {
    const [swaplab2Loaded, setSwaplab2Loaded] = useState(false);

    useEffect(() => {
        //https://vitejs.dev/guide/features.html#webassembly
        if (!swaplab2Loaded) {
            Swaplab2Module({ locateFile: (path) => (path.endsWith(".wasm") ? swaplab2Url : path) }).then((loaded) => {
                setSwaplab2(loaded);
                setSwaplab2Loaded(true);
            });
        }
    });
英文:

Swaplab2 is compiled without the standalone switch and uses emscripten ABI and not WASI https://github.com/emscripten-core/emscripten/wiki/WebAssembly-Standalone

Was easy enough to get around with vitejs

import swaplab2Url from "swaplab2/dist/swaplab2.wasm?url";
import { Swaplab2Module, setSwaplab2 } from "swaplab2";

export function TestPage(): React.ReactElement {
    const [swaplab2Loaded, setSwaplab2Loaded] = useState(false);

    useEffect(() => {
        //https://vitejs.dev/guide/features.html#webassembly
        if (!swaplab2Loaded) {
            Swaplab2Module({ locateFile: (path) => (path.endsWith(".wasm") ? swaplab2Url : path) }).then((loaded) => {
                setSwaplab2(loaded);
                setSwaplab2Loaded(true);
            });
        }
    });


huangapple
  • 本文由 发表于 2023年6月15日 04:45:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76477419.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定