Sha256: 6f057af2b6af238b4679acad871a97f80fb1bbed7c14c19aed51dd85a2f31f19

Contents?: true

Size: 1.42 KB

Versions: 7

Compression:

Stored size: 1.42 KB

Contents

module Hanami
  module Assets
    class Bundler
      # Compresses a JS or CSS file
      #
      # @since 0.3.0
      # @api private
      class Compressor
        # @since 0.3.0
        # @api private
        JAVASCRIPT_EXT      = '.js'.freeze

        # @since 0.3.0
        # @api private
        STYLESHEET_EXT      = '.css'.freeze

        # Return a new instance
        #
        # @since 0.3.0
        # @api private
        def initialize(path, configuration)
          @path = path
          @configuration = configuration
        end

        # @return [String, nil] the compressed contents of the file OR nil if it's not compressable
        #
        # @since 0.3.0
        # @api private
        def compress
          case File.extname(@path)
          when JAVASCRIPT_EXT then _compress(compressor(:js))
          when STYLESHEET_EXT then _compress(compressor(:css))
          end
        end

        private

        # @since 0.3.0
        # @api private
        def compressor(type)
          @configuration.__send__(:"#{ type }_compressor")
        end

        # @since 0.3.0
        # @api private
        def _compress(compressor)
          compressor.compress(@path)
        rescue => e
          warn(
            [
              "Skipping compression of: `#{@path}'",
              "Reason: #{e}\n",
              "\t#{e.backtrace.join("\n\t")}\n\n"
            ].join("\n")
          )
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
hanami-assets-1.1.0.beta1 lib/hanami/assets/bundler/compressor.rb
hanami-assets-1.0.0 lib/hanami/assets/bundler/compressor.rb
hanami-assets-1.0.0.rc1 lib/hanami/assets/bundler/compressor.rb
hanami-assets-1.0.0.beta2 lib/hanami/assets/bundler/compressor.rb
hanami-assets-1.0.0.beta1 lib/hanami/assets/bundler/compressor.rb
hanami-assets-0.4.0 lib/hanami/assets/bundler/compressor.rb
hanami-assets-0.3.0 lib/hanami/assets/bundler/compressor.rb