Skip to content

feat: Add ESM support while maintaining CJS support#561

Merged
andrew-ifrita merged 11 commits into
mainfrom
feat/esm-support
May 19, 2025
Merged

feat: Add ESM support while maintaining CJS support#561
andrew-ifrita merged 11 commits into
mainfrom
feat/esm-support

Conversation

@andrew-ifrita

@andrew-ifrita andrew-ifrita commented May 15, 2025

Copy link
Copy Markdown
Contributor

Use tsup to support both ESM and CJS.

This is effectively replacing #455 which has gone stale.

No logic or tests have been touched.

Motivation

Add both ESM and CJS support in an attempt to modernize the library and open up to more users.

Key Changes

  • build script is now tsup src/index.ts --format esm,cjs --dts
  • All import statements were changes to include a file extension and explicit file
  • Replaced clean script rm -rf with rimraf (dev dep) which is more cross platform
  • explicitly add reflect-metadata as a dev dep in order for e2e tests to pass
    • it is a peer dependency that was slipping by previously
  • added .nvmrc file to use the same node version that CI is using
  • moved chopstick testing dependencies into "devDependencies"

Main files to review

  • package.json
  • tsconfig.json

@andrew-ifrita andrew-ifrita marked this pull request as draft May 15, 2025 09:21
@andrew-ifrita andrew-ifrita changed the title DRAFT: feat: Add ESM support while mainting CJS feat: Add ESM support while mainting CJS May 15, 2025
Explicity add `reflect-metadata` as the peer dependency was not being
installed
@andrew-ifrita andrew-ifrita changed the title feat: Add ESM support while mainting CJS feat: Add ESM support while maintaining CJS support May 15, 2025
@andrew-ifrita andrew-ifrita marked this pull request as ready for review May 15, 2025 12:41
@andrew-ifrita

Copy link
Copy Markdown
Contributor Author

Two questions from my side:

  1. Should I also update the CHANGELOG here?
  2. If this is successfully merged, does this deserve a release? If so, is it purely a minor release since no API actually changed?

@TarikGul TarikGul left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, 1 question - I couldn't find where the new reflect-metadata package was being used?

@TarikGul

Copy link
Copy Markdown
Member

Two questions from my side:

  1. Should I also update the CHANGELOG here?
  2. If this is successfully merged, does this deserve a release? If so, is it purely a minor release since no API actually changed?
  1. For releases we usually update the CHANGELOG at the time of the release, but to make it easier you can definitely add it in this PR if you would like. I think if we started doing that in general it would save us time during the release process.

  2. Yea it would just be a minor release since its adding ESM support.

@andrew-ifrita

andrew-ifrita commented May 15, 2025

Copy link
Copy Markdown
Contributor Author

@TarikGul

  1. For releases we usually update the CHANGELOG at the time of the release, but to make it easier you can definitely add it in this PR if you would like.

I will keep as is for now then.

I couldn't find where the new reflect-metadata package was being used?

It is being used in the test:e2e suite. You can see here in the failing CI. It was reproducible locally as well. It was a peer dependency of typeorm which is a dep of chopsticks which is used in the e2e testing I believe. Unrelated but I'll move chopsticks and the other pure dev/testing dependencies into "devDependencies" in a follow up MR

edit: I fixed the dev dependencies in this PR

@ryanleecode

Copy link
Copy Markdown
Contributor

why follow up can just make the change now.

also latest node lts is 22 not 18 and generally corepack is preferred over nvmrc

@andrew-ifrita

Copy link
Copy Markdown
Contributor Author

@ryanleecode

also latest node lts is 22 not 18

Effectively to keep changes minimal. package.json lists node 18. CI is currently using node 18. If we update the min supported node version, it should happen explicitly in its own PR and not be hidden with other changes, especially when PRs squash commits. I also didn't want to unravel any CI issues that it might bring.

and generally corepack is preferred over nvmrc

Corepack is a little different as it relates to the package managers while nvm (and fnm) manage node version. Aside: corepack will no long be shipped with node. I assume it will be maintained independently but the future looks a little shaky at the moment.

@ryanleecode ryanleecode left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think better to use tshy because currently tsup isnt doing any code splitting and the package isnt tree shakable. i've added some updates in this PR that is against the current PR.

i've also validated the bundle on https://arethetypeswrong.github.io/

#562

@ryanleecode ryanleecode mentioned this pull request May 17, 2025
@andrew-ifrita

Copy link
Copy Markdown
Contributor Author

i think better to use tshy because currently tsup isnt doing any code splitting and the package isnt tree shakable. i've added some updates in this PR that is against the current PR.

That works for me. Left my full thought on the PR before merging it into this one, but it seems fine and I lack any strong enough opinion between the different tooling here. Thanks again for the help

@TarikGul TarikGul left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@andrew-ifrita andrew-ifrita merged commit eaacbb0 into main May 19, 2025
6 checks passed
@andrew-ifrita andrew-ifrita deleted the feat/esm-support branch May 19, 2025 11:59
andrew-ifrita added a commit that referenced this pull request Jun 6, 2025
…ode (#581)

Adding CI job to verify that all examples run as expected and are valid. Currently broken examples are added to an exception list and will be fixed later, but at a minimum, the working examples should not be breaking.

Since adding ESM + CJS support in #561 we can move away from a separate build stage for only the examples. Since the examples will be mostly read, or run as one-off, we can point to building and running with `tsx` which simplifies the whole process.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants