Installation hiccup on Rails 7 with turbo-rails and importmaps
minimul opened this issue · comments
The turbo-rails gem pins thus to the config/importmap.rb
file:
pin '@hotwired/turbo-rails', to: 'turbo.min.js', preload: true
When I ran bin/importmap pin turbo_power
it did pin a version of @hotwired/turbo
but since I didn't want two versions of Turbo, I removed it and received this error:
Uncaught TypeError: Failed to resolve module specifier "@hotwired/turbo". Relative references must start with either "/", "./", or "../".
As a workaround, I just made a "copy" in config/importmap.rb
like so:
pin '@hotwired/turbo-rails', to: 'turbo.min.js', preload: true
+pin @hotwired/turbo', to: 'turbo.min.js', preload: true
After that no problems and I've already implemented a .set_attribute()
stream. 🚀
Thanks for this great gem and NPM package.
Hey @minimul, thanks for opening this issue. I'm thinking about if I can somehow solve this in the library itself.
But I think there is no "sane" way that just works automatically with Import Maps. We probably need to rely on better installation instructions in the README.
In one regard it does work out of the box because it does install the dependency. e.g.
$ bin/importmap pin turbo_power
Pinning "turbo_power" to https://ga.jspm.io/npm:turbo_power@0.1.3/dist/index.js
Pinning "@hotwired/turbo" to https://ga.jspm.io/npm:@hotwired/turbo@7.2.0/dist/turbo.es2017-esm.js
Yes, you then have two versions of Turbo, which is not desired, but it should work out of the box.
Perhaps that awareness alone can go into the README. Or my alternate hack too.
FWIW:
I didn't have to mess with the stock app/javascript/application.js
either:
import '@hotwired/turbo-rails'
import 'controllers'
+ import TurboPower from 'turbo_power'
+ TurboPower.initialize(Turbo.StreamActions)
In short, I didn't have modify to do a import * as Turbo from '@hotwired/turbo'
or import { Turbo } from '@hotwired/turbo'
or those same import statements w/ @hotwired/turbo-rails
and Turbo Power works fine.