# encoding: utf-8 require 'pathname' require 'sprockets/sass_importer' module Sprockets class SassImporter < ::Sass::Importers::Filesystem GLOB = /\*|\[.+\]/ attr_reader :context private :context def initialize(context, root) @context = context super root.to_s end def extensions { 'css' => :scss, 'css.erb' => :scss, 'scss.erb' => :scss, 'sass.erb' => :sass }.merge!(super) end def find(name, options = {}) return nil if name =~ GLOB # globs must be relative engine_from_path(name, root, options) end def find_relative(name, base, options) if name =~ GLOB glob_imports(name, Pathname.new(base), options) else engine_from_path(name, File.dirname(base), options) end end protected def each_globbed_file(glob, base_pathname, options) Dir["#{base_pathname}/#{glob}"].sort.each do |filename| next if filename == options[:filename] yield filename if File.directory?(filename) || context.asset_requirable?(filename) end end def glob_imports(glob, base_pathname, options) contents = "" each_globbed_file(glob, base_pathname.dirname, options) do |filename| if File.directory?(filename) context.depend_on(filename) elsif context.asset_requirable?(filename) context.depend_on(filename) contents << "@import #{Pathname.new(filename).relative_path_from(base_pathname.dirname).to_s.inspect};\n" end end return nil if contents.empty? Sass::Engine.new(contents, options.merge( filename: base_pathname.to_s, importer: self, syntax: :scss )) end def engine_from_path(name, dir, options) full_filename, syntax = Sass::Util.destructure(find_real_file(dir, name, options)) return unless full_filename && File.readable?(full_filename) engine = Sass::Engine.new(evaluate(full_filename), options.merge( filename: full_filename, importer: self, syntax: syntax )) if engine && (filename = engine.options[:filename]) @context.depend_on(filename) end engine end def evaluate(filename) attributes = context.environment.attributes_for(filename) processors = context.environment.preprocessors(attributes.content_type) + attributes.engines.reverse - [Sprockets::ScssTemplate, Sprockets::SassTemplate] context.evaluate(filename, processors: processors) end end end