CashScript / cashscript

⚖️ Easily write and interact with Bitcoin Cash smart contracts
https://cashscript.org
MIT License
112 stars 79 forks source link

fix: enable import of esm build #123

Closed bitjson closed 2 years ago

bitjson commented 2 years ago

Node's ESM import requires file extensions for all imports: https://nodejs.org/api/esm.html#mandatory-file-extensions. Without the .js extensions, a runtime error is thrown when cashscript is imported by esm projects.

This also updates electrum-cash, where some interfaces have changed (and if another package tries to share configuration with ElectrumNetworkProvider, they'll get type errors, as this one is more restrictive).

Finally, the explicit exporting of the network classes (in src/index.ts) solves an issue that esm dependents encounter: The requested module 'cashscript' does not provide an export named 'ElectrumNetworkProvider'.

(And if you're interested in migrating to ESM, hopefully this gets you a lot closer!)

bitjson commented 2 years ago

Ah, I also added 'import/extensions': ['error', { js: 'always' }], in .eslintrc.js; that makes it easier to notice when a rogue import would break esm imports at runtime.

bitjson commented 2 years ago

A project using this PR: https://gitlab.com/GeneralProtocols/anyhedge/library/-/merge_requests/193