ElMassimo / vite_ruby

⚡️ Vite.js in Ruby, bringing joy to your JavaScript experience
https://vite-ruby.netlify.app/
MIT License
1.28k stars 117 forks source link

fix: capistrano deployment docs correction for vite v5 #454

Open tenkiller opened 4 months ago

tenkiller commented 4 months ago

Description 📖

When using Vite v5 and above, Capistrano is not aware that the location of the asset manifest files has moved, and fails on deployment with Rails assets manifest file not found.

Background 📜

When using Vite v5 and above, asset manifest files are output to a vite/.vite/ subdirectory, by default. Meaning that the manifest.json and manifest-assets.json files that used to reside under public/vite/ now reside under public/vite/.vite/.

The Fix 🔨

Update documentation to reflect that there is a difference between the Capistrano configuration when using Vite v4 and Vite v5. The fix when using Vite v5 and above is to set the Capistrano :assets_manifests option. The path(s) need to be relative or absolute.

rgioia commented 4 months ago

@tenkiller Thanks for the update! It started working as you mentioned. Now, the issue I'm facing is that when the Skipping asset precompile, no asset diff found, the deploy fails with the following error: Rails Assets manifest file not found.

Could there be a path conflict with Sprockets searching for the manifest?

set :assets_prefix, 'vite'
set :assets_manifests, ["public/#{fetch(:assets_prefix)}/.vite/manifest*.*"]
ls      /releases/xxxx/public/vite/manifest*
ls      ls: cannot access /releases/xxxx/public/vite/manifest*
ls      10 : No such file or directory

ls      /releases/xxxx/public/vite/.sprockets-manifest*
ls      ls: cannot access /releases/xxxx/public/vite/.sprockets-manifest*'
ls      No such file or directory
tenkiller commented 4 months ago

@rgioia Good point. I had considered including the path for the Rails Asset Pipeline (i.e. sprockets), but we weren't getting any errors on deployment for our setup, so I overlooked it.

By setting :assets_manifests, it overrides the default, which is

[
  "/path/to/release_path/public/#{fetch(:assets_prefix)}/.sprockets-manifest*", 
  "/path/to/release_path/public/#{fetch(:assets_prefix)}/manifest*.*"
]

But, we don't want to add this back in as given because assets_prefix is set to vite. The default prefix for the Rails Asset Pipeline is assets. So, I think, this should remedy it?

set :assets_manifests, [
  "public/assets/.sprockets-manifest*",
  "public/assets/manifest*.*",
  "public/#{fetch(:assets_prefix)}/.vite/manifest*.*"
]
rgioia commented 3 months ago

@tenkiller Thank you! I managed to do what I needed; I successfully switched from Webpacker to Vite.

ElMassimo commented 3 months ago

I don't use Capistrano myself, but if I understand correctly, the instructions are not complete if the app needs to support sprockets, as public/assets would not be automatically added to linked_dirs.

Given that some users still mix sprockets and vite_rails, and some with propshaft it might be better to avoid relying on the internals of capistrano-rails and its usage of assets_prefix, and instead instruct the user to configure:

Happy to accept a PR that removes the example setting assets_prefix and explains how to append these two values.