ixti/jekyll-assets

View on GitHub
lib/jekyll/assets/compressors/uglify.rb

Summary

Maintainability
A
35 mins
Test Coverage
# Frozen-string-literal: true
# Copyright: 2017 - 2018 - MIT License
# Author: Jordon Bedwell
# Encoding: utf-8

module Jekyll
  module Assets
    module Compressors
      class Uglify < Sprockets::UglifierCompressor
        def call(input)
          out = super(input)
          Hook.trigger :asset, :after_compression do |h|
            !out.is_a?(Hash) \
              ? out = h.call(input, out, 'application/javascript')
              : out[:data] = h.call(
                input, out[:data],
                'application/javascript'
              )
          end

          out
        end
      end

      Sprockets.register_compressor "application/javascript", :assets_uglify, Uglify
      Hook.register :env, :after_init, priority: 3 do |e|
        enable = e.asset_config[:compression]
        config = e.asset_config[:compressors][:uglifier].symbolize_keys
        e.js_compressor = nil

        if enable && Utils.javascript? && Utils.activate("uglifier")
          e.js_compressor = Uglify.new(config)
        end
      end
    end
  end
end