Skip to content

Commit

Permalink
Merge with importmap-rails
Browse files Browse the repository at this point in the history
  • Loading branch information
kawakamimoeki committed Oct 17, 2024
1 parent 3ffed77 commit 12d46cf
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ group :development, :test do
gem 'minitest'
gem 'capybara'
gem 'selenium-webdriver'
gem "importmap-rails"
gem "turbo-rails"
end

# To use a debugger
Expand Down
9 changes: 9 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ GEM
activesupport (>= 6.1)
i18n (1.14.6)
concurrent-ruby (~> 1.0)
importmap-rails (2.0.3)
actionpack (>= 6.0.0)
activesupport (>= 6.0.0)
railties (>= 6.0.0)
json (2.7.2)
listen (3.9.0)
rb-fsevent (~> 0.10, >= 0.10.3)
Expand Down Expand Up @@ -252,6 +256,9 @@ GEM
sqlite3 (1.7.3-x86_64-linux)
thor (1.3.2)
timeout (0.4.1)
turbo-rails (2.0.11)
actionpack (>= 6.0.0)
railties (>= 6.0.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
uri (0.13.1)
Expand Down Expand Up @@ -282,13 +289,15 @@ PLATFORMS
DEPENDENCIES
capybara
clapton!
importmap-rails
minitest
puma
rspec
rspec-rails
ruby-openai
selenium-webdriver
sqlite3 (~> 1.4)
turbo-rails

BUNDLED WITH
2.5.17
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,15 @@ module ApplicationCable
end
```

### Using with importmap-rails

Use `clapton_javascript_tag` instead of `javascript_importmap_tags`.

```diff
- <%= javascript_importmap_tags %>
+ <%= clapton_javascript_tag %>
```

### Events

#### clapton:render
Expand Down
30 changes: 22 additions & 8 deletions app/helpers/clapton/clapton_helper.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
module Clapton
module ClaptonHelper

def clapton_javascript_tag
def clapton_javascript_tag(entry_point = "application", importmap: nil)
all_components = Dir.glob(Rails.root.join("app", "components", "**", "*.rb"))
tags = <<~HTML
<script type="importmap">
{
clapton_json = JSON.parse <<~JSON
{
"imports": {
"client": "/clapton/client.js",
"components": "/clapton/components.js",
Expand All @@ -15,10 +14,25 @@ def clapton_javascript_tag
end.join(",\n") }
}
}
</script>
<script type="module" src="/clapton/client.js"></script>
HTML
tags.html_safe
JSON
if defined?(javascript_importmap_tags)
importmap ||= Rails.application.importmap
json = { imports: JSON.parse(importmap.to_json(resolver: self))["imports"].merge(clapton_json["imports"]) }
safe_join [
javascript_inline_importmap_tag(json.to_json),
javascript_importmap_module_preload_tags(importmap, entry_point:),
javascript_import_module_tag(entry_point),
tag.script(type: "module", src: "/clapton/client.js"),
], "\n"
else
html = <<~HTML
<script type="importmap">
#{clapton_json.to_json}
</script>
<script type="module" src="/clapton/client.js"></script>
HTML
html.html_safe
end
end

def clapton_tag
Expand Down
2 changes: 2 additions & 0 deletions test/dummy/app/assets/config/manifest.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
//= link_tree ../images
//= link_directory ../stylesheets .css
//= link_tree ../../javascript .js
//= link_tree ../../../vendor/javascript .js
1 change: 1 addition & 0 deletions test/dummy/app/components/sample_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ def render
box = c(:box, { class: "sample-component" })
text = Clapton::Text.new(@state.message)
box
.add(c(:a, "/tasks").add(c(:text, "Tasks")))
.add(Clapton::BlockQuote.new.add(text))
.add(c(:bq).add(text))
.add(Clapton::Box.new.add(text))
Expand Down
2 changes: 2 additions & 0 deletions test/dummy/app/javascript/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
import "@hotwired/turbo-rails"
4 changes: 4 additions & 0 deletions test/dummy/bin/importmap
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env ruby

require_relative "../config/application"
require "importmap/commands"
4 changes: 4 additions & 0 deletions test/dummy/config/importmap.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Pin npm packages by running ./bin/importmap

pin "application"
pin "@hotwired/turbo-rails", to: "turbo.min.js"
Empty file.

0 comments on commit 12d46cf

Please sign in to comment.