Module: Plezi::Base::BakeSASS

Extended by:
HasCache
Defined in:
lib/plezi/render/sass.rb,
lib/plezi/render/sassc.rb

Overview

This is a baker, not a renderer

Constant Summary collapse

SASS_OPTIONS =
{ cache_store: Sass::CacheStores::Memory.new, style: (ENV['SASS_STYLE'] || ((ENV['ENV'] || ENV['RACK_ENV']) == 'production' ? :compressed : :nested)) }.dup
SCSSC_OPTIONS =
{ filename: :filename, source_map_file: ((ENV['ENV'] || ENV['RACK_ENV']) == 'production' ? false : true), source_map_embed: ((ENV['ENV'] || ENV['RACK_ENV']) == 'production' ? false : true), load_paths: :load_paths, style: (ENV['SASS_STYLE'] || ((ENV['ENV'] || ENV['RACK_ENV']) == 'production' ? :compressed : :nested)) }.dup

Class Method Summary collapse

Methods included from HasCache

extended, get, store

Class Method Details

.call(target) ⇒ Object

Bakes the SASS for the requested target, if a SASS source file is found.



24
25
26
27
# File 'lib/plezi/render/sass.rb', line 24

def call(target)
   return self[target] if File.extname(target) == '.map'.freeze
   review_cache("#{target}.scss", target) || review_cache("#{target}.sass", target)
end

.load_target(filename, target) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/plezi/render/sassc.rb', line 29

def load_target(filename, target)
   return nil unless File.exist?(filename)
   eng = self[filename]
   return self[target] unless eng.nil?  || refresh_sass?(filename)
   map_name = "#{target}.map".freeze
   opt = SCSSC_OPTIONS.dup
   opt[:source_map_file] = map_name if opt[:source_map_file]
   opt[:filename] = filename
   opt[:load_paths] = [ File.basename(target) ]
   data = IO.binread(filename)
   eng = SassC::Engine.new(data, opt)
   css = eng.render
   map = eng.source_map
   self[filename] = eng.dependencies
   self[filename.to_sym] = Time.now
   IO.write map_name, map.to_json(css_uri: File.basename(target))
   self[target] = css
end

.refresh_sass?(sass) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
# File 'lib/plezi/render/sass.rb', line 41

def refresh_sass?(sass)
   mt = self[sass.to_sym]
   return true if mt < File.mtime(sass)
   self[sass].each { |e| return true if File.exist?(e.options[:filename]) && (File.mtime(e.options[:filename]) > mt) }
   false
end

.review_cache(filename, target) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/plezi/render/sass.rb', line 29

def review_cache(filename, target)
   return nil unless File.exist?(filename)
   eng = self[filename]
   return self[target] unless eng.nil? || refresh_sass?(filename)
   self[filename] = (eng = Sass::Engine.for_file(filename, SASS_OPTIONS)).dependencies
   map_name = "#{target}.map".freeze
   css, map = eng.render_with_sourcemap(File.basename(map_name))
   self[filename.to_sym] = Time.now
   IO.write map_name, map.to_json(css_uri: File.basename(target))
   self[target] = css
end