hotwired / stimulus-rails

Use Stimulus in your Ruby on Rails app
https://stimulus.hotwired.dev
MIT License
637 stars 91 forks source link

Generators and tasks do not work in engines #138

Open pbhogan opened 4 months ago

pbhogan commented 4 months ago

When using Stimulus in an engine, running the stimulus controller generator doesn't respect the engine namespace. So it'll generate in app/javascript/controllers instead of app/javascript/engine_name/controllers

Furthermore, the stimulus tasks are not available, so the automatic execution of stimulus:manifest:update by the generator fails.

I know this can all be done manually, but it's unintuitive that it doesn't work when other Rails generators operate correctly.

dhh commented 3 months ago

Feel free to explore a fix to this 👍

pbhogan commented 2 months ago

Small progress update here:

Fixing the Stimulus controller generator seems trivial and I'll submit a pull request for that.

However, more challenging is the getting Stimulus tasks to show up in the engine root without modifying the engine's Rakefile. Any tasks declared with Rails::Railtie.rake_tasks, Rails::Engine.rake_tasks or in lib/tasks will be scoped under namespace :app due to Rakefile loading rails/tasks/engine.rake in Railties, which then scopes under :app and loads tasks.

Critically, there doesn't appear to be any way to add unscoped tasks within a Rails::Engine.

I think the solution is making changes to Railties to have something like Rails::Railtie.unscoped_rake_tasks or Rails::Railtie.engine_rake_tasks (not sure on the best nomenclature here) that would allow an engine to declare tasks that will not appear in host apps, but can be run in the engine root during development.

engine.rake accomplishes this for database-related tasks by doing it manually for Rails, so there is a use-case, albeit niche. Any engine with tasks other than generators could use this.

In the case of Stimulus, we would want app:stimulus:manifest:update to exist for the dummy app, but also stimulus:manifest:update for development of the engine itself when run in the engine project root.

Thoughts? Should I pursue a more significant pull request on Railties to accomplish this @dhh ?

dhh commented 2 months ago

That's a good question. I don't have the answer, but maybe @rafaelfranca does.

pbhogan commented 2 months ago

I've made a pull request to address this. It's not tiny, but the more I got into it, the more related issues and edge-cases I found to fix and clean up. :)

The one drawback is I had to include a small monkeypatch to Rake's DSL to allow creating unscoped top-level tasks.

Incidentally, I discovered ActionCable also has a few similar issues with its generator that needs addressing.