Skip to content

First-class Terser support#818

Closed
johnnyshields wants to merge 4 commits intorails:mainfrom
johnnyshields:autoload-terser
Closed

First-class Terser support#818
johnnyshields wants to merge 4 commits intorails:mainfrom
johnnyshields:autoload-terser

Conversation

@johnnyshields
Copy link
Copy Markdown
Contributor

@johnnyshields johnnyshields commented Aug 7, 2025

This PR ports https://github.com/ahorek/terser-ruby/blob/master/lib/terser/compressor.rb (MIT licensed) to this gem, to add first-class support for Terser in the same manner that Uglifier, YUI, etc. are supported.

Issue raised in Terser gem to notify the maintainers of this port: ahorek/terser-ruby#65


Motivation

Recently, I replaced the Uglifier with Terser in my Gemfile and Rails config, expecting it to be a drop in replacement:

  # in Gemfile
  group :assets do
    gem 'terser'  # was gem 'uglifier'
    ...

  # in config/environments/*.rb
  config.assets.js_compressor = :terser  # was :uglifier

When I deployed to staging, the process errored during bootstrapping because constant Terser could not be found. In a nutshell, the reason why Uglifier works and Terser does not is because Sprockets::UglifierCompressor exists in Sprockets lib and does not require the uglifier gem to already be loaded, while the Terser equivalent is loaded by a Railtie in the terser gem which does require loading terser gem first.

@johnnyshields johnnyshields changed the title Autoload Terser + add tests for it First-class Terser support Aug 7, 2025
Comment thread lib/sprockets/terser_compressor.rb Outdated
def initialize(options = {})
@options = options.dup
@options[:comments] ||= :none
@options.merge!(::Rails.application.config.assets.terser.to_h) if defined?(::Rails)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is probably a better way to do this line. Need to investigate how Sass achieves a similar thing.

@rafaelfranca
Copy link
Copy Markdown
Member

There is no need for this gem to include any code if the terser gem can already do that for us. Thank you for the pull request

@johnnyshields
Copy link
Copy Markdown
Contributor Author

johnnyshields commented Aug 27, 2025

@rafaelfranca please reconsider opening this. Without this PR, you can't do a drop-in upgrade from Uglifier:

config.assets.js_compressor = :terser 

(Refer to the PR description.) This is contrary to user expectations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants