huozhi / bunchee

Zero config bundler for ECMAScript and TypeScript packages
https://npmjs.com/bunchee
859 stars 28 forks source link

Support for Bun could easily be added #530

Closed leo closed 2 months ago

leo commented 2 months ago

We just ran into the following error earlier, which was blocking our CI after having introduced bunchee to a Bun monorepo:

CleanShot 2024-05-01 at 14 04 26@2x

In this case, bunchee announces that typescript is not available, but the reality is that it's not require that is throwing an error, but instead the fact that m (presumably the current module) is not defined, which means that .require is not even available and cannot be invoked.

We temporarily fixed the problem by removing the module resolving logic entirely, which made it work:

CleanShot 2024-05-01 at 14 46 05@2x

As mentioned above, it's a Bun monorepo with several different workspaces, but typescript and tsconfig.json sit at the top, while bunchee is invoked with bunchee --tsconfig ../../tsconfig.json from a workspace.

Perhaps it would make sense to switch over to an entirely different module resolving logic? Happy to give it a try and report back if it resolves our issue, so that you don't need to set up an entire reproduction Bun monorepo. Or perhaps you already have another idea for what the cause could be.

Like I said it is a Bun monorepo, but since bunchee has a Node.js shebang in its binary entry point file, it should still be Node.js that is running bunchee, unless I'm missing something.

We're also not using bun run --bun to overwrite the shebang.

leo commented 2 months ago

After running bunchee with npx (Node.js) and then bunx --bun (Bun), I was able to confirm that this logic doesn't work in Bun, so it seems like we were running bunchee via Bun after all. Or, to be specific, Bun was automatically replacing the Node.js shebang with bun, which might've been a result of us using bun run --filter at the root.


Regardless, the summary is that bunchee doesn't seem to work on Bun. But the good news is that this logic seems to be the only thing that is not working in Bun — the rest is. Perhaps there's an equivalent that bunchee could use here that would work in both Node.js and Bun?