Class: CssCompressor

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

Instance Method Summary collapse

Instance Method Details

#compress(source) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/css_compressor.rb', line 4

def compress(source)
  res = source
  begin
    # First dump our source to a temp file
    file_in = Tempfile.new
    file_in.write(source)
    file_in.flush
    file_in.fsync
    file_in.close
    # Run our command
    res = `yarn run cleancss #{file_in.path}`
    puts 'cleancss returned nothing' if res.blank?
  rescue StandardError => e
    puts "Exception while processing css #{e}"
    ErrorReporting.error(e, 'Exception while compressing css')
  ensure
    file_in.flush
    file_in.fsync
    file_in.close
    file_in.unlink
  end
  res
end