HomePhabricator

[ecash-lib] "module": "CommonJS" + separate "main" and "browser"

Description

[ecash-lib] "module": "CommonJS" + separate "main" and "browser"

Summary:
Currently, integrating ecash-lib into CashTab is very difficult, due to the different environments between jest and webpack:

  • Jest can't handle ES modules, and either fails to import ecash-lib altogether (as it is a module), or, if we change it to be a CommonJS compatible, the generated JS from wasm-bindgen breaks it as import.meta.url can't be used, even if it's never executed.
  • Webpack can't find node:fs/promises, even though it's lazily loaded and never actually imported.

wasm-bindgen can build ffi modules that work for either NodeJS or web, but it can't build one for both.
The simplest solution is to build both use the "main" and "browser" fields in package.json, and to have two different index files for both, one indexBrowser.ts for the browser, and one indexNodeJs.ts for NodeJS.

Also, jest is having issues with ECMAScript Modules (ESM), so we switch to "module": "CommonJS" in tsconfig.json, which will improve overall compatibility of this package.

We now have two init files, which dynamically switch the Ecc (renamed from EccWasm) and the hash functions out with the one for the correct target.

This makes it such that the library is still easy to use; the only function a user has to call is initWasm to load the WebAssembly, and NodeJS / webpack will pick the right module to load.

This diff has been shown to work on NodeJS, jest and webpack, and we could successfully integrate it into CashTab, see D16109.

Test Plan: npm run build

Reviewers: #bitcoin_abc, bytesofman

Reviewed By: #bitcoin_abc, bytesofman

Subscribers: bytesofman

Differential Revision: https://reviews.bitcoinabc.org/D16107

Details

Provenance
tobias_ruckAuthored on May 4 2024, 14:02
tobias_ruckPushed on May 4 2024, 22:05
Reviewer
Restricted Project
Differential Revision
D16107: [ecash-lib] "module": "CommonJS" + separate "main" and "browser"
Parents
rABC7bc682f5d5e1: [e.cash] Fix mobile video display issue
Branches
Unknown
Tags
Unknown