bip39 icon indicating copy to clipboard operation
bip39 copied to clipboard

ESM module support

Open JonasWijne opened this issue 2 years ago • 6 comments

main.ts

import { wordlists } from 'bip39';

console.log(wordlists['EN']);

i get multiple issues, since thisa is cjs, could this be made to work with esm

JonasWijne avatar Nov 18 '21 12:11 JonasWijne

Pull requests are welcome.

I have successfully created some hybrid packages like uint8array-tools.

junderw avatar Nov 18 '21 13:11 junderw

made a repo where i reproduced the issue (https://github.com/jimbertools/vite-bip39), i looked at the problem for 2 days :rofl:

JonasWijne avatar Nov 18 '21 16:11 JonasWijne

Memo for possible solutions:

  • Something like this commit might be helpful. re: wasm_path.ts vs. wasm_path_cjs.ts but this only forks the path logic.
  • ESM would use import meta object and cjs use path join with __dirname... however readFileSync is a node-only concept, and not sure how bundlers would handle it... the above repo forks the browser / node logic further by just importing the wasm directly (webpack is able to understand this, and browsers too)...
  • JSON modules are one way to do it. Chrome 91 contains support for json ESM modules. Another way would be to use fetch in the browser... but I am not sure how bundlers would support it.
    • An alternative would be to move away from json and convert the JSON files to files that export as default the JSON contents (by using sed during build step etc.)
      • This is problematic for people who want to exclude wordlists from their bundle. You would require a bundler with tree shaking.

junderw avatar Nov 18 '21 23:11 junderw

we made a simple ts lib with only what we needed, in the future we will make this feature parity with this lib and make a pull request or something

https://github.com/jimbertools/simple-bip39

JonasWijne avatar Nov 19 '21 15:11 JonasWijne

I have a fork for this: https://github.com/nahoc10/bip39-esm-support

Pull requests welcome, but it seems like your fork completely breaks everything with no attempt to try and maintain compatibility via a hybrid model etc.

Also, you are violating the terms of the ISC license:

provided that the above copyright notice and this permission notice appear in all copies.

You have completely removed the Copyright notice and the permission notice.

junderw avatar Nov 18 '22 15:11 junderw

as i commented in https://github.com/bitcoinjs/bip39/issues/170, there is @scure/bip39 which works in all modern bundlers, is much lighter, and supports ESM

-> https://github.com/paulmillr/scure-bip39

aulneau avatar Nov 20 '22 15:11 aulneau