Replies: 2 comments 8 replies
-
Hi Jonathan! Vite Ruby extends As a result, Vite Ruby is compatible with In summary, if you haven't set Make sure you don't have a If Regarding double installs, |
Beta Was this translation helpful? Give feedback.
-
In my case a misconfigured Note for Googlers, double check your steps that precede vite build. |
Beta Was this translation helpful? Give feedback.
-
Is anyone else running vite-rails with Rails 7? I think maybe not yet?
Because I ran into a problem upgrading my Rails 6.1 app using vite-rails to Rails 7.0. Which I have researched some background on, and can provide a workaround and suggestion for vite-rails fix.
In my Rails 6.1 app, it turns out that Rails will do a
yarn install
as an initial part ofrake assets:precompile
. This is necessary, it installs vite itself, and all your other yarn npm dependencies.In Rails 7.0 -- this stopped happening. So if deploying to a fresh machine,
rake assets:precompile
will get you an error that may look something like this:Diagnosis and history
Rails 6.1 wires up the
asset:precompile
rake task to do a yarn install as a rake pre-requisite, if a./bin/yarn
is detected:—https://github.com/rails/rails/blob/v6.1.7/railties/lib/rails/tasks/yarn.rake#L27-L30
But Rails 7.0 removes this, with this commit message:
— rails/rails@3e2f74c
What does jsbundling-rails do? Something very similar to what Rails 6.1 did:
— https://github.com/rails/jsbundling-rails/blob/98780f28809ebad5d84921ce38dc238472a35812/lib/tasks/jsbundling/build.rake
What does webpacker do? The last non-prerelease version of webpacker, 5.4.3, also does it's own thing with it's own rake task for yarn install, wiring it up as a pre-req to
assets:precompile
:— https://github.com/rails/webpacker/blob/v5.4.3/lib/tasks/webpacker/compile.rake#L37-L42
The webpacker 6.x pre-releases change the code somewhat, but similar approach... although this time using a
yarn:install
task -- webpacker 6.x actually providesyarn:install
task, but with a comment: "Duplicate of the yarn tasks still present in Rails until Webpacker <5 have been deprecated."— https://github.com/rails/webpacker/blob/dc1482790326ca077577dc957b0be068724b9c7c/lib/tasks/yarn.rake#L35-L38
Local workaround
My app still has a
./bin/yarn
(I am not sure if this is reliable for all Rails 7 apps, even all of them using yarn?). And Rails 7 still has ayarn:install
task, it just isn't by default called byassets:precompile
.So I sucessfully fixed this issue simply by adding these lines to my local
./Rakefile
, copied from what Rails 6.1.x used to do:Bingo, solved.
What should vite-rails do?
I think vite-rails should probably be wiring up a
yarn install
as a rake pre-req ofassets:precompile
, as jsbundling-rails and webpacker do, as the comment when Rails 7 removed the pre-req suggested.Although maybe you don't technically need to use
yarn
to use vite and vite-rails? I'm not sure if that is meant to be supported -- if so, I guess it should only do ayarn install
if yarn is present... but I think it should still do so if present, for an easy path Just Works path for people on the golden path of Rails. Checking for presence of./yarn.lock
might make more sense than checking for presence of./bin/yarn
, as you might not have a localyarn
inbin
? (Although technically you might not also (yet) have ayarn.lock
... but you really probably should when runningrake assets:precompile
I think?)But then how do we do the yarn install? We want to ensure a yarn install, but also want to avoid a double yarn install.
What if you are using vite-rails with Rails 6.1? It already does a
yarn install
, then vite-rails also wires it up... now you get it both?That can be avoided if vite-rails does exactly what I do, the rake
enhance
with existingyarn:install
.enhance
should be idempotent in the sense that we doRake::Task["assets:precompile"].enhance [ "yarn:install" ]
as many times as we want,yarn:install
will only be called once.I don't really understand how jsbundling-rails or webpacker are avoiding this when used on Rails 6.1 though... since they don't use the existing
yarn:install
but appear to provide their own rake tasks? When used on Rails 6.1, are people getting a double yarn install? I'm not sure, I can't find anyone complaining about it, haven't tried to verify myself.(For that matter, how do they avoid jsbundling-rails and cssbundling-rails each doing their own
yarn install
, being a double even on rails 7?!?!)And wait, what if someone is using vite-rails and also has webpacker, jsbundling-rails, or both installed -- on Rails 6.1 or 7? Then, if vite-rails were also wiring up a
yarn install
, you could still be getting 2, 3, or moreyarn install
s happening. :(.So I'm not totally sure what to do. I suppose it could just be a doc issue where vite-ruby/rails tells someone to do this themselves in Rails 7 only if they need it... but that seems not very friendly to me, it's pretty confusing to figure out if you need it. I think it would be best if vite-rails does it's best to automatically wire up the
yarn install
if and only if it's needed, but maybe with config to override in either direction if it gets it wrong?@ElMassimo , any thoughts?
Beta Was this translation helpful? Give feedback.
All reactions