Class: Condenser::SassTransformer

Inherits:
Object
  • Object
show all
Defined in:
lib/condenser/transformers/sass_transformer.rb

Overview

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

Direct Known Subclasses

ScssTransformer

Defined Under Namespace

Modules: Functions

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ SassTransformer

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.


50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/condenser/transformers/sass_transformer.rb', line 50

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::Sass::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
end

Instance Attribute Details

#cache_keyObject (readonly)

Returns the value of attribute cache_key.



38
39
40
# File 'lib/condenser/transformers/sass_transformer.rb', line 38

def cache_key
  @cache_key
end

#optionsObject

Returns the value of attribute options.



11
12
13
# File 'lib/condenser/transformers/sass_transformer.rb', line 11

def options
  @options
end

Class Method Details

.cache_keyObject



34
35
36
# File 'lib/condenser/transformers/sass_transformer.rb', line 34

def self.cache_key
  instance.cache_key
end

.call(environment, input) ⇒ Object



30
31
32
# File 'lib/condenser/transformers/sass_transformer.rb', line 30

def self.call(environment, input)
  instance.call(environment, input)
end

.instanceObject

Public: Return singleton instance with default options.

Returns SassProcessor object.



26
27
28
# File 'lib/condenser/transformers/sass_transformer.rb', line 26

def self.instance
  @instance ||= new
end

.setup(environment) ⇒ Object



19
20
21
# File 'lib/condenser/transformers/sass_transformer.rb', line 19

def self.setup(environment)
  require "sassc" unless defined?(::SassC::Engine)
end

.syntaxObject

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



15
16
17
# File 'lib/condenser/transformers/sass_transformer.rb', line 15

def self.syntax
  :sass
end

Instance Method Details

#call(environment, input) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/condenser/transformers/sass_transformer.rb', line 64

def call(environment, input)
  context = environment.new_context_class
  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: context, environment: environment },
    asset: input
  })
  
  engine = SassC::Engine.new(input[:source], engine_options)

  css = Condenser::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({})
  input[:linked_assets]         += context.links
  input[:process_dependencies]  += context.dependencies
end

#nameObject



40
41
42
# File 'lib/condenser/transformers/sass_transformer.rb', line 40

def name
  self.class.name
end