connor4312 / blake3

BLAKE3 hashing for JavaScript: native Node bindings (where available) and WebAssembly
https://connor4312.github.io/blake3/index.html
MIT License
180 stars 21 forks source link

Trying to use Blake3 in React with TS. Build Warnings and Runtime Errors #43

Open allidoisace opened 2 years ago

allidoisace commented 2 years ago

Runtime

index.tsx

import('blake3/browser').then(blake3 => {
  console.log(blake3.hash('foo')); // => Uint8Array
});

ReactDOM.render(
  ...
);
[Error] Unhandled Promise Rejection: TypeError: undefined is not an object (evaluating 'wasm2.memory.buffer')
    getUint8Memory0 (blake3_browser.js:38)
    hash2 (blake3_browser.js:60)
    hash3 (blake3_browser.js:240)
    (anonymous function) (index.tsx:12)
    promiseReactionJob

As posted in an issue elsewhere, they recommended changing the .d.ts names, but they are already present in this version. https://github.com/rustwasm/wasm-bindgen/issues/2200 Screen Shot 2022-04-28 at 1 13 50 PM

Build Warnings

'memory' is not exported by 'node_modules/blake3/dist/wasm/browser/blake3_js_bg.wasm'
'memory' is not exported by 'node_modules/blake3/dist/wasm/browser/blake3_js_bg.wasm'
'__wbindgen_malloc' is not exported by 'node_modules/blake3/dist/wasm/browser/blake3_js_bg.wasm'
'__wbindgen_malloc' is not exported by 'node_modules/blake3/dist/wasm/browser/blake3_js_bg.wasm'
'hash' is not exported by 'node_modules/blake3/dist/wasm/browser/blake3_js_bg.wasm'
'__wbindgen_free' is not exported by 'node_modules/blake3/dist/wasm/browser/blake3_js_bg.wasm'
'create_hasher' is not exported by 'node_modules/blake3/dist/wasm/browser/blake3_js_bg.wasm'
'__wbindgen_malloc' is not exported by 'node_modules/blake3/dist/wasm/browser/blake3_js_bg.wasm'
'create_keyed' is not exported by 'node_modules/blake3/dist/wasm/browser/blake3_js_bg.wasm'
'__wbindgen_malloc' is not exported by 'node_modules/blake3/dist/wasm/browser/blake3_js_bg.wasm'
'__wbindgen_realloc' is not exported by 'node_modules/blake3/dist/wasm/browser/blake3_js_bg.wasm'
'create_derive' is not exported by 'node_modules/blake3/dist/wasm/browser/blake3_js_bg.wasm'
'__wbg_blake3hash_free' is not exported by 'node_modules/blake3/dist/wasm/browser/blake3_js_bg.wasm'
'blake3hash_reader' is not exported by 'node_modules/blake3/dist/wasm/browser/blake3_js_bg.wasm'
'__wbindgen_malloc' is not exported by 'node_modules/blake3/dist/wasm/browser/blake3_js_bg.wasm'
'blake3hash_update' is not exported by 'node_modules/blake3/dist/wasm/browser/blake3_js_bg.wasm'
'__wbindgen_malloc' is not exported by 'node_modules/blake3/dist/wasm/browser/blake3_js_bg.wasm'
'blake3hash_digest' is not exported by 'node_modules/blake3/dist/wasm/browser/blake3_js_bg.wasm'
'__wbindgen_free' is not exported by 'node_modules/blake3/dist/wasm/browser/blake3_js_bg.wasm'
'__wbg_hashreader_free' is not exported by 'node_modules/blake3/dist/wasm/browser/blake3_js_bg.wasm'
'__wbindgen_malloc' is not exported by 'node_modules/blake3/dist/wasm/browser/blake3_js_bg.wasm'
'hashreader_fill' is not exported by 'node_modules/blake3/dist/wasm/browser/blake3_js_bg.wasm'
'__wbindgen_free' is not exported by 'node_modules/blake3/dist/wasm/browser/blake3_js_bg.wasm'
'hashreader_set_position' is not exported by 'node_modules/blake3/dist/wasm/browser/blake3_js_bg.wasm'
leonbotros commented 2 years ago

I was able to to fix this by using this webpack config (using craco for example):

module.exports = {
  webpack: {
    configure: (config) => {
      config.experiments = {
        asyncWebAssembly: true,
      };

      config.module.rules.push({
        test: /\.(wasm)$/,
        type: "webassembly/async",
      });

      return config;
    },
  },
};
allidoisace commented 2 years ago

Will have to see if I can accomplish this with Vite.