-
Notifications
You must be signed in to change notification settings - Fork 123
Decouple from sprockets-rails #60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Decouple from sprockets-rails #60
Conversation
All this does is transfer that dependency into the new `rescuable_asset_error?` method. But this preparatory refactor sets us up to actually decouple the dependency without overcomplicating `Importmap::Map#resolve_asset_paths`.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a better fix would just to rescue all exception and add the exception name, message, and backtrace to the warning.
lib/importmap/engine.rb
Outdated
@@ -7,6 +7,9 @@ module Importmap | |||
class Engine < ::Rails::Engine | |||
config.importmap = ActiveSupport::OrderedOptions.new | |||
config.importmap.sweep_cache = Rails.env.development? || Rails.env.test? | |||
config.importmap.rescuable_asset_errors = [ | |||
defined?(Sprockets::Rails) && Sprockets::Rails::Helper::AssetNotFound, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This only works if import-maps is loaded before sprockets-rails.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you mean after sprockets-rails? I think if importmap-rails was loaded before sprockets-rails this would not work, i.e. defined?(Sprockets::Rails)
would be false
even though it's going to be defined later once sprockets-rails gets require
d.
So there does seem to be a load order dependency here. In the past, I've always worked on applications where sprockets/railtie
was required before Bundler.require(*Rails.groups)
was invoked, because that's how rails has generated it...but that won't be true in rails 7 after rails/rails#43261. I think we can make it work independent of the load order by inserting into this list within an initializer
block.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consumers may be using a different asset pipeline than sprockets-rails, which means that when `Importmap::Map#resolve_asset_paths` invokes the application's `asset_path` helper method, it may need to rescue different exceptions than `Sprockets::Rails::Helper::AssetNotFound`. To support that, we've added a `rescuable_asset_errors` property to the importmap configuration options. We add `Sprockets::Rails::Helper::AssetNotFound` to the array if `Sprockets::Rails` is defined, which should allow the gem to work the same as before in applications that use sprockets-rails. But now it'll also be usable in applications that use something other than sprockets-rails.
34defe5
to
313e2cf
Compare
I'm not against that, but is there any concern that it would swallow too many exceptions? Currently this PR effectively preserves the existing behavior, whereas rescuing everything would be a broader change. |
I'd more concerned if this |
Current fix is fine though, so I think it is fine as is. |
Hey y'all this is embarrassing how long it took me to figure out, but it wasn't until i noticed my It was surprisingly tricky to debug because everything was working as expecting with a quiet browser console and correct html injection so figured it was user config error. It wasn't until i noticed
This might be somewhat common for mature projects who were webpack only, but now coming back to asset pipeline |
Instantly working, you have no idea how far into the weeds i got 🤣 🤦
|
@rromanchuk, I'm not sure if you were suggesting that the Also note that sprockets-rails was a dependency of rails prior to version 7.0, so the only time you'd run into that issue is if you were upgrading to rails 7. And in that case, it's highly recommended to read the upgrade guide, which mentions that you'll need to add sprockets-rails to your Gemfile if your application needs it (i.e. if you aren't using an alternative asset pipeline): |
I did, but i don't (didn't) use sprockets so it's NA information. It wasn't until i read dhh et al posts that talked about pipelines, let alone extensible ones. There isn't any mention of sprockets https://github.com/rails/importmap-rails#installation and running in development obfuscates the until obvious becomes true. https://github.com/rails/importmap-rails
I'm using this because i never want to think about transpiling and precompiling again. Since there is no opinionated default pipeline, in order to run this end to end, the user is required to set one up. The upgrade guide says sprockets is now optional, and this readme makes zero mentions of needing to bring your own dependency. Just a mention in the docs would suffice. |
As noted in #42, the gem had a direct reference to a constant from sprockets-rails here:
importmap-rails/lib/importmap/map.rb
Line 94 in 8d66a09
This is problematic because consumers may be using a different asset pipeline than sprockets-rails. In that case, when
Importmap::Map#resolve_asset_paths
invokes the application'sasset_path
helper method, it may need to rescue different exceptions thanSprockets::Rails::Helper::AssetNotFound
.To support that, we've added a
rescuable_asset_errors
property to the importmap configuration options. We automatically addSprockets::Rails::Helper::AssetNotFound
to the list ifSprockets::Rails
is defined, which should allow the gem to work the same as before in applications that use sprockets-rails. But now it'll also be usable in applications that use something other than sprockets-rails.