-
Notifications
You must be signed in to change notification settings - Fork 782
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
Common: Make Export non-default / ESM Module Support #1632
Comments
Hmm, I thought a default export is rather standard these days (we just added one to rlp), I'd rather find a solution that allows us to work with esm simultaneously rather than remove default exports across our libs? If we output esm module code correctly, can default exports still be used? I'll try some experiments on this today and report back here. It's also okay to have both default and non-default exports as we do today. So I can try to add Common as a normal export and see if it can bypass the default export if that solves the mjs issues. It sounds like we might need a dedicated mjs build to support file extensions though. I'll read more. |
@ryanio ok, thanks for chiming in, then let's put this on hold. I thought that was the consensus along some of the discussions, but I wouldn't take for myself that I would have completely internalized the topic. |
This is super interesting stuff to me and I will continue to update here with my findings. This page has a lot of information: https://www.typescriptlang.org/docs/handbook/esm-node.html I am thinking one solution would be to make @holgerd77 I'll test if the "browser" field would still be respected for the es5 build at I'm not sure if the esm support has landed in our current TS version though or if it's still only available on nightly as the notice on top of the handbook page link above currently suggests. |
Ok this is what I've tried so far. I started by updating typescript to "next" and creating {
"extends": "./tsconfig.prod.json",
"compilerOptions": {
"target": "es2020",
"module": "node12",
"moduleResolution": "node12",
"outDir": "./dist.esm",
},
} Then added this file to the build script. This would be added to "type": "module",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist.esm/index.js",
"require": "./dist/index.js"
}
}, Lots of errors on build now. Here are the two main ones:
There is a lot of interesting discussion about this. Basically, esmodules requires file extensions (boo) so all ts file imports have to be named with '.js'. It seems like this is a hard requirement with no flexibility to be changed in the future (microsoft/TypeScript#42151 (comment))
This one is confusing to me. I understand that json modules should still use These are the only two issues so far (the others were duplicative of these two core reasons), but that's just for common which is a relatively light package, so there might be more things in bigger packages like vm or client. Regardless there would be 100s of lines to fix for the two errors above across the monorepo alone. |
@ryanio thanks for digging in so deep here. 🙏 I have to admit, this is not my favorite problem space. 😋 😬 Maybe we can do some exchange on this during the team call. |
have opened: #1654 |
My 2c-s on this issue: would be great to just switch to ESM, instead of providing cjs/esm double-builds. That's 2x less code to include in npm installs, and 2x less cases to support. Most stuff supports esm these days. Also cjs modules can be imported from esm, but not wise versa. |
Agree on just using ESM everywhere.
|
I want to make noble and ethereum-cryptography v2 ESM-only. Before that, it's important to understand the impact of such decision on all users; such as the monorepo. The idea is to make v7 also pure ESM. See the gist by Sindre (largest NPM package developer out there who moved to ESM) detailing move and problems of hybrid approach. The pure-ESM move is easily reversible and monorepo could always move back to hybrid cjs/esm approach in 7.0.1 if that doesn't work. On a glance, the decision may seem bold. However:
Questions to monorepo maintainers:
|
Interesting, hardhat seems to already support ESM as per last week: https://github.com/NomicFoundation/hardhat/releases/tag/hardhat%402.13.0 cc @alcuadrado is TypeScript the only thing that doesn't support ESM with Hardhat? |
Nop, unfortunately Hardhat's core depends on CJS pretty heavily. The reason for that is that our plugin system is dependent on the order of We need to rework our plugin system for it to fully support ESM. I'm starting to think that maybe we should move to esm-only when it's properly supported by ts-node or some similar project. But note that even after that, the migration would be a pretty large project, as every plugin will need to be adapted. |
Glad that this is being prioritised. Right now, tree-shaking is not straightforward with cjs builds being exported. |
Have given this a re-start for the upcoming breaking releases (broader context: current release work is taking place on Feedback appreciated! 🙂 |
We both do named exports and esm builds now courtesy of @holgerd77 so v7 will include ESM. Woot woot |
Part of #1024
Target branch:
develop
Assigned to: ScottyPoi
Our main object from the Common (for setting the chain/HF state) is exported as a default export. This causes problems like #978 and therefore the default export should be replace by a named export (by simply removing the
default
keyword).This will need a broad set of adoption in the consuming code base (other monorepo libraries), some solid half-manual search-and-replace should help here a lot though. A full automated search-and-replace though is not possible since a Common import is also referenced a lot in e.g. CHANGELOG files, where examples should not be changed. In associated README files examples though should be adopted to the new import pattern, I guess just:
Note that this PR should also branch off from
develop
, likely easy to be forgotten.[ CURRENTLY BLOCKED, SEE THREAD BELOW ]
The text was updated successfully, but these errors were encountered: