stimulus_reflex 3.5.0.pre9 → 3.5.0.rc1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of stimulus_reflex might be problematic. Click here for more details.

Files changed (104) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +0 -1
  3. data/Gemfile.lock +122 -127
  4. data/README.md +13 -19
  5. data/app/assets/javascripts/stimulus_reflex.js +1017 -523
  6. data/app/assets/javascripts/stimulus_reflex.umd.js +940 -496
  7. data/app/channels/stimulus_reflex/channel.rb +9 -24
  8. data/bin/console +0 -2
  9. data/bin/standardize +2 -1
  10. data/lib/generators/stimulus_reflex/stimulus_reflex_generator.rb +68 -9
  11. data/lib/generators/stimulus_reflex/templates/app/controllers/examples_controller.rb.tt +9 -0
  12. data/lib/generators/stimulus_reflex/templates/app/javascript/channels/consumer.js.tt +6 -0
  13. data/lib/generators/stimulus_reflex/templates/app/javascript/channels/index.js.esbuild.tt +4 -0
  14. data/lib/generators/stimulus_reflex/templates/app/javascript/channels/index.js.importmap.tt +2 -0
  15. data/lib/generators/stimulus_reflex/templates/app/javascript/channels/index.js.shakapacker.tt +5 -0
  16. data/lib/generators/stimulus_reflex/templates/app/javascript/channels/index.js.vite.tt +1 -0
  17. data/lib/generators/stimulus_reflex/templates/app/javascript/channels/index.js.webpacker.tt +5 -0
  18. data/lib/generators/stimulus_reflex/templates/app/javascript/config/cable_ready.js.tt +4 -0
  19. data/lib/generators/stimulus_reflex/templates/app/javascript/config/index.js.tt +2 -0
  20. data/lib/generators/stimulus_reflex/templates/app/javascript/config/mrujs.js.tt +9 -0
  21. data/lib/generators/stimulus_reflex/templates/app/javascript/config/stimulus_reflex.js.tt +5 -0
  22. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/%file_name%_controller.js.tt +141 -0
  23. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/application.js.tt +11 -0
  24. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/application_controller.js.tt +74 -0
  25. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/index.js.esbuild.tt +7 -0
  26. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/index.js.importmap.tt +5 -0
  27. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/index.js.shakapacker.tt +5 -0
  28. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/index.js.vite.tt +5 -0
  29. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/index.js.webpacker.tt +5 -0
  30. data/{test/tmp/app/reflexes/user_reflex.rb → lib/generators/stimulus_reflex/templates/app/reflexes/%file_name%_reflex.rb.tt} +38 -9
  31. data/lib/generators/stimulus_reflex/templates/app/reflexes/application_reflex.rb.tt +27 -0
  32. data/lib/generators/stimulus_reflex/templates/app/views/examples/show.html.erb.tt +207 -0
  33. data/lib/generators/stimulus_reflex/templates/config/initializers/cable_ready.rb +27 -0
  34. data/lib/generators/stimulus_reflex/templates/config/initializers/stimulus_reflex.rb +18 -13
  35. data/lib/generators/stimulus_reflex/templates/esbuild.config.mjs.tt +94 -0
  36. data/lib/install/action_cable.rb +155 -0
  37. data/lib/install/broadcaster.rb +90 -0
  38. data/lib/install/bundle.rb +56 -0
  39. data/lib/install/compression.rb +41 -0
  40. data/lib/install/config.rb +87 -0
  41. data/lib/install/development.rb +110 -0
  42. data/lib/install/esbuild.rb +114 -0
  43. data/lib/install/example.rb +22 -0
  44. data/lib/install/importmap.rb +133 -0
  45. data/lib/install/initializers.rb +25 -0
  46. data/lib/install/mrujs.rb +133 -0
  47. data/lib/install/npm_packages.rb +25 -0
  48. data/lib/install/reflexes.rb +25 -0
  49. data/lib/install/shakapacker.rb +64 -0
  50. data/lib/install/spring.rb +54 -0
  51. data/lib/install/updatable.rb +34 -0
  52. data/lib/install/vite.rb +64 -0
  53. data/lib/install/webpacker.rb +90 -0
  54. data/lib/install/yarn.rb +55 -0
  55. data/lib/stimulus_reflex/broadcasters/broadcaster.rb +15 -8
  56. data/lib/stimulus_reflex/broadcasters/page_broadcaster.rb +7 -8
  57. data/lib/stimulus_reflex/broadcasters/selector_broadcaster.rb +10 -10
  58. data/lib/stimulus_reflex/broadcasters/update.rb +3 -0
  59. data/lib/stimulus_reflex/cable_readiness.rb +29 -0
  60. data/lib/stimulus_reflex/cable_ready_channels.rb +6 -5
  61. data/lib/stimulus_reflex/callbacks.rb +17 -1
  62. data/lib/stimulus_reflex/concern_enhancer.rb +6 -4
  63. data/lib/stimulus_reflex/configuration.rb +12 -2
  64. data/lib/stimulus_reflex/dataset.rb +11 -1
  65. data/lib/stimulus_reflex/engine.rb +16 -9
  66. data/lib/stimulus_reflex/html/document.rb +59 -0
  67. data/lib/stimulus_reflex/html/document_fragment.rb +13 -0
  68. data/lib/stimulus_reflex/importmap.rb +6 -3
  69. data/lib/stimulus_reflex/installer.rb +274 -0
  70. data/lib/stimulus_reflex/open_struct_fix.rb +2 -0
  71. data/lib/stimulus_reflex/reflex.rb +40 -31
  72. data/lib/stimulus_reflex/reflex_data.rb +19 -3
  73. data/lib/stimulus_reflex/reflex_factory.rb +6 -3
  74. data/lib/stimulus_reflex/request_parameters.rb +2 -0
  75. data/lib/stimulus_reflex/utils/logger.rb +10 -0
  76. data/lib/stimulus_reflex/utils/sanity_checker.rb +8 -48
  77. data/lib/stimulus_reflex/version.rb +1 -1
  78. data/lib/stimulus_reflex/version_checker.rb +54 -0
  79. data/lib/stimulus_reflex.rb +2 -0
  80. data/lib/tasks/stimulus_reflex/stimulus_reflex.rake +250 -0
  81. data/package.json +36 -28
  82. data/{rollup.config.js → rollup.config.mjs} +6 -24
  83. data/stimulus_reflex.gemspec +16 -19
  84. data/yarn.lock +1331 -748
  85. metadata +129 -79
  86. data/LATEST +0 -1
  87. data/app/assets/javascripts/stimulus_reflex.min.js +0 -2
  88. data/app/assets/javascripts/stimulus_reflex.min.js.map +0 -1
  89. data/app/assets/javascripts/stimulus_reflex.umd.min.js +0 -905
  90. data/app/assets/javascripts/stimulus_reflex.umd.min.js.map +0 -1
  91. data/lib/generators/stimulus_reflex/initializer_generator.rb +0 -14
  92. data/test/broadcasters/broadcaster_test.rb +0 -11
  93. data/test/broadcasters/broadcaster_test_case.rb +0 -39
  94. data/test/broadcasters/nothing_broadcaster_test.rb +0 -31
  95. data/test/broadcasters/page_broadcaster_test.rb +0 -79
  96. data/test/broadcasters/selector_broadcaster_test.rb +0 -173
  97. data/test/callbacks_test.rb +0 -652
  98. data/test/concern_enhancer_test.rb +0 -54
  99. data/test/element_test.rb +0 -254
  100. data/test/generators/stimulus_reflex_generator_test.rb +0 -58
  101. data/test/reflex_test.rb +0 -43
  102. data/test/test_helper.rb +0 -71
  103. data/test/tmp/app/reflexes/application_reflex.rb +0 -12
  104. data/yarn-error.log +0 -4964
@@ -0,0 +1,133 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stimulus_reflex/installer"
4
+
5
+ return if pack_path_missing?
6
+
7
+ mrujs_path = config_path / "mrujs.js"
8
+
9
+ proceed = false
10
+
11
+ if !File.exist?(mrujs_path)
12
+ proceed = if options.key? "mrujs"
13
+ options["mrujs"]
14
+ else
15
+ !no?("✨ Would you like to install and enable mrujs? It's a modern, drop-in replacement for rails-ujs (Y/n)")
16
+ end
17
+ end
18
+
19
+ if proceed
20
+ if bundler == "importmap"
21
+
22
+ if !importmap_path.exist?
23
+ halt "#{friendly_importmap_path} is missing. You need a valid importmap config file to proceed."
24
+ return
25
+ end
26
+
27
+ importmap = importmap_path.read
28
+
29
+ if importmap.include?("pin \"mrujs\"")
30
+ say "⏩ mrujs already pinned. Skipping."
31
+ else
32
+ append_file(importmap_path, <<~RUBY, verbose: false)
33
+ pin "mrujs", to: "https://ga.jspm.io/npm:mrujs@0.10.1/dist/index.module.js"
34
+ RUBY
35
+ say "✅ pin mrujs"
36
+ end
37
+
38
+ if importmap.include?("pin \"mrujs/plugins\"")
39
+ say "⏩ mrujs/plugins already pinned. Skipping."
40
+ else
41
+ append_file(importmap_path, <<~RUBY, verbose: false)
42
+ pin "mrujs/plugins", to: "https://ga.jspm.io/npm:mrujs@0.10.1/plugins/dist/plugins.module.js"
43
+ RUBY
44
+ say "✅ pin mrujs/plugins"
45
+ end
46
+ else
47
+ # queue mrujs for installation
48
+ if package_json.read.include?('"mrujs":')
49
+ say "⏩ mrujs already present. Skipping."
50
+ else
51
+ add_package "mrujs@^0.10.1"
52
+ end
53
+
54
+ # queue @rails/ujs for removal
55
+ if package_json.read.include?('"@rails/ujs":')
56
+ drop_package "@rails/ujs"
57
+ else
58
+ say "⏩ @rails/ujs not present. Skipping."
59
+ end
60
+ end
61
+
62
+ step_path = "/app/javascript/config/"
63
+ mrujs_src = fetch(step_path, "mrujs.js.tt")
64
+
65
+ # create entrypoint/config/mrujs.js if necessary
66
+ copy_file(mrujs_src, mrujs_path) unless mrujs_path.exist?
67
+
68
+ # import mrujs config in entrypoint/config/index.js
69
+ index_path = config_path / "index.js"
70
+ index = index_path.read
71
+ friendly_index_path = index_path.relative_path_from(Rails.root).to_s
72
+ mrujs_pattern = /import ['"].\/mrujs['"]/
73
+ mrujs_import = "import '.\/mrujs'\n" # standard:disable Style/RedundantStringEscape
74
+
75
+ if index.match?(mrujs_pattern)
76
+ say "⏩ mrujs alredy imported in #{friendly_index_path}. Skipping."
77
+ else
78
+ append_file(index_path, mrujs_import, verbose: false)
79
+ say "✅ mrujs imported in #{friendly_index_path}"
80
+ end
81
+
82
+ # remove @rails/ujs from application.js
83
+ rails_ujs_pattern = /import Rails from ['"]@rails\/ujs['"]/
84
+
85
+ lines = pack_path.readlines
86
+ if lines.index { |line| line =~ rails_ujs_pattern }
87
+ gsub_file pack_path, rails_ujs_pattern, "", verbose: false
88
+ say "✅ @rails/ujs removed from #{friendly_pack_path}"
89
+ else
90
+ say "⏩ @rails/ujs not present in #{friendly_pack_path}. Skipping."
91
+ end
92
+
93
+ # set Action View to generate remote forms when using form_with
94
+ application_path = Rails.root.join("config/application.rb")
95
+ application_pattern = /^[^#]*config\.action_view\.form_with_generates_remote_forms = true/
96
+ defaults_pattern = /config\.load_defaults \d\.\d/
97
+
98
+ lines = application_path.readlines
99
+ backup(application_path) do
100
+ if !lines.index { |line| line =~ application_pattern }
101
+ if (index = lines.index { |line| line =~ /^[^#]*#{defaults_pattern}/ })
102
+ gsub_file application_path, /\s*#{defaults_pattern}\n/, verbose: false do
103
+ <<-RUBY
104
+ \n#{lines[index]}
105
+ # form_with helper will generate remote forms by default (mrujs)
106
+ config.action_view.form_with_generates_remote_forms = true
107
+ RUBY
108
+ end
109
+ else
110
+ insert_into_file application_path, after: "class Application < Rails::Application" do
111
+ <<-RUBY
112
+
113
+ # form_with helper will generate remote forms by default (mrujs)
114
+ config.action_view.form_with_generates_remote_forms = true
115
+ RUBY
116
+ end
117
+ end
118
+ end
119
+ say "✅ form_with_generates_remote_forms set to true in config/application.rb"
120
+ end
121
+
122
+ # remove turbolinks from Gemfile because it's incompatible with mrujs (and unnecessary)
123
+ turbolinks_pattern = /^[^#]*gem ["']turbolinks["']/
124
+
125
+ lines = gemfile_path.readlines
126
+ if lines.index { |line| line =~ turbolinks_pattern }
127
+ remove_gem :turbolinks
128
+ else
129
+ say "⏩ turbolinks is not present in Gemfile. Skipping."
130
+ end
131
+ end
132
+
133
+ complete_step :mrujs
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stimulus_reflex/installer"
4
+
5
+ lines = package_json.readlines
6
+
7
+ if !lines.index { |line| line =~ /^\s*["']cable_ready["']: ["'].*#{cr_npm_version}["']/ }
8
+ add_package "cable_ready@#{cr_npm_version}"
9
+ else
10
+ say "⏩ cable_ready npm package is already present. Skipping."
11
+ end
12
+
13
+ if !lines.index { |line| line =~ /^\s*["']stimulus_reflex["']: ["'].*#{sr_npm_version}["']/ }
14
+ add_package "stimulus_reflex@#{sr_npm_version}"
15
+ else
16
+ say "⏩ stimulus_reflex npm package is already present. Skipping."
17
+ end
18
+
19
+ if !lines.index { |line| line =~ /^\s*["']@hotwired\/stimulus["']:/ }
20
+ add_package "@hotwired/stimulus@^3.2"
21
+ else
22
+ say "⏩ @hotwired/stimulus npm package is already present. Skipping."
23
+ end
24
+
25
+ complete_step :npm_packages
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stimulus_reflex/installer"
4
+
5
+ reflexes_path = Rails.root.join("app/reflexes")
6
+ step_path = "/app/reflexes/"
7
+ application_reflex_path = reflexes_path / "application_reflex.rb"
8
+ application_reflex_src = fetch(step_path, "application_reflex.rb.tt")
9
+
10
+ # verify app/reflexes exists and create if necessary
11
+ if reflexes_path.exist?
12
+ say "⏩ app/reflexes directory already exists. Skipping."
13
+ else
14
+ empty_directory reflexes_path
15
+ say "✅ Created app/reflexes directory"
16
+ end
17
+
18
+ if application_reflex_path.exist?
19
+ say "⏩ app/reflexes/application_reflex.rb is alredy present. Skipping."
20
+ else
21
+ copy_file application_reflex_src, application_reflex_path
22
+ say "✅ Created app/reflexes/application_reflex.rb"
23
+ end
24
+
25
+ complete_step :reflexes
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stimulus_reflex/installer"
4
+
5
+ return if pack_path_missing?
6
+
7
+ # verify that all critical dependencies are up to date; if not, queue for later
8
+ lines = package_json.readlines
9
+
10
+ if !lines.index { |line| line =~ /^\s*["']@hotwired\/stimulus-webpack-helpers["']: ["']\^1.0.1["']/ }
11
+ add_package "@hotwired/stimulus-webpack-helpers@^1.0.1"
12
+ else
13
+ say "⏩ @hotwired/stimulus-webpack-helpers npm package is already present. Skipping."
14
+ end
15
+
16
+ step_path = "/app/javascript/controllers/"
17
+ # controller_templates_path = File.expand_path(template_src + "/app/javascript/controllers", File.join(File.dirname(__FILE__)))
18
+ application_controller_src = fetch(step_path, "application_controller.js.tt")
19
+ application_controller_path = controllers_path / "application_controller.js"
20
+ application_js_src = fetch(step_path, "application.js.tt")
21
+ application_js_path = controllers_path / "application.js"
22
+ index_src = fetch(step_path, "index.js.shakapacker.tt")
23
+ index_path = controllers_path / "index.js"
24
+
25
+ # create entrypoint/controllers, as well as the index, application and application_controller
26
+ empty_directory controllers_path unless controllers_path.exist?
27
+
28
+ copy_file(application_controller_src, application_controller_path) unless application_controller_path.exist?
29
+ copy_file(application_js_src, application_js_path) unless application_js_path.exist?
30
+ copy_file(index_src, index_path) unless index_path.exist?
31
+
32
+ controllers_pattern = /import ['"]controllers['"]/
33
+ controllers_commented_pattern = /\s*\/\/\s*#{controllers_pattern}/
34
+
35
+ if pack.match?(controllers_pattern)
36
+ if pack.match?(controllers_commented_pattern)
37
+ proceed = if options.key? "uncomment"
38
+ options["uncomment"]
39
+ else
40
+ !no?("✨ Do you want to import your Stimulus controllers in application.js? (Y/n)")
41
+ end
42
+
43
+ if proceed
44
+ # uncomment_lines only works with Ruby comments 🙄
45
+ lines = pack_path.readlines
46
+ matches = lines.select { |line| line =~ controllers_commented_pattern }
47
+ lines[lines.index(matches.last).to_i] = "import \"controllers\"\n"
48
+ pack_path.write lines.join
49
+ say "✅ Stimulus controllers imported in #{friendly_pack_path}"
50
+ else
51
+ say "🤷 your Stimulus controllers are not being imported in your application.js. We trust that you have a reason for this."
52
+ end
53
+ else
54
+ say "✅ Stimulus controllers imported in #{friendly_pack_path}"
55
+ end
56
+ else
57
+ lines = pack_path.readlines
58
+ matches = lines.select { |line| line =~ /^import / }
59
+ lines.insert lines.index(matches.last).to_i + 1, "import \"controllers\"\n"
60
+ pack_path.write lines.join
61
+ say "✅ Stimulus controllers imported in #{friendly_pack_path}"
62
+ end
63
+
64
+ complete_step :shakapacker
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stimulus_reflex/installer"
4
+
5
+ spring_pattern = /^[^#]*gem ["']spring["']/
6
+
7
+ proceed = false
8
+ lines = gemfile_path.readlines
9
+
10
+ if lines.index { |line| line =~ spring_pattern }
11
+ proceed = if options.key? "spring"
12
+ options["spring"]
13
+ else
14
+ !no?("✨ Would you like to disable the spring gem? \nIt's been removed from Rails 7, and is the frequent culprit behind countless mystery bugs. (Y/n)")
15
+ end
16
+ else
17
+ say "⏩ Spring is not installed."
18
+ end
19
+
20
+ if proceed
21
+ spring_watcher_pattern = /^[^#]*gem ["']spring-watcher-listen["']/
22
+ bin_rails_pattern = /^[^#]*load File.expand_path\("spring", __dir__\)/
23
+
24
+ if (index = lines.index { |line| line =~ spring_pattern })
25
+ remove_gem :spring
26
+
27
+ bin_spring = Rails.root.join("bin/spring")
28
+ if bin_spring.exist?
29
+ run "bin/spring binstub --remove --all"
30
+ say "✅ Removed spring binstubs"
31
+ end
32
+
33
+ bin_rails = Rails.root.join("bin/rails")
34
+ bin_rails_content = bin_rails.readlines
35
+ if (index = bin_rails_content.index { |line| line =~ bin_rails_pattern })
36
+ backup(bin_rails) do
37
+ bin_rails_content[index] = "# #{bin_rails_content[index]}"
38
+ bin_rails.write bin_rails_content.join
39
+ end
40
+ say "✅ Removed spring from bin/rails"
41
+ end
42
+ create_file "tmp/stimulus_reflex_installer/kill_spring", verbose: false
43
+ else
44
+ say "✅ spring has been successfully removed"
45
+ end
46
+
47
+ if lines.index { |line| line =~ spring_watcher_pattern }
48
+ remove_gem "spring-watcher-listen"
49
+ end
50
+ else
51
+ say "⏩ Skipping."
52
+ end
53
+
54
+ complete_step :spring
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stimulus_reflex/installer"
4
+
5
+ if application_record_path.exist?
6
+ lines = application_record_path.readlines
7
+
8
+ if !lines.index { |line| line =~ /^\s*include CableReady::Updatable/ }
9
+ proceed = if options.key? "updatable"
10
+ options["updatable"]
11
+ else
12
+ !no?("✨ Include CableReady::Updatable in Active Record model classes? (Y/n)")
13
+ end
14
+
15
+ unless proceed
16
+ complete_step :updatable
17
+
18
+ puts "⏩ Skipping."
19
+ return
20
+ end
21
+
22
+ index = lines.index { |line| line.include?("class ApplicationRecord < ActiveRecord::Base") }
23
+ lines.insert index + 1, " include CableReady::Updatable\n"
24
+ application_record_path.write lines.join
25
+
26
+ say "✅ included CableReady::Updatable in ApplicationRecord"
27
+ else
28
+ say "⏩ CableReady::Updatable has already been included in Active Record model classes. Skipping."
29
+ end
30
+ else
31
+ say "⏩ ApplicationRecord doesn't exist. Skipping."
32
+ end
33
+
34
+ complete_step :updatable
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stimulus_reflex/installer"
4
+
5
+ return if pack_path_missing?
6
+
7
+ # verify that all critical dependencies are up to date; if not, queue for later
8
+ lines = package_json.readlines
9
+
10
+ if !lines.index { |line| line =~ /^\s*["']stimulus-vite-helpers["']: ["']\^3["']/ }
11
+ add_package "stimulus-vite-helpers@^3"
12
+ else
13
+ say "⏩ @stimulus-vite-helpers npm package is already present. Skipping."
14
+ end
15
+
16
+ step_path = "/app/javascript/controllers/"
17
+ application_controller_src = fetch(step_path, "application_controller.js.tt")
18
+ application_controller_path = controllers_path / "application_controller.js"
19
+ application_js_src = fetch(step_path, "application.js.tt")
20
+ application_js_path = controllers_path / "application.js"
21
+ index_src = fetch(step_path, "index.js.vite.tt")
22
+ index_path = controllers_path / "index.js"
23
+
24
+ # create entrypoint/controllers, as well as the index, application and application_controller
25
+ empty_directory controllers_path unless controllers_path.exist?
26
+
27
+ copy_file(application_controller_src, application_controller_path) unless application_controller_path.exist?
28
+ copy_file(application_js_src, application_js_path) unless application_js_path.exist?
29
+ copy_file(index_src, index_path) unless index_path.exist?
30
+
31
+ controllers_pattern = /import ['"](\.\.\/)?controllers['"]/
32
+ controllers_commented_pattern = /\s*\/\/\s*#{controllers_pattern}/
33
+ prefix = "..\/" # standard:disable Style/RedundantStringEscape
34
+
35
+ if pack.match?(controllers_pattern)
36
+ if pack.match?(controllers_commented_pattern)
37
+ proceed = if options.key? "uncomment"
38
+ options["uncomment"]
39
+ else
40
+ !no?("✨ Do you want to import your Stimulus controllers in application.js? (Y/n)")
41
+ end
42
+
43
+ if proceed
44
+ # uncomment_lines only works with Ruby comments 🙄
45
+ lines = pack_path.readlines
46
+ matches = lines.select { |line| line =~ controllers_commented_pattern }
47
+ lines[lines.index(matches.last).to_i] = "import \"#{prefix}controllers\"\n"
48
+ pack_path.write lines.join
49
+ say "✅ Uncommented Stimulus controllers import in #{friendly_pack_path}"
50
+ else
51
+ say "🤷 your Stimulus controllers are not being imported in your application.js. We trust that you have a reason for this."
52
+ end
53
+ else
54
+ say "⏩ Stimulus controllers are already being imported in #{friendly_pack_path}. Skipping."
55
+ end
56
+ else
57
+ lines = pack_path.readlines
58
+ matches = lines.select { |line| line =~ /^import / }
59
+ lines.insert lines.index(matches.last).to_i + 1, "import \"#{prefix}controllers\"\n"
60
+ pack_path.write lines.join
61
+ say "✅ Stimulus controllers imported in #{friendly_pack_path}"
62
+ end
63
+
64
+ complete_step :vite
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stimulus_reflex/installer"
4
+
5
+ return if pack_path_missing?
6
+
7
+ # verify that all critical dependencies are up to date; if not, queue for later
8
+ lines = package_json.readlines
9
+ if !lines.index { |line| line =~ /^\s*["']webpack["']: ["']\^4.46.0["']/ }
10
+ add_package "webpack@^4.46.0"
11
+ else
12
+ say "⏩ webpack npm package is already present. Skipping."
13
+ end
14
+
15
+ if !lines.index { |line| line =~ /^\s*["']webpack-cli["']: ["']\^3.3.12["']/ }
16
+ add_package "webpack-cli@^3.3.12"
17
+ else
18
+ say "⏩ webpack-cli npm package is already present. Skipping."
19
+ end
20
+
21
+ if !lines.index { |line| line =~ /^\s*["']@rails\/webpacker["']: ["']\^5.4.3["']/ }
22
+ add_package "@rails/webpacker@^5.4.3"
23
+ else
24
+ say "⏩ @rails/webpacker npm package is already present. Skipping."
25
+ end
26
+
27
+ if !lines.index { |line| line =~ /^\s*["']@hotwired\/stimulus-webpack-helpers["']: ["']\^1.0.1["']/ }
28
+ add_package "@hotwired/stimulus-webpack-helpers@^1.0.1"
29
+ else
30
+ say "⏩ @hotwired/stimulus-webpack-helpers npm package is already present. Skipping."
31
+ end
32
+
33
+ if !lines.index { |line| line =~ /^\s*["']webpack-dev-server["']: ["']\^3.11.3["']/ }
34
+ add_dev_package "webpack-dev-server@^3.11.3"
35
+ else
36
+ say "⏩ @webpack-dev-server is already present. Skipping."
37
+ end
38
+
39
+ step_path = "/app/javascript/controllers/"
40
+ application_controller_src = fetch(step_path, "application_controller.js.tt")
41
+ application_controller_path = controllers_path / "application_controller.js"
42
+ application_js_src = fetch(step_path, "application.js.tt")
43
+ application_js_path = controllers_path / "application.js"
44
+ index_src = fetch(step_path, "index.js.webpacker.tt")
45
+ index_path = controllers_path / "index.js"
46
+
47
+ # create entrypoint/controllers, as well as the index, application and application_controller
48
+ empty_directory controllers_path unless controllers_path.exist?
49
+
50
+ copy_file(application_controller_src, application_controller_path) unless application_controller_path.exist?
51
+ # webpacker 5.4 did not colloquially feature a controllers/application.js file
52
+ copy_file(application_js_src, application_js_path) unless application_js_path.exist?
53
+ copy_file(index_src, index_path) unless index_path.exist?
54
+
55
+ controllers_pattern = /import ['"]controllers['"]/
56
+ controllers_commented_pattern = /\s*\/\/\s*#{controllers_pattern}/
57
+
58
+ if pack.match?(controllers_pattern)
59
+ if pack.match?(controllers_commented_pattern)
60
+ proceed = if options.key? "uncomment"
61
+ options["uncomment"]
62
+ else
63
+ !no?("✨ Do you want to import your Stimulus controllers in application.js? (Y/n)")
64
+ end
65
+
66
+ if proceed
67
+ # uncomment_lines only works with Ruby comments 🙄
68
+ lines = pack_path.readlines
69
+ matches = lines.select { |line| line =~ controllers_commented_pattern }
70
+ lines[lines.index(matches.last).to_i] = "import \"controllers\"\n"
71
+ pack_path.write lines.join
72
+ say "✅ Uncommented Stimulus controllers import in #{friendly_pack_path}"
73
+ else
74
+ say "🤷 your Stimulus controllers are not being imported in your application.js. We trust that you have a reason for this."
75
+ end
76
+ else
77
+ say "⏩ Stimulus controllers are already being imported in #{friendly_pack_path}. Skipping."
78
+ end
79
+ else
80
+ lines = pack_path.readlines
81
+ matches = lines.select { |line| line =~ /^import / }
82
+ lines.insert lines.index(matches.last).to_i + 1, "import \"controllers\"\n"
83
+ pack_path.write lines.join
84
+ say "✅ Stimulus controllers imported in #{friendly_pack_path}"
85
+ end
86
+
87
+ # ensure webpacker is installed in the Gemfile
88
+ add_gem "webpacker@5.4.3"
89
+
90
+ complete_step :webpacker
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stimulus_reflex/installer"
4
+
5
+ if !package_json.exist?
6
+ say "⏩ No package.json file found. Skipping."
7
+
8
+ return
9
+ end
10
+
11
+ # run yarn install only when packages are waiting to be added or removed
12
+ add = package_list.exist? ? package_list.readlines.map(&:chomp) : []
13
+ dev = dev_package_list.exist? ? dev_package_list.readlines.map(&:chomp) : []
14
+ drop = drop_package_list.exist? ? drop_package_list.readlines.map(&:chomp) : []
15
+
16
+ json = JSON.parse(package_json.read)
17
+
18
+ if add.present? || dev.present? || drop.present?
19
+
20
+ add.each do |package|
21
+ matches = package.match(/(.+)@(.+)/)
22
+ name, version = matches[1], matches[2]
23
+ json["dependencies"] = {} unless json["dependencies"]
24
+ json["dependencies"][name] = version
25
+ end
26
+
27
+ dev.each do |package|
28
+ matches = package.match(/(.+)@(.+)/)
29
+ name, version = matches[1], matches[2]
30
+ json["devDependencies"] = {} unless json["devDependencies"]
31
+ json["devDependencies"][name] = version
32
+ end
33
+
34
+ drop.each do |package|
35
+ json["dependencies"].delete(package)
36
+ json["devDependencies"].delete(package)
37
+ end
38
+
39
+ package_json.write JSON.pretty_generate(json)
40
+
41
+ system "yarn install --silent"
42
+ else
43
+ say "⏩ No yarn depdencies to add or remove. Skipping."
44
+ end
45
+
46
+ if bundler == "esbuild" && json["scripts"]["build"] != "node esbuild.config.mjs"
47
+ json["scripts"]["build:default"] = json["scripts"]["build"]
48
+ json["scripts"]["build"] = "node esbuild.config.mjs"
49
+ package_json.write JSON.pretty_generate(json)
50
+ say "✅ Your yarn build script has been updated to use esbuild.config.mjs"
51
+ else
52
+ say "⏩ Your yarn build script is already setup. Skipping."
53
+ end
54
+
55
+ complete_step :yarn
@@ -2,16 +2,14 @@
2
2
 
3
3
  module StimulusReflex
4
4
  class Broadcaster
5
- attr_reader :reflex, :logger, :operations
6
- delegate :cable_ready, :permanent_attribute_name, :payload, to: :reflex
7
-
8
- DEFAULT_HTML_WITHOUT_FORMAT = Nokogiri::XML::Node::SaveOptions::DEFAULT_HTML &
9
- ~Nokogiri::XML::Node::SaveOptions::FORMAT
5
+ attr_reader :reflex, :cable_ready, :logger, :operations
6
+ delegate :permanent_attribute_name, :payload, to: :reflex
10
7
 
11
8
  def initialize(reflex)
12
9
  @reflex = reflex
13
10
  @logger = Rails.logger if defined?(Rails.logger)
14
11
  @operations = []
12
+ @cable_ready = StimulusReflex::CableReadyChannels.new(reflex)
15
13
  end
16
14
 
17
15
  def nothing?
@@ -26,7 +24,7 @@ module StimulusReflex
26
24
  false
27
25
  end
28
26
 
29
- def halted(data: {})
27
+ def broadcast_halt(data: {})
30
28
  operations << ["document", :dispatch_event]
31
29
  cable_ready.dispatch_event(
32
30
  name: "stimulus-reflex:morph-halted",
@@ -35,13 +33,22 @@ module StimulusReflex
35
33
  ).broadcast
36
34
  end
37
35
 
38
- def error(data: {}, body: nil)
36
+ def broadcast_forbid(data: {})
37
+ operations << ["document", :dispatch_event]
38
+ cable_ready.dispatch_event(
39
+ name: "stimulus-reflex:morph-forbidden",
40
+ payload: payload,
41
+ stimulus_reflex: data.merge(morph: to_sym)
42
+ ).broadcast
43
+ end
44
+
45
+ def broadcast_error(data: {}, error: nil)
39
46
  operations << ["document", :dispatch_event]
40
47
  cable_ready.dispatch_event(
41
48
  name: "stimulus-reflex:morph-error",
42
49
  payload: payload,
43
50
  stimulus_reflex: data.merge(morph: to_sym),
44
- body: body&.to_s
51
+ error: error&.to_s
45
52
  ).broadcast
46
53
  end
47
54
 
@@ -4,23 +4,22 @@ module StimulusReflex
4
4
  class PageBroadcaster < Broadcaster
5
5
  def broadcast(selectors, data)
6
6
  reflex.controller.process reflex.params[:action]
7
- page_html = reflex.controller.response.body
7
+ document = StimulusReflex::HTML::Document.new(reflex.controller.response.body)
8
8
 
9
- return unless page_html.present?
9
+ return if document.empty?
10
10
 
11
- document = Nokogiri::HTML.parse(page_html)
12
- selectors = selectors.select { |s| document.css(s).present? }
11
+ selectors = selectors.select { |s| document.match(s).present? }
13
12
  selectors.each do |selector|
14
- operations << [selector, :morph]
15
- html = document.css(selector).inner_html(save_with: Broadcaster::DEFAULT_HTML_WITHOUT_FORMAT)
16
- cable_ready.morph(
13
+ operations << [selector, StimulusReflex.config.morph_operation]
14
+ html = document.match(selector).inner_html
15
+ cable_ready.send StimulusReflex.config.morph_operation, {
17
16
  selector: selector,
18
17
  html: html,
19
18
  payload: payload,
20
19
  children_only: true,
21
20
  permanent_attribute_name: permanent_attribute_name,
22
21
  stimulus_reflex: data.merge(morph: to_sym)
23
- )
22
+ }
24
23
  end
25
24
  cable_ready.broadcast
26
25
  end
@@ -7,26 +7,26 @@ module StimulusReflex
7
7
  selectors, html = morph
8
8
  updates = create_update_collection(selectors, html)
9
9
  updates.each do |update|
10
- fragment = Nokogiri::HTML.fragment(update.html.to_s)
11
- match = fragment.at_css(update.selector)
10
+ document = StimulusReflex::HTML::DocumentFragment.new(update.html)
11
+ match = document.match(update.selector)
12
12
  if match.present?
13
- operations << [update.selector, :morph]
14
- cable_ready.morph(
13
+ operations << [update.selector, StimulusReflex.config.morph_operation]
14
+ cable_ready.send StimulusReflex.config.morph_operation, {
15
15
  selector: update.selector,
16
- html: match.inner_html(save_with: Broadcaster::DEFAULT_HTML_WITHOUT_FORMAT),
16
+ html: match.inner_html,
17
17
  payload: payload,
18
18
  children_only: true,
19
19
  permanent_attribute_name: permanent_attribute_name,
20
20
  stimulus_reflex: data.merge(morph: to_sym)
21
- )
21
+ }
22
22
  else
23
- operations << [update.selector, :inner_html]
24
- cable_ready.inner_html(
23
+ operations << [update.selector, StimulusReflex.config.replace_operation]
24
+ cable_ready.send StimulusReflex.config.replace_operation, {
25
25
  selector: update.selector,
26
- html: fragment.to_html,
26
+ html: update.html.to_s,
27
27
  payload: payload,
28
28
  stimulus_reflex: data.merge(morph: to_sym)
29
- )
29
+ }
30
30
  end
31
31
  end
32
32
  end