rails / cssbundling-rails

Bundle and process CSS in Rails with Tailwind, PostCSS, and Sass via Node.js.
MIT License
563 stars 83 forks source link

Add Bun support #130

Closed terracatta closed 9 months ago

terracatta commented 9 months ago

This is a companion PR to https://github.com/rails/jsbundling-rails/pull/167 which was recently merged by @dhh.

Like its companion, this PR adds support for the Bun Javascript bundler. It ensures if you have a bun.lockb file in your repo (or if you have bun in your $PATH and do not have a yarn.lock file) we generate all the files/commands using bun instead of yarn.

I've manually tested every single choice (and spot checked a few choices for regressions on the "yarn side) but here's an example of Tailwind on a repo that just ran jsbundling-rails with the bun option.

Installation

☁  test_app [main] ./bin/rails css:install:tailwind
Build into app/assets/builds
       exist  app/assets/builds
   identical  app/assets/builds/.keep
File unchanged! Either the supplied flag value not found or the content has already been inserted!  app/assets/config/manifest.js
Stop linking stylesheets automatically
        gsub  app/assets/config/manifest.js
File unchanged! Either the supplied flag value not found or the content has already been inserted!  .gitignore
File unchanged! Either the supplied flag value not found or the content has already been inserted!  .gitignore
Remove app/assets/stylesheets/application.css so build output can take over
      remove  app/assets/stylesheets/application.css
Add stylesheet link tag in application layout
File unchanged! Either the supplied flag value not found or the content has already been inserted!  app/views/layouts/application.html.erb
      append  Procfile.dev
Add bin/dev to start foreman
   identical  bin/dev
Install Tailwind (+PostCSS w/ autoprefixer)
      create  tailwind.config.js
      create  app/assets/stylesheets/application.tailwind.css
         run  bun add tailwindcss@latest postcss@latest autoprefixer@latest from "."
bun add v1.0.0 (822a00c4)

 installed tailwindcss@3.3.3 with binaries:
  - tailwind
  - tailwindcss
 installed postcss@8.4.29
 installed autoprefixer@10.4.15 with binaries:
  - autoprefixer

 3 packages installed [566.00ms]
Add build:css script
         run  bun run build:css from "."
$ tailwindcss -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/application.css --minify

Rebuilding...

Done in 104ms.

bin/dev with a change to application.css

☁  test_app [main] ⚡  bin/dev
20:59:11 web.1  | started with pid 18717
20:59:11 js.1   | started with pid 18718
20:59:11 css.1  | started with pid 18719
20:59:11 js.1   | $ bun bun.config.js --watch
20:59:11 css.1  | $ tailwindcss -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/application.css --minify --watch
20:59:11 css.1  |
20:59:11 css.1  | Rebuilding...
20:59:11 css.1  |
20:59:11 css.1  | Done in 113ms.
20:59:11 web.1  | DEBUGGER: Debugger can attach via UNIX domain socket (/var/folders/rf/f7h7ybdj1mg7r7qn9h41hf6w0000gn/T/ruby-debug-sock-501/ruby-debug-jmeller-18717)
20:59:11 web.1  | => Booting Puma
20:59:11 web.1  | => Rails 7.0.8 application starting in development
20:59:11 web.1  | => Run `bin/rails server --help` for more startup options
20:59:11 web.1  | Puma starting in single mode...
20:59:11 web.1  | * Puma version: 5.6.7 (ruby 3.2.2-p53) ("Birdie's Version")
20:59:11 web.1  | *  Min threads: 5
20:59:11 web.1  | *  Max threads: 5
20:59:11 web.1  | *  Environment: development
20:59:11 web.1  | *          PID: 18717
20:59:11 web.1  | * Listening on http://127.0.0.1:3000
20:59:11 web.1  | * Listening on http://[::1]:3000
20:59:11 web.1  | Use Ctrl-C to stop
21:01:14 css.1  |
21:01:14 css.1  | Rebuilding...
21:01:14 css.1  |
21:01:14 css.1  | Done in 92ms.

Recompiling Assets

☁  test_app [main] ⚡  bin/rails assets:precompile
bun install v1.0.0 (822a00c4)

 Done! Checked 93 packages (no changes) [6.00ms]
$ bun bun.config.js
bun install v1.0.0 (822a00c4)

 Done! Checked 93 packages (no changes) [2.00ms]
$ bun bun.config.js
I, [2023-09-11T20:50:25.733773 #15694]  INFO -- : Writing /Users/jmeller/code/terracatta/test_app/public/assets/manifest-b4bf6e57a53c2bdb55b8998cc94cd00883793c1c37c5e5aea3ef6749b4f6d92b.js
I, [2023-09-11T20:50:25.733906 #15694]  INFO -- : Writing /Users/jmeller/code/terracatta/test_app/public/assets/manifest-b4bf6e57a53c2bdb55b8998cc94cd00883793c1c37c5e5aea3ef6749b4f6d92b.js.gz
I, [2023-09-11T20:50:25.734000 #15694]  INFO -- : Writing /Users/jmeller/code/terracatta/test_app/public/assets/application-526f412f240d77c4b277253942a63f63ee919ac2e4379ea6736f8b778079ad44.css
I, [2023-09-11T20:50:25.734049 #15694]  INFO -- : Writing /Users/jmeller/code/terracatta/test_app/public/assets/application-526f412f240d77c4b277253942a63f63ee919ac2e4379ea6736f8b778079ad44.css.gz