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

feat!: esm only build #772

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

feat!: esm only build #772

wants to merge 7 commits into from

Conversation

AuHau
Copy link
Contributor

@AuHau AuHau commented Sep 29, 2022

This PR implements ESM-only support, this on its own is breaking change, but together with it it brings several other changes:

  • the browser bundle is not supposed to be imported like for example await import('https://unpkg.com/@ethersphere/bee-js/dist/index.browser.js')
  • the Data interface used to be extended Uint8Array now in order to get Uint8Array you need to use Data.bytes() function

Great write-up about possible issues, how-to guides, motivation etc. done by
Sindre Sorhus: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

One issue of migrating to ESM is about imports, which now has to have the full file name including the suffix. A bit non-intuitive thing is that even though we work in the TypeScript realm, the import must have .js suffix. TypeScript can work with it, but unfortunately does not provide a way to transpile .ts suffix to .js or add it based on configuration. There are three ways how to solve this:

  • for new projects, just have imports with .js suffix (we should figure out ESLint rule for that)
  • for existing Node backend projects that uses something like ts-node then using --experimental-specifier-resolution=node Node flag, lifts this requirement, although it is good to be cautious about this as this is marked as an experimental flag
  • for libraries (also can be applied for backends) use the babel-plugin-add-import-extension on top of TypeScript transpiled sources. For example see here

@AuHau AuHau force-pushed the esm branch 3 times, most recently from bff8e82 to 6a2a06f Compare October 3, 2022 11:10
.eslintrc.cjs Outdated Show resolved Hide resolved
src/types/index.ts Outdated Show resolved Hide resolved
src/types/index.ts Outdated Show resolved Hide resolved
@AuHau AuHau marked this pull request as ready for review October 12, 2022 13:07
@AuHau AuHau requested a review from vojtechsimetka as a code owner October 12, 2022 13:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants