Skip to content

Commit

Permalink
Improve the importmap setup, inspired by the excellent mission contro…
Browse files Browse the repository at this point in the history
…l jobs gem
  • Loading branch information
damianlegawiec committed Mar 10, 2024
1 parent 0312587 commit 48de572
Show file tree
Hide file tree
Showing 16 changed files with 38 additions and 34 deletions.
6 changes: 4 additions & 2 deletions app/assets/config/spree_backend_manifest.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//= link_tree ../images

//= link sortablejs.js
//= link request_utility.js
//= link_directory ../../javascript/spree/backend .js
//= link_directory ../../javascript/spree/backend/controllers .js
//= link_directory ../../javascript/spree/backend/helpers .js

//= link spree/backend/all.js
//= link spree/backend/all.css

Expand Down
5 changes: 5 additions & 0 deletions app/javascript/spree/backend/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
import "@hotwired/turbo-rails"
import "@rails/request.js"
import "controllers"
import "helpers"
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export default class extends Controller {
static targets = [ "source" ]

copy(event) {
console.log(event)
event.preventDefault()
this.sourceTarget.select()
document.execCommand("copy")
Expand Down
11 changes: 11 additions & 0 deletions app/javascript/spree/backend/controllers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Import and register all your controllers from the importmap under controllers/*

import { application } from "controllers/application"

// Eager load all controllers defined in the import map under controllers/**/*_controller
import { eagerLoadControllersFrom } from "@hotwired/stimulus-loading"
eagerLoadControllersFrom("controllers", application)

// Lazy load controllers as they appear in the DOM (remember not to preload controllers in import map!)
// import { lazyLoadControllersFrom } from "@hotwired/stimulus-loading"
// lazyLoadControllersFrom("controllers", application)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Controller } from "@hotwired/stimulus"
import { Sortable } from "sortablejs"
import { patch } from "request_utility"
import { patch } from "helpers"

export default class extends Controller {
static values = { handle: String }
Expand Down
1 change: 1 addition & 0 deletions app/javascript/spree/backend/helpers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "helpers/request_utility"
File renamed without changes.
2 changes: 1 addition & 1 deletion app/views/spree/admin/shared/_head.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</script>

<%= render 'spree/admin/shared/paths' %>
<%= javascript_importmap_tags %>
<%= javascript_importmap_tags 'application-spree-backend' %>
<%= javascript_include_tag 'spree/backend/all', data: { turbo_track: "reload" } %>
<%= render "spree/admin/shared/translations" %>

Expand Down
11 changes: 8 additions & 3 deletions config/importmap.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
controllers_path = File.expand_path("../app/javascript/controllers", __dir__)
pin_all_from controllers_path, to: 'controllers', under: 'controllers'
pin "application-spree-backend", to: "spree/backend/application.js", preload: true
pin "@hotwired/turbo-rails", to: "turbo.min.js", preload: true
pin "@hotwired/stimulus", to: "stimulus.min.js", preload: true
pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true
pin "@rails/request.js", to: "requestjs.js", preload: true
pin "sortablejs", to: "https://ga.jspm.io/npm:sortablejs@1.15.2/modular/sortable.esm.js"

pin 'request_utility', to: 'request_utility'
pin_all_from Spree::Backend::Engine.root.join("app/javascript/spree/backend/controllers"), under: "controllers", to: "spree/backend/controllers"
pin_all_from Spree::Backend::Engine.root.join("app/javascript/spree/backend/helpers"), under: "helpers", to: "spree/backend/helpers"
13 changes: 0 additions & 13 deletions lib/generators/spree/backend/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,6 @@ def self.source_paths
def install
template 'vendor/assets/javascripts/spree/backend/all.js'
template 'vendor/assets/stylesheets/spree/backend/all.css'

say 'Ensure Request.js are installed'
run 'bin/rails requestjs:install'

say 'Pin SortableJS to importmap.rb'
run 'bin/importmap pin sortablejs'

say 'Pin Spree Dashboard Stimulus controllers in importmap.rb'

append_file 'config/importmap.rb', <<-IMPORTS.strip_heredoc
pin_all_from Spree::Backend::Engine.root.join("app/javascript/controllers"), under: "controllers", to: "controllers"
pin "request_utility", to: "request_utility"
IMPORTS
end
end
end
Expand Down
20 changes: 7 additions & 13 deletions lib/spree/backend/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,15 @@ class Engine < ::Rails::Engine
app.config.spree_backend = Environment.new
end

initializer 'spree.backend.importmap', before: 'importmap' do |app|
app.config.importmap.paths << Engine.root.join('config/importmap.rb')
# https://github.com/rails/importmap-rails?tab=readme-ov-file#sweeping-the-cache-in-development-and-test
app.config.importmap.cache_sweepers << Engine.root.join('app/javascript')
initializer "mission_control-jobs.assets" do |app|
app.config.assets.paths << root.join("app/javascript")
app.config.assets.precompile += %w[ spree_backend_manifest ]
end

# https://github.com/rails/importmap-rails/issues/58#issuecomment-1910256388
initializer 'spree.backend.assets.precompile' do |app|
app.config.assets.paths << Engine.root.join('app/javascript')
app.config.assets.paths << Engine.root.join('app/javascript/controllers')

file_names = Dir.entries(Engine.root.join('app/javascript/controllers')).select do |file|
file.end_with?('.js')
end
file_names.each { |file_name| app.config.assets.precompile << file_name }
initializer 'spree.backend.importmap', before: 'importmap' do |app|
app.config.importmap.paths << root.join('config/importmap.rb')
# https://github.com/rails/importmap-rails?tab=readme-ov-file#sweeping-the-cache-in-development-and-test
app.config.importmap.cache_sweepers << root.join('app/javascript')
end

# filter sensitive information during logging
Expand Down

0 comments on commit 48de572

Please sign in to comment.