cable_ready icon indicating copy to clipboard operation
cable_ready copied to clipboard

CableReady installer

Open julianrubisch opened this issue 3 years ago • 3 comments

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, and cable_ready:install:node, where the main cable_ready:install task switches on the presence of an importmap.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:
    1. `import CableReady from "cable_ready"
    2. if a ./channels/index.js is present, we're likely in a webpacker setup. Use that consumer to initialize CR.
    3. if no such channels index is present, we can fall back on using the cable module exposed by @hotwired/turbo-rails.
    4. else ask the user to do it manually.

I've tested many scenarios locally, but doing a bit of testing certainly wouldn't hurt 🙏

julianrubisch avatar Jan 31 '22 15:01 julianrubisch

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..?

Matt-Yorkley avatar Feb 02 '22 15:02 Matt-Yorkley

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:

Matt-Yorkley avatar Feb 02 '22 15:02 Matt-Yorkley

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?

julianrubisch avatar Feb 02 '22 15:02 julianrubisch

Closing in favor of #233

julianrubisch avatar Feb 09 '23 16:02 julianrubisch