6

使用汇总,我试图捆绑一个打字稿库,该库导入和调用一个包含 wasm 文件的 npm 模块。

只有生成的包不包含任何 wasm 文件内容的痕迹。如何强制它也捆绑 webassembly?


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

// 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()
  ]
};

(完整的示例 repo 可在https://github.com/nmrshll/ed25519获得)

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

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

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


编辑:“ed25519-raw”wasm-pack中包含的 wasm 文件是使用选项生成的--target web

这很重要,因为wasm-pack使用该选项--target browser(默认)运行会生成不同的输出(嵌入了 wasm 但编译问题)

4

0 回答 0