I'm running into an issue using custom Stimulus co...
# avo-2
c
I'm running into an issue using custom Stimulus controllers. I'm using Propshaft/jsbundling-rails/cssbundling-rails/esbuild. I've made a custom _head, and included the relevant bits. The issue basically comes down to: If I want the controllers to actually be registered and available, I need to go through the whole shebang of what happens in controllers/index.js -- I.E. initializing Stimulus. This produces an error in the console about it being initialized twice. If I don't do the whole process then the controller never gets registered properly because it can't see the existing window.Stimulus instance. Anyone have any thoughts about this?
l
That’s weird. Window. Stimulus should be there
Can you try to output window.stimulus in the console after the page loaded?
c
So, that's the interesting part. In the console, it's absolutely present on window. But in the code,
window.Stimulus
returns
undefined
.
I'm assuming it's some sort of isolation/security mechanism the browser implements.
l
Hmmm. It shouldn’t
Have you overriden application.html?
c
I have:
Copy code
<!DOCTYPE html>
<html>
  <head>
    <title>Collection</title>
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>

    <%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
    <%= javascript_include_tag "application", "data-turbo-track": "reload", defer: true %>
  </head>

  <body>
    <main>
      <%= yield %>
    </main>
  </body>
</html>
l
That’s not it
c
That's the only file involving
application.html
in my app.
l
Got it
So you’r probably not overriding it
Hmmm
Try to console log the window object
See if stimulus is there
🤷🏼‍♂️
c
It is.
l
Form the script?
c
Like I said, the console can see the
Stimulus
field of
window
, but the code cannot.
Yeah.
First thing I checked.
l
Lol
Thst IS weird
c
I'll make a minimal repro app and file an issue linking to it.
l
Just to cover our bases, you are not overrifing window.Stimulus somehow by mistake, roght?
c
I'm sure it's something I'm doing wrong, but I don't know where/what it is.
l
Sure. Thank you!
c
I shouldn't be. Maybe I horked something along the way, but until now all the JS stuff has been the auto-generated boilerplate.
l
Got it
Thanks for the details
c
Err, I mis-remembered things. It's Rails-UJS that's being persnickety, not Stimulus. Although the weirdness with that being in
window
but not visible to the code remains...
... does Avo use Rails-UJS?
l
I think not anymore
I’m AFK roght now
Can you check GitHub for app/js/avo.base.js?
c
Aha. Avo is using it.
Issue filed: https://github.com/avo-hq/avo/issues/1632 In preparing the repro case, I clarified for myself that the issue is with Rails-UJS and the Stimulus weirdness is kinda peripheral. I also found a workaround. So, I'm mostly looking for "am I doing anything that you consider obviously wrong / risky?"
One other consideration is that I don't know how -- or if there's even a way -- to dedupe JS dependencies between Avo and my app. As you will see when you're able to look, I'm making a month-selector field and so I'm using flatpickr and one other dep. I'm basically copy-pasting the date field from Avo and hacking it to use an included flatpickr plugin. The end result is that I'm winding up with a second copy of all that stuff getting loaded. So that makes me feel like I'm Doing It Wrong, but with the JS world, it may just be that You Can't Get There From Here.
l
Got it. Thanks for the reproduction repo. That helps a lot
I see what you mean with the duplication issue. I’ll think about it and try to find an elegant solution for it
We could be able to solve that somehow
c
Also, if there's a Right Way to publish Avo plugins/extensions, I'd love to publish the "month field" to Rubygems.
l
The short answer for Avo 2 is no. There isn’t a plugin system
But that’s a yes for Avo 3
It already has a plugin system and you’ll be able to add plugins as engines and publish them to rubygems
So, keep that field for a little while longer. You’ll be able to port it to Avo 3