Stimulus Discourse

New to stimulus, tiny controller directory placement question


Stimulus has built-in support for Webpacker, which makes sense, and the docs say the place the controllers in app/javascript/packs/controllers. From app/javascript/packs/application.js:

const application = Application.start()
const context = require.context("./controllers", true, /\.js$/)

However, according to the rails/webpacker docs:

“Packs” is a special directory made only for webpack entry files so don’t put anything here that you don’t want to link in your views.

So I was expecting app/javascript/controllers, not app/javascript/packs/controllers.

I couldn’t find where this is explained anywhere. Obviously the fix is to either go with it, or change to require.context("../controllers", true, /\.js$/).

I’d like to be able to be as idiomatic as possible, while also being able to explain to people why things are how they are. Any background info or something on this?


you are right only entry points should be in packs and controller directory should be at the same level as packs

I recommend this video from Rails Conf 2018

A must see to understand Webpack and use it correctly in Rails