From c09c638a3242a604eb15255ef2c2bcda1bdd05b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marius=20B=C4=82LTEANU?= Date: Sun, 6 Apr 2025 20:51:56 +0300 Subject: [PATCH] Introduce Stimulus to facilitate JavaScript modernization (#42510). diff --git a/Gemfile b/Gemfile index 32f710c54..bb4bfa4ef 100644 --- a/Gemfile +++ b/Gemfile @@ -16,6 +16,8 @@ gem 'addressable' gem 'rubyzip', '~> 2.4.0' gem 'propshaft', '~> 1.1.0' gem 'rack', '>= 3.1.3' +gem "stimulus-rails", "~> 1.3" +gem "importmap-rails", "~> 2.0" # Ruby Standard Gems gem 'csv', '~> 3.2.8' diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application-legacy.js similarity index 100% rename from app/assets/javascripts/application.js rename to app/assets/javascripts/application-legacy.js diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 9bb26bdec..2a4cd1628 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1805,7 +1805,7 @@ module ApplicationHelper if Setting.wiki_tablesort_enabled? tags << javascript_include_tag('tablesort-5.2.1.min.js', 'tablesort-5.2.1.number.min.js') end - tags << javascript_include_tag('application', 'responsive') + tags << javascript_include_tag('application-legacy', 'responsive') unless User.current.pref.warn_on_leaving_unsaved == '0' warn_text = escape_javascript(l(:text_warn_on_leaving_unsaved)) tags << diff --git a/app/javascript/application.js b/app/javascript/application.js new file mode 100644 index 000000000..72ef077f8 --- /dev/null +++ b/app/javascript/application.js @@ -0,0 +1 @@ +import "controllers" diff --git a/app/javascript/controllers/application.js b/app/javascript/controllers/application.js new file mode 100644 index 000000000..f898b4e6b --- /dev/null +++ b/app/javascript/controllers/application.js @@ -0,0 +1,8 @@ +import { Application } from '@hotwired/stimulus' + +const application = Application.start() + +application.debug = false +window.Stimulus = application + +export { application } diff --git a/app/javascript/controllers/index.js b/app/javascript/controllers/index.js new file mode 100644 index 000000000..6ffb4e9ee --- /dev/null +++ b/app/javascript/controllers/index.js @@ -0,0 +1,3 @@ +import { application } from "controllers/application" +import { eagerLoadControllersFrom } from "@hotwired/stimulus-loading" +eagerLoadControllersFrom("controllers", application) diff --git a/app/views/layouts/base.html.erb b/app/views/layouts/base.html.erb index 3432cb655..bf4b5af9e 100644 --- a/app/views/layouts/base.html.erb +++ b/app/views/layouts/base.html.erb @@ -10,6 +10,7 @@ <%= favicon %> <%= stylesheet_link_tag 'jquery/jquery-ui-1.13.2', 'tribute-5.1.3', 'application', 'responsive', :media => 'all' %> <%= stylesheet_link_tag 'rtl', :media => 'all' if l(:direction) == 'rtl' %> +<%= javascript_importmap_tags %> <%= javascript_heads %> <%= heads_for_theme %> <%= heads_for_auto_complete(@project) %> diff --git a/bin/importmap b/bin/importmap new file mode 100644 index 000000000..36502ab16 --- /dev/null +++ b/bin/importmap @@ -0,0 +1,4 @@ +#!/usr/bin/env ruby + +require_relative "../config/application" +require "importmap/commands" diff --git a/config/importmap.rb b/config/importmap.rb new file mode 100644 index 000000000..cded57738 --- /dev/null +++ b/config/importmap.rb @@ -0,0 +1,6 @@ +# Pin npm packages by running ./bin/importmap + +pin "application" +pin "@hotwired/stimulus", to: "stimulus.min.js" +pin "@hotwired/stimulus-loading", to: "stimulus-loading.js" +pin_all_from "app/javascript/controllers", under: "controllers" -- 2.39.5 (Apple Git-154)