Class: Condenser::BrotliWriter
- Inherits:
-
Object
- Object
- Condenser::BrotliWriter
- Defined in:
- lib/condenser/writers/brotli_writer.rb
Overview
Generates a ‘.br` file using the Brotli algorithm with the brotli gem
Constant Summary collapse
- COMPRESSALBE_TEXT_TYPES =
What mime types should we compress? This list comes from: www.fastly.com/blog/new-gzip-settings-and-deciding-what-compress
%w( text/html application/x-javascript text/css application/javascript text/javascript text/plain text/xml application/json application/xml image/svg+xml)
- COMPRESSALBE_FONT_TYPES =
%w( application/vnd.ms-fontobject application/x-font-opentype application/x-font-truetype application/x-font-ttf font/eot font/opentype font/otf)
- COMPRESSALBE_GENERIC_TYPES =
%w( image/vnd.microsoft.icon image/x-icon )
- ADDED_MIME_TYPES =
['application/brotli']
Instance Attribute Summary collapse
-
#added_mime_types ⇒ Object
readonly
Returns the value of attribute added_mime_types.
-
#mime_types ⇒ Object
readonly
Returns the value of attribute mime_types.
Instance Method Summary collapse
- #call(output_directory, asset) ⇒ Object
- #exist?(asset) ⇒ Boolean
-
#initialize(mime_types: nil, added_mime_types: nil) ⇒ BrotliWriter
constructor
A new instance of BrotliWriter.
- #mode_for_mime_type(mime_type) ⇒ Object
- #path(asset) ⇒ Object
Constructor Details
#initialize(mime_types: nil, added_mime_types: nil) ⇒ BrotliWriter
Returns a new instance of BrotliWriter.
21 22 23 24 |
# File 'lib/condenser/writers/brotli_writer.rb', line 21 def initialize(mime_types: nil, added_mime_types: nil) @mime_types = mime_types || (COMPRESSALBE_TEXT_TYPES + COMPRESSALBE_FONT_TYPES + COMPRESSALBE_GENERIC_TYPES) @added_mime_types = added_mime_types || ADDED_MIME_TYPES end |
Instance Attribute Details
#added_mime_types ⇒ Object (readonly)
Returns the value of attribute added_mime_types.
19 20 21 |
# File 'lib/condenser/writers/brotli_writer.rb', line 19 def added_mime_types @added_mime_types end |
#mime_types ⇒ Object (readonly)
Returns the value of attribute mime_types.
19 20 21 |
# File 'lib/condenser/writers/brotli_writer.rb', line 19 def mime_types @mime_types end |
Instance Method Details
#call(output_directory, asset) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/condenser/writers/brotli_writer.rb', line 44 def call(output_directory, asset) filename = File.join(output_directory, "#{asset.path}.br") FileUtils.mkdir_p(File.dirname(filename)) Condenser::Utils.atomic_write(filename) do |file| file.write(Brotli.deflate(asset.source, { mode: mode_for_mime_type(asset.content_types.last), quality: 11 # lgwin: 10-24 # lgblock: 16-24 think bigger more compression but more mem })) end ["#{asset.filename}.br"] end |
#exist?(asset) ⇒ Boolean
30 31 32 |
# File 'lib/condenser/writers/brotli_writer.rb', line 30 def exist?(asset) ::File.exist?(path(asset)) end |
#mode_for_mime_type(mime_type) ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/condenser/writers/brotli_writer.rb', line 34 def mode_for_mime_type(mime_type) if COMPRESSALBE_TEXT_TYPES.include?(mime_type) :text elsif COMPRESSALBE_FONT_TYPES.include?(mime_type) :font else :generic end end |
#path(asset) ⇒ Object
26 27 28 |
# File 'lib/condenser/writers/brotli_writer.rb', line 26 def path(asset) "#{asset.path}.br" end |