Skip to content

Commit 7c86e33

Browse files
authored
[Bug Fix] Install tw-animate-css for importmap (#385)
* [Bug Fix] Install tw-animate-css for importmap (#317) * chore(installer): document tw-animate CDN filename mismatch
1 parent fce2657 commit 7c86e33

3 files changed

Lines changed: 70 additions & 8 deletions

File tree

gem/lib/generators/ruby_ui/install/templates/tailwind.css.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@import "tailwindcss";
22

33
<% if using_importmap? %>
4-
@import "../../../vendor/javascript/tw-animate-css.js";
4+
@import "../../../vendor/javascript/tw-animate-css.css";
55
<% else %>
66
@import "tw-animate-css";
77
<% end %>

gem/lib/generators/ruby_ui/javascript_utils.rb

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module 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
6582
end
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
require "fileutils"
5+
require "pathname"
6+
require "tmpdir"
7+
require_relative "../../lib/generators/ruby_ui/javascript_utils"
8+
9+
class JavascriptUtilsTest < Minitest::Test
10+
class FakeInstaller
11+
include RubyUI::Generators::JavascriptUtils
12+
13+
attr_reader :download_source, :download_destination
14+
15+
def initialize(root)
16+
@root = Pathname.new(root)
17+
end
18+
19+
def say(*) = nil
20+
21+
def empty_directory(path)
22+
FileUtils.mkdir_p(path)
23+
end
24+
25+
def get(source, destination)
26+
@download_source = source
27+
@download_destination = destination
28+
FileUtils.touch(destination)
29+
end
30+
31+
def rails_root = @root
32+
end
33+
34+
def test_tw_animate_css_is_downloaded_as_css_asset_for_importmap
35+
Dir.mktmpdir do |root|
36+
installer = FakeInstaller.new(root)
37+
38+
installer.pin_with_importmap("tw-animate-css")
39+
40+
assert_equal "https://cdn.jsdelivr.net/npm/tw-animate-css@1.4.0/dist/tw-animate.css", installer.download_source
41+
assert_equal Pathname.new(root).join("vendor/javascript/tw-animate-css.css"), installer.download_destination
42+
assert File.exist?(installer.download_destination)
43+
end
44+
end
45+
end

0 commit comments

Comments
 (0)