Skip to content

Commit 0ede388

Browse files
committed
improve stimulus controller template to import from correct module
Stimulus changed the location of their module from `stimulus` to `@hotwired/stimulus`. Depending on that, the import in the controller template should change. For `stimulus` it should be ```js import { Controller } from "stimulus" ``` For `@hotwired/stimulus` it should be ```js import { Controller } from "@hotwired/stimulus" ```
1 parent 0965284 commit 0ede388

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

docs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ title: Changelog
77

88
## main
99

10+
* Improve Stimulus controller template to import from `stimulus` or `@hotwired/stimulus`.
11+
12+
*Mario Schüttel*
13+
1014
## 2.41.0
1115

1216
* Add `sprockets-rails` development dependency to fix test suite failures when using rails@main.

lib/rails/generators/stimulus/component_generator.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ def create_stimulus_controller
1212
template "component_controller.js", destination
1313
end
1414

15+
def stimulus_module
16+
return "stimulus" if legacy_stimulus?
17+
18+
"@hotwired/stimulus"
19+
end
20+
1521
private
1622

1723
def destination
@@ -21,6 +27,11 @@ def destination
2127
File.join(component_path, class_path, "#{file_name}_component_controller.js")
2228
end
2329
end
30+
31+
def legacy_stimulus?
32+
package_json_pathname = Rails.root.join("package.json")
33+
package_json_pathname.exist? && JSON.parse(package_json_pathname.read).dig("dependencies", "stimulus").present?
34+
end
2435
end
2536
end
2637
end

lib/rails/generators/stimulus/templates/component_controller.js.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Controller } from "stimulus";
1+
import { Controller } from "<%= stimulus_module %>";
22

33
export default class extends Controller {
44
connect() {

test/generators/component_generator_test.rb

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,22 @@ def test_component_with_stimulus_and_inline
216216
assert_file "app/components/user_component_controller.js"
217217
end
218218

219+
def test_component_with_legacy_stimulus_and_sidecar
220+
with_package_json({ dependencies: { "stimulus": "0.0.0" } }) do
221+
run_generator %w[user --stimulus --sidecar]
222+
223+
assert_file "app/components/user_component/user_component_controller.js" do |file|
224+
assert_match(/import { Controller } from "stimulus"/, file)
225+
end
226+
end
227+
end
228+
219229
def test_component_with_stimulus_and_sidecar
220230
run_generator %w[user --stimulus --sidecar]
221231

222-
assert_file "app/components/user_component/user_component_controller.js"
232+
assert_file "app/components/user_component/user_component_controller.js" do |file|
233+
assert_match(/import { Controller } from "@hotwired\/stimulus"/, file)
234+
end
223235
end
224236

225237
def test_component_with_stimulus_and_sidecar_and_inline
@@ -231,4 +243,14 @@ def test_component_with_stimulus_and_sidecar_and_inline
231243

232244
assert_file "app/components/user_component/user_component_controller.js"
233245
end
246+
247+
private
248+
249+
def with_package_json(content, &block)
250+
package_json_pathname = Rails.root.join("package.json")
251+
package_json_pathname.write(JSON.generate(content))
252+
yield
253+
ensure
254+
package_json_pathname.delete
255+
end
234256
end

0 commit comments

Comments
 (0)