将浪费与汇总捆绑在一起时“缺少出口”

时间:2020-02-28 16:53:28

标签: typescript npm webassembly rollup wasm-pack

使用带有选项--target browser(默认)的typescript/deps/ed25519xp将一些rust代码编译到webassembly之后,我在// typescript/src/index.ts export { seed_from_phrase, gen_keypair } from "ed25519xp"; 中得到以下文件:

  • ed25519xp_bg.wasm
  • ed25519xp_bg.d.ts
  • ed25519xp.d.ts
  • ed25519xp.js
  • 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"
  }
}

我的package.json是这样的:

// 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 )捆绑时,出现以下错误:

...
  (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)
...

但是,将.wasm文件评估为WAT时,似乎存在出口:

Get-ChildItem 'Z:\Inbox' -Recurse | Where-Object {(Get-Acl $_.FullName).Access | Where-Object {$_.IdentityReference.Value -notcontains 'domain\username'} } 

(我已将整个仓库上传到https://github.com/nmrshll/ed25519/tree/943fc841693401acc64260fc19d4dda08ae3503d

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

2 个答案:

答案 0 :(得分:2)

改为使用--target web调用wasm-pack。

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

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

答案 1 :(得分:2)

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

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

如上一个答案所述,wasm-pack 模块需要使用“--target web”构建。

如果你想直接编译你的 Rust 代码,我建议你使用 Rust 插件进行汇总。

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