@@ -6,12 +6,15 @@ class InstallGenerator < ::Rails::Generators::Base
6
6
WEBPACKER_SETUP = "require('htmx.org')\n "
7
7
SPROCKETS_SETUP = "//= require htmx\n "
8
8
IMPORTMAP_SETUP = "import \" htmx.org\" \n "
9
+ BUN_SETUP = IMPORTMAP_SETUP
9
10
10
11
desc 'Prep application.js to include HTMX installation for Webpacker, Sprockets or Importmap'
11
12
12
13
# Setup HTMX
13
14
def setup
14
- if importmap?
15
+ if bun?
16
+ setup_bun
17
+ elsif importmap?
15
18
setup_importmap
16
19
elsif webpacker?
17
20
setup_webpacker
@@ -24,6 +27,10 @@ def setup
24
27
25
28
private
26
29
30
+ def bun?
31
+ Pathname . new ( destination_root ) . join ( 'bun.config.js' ) . exist?
32
+ end
33
+
27
34
def webpacker?
28
35
!!defined? ( Webpacker )
29
36
end
@@ -40,38 +47,34 @@ def manifest(javascript_dir)
40
47
Pathname . new ( destination_root ) . join ( javascript_dir , 'application.js' )
41
48
end
42
49
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 )
48
51
if manifest . exist?
49
- append_file manifest , "\n #{ IMPORTMAP_SETUP } "
52
+ append_file manifest , "\n #{ text } "
50
53
else
51
- create_file manifest , IMPORTMAP_SETUP
54
+ create_file manifest , text
52
55
end
53
56
end
54
57
55
- def setup_sprockets
56
- manifest = manifest ( 'app/assets/javascripts' )
58
+ def setup_bun
59
+ run "bun add htmx.org@ #{ Htmx :: Rails :: HTMX_VERSION } "
57
60
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 )
63
62
end
64
63
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 } "
67
66
68
- manifest = manifest ( webpack_source_path )
67
+ add_to_manifest ( manifest ( 'app/javascript' ) , IMPORTMAP_SETUP )
68
+ end
69
69
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 )
75
78
end
76
79
77
80
def webpack_source_path
0 commit comments