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

Bundle a standalone executable #441

Closed
Mwni opened this issue Jul 8, 2022 · 17 comments
Closed

Bundle a standalone executable #441

Mwni opened this issue Jul 8, 2022 · 17 comments
Labels
enhancement New feature or request

Comments

@Mwni
Copy link

Mwni commented Jul 8, 2022

I was wondering if there was a way to bundle your project together with bun into a standalone executable. Much like with nexe or pkg for node.

Given that bun is not widespread, but has the edge on performance, this could reduce a great deal of friction in the deployment of projects.

@Jarred-Sumner Jarred-Sumner added the enhancement New feature or request label Jul 8, 2022
@SrBrahma
Copy link

SrBrahma commented Jul 8, 2022

+1

pkg creates a 30-40MB executable for a simple console.log.

Would be hugely awesome if we could create smaller executables.

@felix-schindler
Copy link

Size of executables from deno compile are also pretty big, smaller executables sound amazing!

@fdemir
Copy link

fdemir commented Jul 8, 2022

sounds great

@ghost
Copy link

ghost commented Jul 8, 2022

@SrBrahma @felix-schindler AFAIK pkg and deno compile (which is still an experimental feature for deno, I believe) create executables by bundling the node/deno runtime (js vm and more) with your scripts and assets, hence large file sizes (even for console.log("hi")). Also, AFAIK, all major javascript engines are virtual machines, they don't produce executable files; so if you want to create an executable that is comparable in size/performance with something, say Zig, would produce, you would need to create a whole new JS engine (compiler), and I doubt this is a goal of Bun, but feel free to correct me :).

@Mwni
Copy link
Author

Mwni commented Jul 9, 2022

@not-pope Doubt anybody expects that. But perhaps the bundled jscore runtime can be stripped of any isolatable components that the project is not using. For example multiprocessing feature set, which is commonly not being used.

@emahuni
Copy link

emahuni commented Jul 9, 2022

I was wondering if there was a way to bundle your project together with bun into a standalone executable. Much like with nexe or pkg for node.

Given that bun is not widespread, but has the edge on performance, this could reduce a great deal of friction in the deployment of projects.

This is exactly what I also had in mind when I saw this. I have Nodejs deployments on Windows and it'd be a deal breaker since the environment doesn't allow for WSL to be run there. The best way would be to compile to exe, and since bun seems to compile code anyway, it'd be great if it can compile to executables straight away. I am not concerned by the size here since Nodejs, in this instance will be taking space anyway... The thing I'm interested is to see if it can be used to make standalone Executables for use in systems where it may not otherwise run properly, or just where we would use, say PKG to create executables.

@rishavs
Copy link

rishavs commented Jul 11, 2022

Highly wish for this. For me this is the edge Deno has on bun. if bun also gets the ability to package the entire project as a single executable, it will make it much easier to make the decision to use bun instead, as bun seems to do everything better than Deno anyway.

@stan-kondrat
Copy link

Like nw.js, bun executable can be concated with .bun bundle file.

You can run following command on Windows to achieve this:
copy /b nw.exe+package.nw app.exe

or following command on Linux:
cat nw package.nw > app && chmod +x app

https://docs.nwjs.io/en/latest/For%20Users/Package%20and%20Distribute/

@paulocoghi
Copy link

paulocoghi commented Dec 23, 2022

The (main) point here is not the executable size or the ability to "compile" javascript into machine code. The point here is the ability to create a single executable which "bundles" both bun and a project's js files, allowing easier distribution.

@paulocoghi
Copy link

Thanks to Dom (on Bun's Discord channel), that pointed me to github.com/theseyan/bkg.

The @theseyan's strategy on bkg is very clean and it may be interesting in bring it into Bun itself in the near future.

@paperdave
Copy link
Member

plans according to jarred is to have something like this in v0.8

image

related issue: #2541 inquiring about how the bun binary can be shrunk down (seems like it can't be shrunk super far)

@paperdave
Copy link
Member

Available in 0.6.0, via the bun build --compile 🎉

@SrBrahma
Copy link

@paperdave so amazing! Thanks for it!

@tsp1998
Copy link

tsp1998 commented Jul 22, 2023

Available in 0.6.0, via the bun build --compile 🎉

Does the actual source files gets transpiled to any other form in this compile process or they get bundled in executable as it is?

@paperdave
Copy link
Member

Available in 0.6.0, via the bun build --compile 🎉

Does the actual source files gets transpiled to any other form in this compile process or they get bundled in executable as it is?

it is like running bun build without --compile, then it takes that code and combines it with bun so it runs such code on startup.

@Nantris
Copy link

Nantris commented Sep 12, 2023

pkg creates a 30-40MB executable for a simple console.log.

Would be hugely awesome if we could create smaller executables.

Can anyone comment on how package size compares?

@paulocoghi
Copy link

paulocoghi commented Sep 12, 2023

@slapbox, here is a table with binary sizes on Linux, using bun build --compile ... as well as bkg and pkg (latest nodejs LTS, v18).

The js source code is just a console.log('Hello World')

Command Binary size xz / gz / zip compressed cold startup warm startup peak memory usage (binary)
bun build ./index.js --compile --outfile hello 94M 23M / 35M / 33M 0.025s 0.025s 32.9M
bkg -o hello . 42M 30M / 35M / 34M 5.430s 0.029s 36.4M
pkg -t node18-linux index.js 45M 13M / 18M / 17M 0.115s 0.116s 38.5M

My takeaway is:

  • bun build --compile ... produces a bigger binary, but provides the fastest startup time (4.6x faster than nodejs)
  • bkg works similarly as pkg, but the extra compression compared to native bun build means slower startup time on the first execution, which is undesired on serverless environments (bkg's readme already mentions it)
  • bun's binary generation is pretty new, and there is a lot of opportunities for improvements, like reduced runtime size by removing unused features (like wasm)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests