Skip to content

Commit b279fd4

Browse files
authored
Merge pull request #130 from terracatta/add_bun_support
Add Bun support
2 parents 6ca4d10 + 2673455 commit b279fd4

File tree

10 files changed

+108
-86
lines changed

10 files changed

+108
-86
lines changed

lib/install/Procfile_for_bun.dev

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
web: env RUBY_DEBUG_OPEN=true bin/rails server
2+
css: bun run build:css --watch
File renamed without changes.

lib/install/bootstrap/install.rb

+12-27
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
require_relative "../helpers"
2+
self.extend Helpers
3+
14
say "Install Bootstrap with Bootstrap Icons, Popperjs/core and Autoprefixer"
25
copy_file "#{__dir__}/application.bootstrap.scss",
36
"app/assets/stylesheets/application.bootstrap.scss"
4-
run "yarn add sass bootstrap bootstrap-icons @popperjs/core postcss postcss-cli autoprefixer nodemon"
7+
run "#{bundler_cmd} add sass bootstrap bootstrap-icons @popperjs/core postcss postcss-cli autoprefixer nodemon"
58

69
inject_into_file "config/initializers/assets.rb", after: /.*Rails.application.config.assets.paths.*\n/ do
710
<<~RUBY
@@ -16,32 +19,14 @@
1619
say %(Add import * as bootstrap from "bootstrap" to your entry point JavaScript file), :red
1720
end
1821

19-
def add_npm_script(name, script, run_script=true)
20-
case `npx -v`.to_f
21-
when 7.1...8.0
22-
say "Add #{name} script"
23-
run %(npm set-script #{name} "#{script}")
24-
run %(yarn #{name}) if run_script
25-
when (8.0..)
26-
say "Add #{name} script"
27-
run %(npm pkg set scripts.#{name}="#{script}")
28-
run %(yarn #{name}) if run_script
29-
else
30-
say %(Add "scripts": { "#{name}": "#{script}" } to your package.json), :green
31-
end
32-
end
33-
34-
add_npm_script("build:css:compile", "sass ./app/assets/stylesheets/application.bootstrap.scss:./app/assets/builds/application.css --no-source-map --load-path=node_modules")
35-
add_npm_script("build:css:prefix", "postcss ./app/assets/builds/application.css --use=autoprefixer --output=./app/assets/builds/application.css")
36-
add_npm_script("build:css", "yarn build:css:compile && yarn build:css:prefix")
37-
add_npm_script("watch:css", "nodemon --watch ./app/assets/stylesheets/ --ext scss --exec \\\"yarn build:css\\\"", false)
22+
add_package_json_script("build:css:compile", "sass ./app/assets/stylesheets/application.bootstrap.scss:./app/assets/builds/application.css --no-source-map --load-path=node_modules")
23+
add_package_json_script("build:css:prefix", "postcss ./app/assets/builds/application.css --use=autoprefixer --output=./app/assets/builds/application.css")
24+
add_package_json_script("build:css", "#{bundler_run_cmd} build:css:compile && #{bundler_run_cmd} build:css:prefix")
25+
add_package_json_script("watch:css", "nodemon --watch ./app/assets/stylesheets/ --ext scss --exec \"#{bundler_run_cmd} build:css\"", false)
3826

3927
gsub_file "Procfile.dev", "build:css --watch", "watch:css"
4028

41-
case `npx -v`.to_f
42-
when (7.1..)
43-
say "Add browserslist config"
44-
run %(npm pkg set browserslist[]=defaults)
45-
else
46-
say %(Add "browserslist": ["defaults"] to your package.json), :green
47-
end
29+
package_json = JSON.parse(File.read("package.json"))
30+
package_json["browserslist"] ||= {}
31+
package_json["browserslist"] = ["defaults"]
32+
File.write("package.json", JSON.pretty_generate(package_json))

lib/install/bulma/install.rb

+6-13
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
1+
require_relative "../helpers"
2+
self.extend Helpers
3+
14
say "Install Bulma"
25
copy_file "#{__dir__}/application.bulma.scss",
36
"app/assets/stylesheets/application.bulma.scss"
4-
run "yarn add sass bulma"
7+
run "#{bundler_cmd} add sass bulma"
58

69
say "Add build:css script"
7-
build_script = "sass ./app/assets/stylesheets/application.bulma.scss:./app/assets/builds/application.css --no-source-map --load-path=node_modules"
8-
9-
case `npx -v`.to_f
10-
when 7.1...8.0
11-
run %(npm set-script build:css "#{build_script}")
12-
run %(yarn build:css)
13-
when (8.0..)
14-
run %(npm pkg set scripts.build:css="#{build_script}")
15-
run %(yarn build:css)
16-
else
17-
say %(Add "scripts": { "build:css": "#{build_script}" } to your package.json), :green
18-
end
10+
add_package_json_script "build:css",
11+
"sass ./app/assets/stylesheets/application.bulma.scss:./app/assets/builds/application.css --no-source-map --load-path=node_modules"

lib/install/helpers.rb

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
require 'json'
2+
3+
module Helpers
4+
def bundler_cmd
5+
using_bun? ? "bun" : "yarn"
6+
end
7+
8+
def bundler_run_cmd
9+
using_bun? ? "bun run" : "yarn"
10+
end
11+
12+
def using_bun?
13+
File.exist?('bun.lockb') || (tool_exists?('bun') && !File.exist?('yarn.lock'))
14+
end
15+
16+
def tool_exists?(tool)
17+
system "command -v #{tool} > /dev/null"
18+
end
19+
20+
def add_package_json_script(name, script, run_script=true)
21+
if using_bun?
22+
package_json = JSON.parse(File.read("package.json"))
23+
package_json["scripts"] ||= {}
24+
package_json["scripts"][name] = script
25+
File.write("package.json", JSON.pretty_generate(package_json))
26+
run %(bun run #{name}) if run_script
27+
else
28+
case `npx -v`.to_f
29+
when 7.1...8.0
30+
say "Add #{name} script"
31+
run %(npm set-script #{name} "#{script}")
32+
run %(yarn #{name}) if run_script
33+
when (8.0..)
34+
say "Add #{name} script"
35+
run %(npm pkg set scripts.#{name}="#{script}")
36+
run %(yarn #{name}) if run_script
37+
else
38+
say %(Add "scripts": { "#{name}": "#{script}" } to your package.json), :green
39+
end
40+
end
41+
end
42+
end

lib/install/install.rb

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
require_relative "helpers"
2+
self.extend Helpers
3+
14
say "Build into app/assets/builds"
25
empty_directory "app/assets/builds"
36
keep_file "app/assets/builds"
@@ -41,10 +44,10 @@
4144
end
4245

4346
if Rails.root.join("Procfile.dev").exist?
44-
append_to_file "Procfile.dev", "css: yarn build:css --watch\n"
47+
append_to_file "Procfile.dev", "css: #{bundler_run_cmd} build:css --watch\n"
4548
else
4649
say "Add default Procfile.dev"
47-
copy_file "#{__dir__}/Procfile.dev", "Procfile.dev"
50+
copy_file "#{__dir__}/#{using_bun? ? "Procfile_for_bun" : "Procfile_for_node"}", "Procfile.dev"
4851

4952
say "Ensure foreman is installed"
5053
run "gem install foreman"

lib/install/postcss/install.rb

+6-13
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
1+
require_relative "../helpers"
2+
self.extend Helpers
3+
14
say "Install PostCSS w/ nesting and autoprefixer"
25
copy_file "#{__dir__}/postcss.config.js", "postcss.config.js"
36
copy_file "#{__dir__}/application.postcss.css", "app/assets/stylesheets/application.postcss.css"
4-
run "yarn add postcss postcss-cli postcss-import postcss-nesting autoprefixer"
7+
run "#{bundler_cmd} add postcss postcss-cli postcss-import postcss-nesting autoprefixer"
58

69
say "Add build:css script"
7-
build_script = "postcss ./app/assets/stylesheets/application.postcss.css -o ./app/assets/builds/application.css"
8-
9-
case `npx -v`.to_f
10-
when 7.1...8.0
11-
run %(npm set-script build:css "#{build_script}")
12-
run %(yarn build:css)
13-
when (8.0..)
14-
run %(npm pkg set scripts.build:css="#{build_script}")
15-
run %(yarn build:css)
16-
else
17-
say %(Add "scripts": { "build:css": "#{build_script}" } to your package.json), :green
18-
end
10+
add_package_json_script "build:css",
11+
"postcss ./app/assets/stylesheets/application.postcss.css -o ./app/assets/builds/application.css"

lib/install/sass/install.rb

+6-13
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
1+
require_relative "../helpers"
2+
self.extend Helpers
3+
14
say "Install Sass"
25
copy_file "#{__dir__}/application.sass.scss", "app/assets/stylesheets/application.sass.scss"
3-
run "yarn add sass"
6+
run "#{bundler_cmd} add sass"
47

58
say "Add build:css script"
6-
build_script = "sass ./app/assets/stylesheets/application.sass.scss:./app/assets/builds/application.css --no-source-map --load-path=node_modules"
7-
8-
case `npx -v`.to_f
9-
when 7.1...8.0
10-
run %(npm set-script build:css "#{build_script}")
11-
run %(yarn build:css)
12-
when (8.0..)
13-
run %(npm pkg set scripts.build:css="#{build_script}")
14-
run %(yarn build:css)
15-
else
16-
say %(Add "scripts": { "build:css": "#{build_script}" } to your package.json), :green
17-
end
9+
add_package_json_script "build:css",
10+
"sass ./app/assets/stylesheets/application.sass.scss:./app/assets/builds/application.css --no-source-map --load-path=node_modules"

lib/install/tailwind/install.rb

+6-13
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
1+
require_relative "../helpers"
2+
self.extend Helpers
3+
14
say "Install Tailwind (+PostCSS w/ autoprefixer)"
25
copy_file "#{__dir__}/tailwind.config.js", "tailwind.config.js"
36
copy_file "#{__dir__}/application.tailwind.css", "app/assets/stylesheets/application.tailwind.css"
4-
run "yarn add tailwindcss@latest postcss@latest autoprefixer@latest"
7+
run "#{bundler_cmd} add tailwindcss@latest postcss@latest autoprefixer@latest"
58

69
say "Add build:css script"
7-
build_script = "tailwindcss -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/application.css --minify"
8-
9-
case `npx -v`.to_f
10-
when 7.1...8.0
11-
run %(npm set-script build:css "#{build_script}")
12-
run %(yarn build:css)
13-
when (8.0..)
14-
run %(npm pkg set scripts.build:css="#{build_script}")
15-
run %(yarn build:css)
16-
else
17-
say %(Add "scripts": { "build:css": "#{build_script}" } to your package.json), :green
18-
end
10+
add_package_json_script "build:css",
11+
"tailwindcss -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/application.css --minify"

lib/tasks/cssbundling/build.rake

+23-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,36 @@
11
namespace :css do
22
desc "Install JavaScript dependencies"
33
task :install do
4-
unless system "yarn install"
5-
raise "cssbundling-rails: Command install failed, ensure yarn is installed"
4+
command = install_command
5+
unless system(command)
6+
raise "cssbundling-rails: Command install failed, ensure #{command.split.first} is installed"
67
end
78
end
89

910
desc "Build your CSS bundle"
1011
build_task = task :build do
11-
unless system "yarn build:css"
12-
raise "cssbundling-rails: Command css:build failed, ensure yarn is installed and `yarn build:css` runs without errors or use SKIP_CSS_BUILD env variable"
12+
command = build_command
13+
unless system(command)
14+
raise "cssbundling-rails: Command build failed, ensure `#{command}` runs without errors"
1315
end
1416
end
15-
build_task.prereqs << :install unless ENV["SKIP_YARN_INSTALL"]
17+
build_task.prereqs << :install unless ENV["SKIP_YARN_INSTALL"] || ENV["SKIP_BUN_INSTALL"]
18+
end
19+
20+
def install_command
21+
return "bun install" if File.exist?('bun.lockb') || (tool_exists?('bun') && !File.exist?('yarn.lock'))
22+
return "yarn install" if File.exist?('yarn.lock') || tool_exists?('yarn')
23+
raise "cssbundling-rails: No suitable tool found for installing JavaScript dependencies"
24+
end
25+
26+
def build_command
27+
return "bun run build:css" if File.exist?('bun.lockb') || (tool_exists?('bun') && !File.exist?('yarn.lock'))
28+
return "yarn build:css" if File.exist?('yarn.lock') || tool_exists?('yarn')
29+
raise "cssbundling-rails: No suitable tool found for building CSS"
30+
end
31+
32+
def tool_exists?(tool)
33+
system "command -v #{tool} > /dev/null"
1634
end
1735

1836
unless ENV["SKIP_CSS_BUILD"]

0 commit comments

Comments
 (0)