Rollup: bundling/embedding wasm code from an external module

妖精的绣舞 提交于 2020-07-22 16:58:44

问题


Using rollup, I'm trying to bundle a typescript library that imports and calls an npm module that contains a wasm file.

Only the resulting bundle contains no trace of the wasm file contents. How can I force it to bundle the webassembly too ?


Here's the key files from what I've tried:

// 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;

My package.json is

{
  "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"
  }
}

And lastly the rollup config is

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

(the complete example repo is available at https://github.com/nmrshll/ed25519 )

The bundle is then generated via node node_modules/.bin/rollup -c

It contains a function _loadWasmModule (sync, src, imports) but that is never called and there is no trace of embedded webassembly (which I'd expect as a hardcoded base64 string).

How can I fix this ?


EDIT: The wasm file contained in "ed25519-raw" was generated with wasm-pack with the option --target web

It matters because running wasm-pack with the option --target browser (the default) generates a different output (with the wasm embedded but compilation problems)

来源:https://stackoverflow.com/questions/60452332/rollup-bundling-embedding-wasm-code-from-an-external-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!