cable_ready
cable_ready copied to clipboard
CableReady installer
I've taken a first stab at the new unified CableReady Installer, and borrowed heavily from https://github.com/hotwired/turbo-rails/blob/main/lib/tasks/turbo_tasks.rake
Let me break it down for you a bit:
- basically we have 2 install tasks,
cable_ready:install:importmap
, andcable_ready:install:node
, where the maincable_ready:install
task switches on the presence of animportmap.rb
file - both installers do a sanity check if Redis and ActionCable are available, and optionally asks the user if Redis should be installed (https://github.com/stimulusreflex/cable_ready/pull/179/files#diff-e469da9bc01523658da7d880f0c454118061110611e92e21c3d69e0232ae983aR8)
- the installers then do their
pin
/yarn add
actions - finally, initializing CableReady in the application's entrypoint. I've taken my own experience as a heuristic here:
- `import CableReady from "cable_ready"
- if a
./channels/index.js
is present, we're likely in a webpacker setup. Use that consumer to initialize CR. - if no such channels index is present, we can fall back on using the
cable
module exposed by@hotwired/turbo-rails
. - else ask the user to do it manually.
I've tested many scenarios locally, but doing a bit of testing certainly wouldn't hurt 🙏
Hey :wave:
I've been playing with CR and SR on a greenfield app that's using Vite and ran into a couple of issues with some of the install tasks for both. I took some rough notes:
The helpers_generator here fails with Vite:
https://github.com/stimulusreflex/cable_ready/blob/1bded053b64e8f497fcf368a258b0326ea4c886f/lib/generators/cable_ready/helpers_generator.rb#L12-L22
It assumes the main_folder
is "app/javascript" and the entrypoint directory is named "packs".
A slight adjustment makes it work for either:
main_folder = defined?(Webpacker) ? Webpacker.config.source_path.to_s.gsub("#{Rails.root}/", "") : "app/javascript"
++ main_folder = "app/frontend" if defined? ViteRuby
filepath = [
"#{main_folder}/controllers/index.js",
"#{main_folder}/controllers/index.ts",
"#{main_folder}/packs/application.js",
"#{main_folder}/packs/application.ts",
++ "#{main_folder}/entrypoints/application.js",
++ "#{main_folder}/entrypoints/application.ts"
]
There are some similar install tasks in SR that also break in Vite but just need a very slightly different path. And there's this Webpacker-specific line that gets added during setup:
StimulusReflex.debug = process.env.RAILS_ENV === 'development'
which throws an error with Vite. It works with a slight (Vite-specific) adjustment:
--StimulusReflex.debug = process.env.RAILS_ENV === 'development'
++StimulusReflex.debug = import.meta.env.DEV
I'm wondering if these could be generalised a bit. I haven't tried with jsbundling-rails
, but it also uses ESBuild so I assume it would be similar to Vite in regards to default paths..?
Obviously it's annoying AF having to cater to Webpacker / Shakapacker(!) / Importmaps / jsbundling-rails / Vite, but from the DX side of things it's really nice if the install tasks Just Work™ without any issues :man_shrugging:
Yeah, so this PR is about the main CR installer (which even doesn’t exist yet!)
shall we pull out your comment into an issue?
Closing in favor of #233