esbuild icon indicating copy to clipboard operation
esbuild copied to clipboard

Mark Bun built-ins as external

Open colinhacks opened this issue 9 months ago • 4 comments

Users of various Vite-based frameworks have been hitting module resolution errors when trying Bun's built-in modules like bun:sqlite. These frameworks generally rely on esbuild's default behavior of marking Node.js built-ins as external & side-effect-free. This PR does the same for Bun's built-ins: bun:* modules and bun itself.

Happy to update this to explicitly list each Bun builtin: bun:test, bun:ffi, bun:sqlite, etc. But we'll likely update this list several times in the future and I was worried about maintainence burden for esbuild. So currently this PR just checks for the bun: prefix. Unlike Node.js, Bun requires usage of the bun: prefix for its built-in modules.

Some of the issues this fixes

  • https://github.com/oven-sh/bun/issues/4761
  • https://github.com/oven-sh/bun/issues/4704
  • https://github.com/withastro/astro/issues/8476
  • https://github.com/vitejs/vite/issues/14351
  • https://github.com/oven-sh/bun/issues/4704

colinhacks avatar Sep 20 '23 00:09 colinhacks

I'm not sure this specific direction (adding bun-specific stuff to esbuild's node target) makes sense because that target is for node, not bun.

evanw avatar Sep 23 '23 18:09 evanw

I'm not sure this specific direction (adding bun-specific stuff to esbuild's node target) makes sense because that target is for node, not bun.

Bun is intended as a drop-in replacement for Node, so if anything in the node target does not work in Bun, it would be a bug in Bun (and not esbuild). A bun target would be more maintenance work for esbuild and would mean existing projects would need to change their esbuild configuration to fully support Bun.

Jarred-Sumner avatar Sep 25 '23 12:09 Jarred-Sumner

Any update on this? Or does anyone have workarounds?

ersinakinci avatar Apr 02 '24 23:04 ersinakinci

I'm not sure if this will work for you, but FYI esbuild already has an API to mark things as external: https://esbuild.github.io/api/#external. Specifically --external:bun:* --external:bun will mark bun as external as well as anything starting with bun:.

evanw avatar Apr 02 '24 23:04 evanw