11module RubyUI
22 module Generators
33 module JavascriptUtils
4+ TW_ANIMATE_CSS_VERSION = "1.4.0"
5+
46 def install_js_package ( package )
57 if using_importmap?
68 pin_with_importmap ( package )
@@ -21,6 +23,8 @@ def pin_with_importmap(package)
2123 case package
2224 when "motion"
2325 pin_motion
26+ when "tw-animate-css"
27+ pin_tw_animate_css
2428 when "tippy.js"
2529 pin_tippy_js
2630 else
@@ -29,23 +33,34 @@ def pin_with_importmap(package)
2933 end
3034
3135 def using_importmap?
32- File . exist? ( Rails . root . join ( "config/importmap.rb" ) ) && File . exist? ( Rails . root . join ( "bin/importmap" ) )
36+ File . exist? ( rails_root . join ( "config/importmap.rb" ) ) && File . exist? ( rails_root . join ( "bin/importmap" ) )
3337 end
3438
35- def using_bun? = File . exist? ( Rails . root . join ( "bun.lock" ) )
39+ def using_bun? = File . exist? ( rails_root . join ( "bun.lock" ) )
40+
41+ def using_npm? = File . exist? ( rails_root . join ( "package-lock.json" ) )
3642
37- def using_npm ? = File . exist? ( Rails . root . join ( "package -lock.json " ) )
43+ def using_pnpm ? = File . exist? ( rails_root . join ( "pnpm -lock.yaml " ) )
3844
39- def using_pnpm ? = File . exist? ( Rails . root . join ( "pnpm-lock.yaml " ) )
45+ def using_yarn ? = File . exist? ( rails_root . join ( "yarn.lock " ) )
4046
41- def using_yarn? = File . exist? ( Rails . root . join ( "yarn.lock" ) )
47+ def pin_tw_animate_css
48+ say <<~TEXT
49+ WARNING: Installing tw-animate-css as a CSS asset because Importmap cannot pin CSS-only package exports.
50+ TEXT
51+
52+ empty_directory rails_root . join ( "vendor/javascript" )
53+ # CDN serves "tw-animate.css"; we save as "tw-animate-css.css" to match package name. Do not "correct" the URL.
54+ get "https://cdn.jsdelivr.net/npm/tw-animate-css@#{ TW_ANIMATE_CSS_VERSION } /dist/tw-animate.css" ,
55+ rails_root . join ( "vendor/javascript/tw-animate-css.css" )
56+ end
4257
4358 def pin_motion
4459 say <<~TEXT
4560 WARNING: Installing motion from CDN because `bin/importmap pin motion` doesn't download the correct file.
4661 TEXT
4762
48- inject_into_file Rails . root . join ( "config/importmap.rb" ) , <<~RUBY
63+ inject_into_file rails_root . join ( "config/importmap.rb" ) , <<~RUBY
4964 pin "motion", to: "https://cdn.jsdelivr.net/npm/motion@11.11.17/+esm"\n
5065 RUBY
5166 end
@@ -55,11 +70,13 @@ def pin_tippy_js
5570 WARNING: Installing tippy.js from CDN because `bin/importmap pin tippy.js` doesn't download the correct file.
5671 TEXT
5772
58- inject_into_file Rails . root . join ( "config/importmap.rb" ) , <<~RUBY
73+ inject_into_file rails_root . join ( "config/importmap.rb" ) , <<~RUBY
5974 pin "tippy.js", to: "https://cdn.jsdelivr.net/npm/tippy.js@6.3.7/+esm"
6075 pin "@popperjs/core", to: "https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.8/+esm"\n
6176 RUBY
6277 end
78+
79+ def rails_root = Rails . root
6380 end
6481 end
6582end
0 commit comments