bun icon indicating copy to clipboard operation
bun copied to clipboard

Bundle a standalone executable

Open Mwni opened this issue 2 years ago • 10 comments

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.

Mwni avatar Jul 08 '22 13:07 Mwni

+1

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

Would be hugely awesome if we could create smaller executables.

ftzi avatar Jul 08 '22 15:07 ftzi

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

felix-schindler avatar Jul 08 '22 16:07 felix-schindler

sounds great

fdemir avatar Jul 08 '22 16:07 fdemir

@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 :).

ghost avatar Jul 08 '22 22:07 ghost

@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.

Mwni avatar Jul 09 '22 03:07 Mwni

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.

emahuni avatar Jul 09 '22 16:07 emahuni

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.

rishavs avatar Jul 11 '22 11:07 rishavs

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/

stan-kondrat avatar Jul 30 '22 21:07 stan-kondrat

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 avatar Dec 23 '22 09:12 paulocoghi

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.

paulocoghi avatar Dec 23 '22 10:12 paulocoghi

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)

paperclover avatar Apr 05 '23 17:04 paperclover

Available in 0.6.0, via the bun build --compile :tada:

paperclover avatar May 16 '23 20:05 paperclover

@paperdave so amazing! Thanks for it!

ftzi avatar May 16 '23 22:05 ftzi

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?

tsp1998 avatar Jul 22 '23 14:07 tsp1998

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.

paperclover avatar Jul 23 '23 05:07 paperclover

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?

Nantris avatar Sep 12 '23 01:09 Nantris

@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)

paulocoghi avatar Sep 12 '23 05:09 paulocoghi