-
Notifications
You must be signed in to change notification settings - Fork 2k
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
ESM all the things #12161
ESM all the things #12161
Conversation
This PR changes all internal code (scripts/, test/, util/) to ESM. The built package for NPM now contains an ESM and CJS entrypoint.
e5aa6ad
to
1c63552
Compare
This is interesting, @lucacasonato. Thanks for opening it. I've got a couple of questions for you. I do quite like the import/export syntax over CommonJS, though I'll have to admit to some ignorance here on the advantages of ESM generally. Was there anything in particular that motivated this PR? And on this point in particular:
This is interesting and reminds me of a problem reported when we started pre-building the package in the first place, #10033. One thing I've noticed in this PR is that the built package contains the data twice: once as |
Oh, a side note: I haven't tested it yet, but upgrading |
Yeah. The main motivation was making sure the module would be directly consumable by browsers or Deno. ( Also tooling is generally moving in the direction of ESM, so would be good to keep the project up to date in that sense. Many developers just starting out with JS will feel a lot more familiar with ESM compared to CJS.
Indeed - this is an unfortunate side-effect caused by the lack of support for importing JSON files directly in ESM right now. This can be resolved in a couple of months once import assertions have stabilized across the ecosystem. The Once import assertions have gained stability across the ecosystem, the ESM entrypoint can be changed to the following: import bcd from "./data.json" asserts { type: "json" };
export default bcd; |
Thanks for the additional info, @lucacasonato. That's quite helpful. @Elchi3 do you have an opinion on whether we ought to take on that size penalty? The package used to be quite a bit larger anyway, so maybe it's small price to pay for expanding the environments that BCD works in. |
@foolip and @vinyldarkscratch: I think you've been most in the weeds for the internal tooling. Any quick impressions on switching to ES modules? I think this might impact a couple of other PRs y'all have open. |
I can't really say that I have much experiences in the benefits and downsides to using ES modules vs. CommonJS @lucacasonato is there a particular problem you're trying to solve that's led you to want to switch to ESM for everything? In regards to adding ESM bindings to the releases, I am definitely down for that. Can we split that out into its own PR for easier and quicker review? |
Yeah, mentioned them 3 comments up (#12161 (comment)). Additionally @ddbeck brought up a good point about
Will do! See #12169 |
I think it's fine but we could check in with the folks that were particularly interested in a smaller package size. I think it was @bershanskiy who investigated this in an earlier PR. Anton, let us know if you have thoughts :) |
Thanks for notifying me of this change. I do like ESM better over CJS, but doubling of produced output size is unfortunate. Please note that the source and the output can evolve independently. That is, the source and tests (
Import assertions are an important aspect of ESM. Since import assertions should roll out across the ecosystem in a couple months, may be we should continue old style of releases for now and then migrate to ESM once they mature? |
Ah! Apologies, I had initially reviewed this PR on my iPhone, so I hadn't seen any of the earlier comments. 😅 I'm all for providing consumer-facing ESM bindings, so that we may maximize compatibility with ease! For our internal code, if our dependencies are switching to becoming ESM-only, then I'll give a +1 for migrating to ESM ourselves, especially since it has no impact on our consumers.
This might cause some merge conflicts with PRs we have open, but since it's all in regards to the imports up top, resolving the conflicts seems straightforward, so I'm not worried about this! |
I'm pretty sure this will require https://github.com/foolip/mdn-bcd-collector to use ESM as well. I gave that a shot once but gave up because of some dependency or error I couldn't be bothered to figure out. But I am going to put off converting mdn-bcd-collector indefinitely until something is broken, so if it's BCD that breaks it and I have to do the work, that's fine with me. |
If it requires consumers to upgrade to ESM, should we aim to get this in a 5.0.0 release? |
I think this should land in a release with a major server bump, since it is a potentially backwards compatibility breaking change; I explained a reason why in the cherry-picked #12169 PR. With these changes, however, almost every consumer will be able to continue using CommonJS, since Luca has added exported ESM bindings alongside the current CJS ones for maximum compatibility. The case of To resolve this, we could have a separate |
Coming back to this briefly: having read the discussion, I want to highlight @bershanskiy:
These are both good points. To unblock this PR, here's what I'm thinking:
@lucacasonato what do you think? |
Could I request we wait on ESM-ing BCD's internals until I've converted mdn-bcd-collector? Since the collector project interacts with BCD on a write-basis, we need to import the development version instead, so we need to follow along with the ESM conversion, and it'll take a little longer than normal due to our testing setup. (Note: we are a special case! Normal BCD consumers will not have to worry about this, since they are using pre-built bundles!) |
Apart from giving you some time to do it, do you need anything else on the BCD side of things to make this easier? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, just minor nits.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me now.
57c38d6
to
ba01cda
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM overall, thank you! Just a small nit regarding the readme:
README.md
Outdated
const bcd = require('@mdn/browser-compat-data'); | ||
import bcd from '@mdn/browser-compat-data'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we revert this change, since BCD isn't ESM-ready yet?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to clarify, this change would have to wait until the first ESM-compatible version of BCD is released, right?
PS: I guess that will be a major version bump?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's correct, see #15775 for the ESM-ready public builds!
|
This pull request has merge conflicts that must be resolved before we can merge this. |
@queengooborg there are lots of conflicts again. Would this be ready to merge after another rebase? I haven't reviewed it at all, it's so big :) |
Once a rebase and my above review comment is addressed, this PR will be ready, yes! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went ahead and performed a rebase (amongst a few small changes to satisfy ESLint) to get this ready for merging. I was also holding off on merging this myself since I had (originally accidentally) contributed to this PR, but I realize we had gotten co-owner preapproval for this anyways.
Thank you @lucacasonato for all your hard work on this, let's get it merged! 👍
This PR changes all internal code (scripts/, test/, util/) to ESM. The
built package for NPM now contains an ESM and CJS entrypoint.
ESM is supported in all LTS Node versions now.