7z-wasm vite引入报错
7z-wasm vite引入报错

7z-wasm vite引入报错

7z-wasm vite引入报错

环境

vite

引入失败

报魔术头错误,发现是文件Url路径错误 http://localhost/node_modules/.vite/dev/7zz.wasm

返回是HTML

控制台报错如下

RuntimeError: abort(CompileError: WebAssembly.instantiate(): expected magic word 00 61 73 6d, found 3c 21 44 4f @+0). Build with -s ASSERTIONS=1 for more info.

解决方案

https://www.bytedaring.wang/post/2023/12/03/wasm_pack_vite/

yarn add --dev vite-plugin-wasm
yarn add --dev vite-plugin-top-level-await

vite.config.js中添加

import wasm from "vite-plugin-wasm";
import topLevelAwait from "vite-plugin-top-level-await";

export default defineConfig({
    plugins: [
        wasm(),
        topLevelAwait()
    ],
    optimizeDeps: {
        exclude: ["7z-wasm"],
    },
});