4

wasm-pack使用选项(默认值)将一些 rust 代码编译到 webassembly 后--target browser,我得到以下文件typescript/deps/ed25519xp

  • ed25519xp_bg.wasm
  • ed25519xp_bg.d.ts
  • ed25519xp.d.ts
  • ed25519xp.js
  • 包.json

我的打字稿文件如下所示:

// typescript/src/index.ts

export { seed_from_phrase, gen_keypair } from "ed25519xp";

我的 package.json 是这样的:

{
  "name": "ed25519",
  "version": "0.1.0",
  "description": "ed25519",
  "main": "typescript/dist/bundle.js",
  "types": "typescript/src/index.ts",
  "dependencies": {
    "ed25519xp": "file:typescript/deps/ed25519xp"
  },
  "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"
  }
}

我的汇总配置如下所示:

// rollup.config.js
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/deps/ed25519xp/ed25519xp_bg.wasm",
    `typescript/src/index.ts`
  ],
  output: {
    sourcemap: true,
    format: "iife",
    name: "ed25519",
    file: `typescript/dist/bundle.js`
  },
  plugins: [
    multi(),
    resolve({
      browser: true,
      extensions: [".js", ".ts", ".wasm"]
    }),
    commonjs({
      include: [
        `typescript/**/*.js`,
        `typescript/**/*.ts`,
        `typescript/**/*.wasm`,
        "node_modules/**"
      ]
    }),
    typescript(),
    wasm()

    // If we're building for prod (npm run build
    // instead of npm run dev), minify
    // terser()
  ]
};

当我运行命令与汇总 ( node node_modules/.bin/rollup -c) 捆绑时,我收到以下错误:

(!) Missing exports
https://rollupjs.org/guide/en/#error-name-is-not-exported-by-module
typescript/deps/ed25519xp/ed25519xp.js
memory is not exported by typescript/deps/ed25519xp/ed25519xp_bg.wasm
29: let cachegetUint8Memory0 = null;
30: function getUint8Memory0() {
31:     if (cachegetUint8Memory0 === null || cachegetUint8Memory0.buffer !== wasm.memory.buffer) {
                                                                                  ^
32:         cachegetUint8Memory0 = new Uint8Array(wasm.memory.buffer);
33:     }
typescript/deps/ed25519xp/ed25519xp.js
memory is not exported by typescript/deps/ed25519xp/ed25519xp_bg.wasm
30: function getUint8Memory0() {
31:     if (cachegetUint8Memory0 === null || cachegetUint8Memory0.buffer !== wasm.memory.buffer) {
32:         cachegetUint8Memory0 = new Uint8Array(wasm.memory.buffer);
                                                       ^
33:     }
34:     return cachegetUint8Memory0;
typescript/deps/ed25519xp/ed25519xp.js
memory is not exported by typescript/deps/ed25519xp/ed25519xp_bg.wasm
30: function getUint8Memory0() {
31:     if (cachegetUint8Memory0 === null || cachegetUint8Memory0.buffer !== wasm.memory.buffer) {
32:         cachegetUint8Memory0 = new Uint8Array(wasm.memory.buffer);
                                                       ^
33:     }
34:     return cachegetUint8Memory0;
typescript/deps/ed25519xp/ed25519xp.js
__wbindgen_malloc is not exported by typescript/deps/ed25519xp/ed25519xp_bg.wasm
107: */
108: export function gen_keypair(phrase) {
109:     var ptr0 = passStringToWasm0(phrase, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
                                                   ^
110:     var len0 = WASM_VECTOR_LEN;
111:     var ret = wasm.gen_keypair(ptr0, len0);
typescript/deps/ed25519xp/ed25519xp.js
__wbindgen_realloc is not exported by typescript/deps/ed25519xp/ed25519xp_bg.wasm
107: */
108: export function gen_keypair(phrase) {
109:     var ptr0 = passStringToWasm0(phrase, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
                                                                           ^
110:     var len0 = WASM_VECTOR_LEN;
111:     var ret = wasm.gen_keypair(ptr0, len0);
typescript/deps/ed25519xp/ed25519xp.js
gen_keypair is not exported by typescript/deps/ed25519xp/ed25519xp_bg.wasm
109:     var ptr0 = passStringToWasm0(phrase, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
110:     var len0 = WASM_VECTOR_LEN;
111:     var ret = wasm.gen_keypair(ptr0, len0);
                        ^
112:     return takeObject(ret);
113: }
typescript/deps/ed25519xp/ed25519xp.js
__wbindgen_malloc is not exported by typescript/deps/ed25519xp/ed25519xp_bg.wasm
124: */
125: export function pubKey_from_pair_bytes(pair_bytes) {
126:     var ptr0 = passArray8ToWasm0(pair_bytes, wasm.__wbindgen_malloc);
                                                       ^
127:     var len0 = WASM_VECTOR_LEN;
128:     var ret = wasm.pubKey_from_pair_bytes(ptr0, len0);
typescript/deps/ed25519xp/ed25519xp.js
pubKey_from_pair_bytes is not exported by typescript/deps/ed25519xp/ed25519xp_bg.wasm
126:     var ptr0 = passArray8ToWasm0(pair_bytes, wasm.__wbindgen_malloc);
127:     var len0 = WASM_VECTOR_LEN;
128:     var ret = wasm.pubKey_from_pair_bytes(ptr0, len0);
                        ^
129:     return takeObject(ret);
130: }
typescript/deps/ed25519xp/ed25519xp.js
__wbindgen_malloc is not exported by typescript/deps/ed25519xp/ed25519xp_bg.wasm
136: */
137: export function sign(message, keypair_bytes) {
138:     var ptr0 = passArray8ToWasm0(message, wasm.__wbindgen_malloc);
                                                    ^
139:     var len0 = WASM_VECTOR_LEN;
140:     var ptr1 = passArray8ToWasm0(keypair_bytes, wasm.__wbindgen_malloc);
typescript/deps/ed25519xp/ed25519xp.js
__wbindgen_malloc is not exported by typescript/deps/ed25519xp/ed25519xp_bg.wasm
138:     var ptr0 = passArray8ToWasm0(message, wasm.__wbindgen_malloc);
139:     var len0 = WASM_VECTOR_LEN;
140:     var ptr1 = passArray8ToWasm0(keypair_bytes, wasm.__wbindgen_malloc);
                                                          ^
141:     var len1 = WASM_VECTOR_LEN;
142:     var ret = wasm.sign(ptr0, len0, ptr1, len1);
typescript/deps/ed25519xp/ed25519xp.js
sign is not exported by typescript/deps/ed25519xp/ed25519xp_bg.wasm
140:     var ptr1 = passArray8ToWasm0(keypair_bytes, wasm.__wbindgen_malloc);
141:     var len1 = WASM_VECTOR_LEN;
142:     var ret = wasm.sign(ptr0, len0, ptr1, len1);
                        ^
143:     return takeObject(ret);
144: }
typescript/deps/ed25519xp/ed25519xp.js
__wbindgen_malloc is not exported by typescript/deps/ed25519xp/ed25519xp_bg.wasm
151: */
152: export function verify(message, pubKey_bytes, sig_bytes) {
153:     var ptr0 = passArray8ToWasm0(message, wasm.__wbindgen_malloc);
                                                    ^
154:     var len0 = WASM_VECTOR_LEN;
155:     var ptr1 = passArray8ToWasm0(pubKey_bytes, wasm.__wbindgen_malloc);
typescript/deps/ed25519xp/ed25519xp.js
__wbindgen_malloc is not exported by typescript/deps/ed25519xp/ed25519xp_bg.wasm
153:     var ptr0 = passArray8ToWasm0(message, wasm.__wbindgen_malloc);
154:     var len0 = WASM_VECTOR_LEN;
155:     var ptr1 = passArray8ToWasm0(pubKey_bytes, wasm.__wbindgen_malloc);
                                                         ^
156:     var len1 = WASM_VECTOR_LEN;
157:     var ptr2 = passArray8ToWasm0(sig_bytes, wasm.__wbindgen_malloc);
typescript/deps/ed25519xp/ed25519xp.js
__wbindgen_malloc is not exported by typescript/deps/ed25519xp/ed25519xp_bg.wasm
155:     var ptr1 = passArray8ToWasm0(pubKey_bytes, wasm.__wbindgen_malloc);
156:     var len1 = WASM_VECTOR_LEN;
157:     var ptr2 = passArray8ToWasm0(sig_bytes, wasm.__wbindgen_malloc);
                                                      ^
158:     var len2 = WASM_VECTOR_LEN;
159:     var ret = wasm.verify(ptr0, len0, ptr1, len1, ptr2, len2);
typescript/deps/ed25519xp/ed25519xp.js
verify is not exported by typescript/deps/ed25519xp/ed25519xp_bg.wasm
157:     var ptr2 = passArray8ToWasm0(sig_bytes, wasm.__wbindgen_malloc);
158:     var len2 = WASM_VECTOR_LEN;
159:     var ret = wasm.verify(ptr0, len0, ptr1, len1, ptr2, len2);
                        ^
160:     return ret !== 0;
161: }
typescript/deps/ed25519xp/ed25519xp.js
__wbindgen_malloc is not exported by typescript/deps/ed25519xp/ed25519xp_bg.wasm
166: */
167: export function seed_from_phrase(phrase) {
168:     var ptr0 = passStringToWasm0(phrase, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
                                                   ^
169:     var len0 = WASM_VECTOR_LEN;
170:     var ret = wasm.seed_from_phrase(ptr0, len0);
typescript/deps/ed25519xp/ed25519xp.js
__wbindgen_realloc is not exported by typescript/deps/ed25519xp/ed25519xp_bg.wasm
166: */
167: export function seed_from_phrase(phrase) {
168:     var ptr0 = passStringToWasm0(phrase, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
                                                                           ^
169:     var len0 = WASM_VECTOR_LEN;
170:     var ret = wasm.seed_from_phrase(ptr0, len0);
typescript/deps/ed25519xp/ed25519xp.js
seed_from_phrase is not exported by typescript/deps/ed25519xp/ed25519xp_bg.wasm
168:     var ptr0 = passStringToWasm0(phrase, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
169:     var len0 = WASM_VECTOR_LEN;
170:     var ret = wasm.seed_from_phrase(ptr0, len0);
                        ^
171:     return takeObject(ret);
172: }
typescript/deps/ed25519xp/ed25519xp.js
memory is not exported by typescript/deps/ed25519xp/ed25519xp_bg.wasm
174: let cachegetInt32Memory0 = null;
175: function getInt32Memory0() {
176:     if (cachegetInt32Memory0 === null || cachegetInt32Memory0.buffer !== wasm.memory.buffer) {
                                                                                   ^
177:         cachegetInt32Memory0 = new Int32Array(wasm.memory.buffer);
178:     }
typescript/deps/ed25519xp/ed25519xp.js
memory is not exported by typescript/deps/ed25519xp/ed25519xp_bg.wasm
175: function getInt32Memory0() {
176:     if (cachegetInt32Memory0 === null || cachegetInt32Memory0.buffer !== wasm.memory.buffer) {
177:         cachegetInt32Memory0 = new Int32Array(wasm.memory.buffer);
                                                        ^
178:     }
179:     return cachegetInt32Memory0;
typescript/deps/ed25519xp/ed25519xp.js
memory is not exported by typescript/deps/ed25519xp/ed25519xp_bg.wasm
175: function getInt32Memory0() {
176:     if (cachegetInt32Memory0 === null || cachegetInt32Memory0.buffer !== wasm.memory.buffer) {
177:         cachegetInt32Memory0 = new Int32Array(wasm.memory.buffer);
                                                        ^
178:     }
179:     return cachegetInt32Memory0;
typescript/deps/ed25519xp/ed25519xp.js
__wbindgen_malloc is not exported by typescript/deps/ed25519xp/ed25519xp_bg.wasm
187: export const __wbg_stack_558ba5917b466edd = function(arg0, arg1) {
188:     var ret = getObject(arg1).stack;
189:     var ptr0 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
                                                ^
190:     var len0 = WASM_VECTOR_LEN;
191:     getInt32Memory0()[arg0 / 4 + 1] = len0;
typescript/deps/ed25519xp/ed25519xp.js
__wbindgen_realloc is not exported by typescript/deps/ed25519xp/ed25519xp_bg.wasm
187: export const __wbg_stack_558ba5917b466edd = function(arg0, arg1) {
188:     var ret = getObject(arg1).stack;
189:     var ptr0 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
                                                                        ^
190:     var len0 = WASM_VECTOR_LEN;
191:     getInt32Memory0()[arg0 / 4 + 1] = len0;
typescript/deps/ed25519xp/ed25519xp.js
__wbindgen_free is not exported by typescript/deps/ed25519xp/ed25519xp_bg.wasm
197:         console.error(getStringFromWasm0(arg0, arg1));
198:     } finally {
199:         wasm.__wbindgen_free(arg0, arg1);
                  ^
200:     }
201: };
typescript/deps/ed25519xp/ed25519xp.js
memory is not exported by typescript/deps/ed25519xp/ed25519xp_bg.wasm
220: 
221: export const __wbindgen_memory = function() {
222:     var ret = wasm.memory;
                        ^
223:     return addHeapObject(ret);
224: };
(!) Broken sourcemap
https://rollupjs.org/guide/en/#warning-sourcemap-is-likely-to-be-incorrect
Plugins that transform code (such as 'wasm') should generate accompanying sourcemaps

虽然,在将 .wasm 文件评估为 WAT 时,导出似乎存在:

...
  (func $seed_from_phrase (export "seed_from_phrase") (type $t6) (param $p0 i32) (param $p1 i32) (result i32)
...
  (func $gen_keypair (export "gen_keypair") (type $t6) (param $p0 i32) (param $p1 i32) (result i32)
...
  (func $__wbindgen_malloc (export "__wbindgen_malloc") (type $t3) (param $p0 i32) (result i32)
...
  (func $__wbindgen_realloc (export "__wbindgen_realloc") (type $t8) (param $p0 i32) (param $p1 i32) (param $p2 i32) (result i32)
...
  (memory $memory (export "memory") 18)
...

(我已经在https://github.com/nmrshll/ed25519/tree/943fc841693401acc64260fc19d4dda08ae3503d上传了整个仓库)

如何解决“缺少出口”捆绑错误?

4

2 回答 2

3

改为调用 wasm-pack --target web

文档:https ://rustwasm.github.io/docs/wasm-pack/commands/build.html#target

这就是使用类似配置为我消除这些错误的原因。虽然,我使用的是 wasm-bindgen 而不是 wasm-pack。

于 2020-03-06T17:42:49.843 回答
2

它有点晚了,但我在使用汇总和已编译的 wasm-pack 节点模块时遇到了同样的问题。(在我的例子中,我有一个使用 wasm 的主页面,我也想在 service worker 中使用相同的 wasm 文件)。

我很快写了一个汇总插件 (poc),它允许您使用 wasm-pack 节点模块并将所需的 wasm 文件复制到您的输出目录。

正如上一个答案中提到的,wasm-pack 模块需要使用“--target web”构建。

如果你想直接编译你的 rust 代码,我推荐使用 rust 插件进行汇总。

你可以在这里找到rollup-wasm-pack-import

于 2021-01-07T14:52:28.003 回答