bjarosze / riot_js-rails

Muut Riot integration with Rails
28 stars 14 forks source link

Silence deprecations for sprockets v3.7 #17

Closed bsedin closed 8 years ago

bsedin commented 8 years ago

Fixes deprecation warning:

DEPRECATION WARNING: Sprockets method `register_engine` is deprecated.
Please register a mime type using `register_mime_type` then
use `register_compressor` or `register_transformer`.
https://github.com/rails/sprockets/blob/master/guides/extending_sprockets.md#supporting-all-versions-of-sprockets-in-processors
 (called from block in register_self at [...]/riot_js-rails-0.6.0/lib/riot_js/rails/processors/sprockets_processor_v3.rb:21)
brauliobo commented 8 years ago

Thanks! 0.6.1 released

bsedin commented 8 years ago

Thanks!

nikhgupta commented 7 years ago

The same deprecation warning is thrown here!

Moreover, instead of silencing the deprecation on Sprockets 3.7 onwards, its better to make it work for all versions of sprockets (2,3 and 4)!

Something like this should work for line 16:

if env.respond_to?(:register_transformer)
  env.register_mime_type 'text/haml', extensions: ['.haml']
  env.register_preprocessor 'text/haml', ::Tilt::HamlTemplate
end

if env.respond_to?(:register_engine)
  args = ['.haml', ::Tilt::HamlTemplate]
  args << { mime_type: 'text/haml', silence_deprecation: true } if Sprockets::VERSION.start_with?("3")
  env.register_engine(*args)
end

You can wrap this up into a utility method, like this:

def register_engine_for_all_sprockets_version(extension, mime_type, preprocessor)
  if env.respond_to?(:register_transformer)
    env.register_mime_type mime_type, extensions: [ extension ]
    env.register_preprocessor mime_type, preprocessor
  end

  if env.respond_to?(:register_engine)
    args = [extension, preprocessor]
    args << { mime_type: mime_type, silence_deprecation: true } if Sprockets::VERSION.start_with?("3")
    env.register_engine(*args)
  end
end

register_engine_for_all_sprockets_version(".haml", "text/haml", ::Tilt::HamlTemplate)
brauliobo commented 7 years ago

@nikhgupta https://github.com/bjarosze/riot_js-rails/pull/18 was merged