From bd63297c31aceb079595c3a3d9007b0e4df505b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Thu, 13 Mar 2014 21:13:33 -0300 Subject: [PATCH] Define our own importer instead of monkey patching sprockets --- lib/sass/rails/importer.rb | 112 ++++++--------------------------------------- lib/sass/rails/template.rb | 4 +- 2 files changed, 16 insertions(+), 100 deletions(-) diff --git a/lib/sass/rails/importer.rb b/lib/sass/rails/importer.rb index 23c03e9..e31302c 100644 --- a/lib/sass/rails/importer.rb +++ b/lib/sass/rails/importer.rb @@ -1,104 +1,20 @@ require 'sprockets/sass_importer' -module Sprockets - class SassImporter < Sass::Importers::Filesystem - GLOB = /\*|\[.+\]/ - - attr_reader :context - private :context - - def extensions - { - 'css' => :scss, - 'css.scss' => :scss, - 'css.sass' => :sass, - 'css.erb' => :scss, - 'scss.erb' => :scss, - 'sass.erb' => :sass, - 'css.scss.erb' => :scss, - 'css.sass.erb' => :sass - }.merge!(super) - 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 - - def find(name, options) - if name =~ GLOB - nil # globs must be relative - else - engine_from_path(name, root, options) - end - end - - 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) - depend_on(filename) - elsif context.asset_requirable?(filename) - depend_on(filename) - contents << "@import #{Pathname.new(filename).relative_path_from(base_pathname.dirname).to_s.inspect};\n" - end +module Sass + module Rails + class SassImporter < Sprockets::SassImporter + def extensions + { + 'css' => :scss, + 'css.scss' => :scss, + 'css.sass' => :sass, + 'css.erb' => :scss, + 'scss.erb' => :scss, + 'sass.erb' => :sass, + 'css.scss.erb' => :scss, + 'css.sass.erb' => :sass + }.merge!(super) end - return nil if contents.empty? - Sass::Engine.new(contents, options.merge( - :filename => base_pathname.to_s, - :importer => self, - :syntax => :scss - )) end - - private - - def depend_on(filename) - context.depend_on(filename) - context.depend_on(globbed_file_parent(filename)) - end - - def globbed_file_parent(filename) - if File.directory?(filename) - File.expand_path('..', filename) - else - File.dirname(filename) - end - 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( - syntax: syntax, - filename: full_filename, - importer: self - )) - - 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 diff --git a/lib/sass/rails/template.rb b/lib/sass/rails/template.rb index 9def8d7..be7ccca 100644 --- a/lib/sass/rails/template.rb +++ b/lib/sass/rails/template.rb @@ -12,8 +12,8 @@ def evaluate(context, locals, &block) :line => line, :syntax => syntax, :cache_store => cache_store, - :importer => Sprockets::SassImporter.new(context.pathname.to_s), - :load_paths => context.environment.paths.map { |path| Sprockets::SassImporter.new(path.to_s) }, + :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 -- 1.9.3