Class: Sprockets::Utils::Gzip

Inherits:
Object
  • Object
show all
Defined in:
lib/sprockets/utils/gzip.rb

Defined Under Namespace

Modules: ZlibArchiver, ZopfliArchiver

Constant Summary collapse

COMPRESSIBLE_MIME_TYPES =

What non-text mime types should we compress? This list comes from: https://www.fastly.com/blog/new-gzip-settings-and-deciding-what-compress

{
  "application/vnd.ms-fontobject" => true,
  "application/x-font-opentype" => true,
  "application/x-font-ttf" => true,
  "image/x-icon" => true,
  "image/svg+xml" => true
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(asset, archiver: ZlibArchiver) ⇒ Gzip

Private: Generates a gzipped file based off of reference file.



45
46
47
48
49
50
# File 'lib/sprockets/utils/gzip.rb', line 45

def initialize(asset, archiver: ZlibArchiver)
  @content_type  = asset.content_type
  @source        = asset.source
  @charset       = asset.charset
  @archiver      = archiver
end

Instance Attribute Details

#archiverObject (readonly)

Returns the value of attribute archiver.



42
43
44
# File 'lib/sprockets/utils/gzip.rb', line 42

def archiver
  @archiver
end

#charsetObject (readonly)

Returns the value of attribute charset.



42
43
44
# File 'lib/sprockets/utils/gzip.rb', line 42

def charset
  @charset
end

#content_typeObject (readonly)

Returns the value of attribute content_type.



42
43
44
# File 'lib/sprockets/utils/gzip.rb', line 42

def content_type
  @content_type
end

#sourceObject (readonly)

Returns the value of attribute source.



42
43
44
# File 'lib/sprockets/utils/gzip.rb', line 42

def source
  @source
end

Instance Method Details

#can_compress?Boolean

Private: Returns whether or not an asset can be compressed.

We want to compress any file that is text based. You do not want to compress binary files as they may already be compressed and running them through a compression algorithm would make them larger.

Return Boolean.

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
# File 'lib/sprockets/utils/gzip.rb', line 70

def can_compress?
  # The "charset" of a mime type is present if the value is
  # encoded text. We can check this value to see if the asset
  # can be compressed.
  #
  # We also check against our list of non-text compressible mime types
  @charset || COMPRESSIBLE_MIME_TYPES.include?(@content_type)
end

#cannot_compress?Boolean

Private: Opposite of can_compress?.

Returns Boolean.

Returns:

  • (Boolean)


82
83
84
# File 'lib/sprockets/utils/gzip.rb', line 82

def cannot_compress?
  !can_compress?
end

#compress(file, target) ⇒ Object

Private: Generates a gzipped file based off of reference asset.

Compresses the target asset's contents and puts it into a file with the same name plus a .gz extension in the same folder as the original. Does not modify the target asset.

Returns nothing.



93
94
95
96
97
98
99
# File 'lib/sprockets/utils/gzip.rb', line 93

def compress(file, target)
  mtime = Sprockets::PathUtils.stat(target).mtime
  archiver.call(file, source)
  File.utime(mtime, mtime, file.path)

  nil
end