Skip to content

First-class Terser support #818

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

johnnyshields
Copy link
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
def initialize(options = {})
@options = options.dup
@options[:comments] ||= :none
@options.merge!(::Rails.application.config.assets.terser.to_h) if defined?(::Rails)
Copy link
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.

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.

1 participant