Skip to content

Commit 0251ed1

Browse files
committed
Add bun support
1 parent 6075b96 commit 0251ed1

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

lib/generators/htmx/install_generator.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ class InstallGenerator < ::Rails::Generators::Base
1111

1212
# Setup HTMX
1313
def setup
14-
if importmap?
14+
if bun_configured?
15+
setup_bun
16+
elsif importmap?
1517
setup_importmap
1618
elsif webpacker?
1719
setup_webpacker
@@ -24,6 +26,10 @@ def setup
2426

2527
private
2628

29+
def bun_configured?
30+
Pathname.new(destination_root).join('bun.config.js').exist?
31+
end
32+
2733
def webpacker?
2834
!!defined?(Webpacker)
2935
end
@@ -40,6 +46,17 @@ def manifest(javascript_dir)
4046
Pathname.new(destination_root).join(javascript_dir, 'application.js')
4147
end
4248

49+
def setup_bun
50+
run 'bun add htmx.org'
51+
52+
manifest = manifest('app/javascript')
53+
if manifest.exist?
54+
append_file manifest, "\n#{IMPORTMAP_SETUP}"
55+
else
56+
create_file manifest, IMPORTMAP_SETUP
57+
end
58+
end
59+
4360
def setup_importmap
4461
run 'bin/importmap pin htmx.org'
4562

spec/generators/install_generator_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,20 @@
114114
end
115115
end
116116

117+
context 'with bun configured' do
118+
before do
119+
generate_bun_config
120+
end
121+
122+
it 'updates file with htmx import' do
123+
run_generator
124+
assert_file(
125+
'app/javascript/application.js',
126+
"\n#{Htmx::Generators::InstallGenerator::IMPORTMAP_SETUP}"
127+
)
128+
end
129+
end
130+
117131
context 'with no asset pipeline' do
118132
before do
119133
hide_const('Webpacker')

spec/support/files_helper.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,10 @@ def generate_application_js(location_folder)
77
FileUtils.mkdir_p(pathname)
88
File.write("#{pathname}/application.js", '')
99
end
10+
11+
def generate_bun_config
12+
FileUtils.mkdir_p(destination_root)
13+
File.write("#{destination_root}/bun.config.js", "// Some JS\n")
14+
end
1015
end
1116
end

0 commit comments

Comments
 (0)