codemod icon indicating copy to clipboard operation
codemod copied to clipboard

Migrate to ESM

Open ItsHarper opened this issue 5 months ago • 1 comments

As Babel explains in their blog post about Babel 8 beta, node.js 20 supports calling require() on ESM modules, so going esm-only shouldn't break any users on version 20+:

Babel 8 will ship as an ESM-only package. We've been trying to figure out how to make this work for very long time, and finally with the require(esm) support shipped in Node.js 20 we are comfortable with not providing a CommonJS alternative.

As someone with experience in this area, I can promise you it is far, far easier to maintain an ESM-only package. The slow death of CommonJS is truly something to celebrate. I tried fixing the failing test in #927 without migrating to ESM, and I could not figure out why I couldn't get built-in fetch working. Even if switching to ESM doesn't fix those problems directly, it will be a much more straightforward environment to debug (though honestly I do expect the issues I was having to go away with ESM).

#923 is already open, but it's incredibly heavy-handed, and I certainly do not fault you for not engaging with it. I wouldn't even know where to begin reviewing it.

I'm planning on submitting a series of PRs to do this, and I'll use this issue as a source of truth and discussion point for the steps to get there. That way each step can easily be reviewed. Even the final diff should be much, much smaller than #923 though (I truly do not know why it changes so many lines). I'm planning to do this work in a fork regardless of whether my PRs get merged or not.

  • [ ] Update to PNPM 10:
    • https://github.com/codemod-js/codemod/pull/929
    • Currently, if you have PNPM 10 installed locally, it will just mostly-silently let you use it, but everything will break because the default settings changed.
  • [ ] Allow building all packages and running all tests with a single command
    • https://github.com/codemod-js/codemod/pull/930
    • This is ~~just~~ to make it easier to verify locally that I haven't broken things for the other steps.
    • I'm actually pretty sure that the way we were running tests did not ensure that all of the latest code was being tested, so this fixes that as well.
  • [ ] Update Typescript
    • https://github.com/codemod-js/codemod/pull/931
    • Modern Typescript versions have better support for consolidating configuration files (see next step), and have options to support ESM in nicer ways.
  • [ ] Consolidate Typescript configuration
    • https://github.com/codemod-js/codemod/pull/932
    • The ESM migration will require a lot of typescript configuration changes, and that will be easier to review if I'm only changing one file instead of like 7.
  • [ ] Update jest
    • This will fix access to the built-in fetch function from the tests, which will fix the test that fails on node 24.
  • [ ] Maybe another step or two? I've written down the stepping stones that came to mind, but I'm probably forgetting something. I'll update this issue as needed.
  • [ ] Migrate to ESM
  • [ ] Remove cross-fetch, bump minimum version in engines field to 18
  • [ ] Release with a bumped major version number

ItsHarper avatar Aug 14 '25 15:08 ItsHarper

By the way, my ultimate motivation for doing all of this is to update codemod and then webcrack to use Babel 8 (currently in beta).

ItsHarper avatar Aug 15 '25 03:08 ItsHarper