Class: Sprockets::SassProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/sprockets/sass_processor.rb,
lib/sprockets/sass_cache_store.rb

Overview

Processor 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

ScssProcessor

Defined Under Namespace

Modules: Functions Classes: CacheStore

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

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.

47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sprockets/sass_processor.rb', line 47

def initialize(options = {}, &block)
  @cache_version = options[:cache_version]
  @cache_key = "#{self.class.name}:#{VERSION}:#{Autoload::Sass::VERSION}:#{@cache_version}".freeze
  @importer_class = options[:importer] || Sass::Importers::Filesystem
  @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


39
40
41
# File 'lib/sprockets/sass_processor.rb', line 39

def cache_key
  @cache_key
end

Class Method Details

.cache_keyObject


35
36
37
# File 'lib/sprockets/sass_processor.rb', line 35

def self.cache_key
  instance.cache_key
end

.call(input) ⇒ Object


31
32
33
# File 'lib/sprockets/sass_processor.rb', line 31

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

.instanceObject

Public: Return singleton instance with default options.

Returns SassProcessor object.


27
28
29
# File 'lib/sprockets/sass_processor.rb', line 27

def self.instance
  @instance ||= new
end

.syntaxObject

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


20
21
22
# File 'lib/sprockets/sass_processor.rb', line 20

def self.syntax
  :sass
end

Instance Method Details

#call(input) ⇒ Object


59
60
61
62
63
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
92
93
94
# File 'lib/sprockets/sass_processor.rb', line 59

def call(input)
  context = input[:environment].context_class.new(input)

  engine_options = merge_options({
    filename: input[:filename],
    syntax: self.class.syntax,
    cache_store: build_cache_store(input, @cache_version),
    load_paths: context.environment.paths.map { |p| @importer_class.new(p.to_s) },
    importer: @importer_class.new(Pathname.new(context.filename).to_s),
    sprockets: {
      context: context,
      environment: input[:environment],
      dependencies: context.[:dependencies]
    }
  })

  engine = Autoload::Sass::Engine.new(input[:data], engine_options)

  css, map = Utils.module_include(Autoload::Sass::Script::Functions, @functions) do
    engine.render_with_sourcemap('')
  end

  css = css.sub("\n/*# sourceMappingURL= */\n", '')

  map = SourceMapUtils.format_source_map(JSON.parse(map.to_json(css_uri: '')), input)
  map = SourceMapUtils.combine_source_maps(input[:metadata][:map], map)

  # Track all imported files
  sass_dependencies = Set.new([input[:filename]])
  engine.dependencies.map do |dependency|
    sass_dependencies << dependency.options[:filename]
    context.[:dependencies] << URIUtils.build_file_digest_uri(dependency.options[:filename])
  end

  context..merge(data: css, sass_dependencies: sass_dependencies, map: map)
end