ahorek / terser-ruby

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Usage with legacy applications

sobrinho opened this issue · comments

Trying to use with a legacy application in Rails 3.2 and Sprockets does not have the Sprockets::DigestUtils.

Any way to workaround?

hi,
only Sprockets 3.x and 4.x are supported. Sprockets 2.x is too old and Rails 3 reached its end of support in 2016.

1/ you may try to write your own adapter based on https://github.com/rails/sprockets/blob/2.x/lib/sprockets/uglifier_compressor.rb
2/ use https://github.com/lautis/uglifier that should still work
3/ upgrade your application

the main reason for this gem is to support latest ECMA 6+ features. You'll unlikely need latest features for a legacy application based on Rails 3. There's no reason to support it officially, sorry.

We are using Rails LTS and we have webpack configured with sprockets.

Not a regular use case, though 😂

I made it work with that:

# lib/sprockets/digest_utils.rb
# Dummy file to make terser work with legacy sprockets.
#
# We are just workarounding the missing module and using a custom compressor over terser.
#
# See https://github.com/ahorek/terser-ruby/issues/11
if Sprockets::VERSION != '2.2.3'
  raise 'This patch only works with Sprockets 2.2.3, be sure to repatch when upgrade'
end

And the compressor:

# lib/custom_terser_compressor.rb
class CustomTerserCompressor
  def compress(source)
    terser.compile(source)
  end

  def terser
    @terser ||= begin
      require 'terser'
      Terser.new(comments: :none)
    end
  end
end

And:

  config.assets.js_compressor = CustomTerserCompressor.new

Thanks! ❤️

This gem doesn't depend on the exact rails/sprockets version directly. Unfortunately, each version of legacy sprockets has a different API. In such cases, it's better to write your own adapter as you did. It's a 100% valid solution. I'm glad it works for you!