Sha256: 993f31d9a13787fb4d91ad625e8d3799d9af7c936939d7790b71594acf1a82b0

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

require 'sass-css-importer'
module Sass::Script::Functions
  def asset(url)
    assert_type url, :String
    Sass::Script::Value::String.new("url(\"#{Maglove::Engine.config.asset_uri}/#{url.value}\")")
  end

  def asset_data(url)
    assert_type url, :String
    theme = options[:locals][:theme]
    asset_path = "dist/themes/#{theme}/#{url.value}"
    asset_contents = File.open(asset_path).read
    base64_string = Base64.strict_encode64(asset_contents)
    mime_type = Workspace.file(asset_path).mimetype
    data_uri = "data:#{mime_type};base64,#{base64_string}"
    Sass::Script::Value::String.new("url(#{data_uri})")
  end
  declare(:asset_data, [:url])
end

module MagLove
  module Tilt
    class ScssTemplate < ::Tilt::SassTemplate
      self.default_mime_type = 'text/css'

      def prepare; end

      def evaluate(scope, locals, &block)
        prepared_data = "@base: \"#{locals[:base_path].sub('src/', '../../')}\";\n#{data}"
        engine = Sass::Engine.new(prepared_data, sass_options.merge({ locals: locals }))
        @output ||= engine.render
      end

      private

      def sass_options
        options.merge(filename: eval_file, line: line, syntax: :scss)
      end
    end
  end
end

Tilt.mappings["scss"] = [MagLove::Tilt::ScssTemplate]

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
maglove-2.0.7 lib/maglove/tilt/scss_template.rb