Skip to content

Commit

Permalink
Allow to add to Alchemys importmap from Rails application
Browse files Browse the repository at this point in the history
Rails applications are technically engines, but now really. For example
the `engine_name` is the `config/application.rb` modularized class
name. Which makes it not nice for this interface from an app developers
perspective.

Let's make the interface more explicit by using a options hash with readable keys.

## Example

    Rails.application.config.before_initialize do
      Alchemy.admin_importmaps.add({
        importmap_path: Rails.application.root.join("config/alchemy/importmap.rb"),
        source_paths: [
          Rails.application.root.join("app/javascript/components/product_select.js")
        ],
        name: "product_select"
      })
    end
  • Loading branch information
tvdeyen committed May 22, 2024
1 parent a1e0391 commit 9c4c3ce
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 18 deletions.
6 changes: 2 additions & 4 deletions app/views/layouts/alchemy/admin.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@
<%= render 'alchemy/admin/partials/routes' %>
<%= javascript_include_tag('alchemy/admin/all', 'data-turbo-track' => true) %>
<%= javascript_importmap_tags("alchemy_admin", importmap: Alchemy.importmap) %>
<% if Alchemy.admin_js_imports.any? %>
<% Alchemy.admin_js_imports.each do |path| %>
<script type="module">
<% Alchemy.admin_js_imports.each do |path| %>
import "<%= path %>"
<% end %>
import "<%= path %>"
</script>
<% end %>
<%= yield :javascript_includes %>
Expand Down
2 changes: 1 addition & 1 deletion config/alchemy/importmap.rb → config/importmap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
pin "tinymce", to: "tinymce.min.js", preload: true

pin "alchemy_admin", to: "alchemy_admin.js", preload: true
pin_all_from File.expand_path("../../app/javascript/alchemy_admin", __dir__), under: "alchemy_admin", preload: true
pin_all_from File.expand_path("../app/javascript/alchemy_admin", __dir__), under: "alchemy_admin", preload: true
23 changes: 18 additions & 5 deletions lib/alchemy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def self.admin_js_imports=(sources)
@_admin_js_imports = Set[sources]
end

# Additional importmaps from engines to be included in the Alchemy admin UI
# Additional importmaps to be included in the Alchemy admin UI
#
# Be sure to also pin modules with +Alchemy.importmap+.
#
Expand All @@ -76,13 +76,26 @@ def self.admin_js_imports=(sources)
#
# # lib/alchemy/solidus/engine.rb
# initializer "alchemy_solidus.assets", before: "alchemy.importmap" do |app|
# Alchemy.engine_importmaps.add(Engine)
# Alchemy.admin_importmaps.add({
# importmap_path: root.join("config/importmap.rb"),
# source_paths: [
# root.join("app/javascript")
# ],
# name: "alchemy_solidus"
# })
# app.config.assets.precompile << "alchemy_solidus/manifest.js"
# end
#
# @return [Set<Rails::Engine>]
def self.engine_importmaps
@_engine_importmaps ||= Set.new([Alchemy::Engine])
# @return [Set<Hash>]
def self.admin_importmaps
@_admin_importmaps ||= Set.new([{
importmap_path: Engine.root.join("config/importmap.rb"),
source_paths: [
Engine.root.join("app/javascript"),
Engine.root.join("vendor/javascript")
],
name: "alchemy_admin"
}])
end

# Define page publish targets
Expand Down
14 changes: 6 additions & 8 deletions lib/alchemy/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@ class Engine < Rails::Engine
initializer "alchemy.importmap" do |app|
watch_paths = []

Alchemy.engine_importmaps.each do |engine|
Alchemy.importmap.draw engine.root.join("config/alchemy", "importmap.rb")
package_path = engine.root.join("app/javascript")
watch_paths << package_path
vendor_packages_path = engine.root.join("vendor/javascript")
app.config.assets.paths += [package_path, vendor_packages_path]
if engine.engine_name != "alchemy"
Alchemy.admin_js_imports.add(engine.engine_name)
Alchemy.admin_importmaps.each do |admin_import|
Alchemy.importmap.draw admin_import[:importmap_path]
watch_paths += admin_import[:source_paths]
app.config.assets.paths += admin_import[:source_paths]
if admin_import[:name] != "alchemy_admin"
Alchemy.admin_js_imports.add(admin_import[:name])
end
end

Expand Down
22 changes: 22 additions & 0 deletions spec/libraries/alchemy_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require "rails_helper"

RSpec.describe Alchemy do
describe ".admin_importmaps" do
subject { Alchemy.admin_importmaps }

it "returns a Set of admin importmaps" do
is_expected.to be_a(Set)
end

it "includes alchemy_admin importmap" do
expect(subject.first).to eq({
importmap_path: Alchemy::Engine.root.join("config/importmap.rb"),
name: "alchemy_admin",
source_paths: [
Alchemy::Engine.root.join("app/javascript"),
Alchemy::Engine.root.join("vendor/javascript")
]
})
end
end
end

0 comments on commit 9c4c3ce

Please sign in to comment.