Skip to content

Add Bun support #130

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

Merged
merged 1 commit into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/install/Procfile_for_bun.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
web: env RUBY_DEBUG_OPEN=true bin/rails server
css: bun run build:css --watch
File renamed without changes.
39 changes: 12 additions & 27 deletions lib/install/bootstrap/install.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
require_relative "../helpers"
self.extend Helpers

say "Install Bootstrap with Bootstrap Icons, Popperjs/core and Autoprefixer"
copy_file "#{__dir__}/application.bootstrap.scss",
"app/assets/stylesheets/application.bootstrap.scss"
run "yarn add sass bootstrap bootstrap-icons @popperjs/core postcss postcss-cli autoprefixer nodemon"
run "#{bundler_cmd} add sass bootstrap bootstrap-icons @popperjs/core postcss postcss-cli autoprefixer nodemon"

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

def add_npm_script(name, script, run_script=true)
case `npx -v`.to_f
when 7.1...8.0
say "Add #{name} script"
run %(npm set-script #{name} "#{script}")
run %(yarn #{name}) if run_script
when (8.0..)
say "Add #{name} script"
run %(npm pkg set scripts.#{name}="#{script}")
run %(yarn #{name}) if run_script
else
say %(Add "scripts": { "#{name}": "#{script}" } to your package.json), :green
end
end

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")
add_npm_script("build:css:prefix", "postcss ./app/assets/builds/application.css --use=autoprefixer --output=./app/assets/builds/application.css")
add_npm_script("build:css", "yarn build:css:compile && yarn build:css:prefix")
add_npm_script("watch:css", "nodemon --watch ./app/assets/stylesheets/ --ext scss --exec \\\"yarn build:css\\\"", false)
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")
add_package_json_script("build:css:prefix", "postcss ./app/assets/builds/application.css --use=autoprefixer --output=./app/assets/builds/application.css")
add_package_json_script("build:css", "#{bundler_run_cmd} build:css:compile && #{bundler_run_cmd} build:css:prefix")
add_package_json_script("watch:css", "nodemon --watch ./app/assets/stylesheets/ --ext scss --exec \"#{bundler_run_cmd} build:css\"", false)

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

case `npx -v`.to_f
when (7.1..)
say "Add browserslist config"
run %(npm pkg set browserslist[]=defaults)
else
say %(Add "browserslist": ["defaults"] to your package.json), :green
end
package_json = JSON.parse(File.read("package.json"))
package_json["browserslist"] ||= {}
package_json["browserslist"] = ["defaults"]
File.write("package.json", JSON.pretty_generate(package_json))
19 changes: 6 additions & 13 deletions lib/install/bulma/install.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
require_relative "../helpers"
self.extend Helpers

say "Install Bulma"
copy_file "#{__dir__}/application.bulma.scss",
"app/assets/stylesheets/application.bulma.scss"
run "yarn add sass bulma"
run "#{bundler_cmd} add sass bulma"

say "Add build:css script"
build_script = "sass ./app/assets/stylesheets/application.bulma.scss:./app/assets/builds/application.css --no-source-map --load-path=node_modules"

case `npx -v`.to_f
when 7.1...8.0
run %(npm set-script build:css "#{build_script}")
run %(yarn build:css)
when (8.0..)
run %(npm pkg set scripts.build:css="#{build_script}")
run %(yarn build:css)
else
say %(Add "scripts": { "build:css": "#{build_script}" } to your package.json), :green
end
add_package_json_script "build:css",
"sass ./app/assets/stylesheets/application.bulma.scss:./app/assets/builds/application.css --no-source-map --load-path=node_modules"
42 changes: 42 additions & 0 deletions lib/install/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'json'

module Helpers
def bundler_cmd
using_bun? ? "bun" : "yarn"
end

def bundler_run_cmd
using_bun? ? "bun run" : "yarn"
end

def using_bun?
File.exist?('bun.lockb') || (tool_exists?('bun') && !File.exist?('yarn.lock'))
end

def tool_exists?(tool)
system "command -v #{tool} > /dev/null"
end

def add_package_json_script(name, script, run_script=true)
if using_bun?
package_json = JSON.parse(File.read("package.json"))
package_json["scripts"] ||= {}
package_json["scripts"][name] = script
File.write("package.json", JSON.pretty_generate(package_json))
run %(bun run #{name}) if run_script
else
case `npx -v`.to_f
when 7.1...8.0
say "Add #{name} script"
run %(npm set-script #{name} "#{script}")
run %(yarn #{name}) if run_script
when (8.0..)
say "Add #{name} script"
run %(npm pkg set scripts.#{name}="#{script}")
run %(yarn #{name}) if run_script
else
say %(Add "scripts": { "#{name}": "#{script}" } to your package.json), :green
end
end
end
end
7 changes: 5 additions & 2 deletions lib/install/install.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
require_relative "helpers"
self.extend Helpers

say "Build into app/assets/builds"
empty_directory "app/assets/builds"
keep_file "app/assets/builds"
Expand Down Expand Up @@ -41,10 +44,10 @@
end

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

say "Ensure foreman is installed"
run "gem install foreman"
Expand Down
19 changes: 6 additions & 13 deletions lib/install/postcss/install.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
require_relative "../helpers"
self.extend Helpers

say "Install PostCSS w/ nesting and autoprefixer"
copy_file "#{__dir__}/postcss.config.js", "postcss.config.js"
copy_file "#{__dir__}/application.postcss.css", "app/assets/stylesheets/application.postcss.css"
run "yarn add postcss postcss-cli postcss-import postcss-nesting autoprefixer"
run "#{bundler_cmd} add postcss postcss-cli postcss-import postcss-nesting autoprefixer"

say "Add build:css script"
build_script = "postcss ./app/assets/stylesheets/application.postcss.css -o ./app/assets/builds/application.css"

case `npx -v`.to_f
when 7.1...8.0
run %(npm set-script build:css "#{build_script}")
run %(yarn build:css)
when (8.0..)
run %(npm pkg set scripts.build:css="#{build_script}")
run %(yarn build:css)
else
say %(Add "scripts": { "build:css": "#{build_script}" } to your package.json), :green
end
add_package_json_script "build:css",
"postcss ./app/assets/stylesheets/application.postcss.css -o ./app/assets/builds/application.css"
19 changes: 6 additions & 13 deletions lib/install/sass/install.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
require_relative "../helpers"
self.extend Helpers

say "Install Sass"
copy_file "#{__dir__}/application.sass.scss", "app/assets/stylesheets/application.sass.scss"
run "yarn add sass"
run "#{bundler_cmd} add sass"

say "Add build:css script"
build_script = "sass ./app/assets/stylesheets/application.sass.scss:./app/assets/builds/application.css --no-source-map --load-path=node_modules"

case `npx -v`.to_f
when 7.1...8.0
run %(npm set-script build:css "#{build_script}")
run %(yarn build:css)
when (8.0..)
run %(npm pkg set scripts.build:css="#{build_script}")
run %(yarn build:css)
else
say %(Add "scripts": { "build:css": "#{build_script}" } to your package.json), :green
end
add_package_json_script "build:css",
"sass ./app/assets/stylesheets/application.sass.scss:./app/assets/builds/application.css --no-source-map --load-path=node_modules"
19 changes: 6 additions & 13 deletions lib/install/tailwind/install.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
require_relative "../helpers"
self.extend Helpers

say "Install Tailwind (+PostCSS w/ autoprefixer)"
copy_file "#{__dir__}/tailwind.config.js", "tailwind.config.js"
copy_file "#{__dir__}/application.tailwind.css", "app/assets/stylesheets/application.tailwind.css"
run "yarn add tailwindcss@latest postcss@latest autoprefixer@latest"
run "#{bundler_cmd} add tailwindcss@latest postcss@latest autoprefixer@latest"

say "Add build:css script"
build_script = "tailwindcss -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/application.css --minify"

case `npx -v`.to_f
when 7.1...8.0
run %(npm set-script build:css "#{build_script}")
run %(yarn build:css)
when (8.0..)
run %(npm pkg set scripts.build:css="#{build_script}")
run %(yarn build:css)
else
say %(Add "scripts": { "build:css": "#{build_script}" } to your package.json), :green
end
add_package_json_script "build:css",
"tailwindcss -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/application.css --minify"
28 changes: 23 additions & 5 deletions lib/tasks/cssbundling/build.rake
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@
namespace :css do
desc "Install JavaScript dependencies"
task :install do
unless system "yarn install"
raise "cssbundling-rails: Command install failed, ensure yarn is installed"
command = install_command
unless system(command)
raise "cssbundling-rails: Command install failed, ensure #{command.split.first} is installed"
end
end

desc "Build your CSS bundle"
build_task = task :build do
unless system "yarn build:css"
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"
command = build_command
unless system(command)
raise "cssbundling-rails: Command build failed, ensure `#{command}` runs without errors"
end
end
build_task.prereqs << :install unless ENV["SKIP_YARN_INSTALL"]
build_task.prereqs << :install unless ENV["SKIP_YARN_INSTALL"] || ENV["SKIP_BUN_INSTALL"]
end

def install_command
return "bun install" if File.exist?('bun.lockb') || (tool_exists?('bun') && !File.exist?('yarn.lock'))
return "yarn install" if File.exist?('yarn.lock') || tool_exists?('yarn')
raise "cssbundling-rails: No suitable tool found for installing JavaScript dependencies"
end

def build_command
return "bun run build:css" if File.exist?('bun.lockb') || (tool_exists?('bun') && !File.exist?('yarn.lock'))
return "yarn build:css" if File.exist?('yarn.lock') || tool_exists?('yarn')
raise "cssbundling-rails: No suitable tool found for building CSS"
end

def tool_exists?(tool)
system "command -v #{tool} > /dev/null"
end

unless ENV["SKIP_CSS_BUILD"]
Expand Down