Skip to content
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

Add bun support #12

Merged
merged 5 commits into from
Nov 29, 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
49 changes: 26 additions & 23 deletions lib/generators/htmx/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ class InstallGenerator < ::Rails::Generators::Base
WEBPACKER_SETUP = "require('htmx.org')\n"
SPROCKETS_SETUP = "//= require htmx\n"
IMPORTMAP_SETUP = "import \"htmx.org\"\n"
BUN_SETUP = IMPORTMAP_SETUP

desc 'Prep application.js to include HTMX installation for Webpacker, Sprockets or Importmap'

# Setup HTMX
def setup
if importmap?
if bun?
setup_bun
elsif importmap?
setup_importmap
elsif webpacker?
setup_webpacker
Expand All @@ -24,6 +27,10 @@ def setup

private

def bun?
Pathname.new(destination_root).join('bun.config.js').exist?
end

def webpacker?
!!defined?(Webpacker)
end
Expand All @@ -40,38 +47,34 @@ def manifest(javascript_dir)
Pathname.new(destination_root).join(javascript_dir, 'application.js')
end

def setup_importmap
run 'bin/importmap pin htmx.org'

manifest = manifest('app/javascript')

def add_to_manifest(manifest, text)
if manifest.exist?
append_file manifest, "\n#{IMPORTMAP_SETUP}"
append_file manifest, "\n#{text}"
else
create_file manifest, IMPORTMAP_SETUP
create_file manifest, text
end
end

def setup_sprockets
manifest = manifest('app/assets/javascripts')
def setup_bun
run "bun add htmx.org@#{Htmx::Rails::HTMX_VERSION}"

if manifest.exist?
append_file manifest, "\n#{SPROCKETS_SETUP}"
else
create_file manifest, SPROCKETS_SETUP
end
add_to_manifest(manifest('app/javascript'), BUN_SETUP)
end

def setup_webpacker
run 'yarn add htmx.org'
def setup_importmap
run "bin/importmap pin htmx.org#{Htmx::Rails::HTMX_VERSION}"

manifest = manifest(webpack_source_path)
add_to_manifest(manifest('app/javascript'), IMPORTMAP_SETUP)
end

if manifest.exist?
append_file(manifest, "\n#{WEBPACKER_SETUP}")
else
create_file(manifest, WEBPACKER_SETUP)
end
def setup_sprockets
add_to_manifest(manifest('app/assets/javascripts'), SPROCKETS_SETUP)
end

def setup_webpacker
run "yarn add htmx.org#{Htmx::Rails::HTMX_VERSION}"

add_to_manifest(manifest(webpack_source_path), WEBPACKER_SETUP)
end

def webpack_source_path
Expand Down
30 changes: 30 additions & 0 deletions spec/generators/install_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,36 @@
end
end

context 'with bun configured' do
before do
generate_bun_config
end

context 'when `application.js` exists' do
before do
generate_application_js('/app/javascript')
end

it 'updates file with htmx import' do
run_generator
assert_file(
'app/javascript/application.js',
"\n#{Htmx::Generators::InstallGenerator::IMPORTMAP_SETUP}"
)
end
end

context 'when `application.js` does not exists' do
it 'creates `application.js` file with htmx require' do
run_generator
assert_file(
'app/javascript/application.js',
Htmx::Generators::InstallGenerator::IMPORTMAP_SETUP
)
end
end
end

context 'with no asset pipeline' do
before do
hide_const('Webpacker')
Expand Down
5 changes: 5 additions & 0 deletions spec/support/files_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@ def generate_application_js(location_folder)
FileUtils.mkdir_p(pathname)
File.write("#{pathname}/application.js", '')
end

def generate_bun_config
FileUtils.mkdir_p(destination_root)
File.write("#{destination_root}/bun.config.js", "// Some JS\n")
end
end
end
Loading