class PurgecssSprockets::PostProcessor

Public Class Methods

call(input) click to toggle source
# File lib/purgecss_sprockets/post_processor.rb, line 36
def self.call(input)
  filename = input[:filename]
  source   = input[:data]
  context  = input[:environment].context_class.new(input)

  result = run(filename, source, context)
  context.metadata.merge(data: result)
end
new(filename, &block) click to toggle source
# File lib/purgecss_sprockets/post_processor.rb, line 3
def initialize(filename, &block)
  @filename = filename
  @source   = block.call
end
run(filename, source, context) click to toggle source
# File lib/purgecss_sprockets/post_processor.rb, line 12
def self.run(filename, source, context)
  return source if PurgecssSprockets.disabled

  filepath_from_root = filename.gsub("#{Rails.root}/", '')
  if context && context.environment
    if PurgecssSprockets.exclude_files.include?(filepath_from_root)
      context.environment.logger&.info "Skipping CSS purge of #{filename}"
      return source
    else
      context.environment.logger&.info "Purging CSS from #{filename}"
    end
  end

  file = Tempfile.new(filename)
  file.write(source.force_encoding('UTF-8'))
  file.rewind

  purgecss_result = JSON.parse(`#{PurgecssSprockets.purgecss_cmd} --css #{file.path} --config purgecss.config.js`)
  file.close
  file.unlink

  purgecss_result[0]['css']
end

Public Instance Methods

render(context, empty_hash_wtf) click to toggle source
# File lib/purgecss_sprockets/post_processor.rb, line 8
def render(context, empty_hash_wtf)
  self.class.run(@filename, @source, context)
end