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

Allow deadstones to run in the browser while maintaining default Node compatibility and add ESM support #9

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

adamreisnz
Copy link

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:

  1. The wasm.js file no longer exports the wasm instance as the default export, instead it exposes a helper called loadWasm
  2. This loader can be given a fetchPath. If given, the loader will attempt to use fetch first to load the wasm file.
  3. If no 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 @yishn
  4. Once the wasm file has been loaded it is cached and returned upon subsequent calls to loadWasm()
  5. The main.js file is updated to use the new loadWasm() helper

I'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:

import {guess, useFetch} from '@sabaki/deadstones'
import wasm from '@/assets/wasm/deadstones_bg.wasm?url'

//Instruct deadstones to use fetch
useFetch(wasm)

//Guess dead stones
const dead = await guess(...)

@adamreisnz
Copy link
Author

Hi @yishn, just checking if there is any interest to merge these change in? I note the package hasn't been updated for a while, so perhaps it has been abandoned/no longer maintained.

I'm happy to release a fork of it on npm if that's the case, but wanted to double check with you first.

I'd also be happy to become a co-maintainer if that's something you'd be interested in.

FYI I'm using it for the following project: https://letsplaygo.com/editor
It contains an online/web-based editor, and I'm leveraging the deadstones module for score estimation purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant