Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to add to Alchemy's importmap from Rails application #2884

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 %>"
tvdeyen marked this conversation as resolved.
Show resolved Hide resolved
</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
38 changes: 38 additions & 0 deletions spec/libraries/alchemy_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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

context "with additional importmaps" do
before do
Alchemy.admin_importmaps.add({
importmap_path: Rails.root.join("config/importmap.rb"),
name: "additional_importmap",
source_paths: [Rails.root.join("app/javascript")]
})
end

it "adds additional importmap to admin imports" do
initializer = Alchemy::Engine.initializers.find { _1.name == "alchemy.importmap" }
tvdeyen marked this conversation as resolved.
Show resolved Hide resolved
expect(Alchemy.admin_js_imports).to receive(:add).with("additional_importmap")
initializer.run(Rails.application)
end
end
end
end