bullet_train icon indicating copy to clipboard operation
bullet_train copied to clipboard

Running `bin/rails g stimulus controllername` overwrites app/javascript/controllers/index.js

Open pascallaliberte opened this issue 2 years ago • 2 comments

When following online examples, there's a command to scaffold a Stimulus controller.

For example, this can be used to create an appointment_controller.js file in app/javascript/controllers.

bin/rails g stimulus appointment

However, this command also completely overwrites the contents of app/javascript/controllers/index.js with this:

// This file is auto-generated by ./bin/rails stimulus:manifest:update
// Run that command whenever you add a new controller or create them with
// ./bin/rails generate stimulus controllerName

import { application } from "./application"

import AppointmentController from "./appointment_controller"
application.register("appointment", AppointmentController)

This of course breaks every other Stimulus controllers Bullet Train depends on.

pascallaliberte avatar Sep 05 '22 13:09 pascallaliberte

From the README of stimulus-rails:

With an application using a JavaScript bundler, controllers need to be imported and registered directly in the index.js file. But this can be done automatically using either the Stimulus generator (./bin/rails generate stimulus [controller]) or the dedicated stimulus:manifest:update task. Either will overwrite the controllers/index.js file.

This is a legitimate way to use Stimulus and doesn't seem to include a flag to prevent overwriting app/javascript/controllers/index.js.

Maybe there's a way to catch that in BT and prevent overwriting controllers/index.js automatically? Maybe by overwriting the generator?

pascallaliberte avatar Sep 05 '22 14:09 pascallaliberte

@pascallaliberte I opened a PR over at https://github.com/hotwired/stimulus-rails/pull/116, but you can test it out for the time being by using this in the Gemfile and then using the --append flag with the same command:

gem "stimulus-rails", git: "[email protected]:gazayas/stimulus-rails.git", branch: "append-controllers-to-index"
bin/rails g stimulus appointment --append

gazayas avatar Jan 28 '23 05:01 gazayas

This line makes me think that we're doing something we shouldn't:

// This file is auto-generated by ./bin/rails stimulus:manifest:update

It sounds like we shouldn't manually construct app/javascript/controllers/index.js but should allow it to be auto-generated as expected.

I don't yet have a suggestion as to how we should change it. Just noting that it seems like we've strayed off of the happy path for stimulus.

jagthedrummer avatar Aug 25 '23 18:08 jagthedrummer

@jagthedrummer:

I feel it's pretty common for folks to update controllers/index.js.

I also suspect that generator isn't used frequently in the wild and has been created as a hopeful utility rather than an extraction. Just my sense. The happy path nowadays is to use a glob and automatically register all controllers inside a directory. We do this with esbuild, replicating the webpack example from the docs.

All that said: I filed the issue because a member of the BT community reported the bug.

But we can close this one. The PR over at https://github.com/hotwired/stimulus-rails/pull/116 will point here.

pascallaliberte avatar Aug 29 '23 17:08 pascallaliberte