Class: CssCompressor
- Inherits:
-
Object
- Object
- CssCompressor
- Defined in:
- lib/css_compressor.rb
Overview
Shells out to the cleancss Yarn binary to minify a CSS string at
runtime. Used by mailers and dynamic-style generators that build
stylesheets at request time rather than via the asset pipeline.
Instance Method Summary collapse
Instance Method Details
#compress(source) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/css_compressor.rb', line 8 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 |