excid3 / esbuild-rails

Esbuild Rails plugin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

File to paste the "import stimulus controllers and register them" into

notapatch opened this issue · comments

I'm just checking the usage instructions for esbuild-rails. It doesn't say which file you are supposed to paste the code into. The import hotwired/stimulus code looks like the start of javascript/controllers/application.js . But in the screencast, you pasted just the controller code into the autogenerated file javascript/controllers/index.js.

Import Stimulus controllers and register them:

// app/javascript/ ???
import { Application } from "@hotwired/stimulus"
const application = Application.start()

import controllers from "./**/*_controller.js"
controllers.forEach((controller) => {
  application.register(controller.name, controller.module.default)
})

You can import code in any JavaScript file.

It doesn't matter if you import stimulus controllers on the index.js or the application.js.

The reason I put it in index was that's where rails imports stimulus controllers typically.

Thank you for explaining that to me, Chris. I'm not going to be the only person who wonders if appending code to a file tagged as auto-generated is a good idea.

So I was hoping to update the README to add a filename to make it easier for people using the library, especially as Rails JS has changed again and esbuild is new to everyone.

// app/javascript/controllers/application.js   <=== add this line or index.js
import { Application } from "@hotwired/stimulus"
const application = Application.start()

import controllers from "./**/*_controller.js"
controllers.forEach((controller) => {
  application.register(controller.name, controller.module.default)
})

...