Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bun.build (with target browser) produces nodejs target if the dependency is imported in the same script that runs Bun.build #14253

Open
spirobel opened this issue Sep 30, 2024 · 0 comments
Labels
bug Something isn't working needs triage

Comments

@spirobel
Copy link

What version of Bun is running?

1.1.29

What platform is your computer?

Linux 6.8.0-45-generic x86_64 x86_64

What steps can reproduce the bug?

The webframework that I wrote for bun has a function to build frontends. ( url.frontend("nameOfFrontendEntrypoint.tsx") )
It populates an array with the name of the frontend. To build them, the build function imports the backend code that the consumer (/developer) of this webframework wrote. This executes all the invocations of url.frontend and populates the array with all the frontend files that are to be built.

The issue is that if the frontend and the backend use the same library (that is different depending on if it is used in the browser or nodejs. But still the same npm package. @solana/web3.js is an example for that) then the compiled frontend will complain about missing nodejs features like buffer, process, https, global and so on. This is not the case if it is not imported in the backend. If it is just imported in the frontend, the Bun.build works perfectly fine. It seems to only get triggered if it is imported in the same .ts file that runs Bun.build

I made a change now that runs the Bun.build code in a different process:
spirobel/mininext@5d45fd9
this change made the issue disappear. So the assumption is, that during Bun.build, imports are cached somehow. I started to read the source code

return bun.BundleV2.generateFromJavaScript(
https://github.com/oven-sh/bun/blob/main/src/bundler/bundle_v2.zig but I dont have much time to dig deeper. I assume that the import gets visited first and is then cached and reused even though the target is different.

TLDR: use mininext v 0.5.1 and use @solana/web3.js in both frontend and backend.

What is the expected behavior?

the expected behavior is that Bun.build with target browser produces a bundle that is not dependent on a nodejs library, regardless of if the library is imported in the script that runs Bun.build or not.

The issue is that if the frontend and the backend use the same library (that is different depending on if it is used in the browser or nodejs. But still the same npm package. @solana/web3.js is an example for that) then the compiled frontend will complain about missing nodejs features like buffer, process, https, global and so on. This is not the case if it is not imported in the backend. If it is just imported in the frontend, the Bun.build works perfectly fine. It seems to only get triggered if it is imported in the same .ts file that runs Bun.build

What do you see instead?

The issue is that if the frontend and the backend use the same library (that is different depending on if it is used in the browser or nodejs. But still the same npm package. @solana/web3.js is an example for that) then the compiled frontend will complain about missing nodejs features like buffer, process, https, global and so on. This is not the case if it is not imported in the backend. If it is just imported in the frontend, the Bun.build works perfectly fine. It seems to only get triggered if it is imported in the same .ts file that runs Bun.build

Additional information

I can produce an example repo if necessary. lmk if you can not reproduce / something is unclear

@spirobel spirobel added bug Something isn't working needs triage labels Sep 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage
Projects
None yet
Development

No branches or pull requests

1 participant