Skip to content

Commit 43dc98d

Browse files
Add bun support (#12)
* Add bun support * Use version constant to download & install js Co-authored-by: Julian Pasquale <[email protected]> * Rename method to follow convention * Use defined version constant to download & install the library * Add constant to make clear usage --------- Co-authored-by: Julian Pasquale <[email protected]>
1 parent 6075b96 commit 43dc98d

File tree

3 files changed

+61
-23
lines changed

3 files changed

+61
-23
lines changed

lib/generators/htmx/install_generator.rb

+26-23
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ class InstallGenerator < ::Rails::Generators::Base
66
WEBPACKER_SETUP = "require('htmx.org')\n"
77
SPROCKETS_SETUP = "//= require htmx\n"
88
IMPORTMAP_SETUP = "import \"htmx.org\"\n"
9+
BUN_SETUP = IMPORTMAP_SETUP
910

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

1213
# Setup HTMX
1314
def setup
14-
if importmap?
15+
if bun?
16+
setup_bun
17+
elsif importmap?
1518
setup_importmap
1619
elsif webpacker?
1720
setup_webpacker
@@ -24,6 +27,10 @@ def setup
2427

2528
private
2629

30+
def bun?
31+
Pathname.new(destination_root).join('bun.config.js').exist?
32+
end
33+
2734
def webpacker?
2835
!!defined?(Webpacker)
2936
end
@@ -40,38 +47,34 @@ def manifest(javascript_dir)
4047
Pathname.new(destination_root).join(javascript_dir, 'application.js')
4148
end
4249

43-
def setup_importmap
44-
run 'bin/importmap pin htmx.org'
45-
46-
manifest = manifest('app/javascript')
47-
50+
def add_to_manifest(manifest, text)
4851
if manifest.exist?
49-
append_file manifest, "\n#{IMPORTMAP_SETUP}"
52+
append_file manifest, "\n#{text}"
5053
else
51-
create_file manifest, IMPORTMAP_SETUP
54+
create_file manifest, text
5255
end
5356
end
5457

55-
def setup_sprockets
56-
manifest = manifest('app/assets/javascripts')
58+
def setup_bun
59+
run "bun add htmx.org@#{Htmx::Rails::HTMX_VERSION}"
5760

58-
if manifest.exist?
59-
append_file manifest, "\n#{SPROCKETS_SETUP}"
60-
else
61-
create_file manifest, SPROCKETS_SETUP
62-
end
61+
add_to_manifest(manifest('app/javascript'), BUN_SETUP)
6362
end
6463

65-
def setup_webpacker
66-
run 'yarn add htmx.org'
64+
def setup_importmap
65+
run "bin/importmap pin htmx.org#{Htmx::Rails::HTMX_VERSION}"
6766

68-
manifest = manifest(webpack_source_path)
67+
add_to_manifest(manifest('app/javascript'), IMPORTMAP_SETUP)
68+
end
6969

70-
if manifest.exist?
71-
append_file(manifest, "\n#{WEBPACKER_SETUP}")
72-
else
73-
create_file(manifest, WEBPACKER_SETUP)
74-
end
70+
def setup_sprockets
71+
add_to_manifest(manifest('app/assets/javascripts'), SPROCKETS_SETUP)
72+
end
73+
74+
def setup_webpacker
75+
run "yarn add htmx.org#{Htmx::Rails::HTMX_VERSION}"
76+
77+
add_to_manifest(manifest(webpack_source_path), WEBPACKER_SETUP)
7578
end
7679

7780
def webpack_source_path

spec/generators/install_generator_spec.rb

+30
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,36 @@
114114
end
115115
end
116116

117+
context 'with bun configured' do
118+
before do
119+
generate_bun_config
120+
end
121+
122+
context 'when `application.js` exists' do
123+
before do
124+
generate_application_js('/app/javascript')
125+
end
126+
127+
it 'updates file with htmx import' do
128+
run_generator
129+
assert_file(
130+
'app/javascript/application.js',
131+
"\n#{Htmx::Generators::InstallGenerator::IMPORTMAP_SETUP}"
132+
)
133+
end
134+
end
135+
136+
context 'when `application.js` does not exists' do
137+
it 'creates `application.js` file with htmx require' do
138+
run_generator
139+
assert_file(
140+
'app/javascript/application.js',
141+
Htmx::Generators::InstallGenerator::IMPORTMAP_SETUP
142+
)
143+
end
144+
end
145+
end
146+
117147
context 'with no asset pipeline' do
118148
before do
119149
hide_const('Webpacker')

spec/support/files_helper.rb

+5
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)