Rollup:从外部模块捆绑/嵌入 wasm 代码

2024-01-27

使用 rollup,我尝试捆绑一个 typescript 库,该库导入并调用包含 wasm 文件的 npm 模块。

只有生成的包不包含 wasm 文件内容的痕迹。我怎样才能强制它捆绑网络程序集?


这是我尝试过的关键文件:

// typescript/src/index.ts

import * as libEd from "ed25519-raw";

export const seed_from_phrase = libEd.seed_from_phrase;
export const gen_keypair = libEd.gen_keypair;

我的 package.json 是

{
  "name": "ed25519",
  "version": "0.1.0",
  "description": "ed25519",
  "main": "typescript/dist/bundle.cjs.js",
  "types": "typescript/src/index.ts",
  "dependencies": {
    "ed25519-raw": "git+https://github.com/cmanteiga/ed25519-raw"
  },
  "devDependencies": {
    "@types/node": "^13.7.1",
    "typescript": "^3.7.5",
    "@rollup/plugin-commonjs": "^11.0.2",
    "@rollup/plugin-multi-entry": "^3.0.0",
    "@rollup/plugin-node-resolve": "^7.1.1",
    "@rollup/plugin-wasm": "^3.0.0",
    "rollup": "^1.31.1",
    "rollup-plugin-typescript2": "^0.26.0"
  }
}

最后汇总配置是

import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import typescript from "rollup-plugin-typescript2";
import multi from "@rollup/plugin-multi-entry";
import wasm from "@rollup/plugin-wasm";

export default {
  input: [`typescript/src/index.ts`],
  output: {
    sourcemap: true,
    format: "cjs",
    name: "ed25519",
    file: `typescript/dist/bundle.cjs.js`
  },
  plugins: [
    multi(),
    typescript(),
    commonjs({
      include: [
        `typescript/src/**/*.js`,
        `typescript/src/**/*.ts`,
        `typescript/src/**/*.wasm`,
        "node_modules/**"
      ]
    }),
    resolve({
      browser: true,
      extensions: [".js", ".ts", ".wasm"]
    }),
    wasm()
  ]
};

(完整的示例存储库可在https://github.com/nmrshll/ed25519 https://github.com/nmrshll/ed25519 )

然后通过生成捆绑包node node_modules/.bin/rollup -c

它包含一个function _loadWasmModule (sync, src, imports)但它从未被调用,并且没有嵌入 WebAssembly 的痕迹(我希望将其作为硬编码的 Base64 字符串)。

我怎样才能解决这个问题 ?


编辑: wasm 文件包含在“ed25519-原始” https://github.com/cmanteiga/ed25519-raw是用生成的wasm-pack与选项--target web

这很重要,因为跑步wasm-pack与选项--target browser(默认)生成不同的输出(嵌入了 wasm 但存在编译问题)


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Rollup:从外部模块捆绑/嵌入 wasm 代码 的相关文章

随机推荐