Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Browser API depends on webpack #8

Closed
qgustavor opened this issue Jan 23, 2020 · 5 comments
Closed

Browser API depends on webpack #8

qgustavor opened this issue Jan 23, 2020 · 5 comments
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@qgustavor
Copy link

I tried importing it using const blake3 = await import('https://unpkg.com/[email protected]/browser.js?module') and it failed with Uncaught ReferenceError: exports is not defined. Seems the code is a mix of ES modules and CommonJS modules.

I also tried importing it using Parcel and it failed with Imports argument must be present and must be an object. Checking this issue seems Parcel's WASM importing behavior is different from webpack's. Then, after, I tried importing it using webpack, it worked.

Is there a way to make it not dependent on webpack behavior?

@connor4312
Copy link
Owner

Good issue. I didn't bother having an entirely separate, pure ES module tree here like I normally do because wasm is not tree shakable anyway. But I didn't recognize that that would trip up non-Webpack bundlers. Will make a patch release this evening or tomorrow.

@connor4312 connor4312 added bug Something isn't working good first issue Good for newcomers labels Jan 23, 2020
@connor4312
Copy link
Owner

Should be good to go in 2.0.1. ./browser.js now points to an esm build. Let me know if you run into any more problems, happy hashing.

@qgustavor
Copy link
Author

qgustavor commented Jan 27, 2020

I just want to note that making it a ESM build just makes it easier to be imported into browsers without requiring a bundler, in the other hand it don't solves the issue.

The issue is that blake3_js.js imports "blake3_js_bg.wasm" as a ES module and it is not standardized yet, so browsers don't support it and some bundlers handle it in different ways than Webpack.

"blake3_js.js" is generated using wasm-pack and seems it don't support Parcel. You can use --target web, which will create a module that already handles WASM loading without depending on the bundler implementation, but then you need to initialize the module manually.

So seems it's hard to implement without breaking backyards compatibility.

@connor4312
Copy link
Owner

I'll look into this some more this evening most likely.

@connor4312 connor4312 reopened this Jan 27, 2020
@connor4312
Copy link
Owner

connor4312 commented Feb 12, 2020

I've fixed this in 8c48e49.

There's now a new import, blake3/browser-async, that exports an function which returns a promise that resolves to the module.

import load from 'blake3/browser-async';

load().then(blake3 => {
  console.log(blake3.hash('hello world'));
});

I expanded the integration tests to test both Webpack and a plain <script type="module"> import.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants