Skip to content

feat: esm#4509

Draft
florian-lefebvre wants to merge 5 commits intolovell:mainfrom
florian-lefebvre:feat/build-step
Draft

feat: esm#4509
florian-lefebvre wants to merge 5 commits intolovell:mainfrom
florian-lefebvre:feat/build-step

Conversation

@florian-lefebvre
Copy link

@florian-lefebvre florian-lefebvre commented Mar 18, 2026

Closes #2981

I kept this PR as minimal as I could. It introduces esbuild to be able to compile ESM to CJS to support both:

  • I had to migrate lib to ESM because esbuild doesn't support transforming require() into import
  • I had to write a little esbuild plugin to transform TLAs into require()

Things left to do where I'll need some help:

  • I added a build script for esm/cjs but I'm not sure how it relates to install/build.js
  • I'm not sure how tests are currently setup so unsure how to properly test esm/cjs
  • I left some require() calls in libvips.js because they only seem consumed by binding.gyp

}

if (sharp) {
module.exports = sharp;
Copy link
Author

Choose a reason for hiding this comment

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

Conditional exports are not supported by ESM but it doesn't matter here because importing the module will throw as needed

@lovell
Copy link
Owner

lovell commented Mar 20, 2026

This is great, thank you for doing the hard work Florian, and at less of an impact in terms of changed code than I had imagined, merci donc d'avoir fait simple.

For the v0.35.x releases I'd like to maintain compatibility with versions of Node.js where ESM isn't fully available and then I suspect the v0.36.x releases (or maybe v1.0.x if I'm feeling lucky after 13 years) is the place for this to land and wish CommonJS bon voyage.

As an aside, I've been invited to (and accepted) the Astro event in London in a few weeks and spotted your name on the list so I might be able to thank you in person for this.

@florian-lefebvre
Copy link
Author

Glad I can help :) Yeah I saw you on the list for the event, looking forward to meeting you! I'll give a talk on fonts

Btw I was thinking about this PR during the weekend and I don't know if the packages under npm can be updated to ESM yet. Maybe worth waiting for 0.36 or 1.0 because altho it's just matter of importing the .node file, not sure if that'd work for a CJS project

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.

Add support to ES Modules

2 participants