[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