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

Stimulus controller structure and stimulus-rails gem #4417

Merged
merged 1 commit into from
Feb 2, 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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ gem "observer", "~> 0.1.2" # launchdarkly-server-sdk-8.0.0
# Assets
gem "sprockets-rails", "~> 3.4"
gem "importmap-rails", "~> 2.0"
gem "stimulus-rails", "~> 1.3" # this adds stimulus-loading.js so it must be available at runtime

group :assets, :development do
gem "tailwindcss-rails", "~> 2.3"
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,8 @@ GEM
activesupport (>= 5.2)
sprockets (>= 3.0.0)
statsd-instrument (3.6.1)
stimulus-rails (1.3.3)
railties (>= 6.0.0)
stringio (3.1.0)
strong_migrations (1.7.0)
activerecord (>= 5.2)
Expand Down Expand Up @@ -842,6 +844,7 @@ DEPENDENCIES
simplecov-cobertura (~> 2.1)
sprockets-rails (~> 3.4)
statsd-instrument (~> 3.5)
stimulus-rails (~> 1.3)
strong_migrations (~> 1.7)
tailwindcss-rails (~> 2.3)
terser (~> 1.2)
Expand Down Expand Up @@ -1107,6 +1110,7 @@ CHECKSUMS
sprockets (4.2.1) sha256=951b13dd2f2fcae840a7184722689a803e0ff9d2702d902bd844b196da773f97
sprockets-rails (3.4.2) sha256=36d6327757ccf7460a00d1d52b2d5ef0019a4670503046a129fa1fb1300931ad
statsd-instrument (3.6.1) sha256=fdaf73665c9a4d99aeddcda2e70fc266935919225dc0bf01257234f59f8f55df
stimulus-rails (1.3.3) sha256=4d1f9ab1d64e605f4c9cdd4cc530a9538b510606d32d02249d106256845c562c
stringio (3.1.0) sha256=c1f6263ae03a15025e51194ab19b06b15e06adcaaedb7f5f6c06ab60f5d67718
strong_migrations (1.7.0) sha256=c60e164ef24a80db2b5c40353a8f9225a636e8ab52be42a47cd1923b88c9829c
swd (2.0.3) sha256=4cdbe2a4246c19f093fce22e967ec3ebdd4657d37673672e621bf0c7eb770655
Expand Down
1 change: 1 addition & 0 deletions app/javascript/application.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
import Rails from "@rails/ujs";
Rails.start();
import "controllers"

import "src/api_key_form";
import "src/autocomplete";
Expand Down
9 changes: 9 additions & 0 deletions app/javascript/controllers/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Application } from "@hotwired/stimulus"

const application = Application.start()

// Configure Stimulus development experience
application.debug = false
window.Stimulus = application

export { application }
11 changes: 11 additions & 0 deletions app/javascript/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)
17 changes: 11 additions & 6 deletions config/importmap.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
# Pin npm packages by running ./bin/importmap

pin "application", preload: true
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: preload: true is the default.

pin "jquery" # @3.7.1
pin "@rails/ujs", to: "@rails--ujs.js" # @7.1.3
pin "application"
pin_all_from "app/javascript/src", under: "src"

pin "clipboard" # @2.0.11
# stimulus.min.js is a compiled asset from stimulus-rails gem
pin "@hotwired/stimulus", to: "stimulus.min.js"
# stimulus-loading.js is a compiled asset only available from stimulus-rails gem
pin "@hotwired/stimulus-loading", to: "stimulus-loading.js"
pin_all_from "app/javascript/controllers", under: "controllers"

# vendored and adapted from https://github.com/mdo/github-buttons/blob/master/src/js.js
pin "github-buttons"
# vendored from github in the before times
# vendored from github in the before times, not compatible with newest version without changes
pin "webauthn-json"

pin "jquery" # @3.7.1
pin "@rails/ujs", to: "@rails--ujs.js" # @7.1.3
pin "clipboard" # @2.0.11
Loading