Class: PurgecssSprockets::PostProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/purgecss_sprockets/post_processor.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, &block) ⇒ PostProcessor

Returns a new instance of PostProcessor.



3
4
5
6
# File 'lib/purgecss_sprockets/post_processor.rb', line 3

def initialize(filename, &block)
  @filename = filename
  @source   = block.call
end

Class Method Details

.call(input) ⇒ Object



36
37
38
39
40
41
42
43
# 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..merge(data: result)
end

.run(filename, source, context) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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

Instance Method Details

#render(context, empty_hash_wtf) ⇒ Object



8
9
10
# File 'lib/purgecss_sprockets/post_processor.rb', line 8

def render(context, empty_hash_wtf)
  self.class.run(@filename, @source, context)
end