diff --git a/app/assets/config/spree_backend_manifest.js b/app/assets/config/spree_backend_manifest.js index 0603252b7c..37ed762232 100644 --- a/app/assets/config/spree_backend_manifest.js +++ b/app/assets/config/spree_backend_manifest.js @@ -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 diff --git a/app/javascript/spree/backend/application.js b/app/javascript/spree/backend/application.js new file mode 100644 index 0000000000..a3ece77c09 --- /dev/null +++ b/app/javascript/spree/backend/application.js @@ -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" \ No newline at end of file diff --git a/app/javascript/controllers/clipboard_controller.js b/app/javascript/spree/backend/controllers/clipboard_controller.js similarity index 91% rename from app/javascript/controllers/clipboard_controller.js rename to app/javascript/spree/backend/controllers/clipboard_controller.js index fa206845ad..e80b0b9e70 100644 --- a/app/javascript/controllers/clipboard_controller.js +++ b/app/javascript/spree/backend/controllers/clipboard_controller.js @@ -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") diff --git a/app/javascript/spree/backend/controllers/index.js b/app/javascript/spree/backend/controllers/index.js new file mode 100644 index 0000000000..19562612cd --- /dev/null +++ b/app/javascript/spree/backend/controllers/index.js @@ -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) \ No newline at end of file diff --git a/app/javascript/controllers/password_toggle_controller.js b/app/javascript/spree/backend/controllers/password_toggle_controller.js similarity index 100% rename from app/javascript/controllers/password_toggle_controller.js rename to app/javascript/spree/backend/controllers/password_toggle_controller.js diff --git a/app/javascript/controllers/product_edit_controller.js b/app/javascript/spree/backend/controllers/product_edit_controller.js similarity index 100% rename from app/javascript/controllers/product_edit_controller.js rename to app/javascript/spree/backend/controllers/product_edit_controller.js diff --git a/app/javascript/controllers/sortable_tree_controller.js b/app/javascript/spree/backend/controllers/sortable_tree_controller.js similarity index 97% rename from app/javascript/controllers/sortable_tree_controller.js rename to app/javascript/spree/backend/controllers/sortable_tree_controller.js index c8d57b5ba2..0fddc86681 100644 --- a/app/javascript/controllers/sortable_tree_controller.js +++ b/app/javascript/spree/backend/controllers/sortable_tree_controller.js @@ -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 } diff --git a/app/javascript/controllers/spree_controller.js b/app/javascript/spree/backend/controllers/spree_controller.js similarity index 100% rename from app/javascript/controllers/spree_controller.js rename to app/javascript/spree/backend/controllers/spree_controller.js diff --git a/app/javascript/controllers/upload_button_controller.js b/app/javascript/spree/backend/controllers/upload_button_controller.js similarity index 100% rename from app/javascript/controllers/upload_button_controller.js rename to app/javascript/spree/backend/controllers/upload_button_controller.js diff --git a/app/javascript/controllers/webhooks_subscriber_events_controller.js b/app/javascript/spree/backend/controllers/webhooks_subscriber_events_controller.js similarity index 100% rename from app/javascript/controllers/webhooks_subscriber_events_controller.js rename to app/javascript/spree/backend/controllers/webhooks_subscriber_events_controller.js diff --git a/app/javascript/spree/backend/helpers/index.js b/app/javascript/spree/backend/helpers/index.js new file mode 100644 index 0000000000..f2ce105e10 --- /dev/null +++ b/app/javascript/spree/backend/helpers/index.js @@ -0,0 +1 @@ +export * from "helpers/request_utility" \ No newline at end of file diff --git a/app/javascript/request_utility.js b/app/javascript/spree/backend/helpers/request_utility.js similarity index 100% rename from app/javascript/request_utility.js rename to app/javascript/spree/backend/helpers/request_utility.js diff --git a/app/views/spree/admin/shared/_head.html.erb b/app/views/spree/admin/shared/_head.html.erb index d32766e5d9..b8979c05fd 100644 --- a/app/views/spree/admin/shared/_head.html.erb +++ b/app/views/spree/admin/shared/_head.html.erb @@ -22,7 +22,7 @@ <%= 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" %> diff --git a/config/importmap.rb b/config/importmap.rb index 743c9a31d8..5740e9e01e 100644 --- a/config/importmap.rb +++ b/config/importmap.rb @@ -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" diff --git a/lib/generators/spree/backend/install/install_generator.rb b/lib/generators/spree/backend/install/install_generator.rb index 6c8793037a..8ded326d47 100644 --- a/lib/generators/spree/backend/install/install_generator.rb +++ b/lib/generators/spree/backend/install/install_generator.rb @@ -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 diff --git a/lib/spree/backend/engine.rb b/lib/spree/backend/engine.rb index 6619354eba..4125acc131 100644 --- a/lib/spree/backend/engine.rb +++ b/lib/spree/backend/engine.rb @@ -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