class Condenser::SassTransformer

Transformer engine class for the SASS/SCSS compiler. Depends on the `sass` gem.

For more infomation see:

https://github.com/sass/sass
https://github.com/rails/sass-rails

Attributes

cache_key[R]
options[RW]

Public Class Methods

cache_key() click to toggle source
# File lib/condenser/transformers/sass_transformer.rb, line 36
def self.cache_key
  instance.cache_key
end
call(environment, input) click to toggle source
# File lib/condenser/transformers/sass_transformer.rb, line 32
def self.call(environment, input)
  instance.call(environment, input)
end
instance() click to toggle source

Public: Return singleton instance with default options.

Returns SassProcessor object.

# File lib/condenser/transformers/sass_transformer.rb, line 28
def self.instance
  @instance ||= new
end
new(options = {}, &block) click to toggle source

Public: Initialize template with custom options.

options - Hash cache_version - String custom cache version. Used to force a cache

change after code changes are made to Sass Functions.
# File lib/condenser/transformers/sass_transformer.rb, line 52
def initialize(options = {}, &block)
  @options = options
  @cache_version = options[:cache_version]
  # @cache_key = "#{self.class.name}:#{VERSION}:#{Autoload::Sass::VERSION}:#{@cache_version}".freeze
  @importer_class = options[:importer] || Condenser::SassTransformer::Importer
  
  @sass_config = options[:sass_config] || {}
  @functions = Module.new do
    include Functions
    include options[:functions] if options[:functions]
    class_eval(&block) if block_given?
  end
  # puts @functions.method(:asset_path).source_location
end
setup(environment) click to toggle source
# File lib/condenser/transformers/sass_transformer.rb, line 21
def self.setup(environment)
  require "sassc" unless defined?(::SassC::Engine)
end
syntax() click to toggle source

Internal: Defines default sass syntax to use. Exposed so the ScssProcessor may override it.

# File lib/condenser/transformers/sass_transformer.rb, line 17
def self.syntax
  :sass
end

Public Instance Methods

call(environment, input) click to toggle source
# File lib/condenser/transformers/sass_transformer.rb, line 67
def call(environment, input)
  # context = input[:environment].context_class.new(input)
  engine_options = merge_options({
    syntax:       self.class.syntax,
    filename:     input[:filename],
    source_map_file: "#{input[:filename]}.map",
    source_map_contents: true,
    # cache_store:  Cache.new(environment.cache),
    load_paths:   environment.path,
    importer:     @importer_class,
    condenser: {
      context: environment.new_context_class,
      environment: environment
    },
    asset: input
  })

  engine = SassC::Engine.new(input[:source], engine_options)
  
  css = Utils.module_include(SassC::Script::Functions, @functions) do
    engine.render
  end
  css.delete_suffix!("\n/*# sourceMappingURL=#{File.basename(input[:filename])}.map */")
  # engine.source_map
  # css = css.delete_suffix!("\n/*# sourceMappingURL= */\n")


  input[:source] = css
  # input[:map] = map.to_json({})
end
name() click to toggle source
# File lib/condenser/transformers/sass_transformer.rb, line 42
def name
  self.class.name
end

Private Instance Methods

build_cache_store(input, version) click to toggle source

Public: Build the cache store to be used by the Sass engine.

input - the input hash. version - the cache version.

Override this method if you need to use a different cache than the Condenser cache.

# File lib/condenser/transformers/sass_transformer.rb, line 107
def build_cache_store(input, version)
  CacheStore.new(input[:cache], version)
end
merge_options(options) click to toggle source
# File lib/condenser/transformers/sass_transformer.rb, line 111
def merge_options(options)
  defaults = @sass_config.dup

  if load_paths = defaults.delete(:load_paths)
    options[:load_paths] += load_paths
  end

  options.merge!(defaults)
  options
end