stimulus-rails 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 23f5108a2b5f7377a6148df9c993a7a637b82a34e3a13fa5e2aacca4412306eb
4
- data.tar.gz: 5a7076d26e257566a2c17f60f0c19ad72623f648cf8d6390ca24cbec498f2b64
3
+ metadata.gz: 79e7cd48f25a99d585cf5a257256a1d52d26d131e7cdb7a7de81135752e19ca3
4
+ data.tar.gz: bead4bdbef70c56d7343755884185eb399d93b0718f37b9988163a74aad9fe81
5
5
  SHA512:
6
- metadata.gz: f45d9def4599c3b2b872b65125f2d0164d7ab3065f5fc0d179fe90d2370840bffd4056c890d2f6f6c5099ccccc72ae6b75b0c09705e32f4656366e37ff4e0c8c
7
- data.tar.gz: 2d463a499ede333ea3a7ceae20d6049f2ee703f35d2c3fe8acab90fa56f37f41bf190c55ec5a169c0393afd34344716aa7a436713aa0a887f1376b7a3165a2a5
6
+ metadata.gz: 83eb18cb343693c7535aa193fe16251b69a29196225c019672404ea02a45280296f6d5c2c163923e5fee4611ea12493d48040a9b1872f00d89cc0955e9677970
7
+ data.tar.gz: 4056db548e949647e7a7d66ba5e8759bb8e0a17f74f2fb4e8e23ddbb41d6d8eab5e729e430a07351554d893c756cc07cf0e15b71d236afb0bc0e23838f8b67df
data/README.md CHANGED
@@ -7,7 +7,9 @@ Stimulus for Rails makes it easy to use this modest framework with both import-m
7
7
 
8
8
  ## Installation
9
9
 
10
- This gem is automatically configured for applications made with Rails 7+ (unless `--skip-hotwire` is passed to the generator). But if you're on Rails 6, you can install it manually with:
10
+ This gem is automatically configured for applications made with Rails 7+ (unless `--skip-hotwire` is passed to the generator). But if you're on Rails 6, you can install it with the installer or manually:
11
+
12
+ ### Installing with installer
11
13
 
12
14
  1. Add the `stimulus-rails` gem to your Gemfile: `gem 'stimulus-rails'`
13
15
  2. Run `./bin/bundle install`.
@@ -17,6 +19,89 @@ The installer will automatically detect whether you're using an [import map](htt
17
19
 
18
20
  The installer amends your JavaScript entry point at `app/javascript/application.js` to import the `app/javascript/controllers/index.js` file, which is responsible for setting up your Stimulus application and registering your controllers.
19
21
 
22
+ ### Installing manually
23
+
24
+ Note that we recommend running the installer as described above. But the following is helpful if you encounter errors while running the installer (e.g., if there are conflicts with existing files) or if you just like doing stuff manually. Follow the instructions for import map *or* JavaScript bundler, depending on your setup.
25
+
26
+ 1. Add the `stimulus-rails` gem to your Gemfile: `gem 'stimulus-rails'`
27
+ 2. Run `./bin/bundle install`.
28
+
29
+ #### With import map
30
+
31
+ 3. Create `app/javascript/controllers/index.js` and load your controllers like this:
32
+ ```javascript
33
+ import { application } from "controllers/application"
34
+
35
+ // Eager load all controllers defined in the import map under controllers/**/*_controller
36
+ import { eagerLoadControllersFrom } from "@hotwired/stimulus-loading"
37
+ eagerLoadControllersFrom("controllers", application)
38
+ ```
39
+
40
+ 4. Create `app/javascript/controllers/application.js` with the following content:
41
+ ```javascript
42
+ import { Application } from "@hotwired/stimulus"
43
+
44
+ const application = Application.start()
45
+
46
+ // Configure Stimulus development experience
47
+ application.debug = false
48
+ window.Stimulus = application
49
+
50
+ export { application }
51
+ ```
52
+
53
+ 5. Add the following line to `app/javascript/application.js` to import all your controllers:
54
+ ```javascript
55
+ import "controllers"
56
+ ```
57
+
58
+ 6. Finally, Pin Stimulus and controllers in `config/importmap.rb` by adding:
59
+ ```ruby
60
+ pin "@hotwired/stimulus", to: "stimulus.min.js", preload: true
61
+ pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true
62
+ pin_all_from "app/javascript/controllers", under: "controllers"
63
+
64
+ ```
65
+
66
+ #### With JavaScript bundler
67
+
68
+ 3. Create `app/javascript/controllers/index.js` and load your controllers.
69
+
70
+ Make sure to change `HelloController` to an actual controller and repeat for every controller you have or use the command mentioned in the comments below:
71
+ ```javascript
72
+ // This file is auto-generated by ./bin/rails stimulus:manifest:update
73
+ // Run that command whenever you add a new controller or create them with
74
+ // ./bin/rails generate stimulus controllerName
75
+
76
+ import { application } from "./application"
77
+
78
+ import HelloController from "./hello_controller"
79
+ application.register("hello", HelloController)
80
+ ```
81
+
82
+ 4. Create `app/javascript/controllers/application.js` with the following content:
83
+ ```javascript
84
+ import { Application } from "@hotwired/stimulus"
85
+
86
+ const application = Application.start()
87
+
88
+ // Configure Stimulus development experience
89
+ application.debug = false
90
+ window.Stimulus = application
91
+
92
+ export { application }
93
+ ```
94
+
95
+ 5. Add the following line to `app/javascript/application.js` to import all your controllers:
96
+ ```javascript
97
+ import "controllers"
98
+ ```
99
+
100
+ 6. Finally, add the Stimulus package to yarn:
101
+ ```bash
102
+ yarn add @hotwired/stimulus
103
+ ```
104
+
20
105
 
21
106
  ## Usage with import map
22
107
 
@@ -19,7 +19,7 @@ function extractControllerNamesFrom(element) {
19
19
  function loadController(name) {
20
20
  import(controllerFilename(name))
21
21
  .then(module => registerController(name, module))
22
- .catch(error => console.log(`Failed to autoload controller: ${name}`, error))
22
+ .catch(error => console.error(`Failed to autoload controller: ${name}`, error))
23
23
  }
24
24
 
25
25
  function controllerFilename(name) {
@@ -21,7 +21,7 @@ function registerControllerFromPath(path, under, application) {
21
21
 
22
22
  import(path)
23
23
  .then(module => application.register(name, module.default))
24
- .catch(error => console.log(`Failed to register controller: ${name} (${path})`, error))
24
+ .catch(error => console.error(`Failed to register controller: ${name} (${path})`, error))
25
25
  }
26
26
 
27
27
  console.warn("stimulus-importmap-autoload.js has been deprecated in favor of stimulus-loading.js")
@@ -21,9 +21,11 @@ function registerControllerFromPath(path, under, application) {
21
21
  .replace(/\//g, "--")
22
22
  .replace(/_/g, "-")
23
23
 
24
- import(path)
25
- .then(module => registerController(name, module, application))
26
- .catch(error => console.debug(`Failed to register controller: ${name} (${path})`, error))
24
+ if (!(name in registeredControllers)) {
25
+ import(path)
26
+ .then(module => registerController(name, module, application))
27
+ .catch(error => console.error(`Failed to register controller: ${name} (${path})`, error))
28
+ }
27
29
  }
28
30
 
29
31
 
@@ -64,9 +66,11 @@ function extractControllerNamesFrom(element) {
64
66
  }
65
67
 
66
68
  function loadController(name, under, application) {
67
- import(controllerFilename(name, under))
68
- .then(module => registerController(name, module, application))
69
- .catch(error => console.debug(`Failed to autoload controller: ${name}`, error))
69
+ if (!(name in registeredControllers)) {
70
+ import(controllerFilename(name, under))
71
+ .then(module => registerController(name, module, application))
72
+ .catch(error => console.error(`Failed to autoload controller: ${name}`, error))
73
+ }
70
74
  }
71
75
 
72
76
  function controllerFilename(name, under) {
@@ -74,8 +78,6 @@ function controllerFilename(name, under) {
74
78
  }
75
79
 
76
80
  function registerController(name, module, application) {
77
- if (name in registeredControllers) return
78
-
79
81
  application.register(name, module.default)
80
82
  registeredControllers[name] = true
81
83
  }
@@ -11,9 +11,12 @@ say "Import Stimulus controllers"
11
11
  append_to_file "app/javascript/application.js", %(import "controllers"\n)
12
12
 
13
13
  say "Pin Stimulus"
14
- append_to_file "config/importmap.rb" do <<-RUBY
15
- pin "@hotwired/stimulus", to: "stimulus.min.js", preload: true
16
- pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true
17
- pin_all_from "app/javascript/controllers", under: "controllers"
18
- RUBY
19
- end
14
+ say %(Appending: pin "@hotwired/stimulus", to: "stimulus.min.js", preload: true")
15
+ append_to_file "config/importmap.rb", %(pin "@hotwired/stimulus", to: "stimulus.min.js", preload: true\n)
16
+
17
+ say %(Appending: pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true)
18
+ append_to_file "config/importmap.rb", %(pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true\n)
19
+
20
+ say "Pin all controllers"
21
+ say %(Appending: pin_all_from "app/javascript/controllers", under: "controllers")
22
+ append_to_file "config/importmap.rb", %(pin_all_from "app/javascript/controllers", under: "controllers"\n)
@@ -11,7 +11,7 @@ if (Rails.root.join("app/javascript/application.js")).exist?
11
11
  say "Import Stimulus controllers"
12
12
  append_to_file "app/javascript/application.js", %(import "./controllers"\n)
13
13
  else
14
- say %(You must import "./controllers" in your JavaScript entrypoint), :red
14
+ say %(Couldn't find "app/javascript/application.js".\nYou must import "./controllers" in your JavaScript entrypoint file), :red
15
15
  end
16
16
 
17
17
  say "Install Stimulus"
@@ -1,3 +1,3 @@
1
1
  module Stimulus
2
- VERSION = "1.0.2"
2
+ VERSION = "1.0.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stimulus-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Stephenson
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2021-12-16 00:00:00.000000000 Z
13
+ date: 2022-02-19 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: railties