Stimulus Discourse

New to stimulus, tiny controller directory placement question


#1

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$/)
application.load(definitionsFromContext(context))

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?


#2

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 https://rossta.net/talks/webpack-survival-guide-rails.html

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