stimulus_reflex 3.5.0.rc2 → 3.5.0.rc4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +22 -18
  3. data/app/assets/javascripts/stimulus_reflex.js +8 -6
  4. data/app/assets/javascripts/stimulus_reflex.umd.js +8 -5
  5. data/app/channels/stimulus_reflex/channel.rb +49 -53
  6. data/lib/generators/stimulus_reflex/stimulus_reflex_generator.rb +6 -18
  7. data/lib/generators/stimulus_reflex/templates/app/javascript/config/cable_ready.js.tt +6 -2
  8. data/lib/generators/stimulus_reflex/templates/app/javascript/config/index.js.tt +7 -2
  9. data/lib/generators/stimulus_reflex/templates/app/javascript/config/stimulus_reflex.js.tt +5 -0
  10. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/%file_name%_controller.js.tt +6 -2
  11. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/application.js.tt +5 -1
  12. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/index.js.importmap.tt +7 -1
  13. data/lib/install/config.rb +5 -5
  14. data/lib/install/esbuild.rb +1 -1
  15. data/lib/install/importmap.rb +2 -2
  16. data/lib/install/mrujs.rb +10 -5
  17. data/lib/install/npm_packages.rb +1 -1
  18. data/lib/install/shakapacker.rb +1 -1
  19. data/lib/install/vite.rb +1 -1
  20. data/lib/install/webpacker.rb +1 -1
  21. data/lib/install/yarn.rb +4 -4
  22. data/lib/stimulus_reflex/cable_readiness.rb +1 -7
  23. data/lib/stimulus_reflex/installer.rb +88 -7
  24. data/lib/stimulus_reflex/reflex.rb +14 -29
  25. data/lib/stimulus_reflex/reflex_data.rb +17 -17
  26. data/lib/stimulus_reflex/reflex_factory.rb +49 -26
  27. data/lib/stimulus_reflex/version.rb +1 -1
  28. data/lib/tasks/stimulus_reflex/stimulus_reflex.rake +21 -52
  29. data/package.json +3 -3
  30. data/stimulus_reflex.gemspec +2 -2
  31. data/yarn.lock +518 -444
  32. metadata +10 -10
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 332456c0049281bf252fc97db164c2304a45b19b0087b5ca9cfc41288a8c7457
4
- data.tar.gz: 5eed33fa8212d85f370167f91868cbc2b1c611daf66286cec0a5d1414136f889
3
+ metadata.gz: de1231151ed33d07ced1d833af4ed33e44f65715fe9efa2b47a609839cb02772
4
+ data.tar.gz: a14a7c5e9d642aca0d64bcd240b4d8f0eea0c4cdd48a4588f452fcf538453b44
5
5
  SHA512:
6
- metadata.gz: 608f0f87a1910cc47df7a9e1a2b6a4f6169fc548e575c08ea1ed064a64582a026707a4d9e0898e340437d86957fed85bc0d3c727e3272b2d339a4fb74a5d44bb
7
- data.tar.gz: 6c73096dd78ebd2801dddc6c421d21823bf87f51a513c7adcf0c2bfd72d0ebe360b914187ebb728155580a1136c533c4505ffb9f5ed232a7b237927f5ea6a9f9
6
+ metadata.gz: be8a19b025cb8d5b5ae75430b60386f9e2e3e0eb68e6549567aa6e2af99960bd659e51b2f9ba66d8daf324ce2ac7b1230e8efd1f3b52f69ecb7ecf6e49b9d077
7
+ data.tar.gz: b0eed2ce16618c6db978cafb7d12e4550a860c48343c7fd885fa68efe53bbe37c61f0d53755bbb3ff7715f536c5b131f2bdd0c68a782d5f67c990540dc6dd2f8
data/Gemfile.lock CHANGED
@@ -1,12 +1,12 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- stimulus_reflex (3.5.0.rc2)
4
+ stimulus_reflex (3.5.0.rc4)
5
5
  actioncable (>= 5.2, < 8)
6
6
  actionpack (>= 5.2, < 8)
7
7
  actionview (>= 5.2, < 8)
8
8
  activesupport (>= 5.2, < 8)
9
- cable_ready (>= 5.0.0.rc2)
9
+ cable_ready (~> 5.0)
10
10
  nokogiri (~> 1.0)
11
11
  rack (>= 2, < 4)
12
12
  railties (>= 5.2, < 8)
@@ -82,7 +82,7 @@ GEM
82
82
  tzinfo (~> 2.0)
83
83
  ast (2.4.2)
84
84
  builder (3.2.4)
85
- cable_ready (5.0.0.rc2)
85
+ cable_ready (5.0.1)
86
86
  actionpack (>= 5.2)
87
87
  actionview (>= 5.2)
88
88
  activesupport (>= 5.2)
@@ -95,13 +95,13 @@ GEM
95
95
  erubi (1.12.0)
96
96
  globalid (1.1.0)
97
97
  activesupport (>= 5.0)
98
- i18n (1.12.0)
98
+ i18n (1.14.1)
99
99
  concurrent-ruby (~> 1.0)
100
100
  json (2.6.3)
101
101
  language_server-protocol (3.17.0.3)
102
- loofah (2.19.1)
102
+ loofah (2.21.3)
103
103
  crass (~> 1.0.2)
104
- nokogiri (>= 1.5.9)
104
+ nokogiri (>= 1.12.0)
105
105
  magic_frozen_string_literal (1.2.0)
106
106
  mail (2.8.1)
107
107
  mini_mime (>= 0.1.1)
@@ -111,7 +111,7 @@ GEM
111
111
  marcel (1.0.2)
112
112
  method_source (1.0.0)
113
113
  mini_mime (1.1.2)
114
- minitest (5.18.0)
114
+ minitest (5.18.1)
115
115
  mocha (1.13.0)
116
116
  net-imap (0.3.4)
117
117
  date
@@ -123,15 +123,15 @@ GEM
123
123
  net-smtp (0.3.3)
124
124
  net-protocol
125
125
  nio4r (2.5.8)
126
- nokogiri (1.14.2-x86_64-darwin)
126
+ nokogiri (1.16.2-x86_64-darwin)
127
127
  racc (~> 1.4)
128
- nokogiri (1.14.2-x86_64-linux)
128
+ nokogiri (1.16.2-x86_64-linux)
129
129
  racc (~> 1.4)
130
130
  parallel (1.22.1)
131
131
  parser (3.2.1.0)
132
132
  ast (~> 2.4.1)
133
- racc (1.6.2)
134
- rack (2.2.6.4)
133
+ racc (1.7.3)
134
+ rack (2.2.8.1)
135
135
  rack-test (2.1.0)
136
136
  rack (>= 1.3)
137
137
  rails (7.0.4.2)
@@ -148,11 +148,13 @@ GEM
148
148
  activesupport (= 7.0.4.2)
149
149
  bundler (>= 1.15.0)
150
150
  railties (= 7.0.4.2)
151
- rails-dom-testing (2.0.3)
152
- activesupport (>= 4.2.0)
151
+ rails-dom-testing (2.1.1)
152
+ activesupport (>= 5.0.0)
153
+ minitest
153
154
  nokogiri (>= 1.6)
154
- rails-html-sanitizer (1.5.0)
155
- loofah (~> 2.19, >= 2.19.1)
155
+ rails-html-sanitizer (1.6.0)
156
+ loofah (~> 2.21)
157
+ nokogiri (~> 1.14)
156
158
  railties (7.0.4.2)
157
159
  actionpack (= 7.0.4.2)
158
160
  activesupport (= 7.0.4.2)
@@ -188,7 +190,7 @@ GEM
188
190
  language_server-protocol (~> 3.17.0.2)
189
191
  rubocop (= 1.44.1)
190
192
  rubocop-performance (= 1.15.2)
191
- thor (1.2.1)
193
+ thor (1.2.2)
192
194
  thread-local (1.1.0)
193
195
  timeout (0.3.2)
194
196
  tzinfo (2.0.6)
@@ -197,10 +199,12 @@ GEM
197
199
  websocket-driver (0.7.5)
198
200
  websocket-extensions (>= 0.1.0)
199
201
  websocket-extensions (0.1.5)
200
- zeitwerk (2.6.7)
202
+ zeitwerk (2.6.8)
201
203
 
202
204
  PLATFORMS
203
205
  x86_64-darwin-19
206
+ x86_64-darwin-22
207
+ x86_64-darwin-23
204
208
  x86_64-linux
205
209
 
206
210
  DEPENDENCIES
@@ -213,4 +217,4 @@ DEPENDENCIES
213
217
  stimulus_reflex!
214
218
 
215
219
  BUNDLED WITH
216
- 2.2.33
220
+ 2.4.14
@@ -945,7 +945,7 @@ const extractDataAttributes = element => {
945
945
 
946
946
  var name = "stimulus_reflex";
947
947
 
948
- var version = "3.5.0-rc2";
948
+ var version = "3.5.0-rc4";
949
949
 
950
950
  var description = "Build reactive applications with the Rails tooling you already know and love.";
951
951
 
@@ -995,7 +995,7 @@ var peerDependencies = {
995
995
  var dependencies = {
996
996
  "@hotwired/stimulus": "^3",
997
997
  "@rails/actioncable": "^6 || ^7",
998
- cable_ready: "5.0.0-rc2"
998
+ cable_ready: "^5.0.0"
999
999
  };
1000
1000
 
1001
1001
  var devDependencies = {
@@ -1009,7 +1009,7 @@ var devDependencies = {
1009
1009
  "prettier-standard": "^16.4.1",
1010
1010
  rollup: "^3.19.1",
1011
1011
  "toastify-js": "^1.12.0",
1012
- vitepress: "^1.0.0-alpha.56"
1012
+ vitepress: "^1.0.0-beta.1"
1013
1013
  };
1014
1014
 
1015
1015
  var packageInfo = {
@@ -1260,7 +1260,7 @@ const findControllerByReflexName = (reflexName, controllers) => {
1260
1260
  const identifier = reflexNameToControllerIdentifier(extractReflexName(reflexName));
1261
1261
  return identifier === controller.identifier;
1262
1262
  }));
1263
- return controller || controllers[0];
1263
+ return controller;
1264
1264
  };
1265
1265
 
1266
1266
  const scanForReflexes = debounce((() => {
@@ -1281,7 +1281,8 @@ const scanForReflexesOnElement = (element, controller = null) => {
1281
1281
  const controllerName = controller ? controller.identifier : "stimulus-reflex";
1282
1282
  actions.push(`${reflexName.split("->")[0]}->${controllerName}#__perform`);
1283
1283
  const parentControllerElement = element.closest(`[data-controller~=${controllerName}]`);
1284
- if (!parentControllerElement) {
1284
+ const elementPreviouslyHadStimulusReflexController = element === parentControllerElement && controllerName === "stimulus-reflex";
1285
+ if (!parentControllerElement || elementPreviouslyHadStimulusReflexController) {
1285
1286
  controllers.push(controllerName);
1286
1287
  }
1287
1288
  }));
@@ -1440,6 +1441,7 @@ document.addEventListener("readystatechange", (() => {
1440
1441
 
1441
1442
  var StimulusReflex = Object.freeze({
1442
1443
  __proto__: null,
1444
+ StimulusReflexController: StimulusReflexController,
1443
1445
  initialize: initialize,
1444
1446
  reflexes: reflexes,
1445
1447
  register: register,
@@ -1467,4 +1469,4 @@ const global = {
1467
1469
 
1468
1470
  window.StimulusReflex = global;
1469
1471
 
1470
- export { global as default, initialize, reflexes, register, scanForReflexes, scanForReflexesOnElement, useReflex };
1472
+ export { StimulusReflexController, global as default, initialize, reflexes, register, scanForReflexes, scanForReflexesOnElement, useReflex };
@@ -869,7 +869,7 @@
869
869
  return attrs;
870
870
  };
871
871
  var name = "stimulus_reflex";
872
- var version = "3.5.0-rc2";
872
+ var version = "3.5.0-rc4";
873
873
  var description = "Build reactive applications with the Rails tooling you already know and love.";
874
874
  var keywords = [ "ruby", "rails", "websockets", "actioncable", "turbolinks", "reactive", "cable", "ujs", "ssr", "stimulus", "reflex", "stimulus_reflex", "dom", "morphdom" ];
875
875
  var homepage = "https://docs.stimulusreflex.com";
@@ -902,7 +902,7 @@
902
902
  var dependencies = {
903
903
  "@hotwired/stimulus": "^3",
904
904
  "@rails/actioncable": "^6 || ^7",
905
- cable_ready: "5.0.0-rc2"
905
+ cable_ready: "^5.0.0"
906
906
  };
907
907
  var devDependencies = {
908
908
  "@open-wc/testing": "^3.1.7",
@@ -915,7 +915,7 @@
915
915
  "prettier-standard": "^16.4.1",
916
916
  rollup: "^3.19.1",
917
917
  "toastify-js": "^1.12.0",
918
- vitepress: "^1.0.0-alpha.56"
918
+ vitepress: "^1.0.0-beta.1"
919
919
  };
920
920
  var packageInfo = {
921
921
  name: name,
@@ -1152,7 +1152,7 @@
1152
1152
  const identifier = reflexNameToControllerIdentifier(extractReflexName(reflexName));
1153
1153
  return identifier === controller.identifier;
1154
1154
  }));
1155
- return controller || controllers[0];
1155
+ return controller;
1156
1156
  };
1157
1157
  const scanForReflexes = debounce((() => {
1158
1158
  const reflexElements = document.querySelectorAll(`[${Schema.reflex}]`);
@@ -1171,7 +1171,8 @@
1171
1171
  const controllerName = controller ? controller.identifier : "stimulus-reflex";
1172
1172
  actions.push(`${reflexName.split("->")[0]}->${controllerName}#__perform`);
1173
1173
  const parentControllerElement = element.closest(`[data-controller~=${controllerName}]`);
1174
- if (!parentControllerElement) {
1174
+ const elementPreviouslyHadStimulusReflexController = element === parentControllerElement && controllerName === "stimulus-reflex";
1175
+ if (!parentControllerElement || elementPreviouslyHadStimulusReflexController) {
1175
1176
  controllers.push(controllerName);
1176
1177
  }
1177
1178
  }));
@@ -1318,6 +1319,7 @@
1318
1319
  }));
1319
1320
  var StimulusReflex = Object.freeze({
1320
1321
  __proto__: null,
1322
+ StimulusReflexController: StimulusReflexController,
1321
1323
  initialize: initialize,
1322
1324
  reflexes: reflexes,
1323
1325
  register: register,
@@ -1342,6 +1344,7 @@
1342
1344
  }
1343
1345
  };
1344
1346
  window.StimulusReflex = global;
1347
+ exports.StimulusReflexController = StimulusReflexController;
1345
1348
  exports.default = global;
1346
1349
  exports.initialize = initialize;
1347
1350
  exports.reflexes = reflexes;
@@ -1,8 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class StimulusReflex::Channel < StimulusReflex.configuration.parent_channel.constantize
4
- attr_reader :reflex_data
5
-
6
4
  def stream_name
7
5
  [params[:channel], connection.connection_identifier].reject(&:blank?).join(":")
8
6
  end
@@ -13,76 +11,74 @@ class StimulusReflex::Channel < StimulusReflex.configuration.parent_channel.cons
13
11
  end
14
12
 
15
13
  def receive(data)
16
- @reflex_data = StimulusReflex::ReflexData.new(data)
17
14
  begin
18
- begin
19
- reflex = StimulusReflex::ReflexFactory.create_reflex_from_data(self, @reflex_data)
20
- delegate_call_to_reflex reflex
21
- rescue => exception
22
- error = exception_with_backtrace(exception)
23
- error_message = "\e[31mReflex #{reflex_data.target} failed: #{error[:message]} [#{reflex_data.url}]\e[0m\n#{error[:stack]}"
15
+ reflex = StimulusReflex::ReflexFactory.new(self, data).call
16
+ delegate_call_to_reflex reflex
17
+ rescue => exception
18
+ error = exception_with_backtrace(exception)
24
19
 
25
- if reflex
26
- reflex.rescue_with_handler(exception)
27
- reflex.logger&.error error_message
28
- reflex.broadcast_error data: data, error: "#{exception} #{exception.backtrace.first.split(":in ")[0] if Rails.env.development?}"
29
- else
30
- unless exception.is_a?(StimulusReflex::Reflex::VersionMismatchError)
31
- StimulusReflex.config.logger.error error_message
32
- end
20
+ if reflex
21
+ error_message = "\e[31mReflex #{reflex.data.target} failed: #{error[:message]} [#{reflex.url}]\e[0m\n#{error[:stack]}"
22
+ reflex.rescue_with_handler(exception)
23
+ reflex.logger&.error error_message
24
+ reflex.broadcast_error data: data, error: "#{exception} #{exception.backtrace.first.split(":in ")[0] if Rails.env.development?}"
25
+ else
26
+ error_message = "\e[31mReflex failed: #{error[:message]} \e[0m\n#{error[:stack]}"
27
+ unless exception.is_a?(StimulusReflex::VersionMismatchError)
28
+ StimulusReflex.config.logger.error error_message
29
+ end
33
30
 
34
- if error_message.to_s.include? "No route matches"
35
- initializer_path = Rails.root.join("config", "initializers", "stimulus_reflex.rb")
31
+ if error_message.to_s.include? "No route matches"
32
+ initializer_path = Rails.root.join("config", "initializers", "stimulus_reflex.rb")
36
33
 
37
- StimulusReflex.config.logger.warn <<~NOTE
38
- \e[33mNOTE: StimulusReflex failed to locate a matching route and could not re-render the page.
34
+ StimulusReflex.config.logger.warn <<~NOTE
35
+ \e[33mNOTE: StimulusReflex failed to locate a matching route and could not re-render the page.
39
36
 
40
- If your app uses Rack middleware to rewrite part of the request path, you must enable those middleware modules in StimulusReflex.
41
- The StimulusReflex initializer should be located at #{initializer_path}, or you can generate it with:
37
+ If your app uses Rack middleware to rewrite part of the request path, you must enable those middleware modules in StimulusReflex.
38
+ The StimulusReflex initializer should be located at #{initializer_path}, or you can generate it with:
42
39
 
43
- $ bundle exec rails generate stimulus_reflex:config
40
+ $ bundle exec rails generate stimulus_reflex:config
44
41
 
45
- Configure any required middleware:
42
+ Configure any required middleware:
46
43
 
47
- StimulusReflex.configure do |config|
48
- config.middleware.use FirstRackMiddleware
49
- config.middleware.use SecondRackMiddleware
50
- end\e[0m
44
+ StimulusReflex.configure do |config|
45
+ config.middleware.use FirstRackMiddleware
46
+ config.middleware.use SecondRackMiddleware
47
+ end\e[0m
51
48
 
52
- NOTE
53
- end
49
+ NOTE
54
50
  end
55
- return
56
51
  end
52
+ return
53
+ end
57
54
 
58
- if reflex.halted?
59
- reflex.broadcast_halt data: data
60
- elsif reflex.forbidden?
61
- reflex.broadcast_forbid data: data
62
- else
63
- begin
64
- reflex.broadcast(reflex_data.selectors, data)
65
- rescue => exception
66
- reflex.rescue_with_handler(exception)
67
- error = exception_with_backtrace(exception)
68
- reflex.broadcast_error data: data, error: "#{exception} #{exception.backtrace.first.split(":in ")[0] if Rails.env.development?}"
69
- reflex.logger&.error "\e[31mReflex failed to re-render: #{error[:message]} [#{reflex_data.url}]\e[0m\n#{error[:stack]}"
70
- end
71
- end
72
- ensure
73
- if reflex
74
- commit_session(reflex)
75
- report_failed_basic_auth(reflex) if reflex.controller?
76
- reflex.logger&.log_all_operations
55
+ if reflex.halted?
56
+ reflex.broadcast_halt data: data
57
+ elsif reflex.forbidden?
58
+ reflex.broadcast_forbid data: data
59
+ else
60
+ begin
61
+ reflex.broadcast(reflex.selectors, data)
62
+ rescue => exception
63
+ reflex.rescue_with_handler(exception)
64
+ error = exception_with_backtrace(exception)
65
+ reflex.broadcast_error data: data, error: "#{exception} #{exception.backtrace.first.split(":in ")[0] if Rails.env.development?}"
66
+ reflex.logger&.error "\e[31mReflex failed to re-render: #{error[:message]} [#{reflex.url}]\e[0m\n#{error[:stack]}"
77
67
  end
78
68
  end
69
+ ensure
70
+ if reflex
71
+ commit_session(reflex)
72
+ report_failed_basic_auth(reflex) if reflex.controller?
73
+ reflex.logger&.log_all_operations
74
+ end
79
75
  end
80
76
 
81
77
  private
82
78
 
83
79
  def delegate_call_to_reflex(reflex)
84
- method_name = reflex_data.method_name
85
- arguments = reflex_data.arguments
80
+ method_name = reflex.method_name
81
+ arguments = reflex.data.arguments
86
82
  method = reflex.method(method_name)
87
83
 
88
84
  policy = StimulusReflex::ReflexMethodInvocationPolicy.new(method, arguments)
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "rails/generators"
4
4
  require "stimulus_reflex/version"
5
+ require "stimulus_reflex/installer"
5
6
 
6
7
  class StimulusReflexGenerator < Rails::Generators::NamedBase
7
8
  source_root File.expand_path("templates", __dir__)
@@ -13,33 +14,20 @@ class StimulusReflexGenerator < Rails::Generators::NamedBase
13
14
  def execute
14
15
  actions.map!(&:underscore)
15
16
 
16
- cached_entrypoint = Rails.root.join("tmp/stimulus_reflex_installer/entrypoint")
17
- if cached_entrypoint.exist?
18
- entrypoint = File.read(cached_entrypoint)
19
- else
20
- entrypoint = [
21
- Rails.root.join("app/javascript"),
22
- Rails.root.join("app/frontend")
23
- ].find(&:exist?) || "app/javascript"
24
- puts "Where do JavaScript files live in your app? Our best guess is: \e[1#{entrypoint}\e[22m 🤔"
25
- puts "Press enter to accept this, or type a different path."
26
- print "> "
27
- input = Rails.env.test? ? "tmp/app/javascript" : $stdin.gets.chomp
28
- entrypoint = input unless input.blank?
29
- end
17
+ reflex_entrypoint = Rails.env.test? ? "tmp/app/reflexes" : "app/reflexes"
18
+ reflex_src = "app/reflexes/%file_name%_reflex.rb.tt"
19
+ reflex_path = Rails.root.join(reflex_entrypoint, "#{file_name}_reflex.rb")
20
+
21
+ template(reflex_src, reflex_path) unless options[:skip_reflex]
30
22
 
31
23
  if !options[:skip_stimulus] && entrypoint.blank?
32
24
  puts "❌ You must specify a valid JavaScript entrypoint."
33
25
  exit
34
26
  end
35
27
 
36
- reflex_entrypoint = Rails.env.test? ? "tmp/app/reflexes" : "app/reflexes"
37
- reflex_src = "app/reflexes/%file_name%_reflex.rb.tt"
38
- reflex_path = Rails.root.join(reflex_entrypoint, "#{file_name}_reflex.rb")
39
28
  stimulus_controller_src = "app/javascript/controllers/%file_name%_controller.js.tt"
40
29
  stimulus_controller_path = Rails.root.join(entrypoint, "controllers/#{file_name}_controller.js")
41
30
 
42
- template(reflex_src, reflex_path) unless options[:skip_reflex]
43
31
  template(stimulus_controller_src, stimulus_controller_path) unless options[:skip_stimulus]
44
32
 
45
33
  if file_name == "example"
@@ -1,4 +1,8 @@
1
- import consumer from '../channels/consumer'
2
- import CableReady from 'cable_ready'
1
+ <%- if bundler.importmap? -%>
2
+ import consumer from "channels/consumer"
3
+ <%- else -%>
4
+ import consumer from "../channels/consumer"
5
+ <%- end -%>
6
+ import CableReady from "cable_ready"
3
7
 
4
8
  CableReady.initialize({ consumer })
@@ -1,2 +1,7 @@
1
- import './cable_ready'
2
- import './stimulus_reflex'
1
+ <%- if bundler.importmap? -%>
2
+ import "config/cable_ready"
3
+ import "config/stimulus_reflex"
4
+ <%- else -%>
5
+ import "./cable_ready"
6
+ import "./stimulus_reflex"
7
+ <%- end -%>
@@ -1,5 +1,10 @@
1
+ <%- if bundler.importmap? -%>
2
+ import { application } from "controllers/application"
3
+ import controller from "controllers/application_controller"
4
+ <%- else -%>
1
5
  import { application } from "../controllers/application"
2
6
  import controller from "../controllers/application_controller"
7
+ <%- end -%>
3
8
  import StimulusReflex from "stimulus_reflex"
4
9
 
5
10
  StimulusReflex.initialize(application, { controller, isolate: true })
@@ -1,7 +1,11 @@
1
- import ApplicationController from './application_controller'
1
+ <%- if bundler.importmap? -%>
2
+ import ApplicationController from "controllers/application_controller"
3
+ <%- else -%>
4
+ import ApplicationController from "./application_controller"
5
+ <%- end -%>
2
6
 
3
7
  <%- if class_name == "Example" -%>
4
- //
8
+ //
5
9
  // This is the Stimulus controller for the Example Reflex.
6
10
  //
7
11
  // It corresponds to the server-side Reflex class located at /app/reflexes/example.rb
@@ -1,11 +1,15 @@
1
1
  import { Application } from "@hotwired/stimulus"
2
+ <%- if bundler.importmap? %>
3
+ import consumer from "channels/consumer"
4
+ <%- else %>
2
5
  import consumer from "../channels/consumer"
6
+ <%- end %>
3
7
 
4
8
  const application = Application.start()
5
9
 
6
10
  // Configure Stimulus development experience
7
11
  application.debug = false
8
12
  application.consumer = consumer
9
- window.Stimulus = application
13
+ window.Stimulus = application
10
14
 
11
15
  export { application }
@@ -1,5 +1,11 @@
1
- import { application } from "./application"
1
+ // Import and register all your controllers from the importmap under controllers/*
2
+
3
+ import { application } from "controllers/application"
2
4
 
3
5
  // Eager load all controllers defined in the import map under controllers/**/*_controller
4
6
  import { eagerLoadControllersFrom } from "@hotwired/stimulus-loading"
5
7
  eagerLoadControllersFrom("controllers", application)
8
+
9
+ // Lazy load controllers as they appear in the DOM (remember not to preload controllers in import map!)
10
+ // import { lazyLoadControllersFrom } from "@hotwired/stimulus-loading"
11
+ // lazyLoadControllersFrom("controllers", application)
@@ -20,7 +20,7 @@ if index_path.exist?
20
20
  say "⏩ #{friendly_index_path} already exists. Skipping"
21
21
  else
22
22
  backup(index_path, delete: true) do
23
- copy_file(index_src, index_path)
23
+ template(index_src, index_path)
24
24
  end
25
25
  say "✅ Created #{friendly_index_path}"
26
26
  end
@@ -50,22 +50,22 @@ else
50
50
  end
51
51
 
52
52
  # create entrypoint/config/cable_ready.js and make sure it's imported in application.js
53
- copy_file(cable_ready_src, cable_ready_path) unless cable_ready_path.exist?
53
+ template(cable_ready_src, cable_ready_path) unless cable_ready_path.exist?
54
54
 
55
55
  # create entrypoint/config/stimulus_reflex.js and make sure it's imported in application.js
56
- copy_file(stimulus_reflex_src, stimulus_reflex_path) unless stimulus_reflex_path.exist?
56
+ template(stimulus_reflex_src, stimulus_reflex_path) unless stimulus_reflex_path.exist?
57
57
 
58
58
  if stimulus_reflex_path.read.include?("StimulusReflex.debug =")
59
59
  say "⏩ Development environment options are already set in #{friendly_stimulus_reflex_path}. Skipping"
60
60
  else
61
- if ["webpacker", "shakapacker"].include?(bundler)
61
+ if bundler.webpacker? || bundler.shakapacker?
62
62
  append_file(stimulus_reflex_path, <<~JS, verbose: false)
63
63
 
64
64
  if (process.env.RAILS_ENV === 'development') {
65
65
  StimulusReflex.debug = true
66
66
  }
67
67
  JS
68
- elsif bundler == "vite"
68
+ elsif bundler.vite?
69
69
  append_file(stimulus_reflex_path, <<~JS, verbose: false) unless stimulus_reflex_path.read.include?("StimulusReflex.debug")
70
70
 
71
71
  if (import.meta.env.MODE === "development") {
@@ -5,7 +5,7 @@ require "stimulus_reflex/installer"
5
5
  return if pack_path_missing?
6
6
 
7
7
  # verify that all critical dependencies are up to date; if not, queue for later
8
- lines = package_json.readlines
8
+ lines = package_json_path.readlines
9
9
 
10
10
  if !lines.index { |line| line =~ /^\s*["']esbuild-rails["']: ["']\^1.0.3["']/ }
11
11
  add_package "esbuild-rails@^1.0.3"
@@ -104,12 +104,12 @@ backup(application_js_path) do
104
104
  if application_js_path.read.include?("import consumer")
105
105
  say "⏩ #{friendly_application_js_path} is present. Skipping."
106
106
  else
107
- inject_into_file application_js_path, "import consumer from \"../channels/consumer\"\n", after: "import { Application } from \"@hotwired/stimulus\"\n", verbose: false
107
+ inject_into_file application_js_path, "import consumer from \"channels/consumer\"\n", after: "import { Application } from \"@hotwired/stimulus\"\n", verbose: false
108
108
  inject_into_file application_js_path, "application.consumer = consumer\n", after: "application.debug = false\n", verbose: false
109
109
  say "✅ #{friendly_application_js_path} has been updated to import the Action Cable consumer"
110
110
  end
111
111
  else
112
- copy_file(application_js_src, application_js_path)
112
+ template(application_js_src, application_js_path)
113
113
  say "✅ #{friendly_application_js_path} has been created"
114
114
  end
115
115
  end
data/lib/install/mrujs.rb CHANGED
@@ -17,7 +17,7 @@ if !File.exist?(mrujs_path)
17
17
  end
18
18
 
19
19
  if proceed
20
- if bundler == "importmap"
20
+ if bundler.importmap?
21
21
 
22
22
  if !importmap_path.exist?
23
23
  halt "#{friendly_importmap_path} is missing. You need a valid importmap config file to proceed."
@@ -45,14 +45,14 @@ if proceed
45
45
  end
46
46
  else
47
47
  # queue mrujs for installation
48
- if package_json.read.include?('"mrujs":')
48
+ if package_json_path.read.include?('"mrujs":')
49
49
  say "⏩ mrujs already present. Skipping."
50
50
  else
51
51
  add_package "mrujs@^0.10.1"
52
52
  end
53
53
 
54
54
  # queue @rails/ujs for removal
55
- if package_json.read.include?('"@rails/ujs":')
55
+ if package_json_path.read.include?('"@rails/ujs":')
56
56
  drop_package "@rails/ujs"
57
57
  else
58
58
  say "⏩ @rails/ujs not present. Skipping."
@@ -70,10 +70,15 @@ if proceed
70
70
  index = index_path.read
71
71
  friendly_index_path = index_path.relative_path_from(Rails.root).to_s
72
72
  mrujs_pattern = /import ['"].\/mrujs['"]/
73
- mrujs_import = "import '.\/mrujs'\n" # standard:disable Style/RedundantStringEscape
73
+
74
+ mrujs_import = if bundler.importmap?
75
+ %(import "config/mrujs"\n)
76
+ else
77
+ %(import "./mrujs"\n)
78
+ end
74
79
 
75
80
  if index.match?(mrujs_pattern)
76
- say "⏩ mrujs alredy imported in #{friendly_index_path}. Skipping."
81
+ say "⏩ mrujs already imported in #{friendly_index_path}. Skipping."
77
82
  else
78
83
  append_file(index_path, mrujs_import, verbose: false)
79
84
  say "✅ mrujs imported in #{friendly_index_path}"
@@ -2,7 +2,7 @@
2
2
 
3
3
  require "stimulus_reflex/installer"
4
4
 
5
- lines = package_json.readlines
5
+ lines = package_json_path.readlines
6
6
 
7
7
  if !lines.index { |line| line =~ /^\s*["']cable_ready["']: ["'].*#{cr_npm_version}["']/ }
8
8
  add_package "cable_ready@#{cr_npm_version}"
@@ -5,7 +5,7 @@ require "stimulus_reflex/installer"
5
5
  return if pack_path_missing?
6
6
 
7
7
  # verify that all critical dependencies are up to date; if not, queue for later
8
- lines = package_json.readlines
8
+ lines = package_json_path.readlines
9
9
 
10
10
  if !lines.index { |line| line =~ /^\s*["']@hotwired\/stimulus-webpack-helpers["']: ["']\^1.0.1["']/ }
11
11
  add_package "@hotwired/stimulus-webpack-helpers@^1.0.1"
data/lib/install/vite.rb CHANGED
@@ -5,7 +5,7 @@ require "stimulus_reflex/installer"
5
5
  return if pack_path_missing?
6
6
 
7
7
  # verify that all critical dependencies are up to date; if not, queue for later
8
- lines = package_json.readlines
8
+ lines = package_json_path.readlines
9
9
 
10
10
  if !lines.index { |line| line =~ /^\s*["']stimulus-vite-helpers["']: ["']\^3["']/ }
11
11
  add_package "stimulus-vite-helpers@^3"
@@ -5,7 +5,7 @@ require "stimulus_reflex/installer"
5
5
  return if pack_path_missing?
6
6
 
7
7
  # verify that all critical dependencies are up to date; if not, queue for later
8
- lines = package_json.readlines
8
+ lines = package_json_path.readlines
9
9
  if !lines.index { |line| line =~ /^\s*["']webpack["']: ["']\^4.46.0["']/ }
10
10
  add_package "webpack@^4.46.0"
11
11
  else