rails / jsbundling-rails

Bundle and transpile JavaScript in Rails with esbuild, rollup.js, or Webpack.
MIT License
831 stars 143 forks source link

Add Bun support #167

Closed terracatta closed 1 year ago

terracatta commented 1 year ago

What is Bun?

Bun is both a JS runtime and an extremely fast JS Bundler (even faster than esbuild) that recently hit 1.0.0 last week. This PR brings the bundler portion of Bun to the Rails community! 🎉

Example Runs

Installing

$ test_app [main] ./bin/rails javascript:install:bun
Compile into app/assets/builds
      create  app/assets/builds
      create  app/assets/builds/.keep
      append  app/assets/config/manifest.js
      append  .gitignore
      append  .gitignore
Add JavaScript include tag in application layout
      insert  app/views/layouts/application.html.erb
Create default entrypoint in app/javascript/application.js
      create  app/javascript
      create  app/javascript/application.js
Add default package.json
      create  package.json
Add bin/dev to start foreman
      create  bin/dev
Add default Procfile.dev
      create  Procfile.dev
Ensure foreman is installed
         run  gem install foreman from "."
Successfully installed foreman-0.87.2
Parsing documentation for foreman-0.87.2
Done installing documentation for foreman after 0 seconds
1 gem installed
Add default bun.config.js
      create  bun.config.js
Add build script to package.json

Running bin/dev and then editing app/javascript/application.js to demo --watch

$ test_app [main] âš¡  bin/dev
23:15:02 web.1  | started with pid 82050
23:15:02 js.1   | started with pid 82051
23:15:02 js.1   | $ bun bun.config.js --watch
23:15:02 web.1  | DEBUGGER: Debugger can attach via UNIX domain socket (/var/folders/rf/f7h7ybdj1mg7r7qn9h41hf6w0000gn/T/ruby-debug-sock-501/ruby-debug-jmeller-82050)
23:15:02 web.1  | => Booting Puma
23:15:02 web.1  | => Rails 7.0.8 application starting in development
23:15:02 web.1  | => Run `bin/rails server --help` for more startup options
23:15:02 web.1  | Puma starting in single mode...
23:15:02 web.1  | * Puma version: 5.6.7 (ruby 3.2.2-p53) ("Birdie's Version")
23:15:02 web.1  | *  Min threads: 5
23:15:02 web.1  | *  Max threads: 5
23:15:02 web.1  | *  Environment: development
23:15:02 web.1  | *          PID: 82050
23:15:02 web.1  | * Listening on http://127.0.0.1:3000
23:15:02 web.1  | * Listening on http://[::1]:3000
23:15:02 web.1  | Use Ctrl-C to stop
23:16:39 js.1   | File changed: application.js. Rebuilding...

Running bin/rails assets:precompile

$ test_app [main] âš¡  bin/rails assets:precompile
bun install v1.0.0 (822a00c4)
[4.00ms] done
No packages! Deleted empty lockfile
$ bun bun.config.js
I, [2023-09-10T23:24:13.851026 #82844]  INFO -- : Writing /Users/jmeller/code/terracatta/test_app/public/assets/application-6d532f823be8357a3451bb8bff7e218bd92382c27b49774e13ec9020f79373b0.js
I, [2023-09-10T23:24:13.851112 #82844]  INFO -- : Writing /Users/jmeller/code/terracatta/test_app/public/assets/application-6d532f823be8357a3451bb8bff7e218bd92382c27b49774e13ec9020f79373b0.js.gz
I, [2023-09-10T23:24:13.851211 #82844]  INFO -- : Writing /Users/jmeller/code/terracatta/test_app/public/assets/application.js-3679f7c6de8718fd7613ae1cef9dab742f2013d54a17f9d194505021a9ea91f0.map
I, [2023-09-10T23:24:13.851255 #82844]  INFO -- : Writing /Users/jmeller/code/terracatta/test_app/public/assets/application.js-3679f7c6de8718fd7613ae1cef9dab742f2013d54a17f9d194505021a9ea91f0.map.gz

Notes

TheDevMinerTV commented 1 year ago

Now you can write typescript again without it taking "forever" to compile.