Class: Radical::AssetCompiler

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

Class Method Summary collapse

Class Method Details

.compile(assets, path:, compressor: :none) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/radical/asset_compiler.rb', line 14

def self.compile(assets, path:, compressor: :none)
  s = assets.map(&:content).join("\n")
  ext = assets.first.ext

  # hash the contents of each concatenated asset
  hash = Digest::SHA1.hexdigest s

  # use hash to bust the cache
  name = "#{hash}#{ext}"
  filename = File.join(path, name)

  case compressor
  when :gzip
    name = "#{name}.gz"
    gzip("#{filename}.gz", s)
  when :brotli
    name = "#{name}.br"
    File.write("#{filename}.br", Brotli.deflate(s, mode: :text, quality: 11))
  else
    File.write(filename, s)
  end

  # output asset path for browser
  "/assets/#{name}"
end

.gzip(filename, content) ⇒ Object



9
10
11
12
# File 'lib/radical/asset_compiler.rb', line 9

def self.gzip(filename, content)
  # nil, 31 == support for gunzip
  File.write(filename, Zlib::Deflate.new(nil, 31).deflate(content, Zlib::FINISH))
end