Sha256: 434e861d0c47854b9f1d509abf9edea52d7e5d763747ef5fa4c8cf82cbcc5ff2

Contents?: true

Size: 1.97 KB

Versions: 9

Compression:

Stored size: 1.97 KB

Contents

require 'tilt'

module Sprockets
  # This custom Tilt handler replaces the one built into Tilt. The
  # main difference is that it uses a custom importer that plays nice
  # with sprocket's caching system.
  #
  # See `SassImporter` for more infomation.
  class SassTemplate < Tilt::Template
    self.default_mime_type = 'text/css'

    def self.engine_initialized?
      defined?(::Sass::Engine) && defined?(::Sass::Script::Functions) &&
        ::Sass::Script::Functions < Sprockets::SassFunctions
    end

    def initialize_engine
      # Double check constant to avoid tilt warning
      unless defined? ::Sass
        require_template_library 'sass'
      end

      # Install custom functions. It'd be great if this didn't need to
      # be installed globally, but could be passed into Engine as an
      # option.
      ::Sass::Script::Functions.send :include, Sprockets::SassFunctions
    end

    def prepare
    end

    def syntax
      :sass
    end

    def evaluate(context, locals, &block)
      # Use custom importer that knows about Sprockets Caching
      cache_store = SassCacheStore.new(context.environment)

      options = {
        :filename => eval_file,
        :line => line,
        :syntax => syntax,
        :cache_store => cache_store,
        :importer => SassImporter.new(context.pathname.to_s),
        :load_paths => context.environment.paths.map { |path| SassImporter.new(path.to_s) },
        :sprockets => {
          :context => context,
          :environment => context.environment
        }
      }

      result = ::Sass::Engine.new(data, options).render

      # Track all imported files
      filenames = ([options[:importer].imported_filenames] + options[:load_paths].map(&:imported_filenames)).flatten.uniq
      filenames.each { |filename| context.depend_on(filename) }

      result
    rescue ::Sass::SyntaxError => e
      # Annotates exception message with parse line number
      context.__LINE__ = e.sass_backtrace.first[:line]
      raise e
    end
  end
end

Version data entries

9 entries across 9 versions & 4 rubygems

Version Path
sprockets-2.12.5 lib/sprockets/sass_template.rb
arcabouco-0.2.13 vendor/bundle/gems/sprockets-2.12.4/lib/sprockets/sass_template.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/sprockets-2.12.4/lib/sprockets/sass_template.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/sprockets-2.12.3/lib/sprockets/sass_template.rb
sprockets-2.12.4 lib/sprockets/sass_template.rb
sprockets-2.12.3 lib/sprockets/sass_template.rb
sprockets-2.12.2 lib/sprockets/sass_template.rb
sprockets-2.12.1 lib/sprockets/sass_template.rb
sprockets-2.12.0 lib/sprockets/sass_template.rb