Allow deadstones to run in the browser while maintaining default Node compatibility and add ESM support #9
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR addresses issue #1
(cc @heroboy)
The problem was that this library defaulted to trying to find the wasm file using the local file system, and was only using fetch as a fallback. Because this was happening immediately on import, there is no way to prevent this from erroring in the browser, even when using polyfills/shims for the Node modules.
The solution presented in this PR solves this by doing the following:
wasm.js
file no longer exports thewasm
instance as the default export, instead it exposes a helper calledloadWasm
fetchPath
. If given, the loader will attempt to use fetch first to load the wasm file.fetchPath
is given, it will fallback to to the local file system so that backwards compatibility and out-of-the box node support is maintained, as you preferred @yishnwasm
file has been loaded it is cached and returned upon subsequent calls toloadWasm()
main.js
file is updated to use the newloadWasm()
helperI've also included
.mjs
equivalent files for people using ESM modules, while preserving the CJS implementation for backwards compatibility.This should be fully backwards compatible with the current version, but now no longer triggering errors when used in the browser.
It can now be used in the browser as follows: