Skip to content

Commit bd63297

Browse files
Rafael Mendonça Françamatthewd
authored andcommitted
Define our own importer instead of monkey patching sprockets
1 parent a62bdf8 commit bd63297

File tree

2 files changed

+16
-100
lines changed

2 files changed

+16
-100
lines changed

lib/sass/rails/importer.rb

Lines changed: 14 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,20 @@
11
require 'sprockets/sass_importer'
22

3-
module Sprockets
4-
class SassImporter < Sass::Importers::Filesystem
5-
GLOB = /\*|\[.+\]/
6-
7-
attr_reader :context
8-
private :context
9-
10-
def extensions
11-
{
12-
'css' => :scss,
13-
'css.scss' => :scss,
14-
'css.sass' => :sass,
15-
'css.erb' => :scss,
16-
'scss.erb' => :scss,
17-
'sass.erb' => :sass,
18-
'css.scss.erb' => :scss,
19-
'css.sass.erb' => :sass
20-
}.merge!(super)
21-
end
22-
23-
def find_relative(name, base, options)
24-
if name =~ GLOB
25-
glob_imports(name, Pathname.new(base), options)
26-
else
27-
engine_from_path(name, File.dirname(base), options)
28-
end
29-
end
30-
31-
def find(name, options)
32-
if name =~ GLOB
33-
nil # globs must be relative
34-
else
35-
engine_from_path(name, root, options)
36-
end
37-
end
38-
39-
def each_globbed_file(glob, base_pathname, options)
40-
Dir["#{base_pathname}/#{glob}"].sort.each do |filename|
41-
next if filename == options[:filename]
42-
yield filename if File.directory?(filename) || context.asset_requirable?(filename)
43-
end
44-
end
45-
46-
def glob_imports(glob, base_pathname, options)
47-
contents = ""
48-
each_globbed_file(glob, base_pathname.dirname, options) do |filename|
49-
if File.directory?(filename)
50-
depend_on(filename)
51-
elsif context.asset_requirable?(filename)
52-
depend_on(filename)
53-
contents << "@import #{Pathname.new(filename).relative_path_from(base_pathname.dirname).to_s.inspect};\n"
54-
end
3+
module Sass
4+
module Rails
5+
class SassImporter < Sprockets::SassImporter
6+
def extensions
7+
{
8+
'css' => :scss,
9+
'css.scss' => :scss,
10+
'css.sass' => :sass,
11+
'css.erb' => :scss,
12+
'scss.erb' => :scss,
13+
'sass.erb' => :sass,
14+
'css.scss.erb' => :scss,
15+
'css.sass.erb' => :sass
16+
}.merge!(super)
5517
end
56-
return nil if contents.empty?
57-
Sass::Engine.new(contents, options.merge(
58-
:filename => base_pathname.to_s,
59-
:importer => self,
60-
:syntax => :scss
61-
))
6218
end
63-
64-
private
65-
66-
def depend_on(filename)
67-
context.depend_on(filename)
68-
context.depend_on(globbed_file_parent(filename))
69-
end
70-
71-
def globbed_file_parent(filename)
72-
if File.directory?(filename)
73-
File.expand_path('..', filename)
74-
else
75-
File.dirname(filename)
76-
end
77-
end
78-
79-
def engine_from_path(name, dir, options)
80-
full_filename, syntax = Sass::Util.destructure(find_real_file(dir, name, options))
81-
return unless full_filename && File.readable?(full_filename)
82-
83-
engine = Sass::Engine.new(evaluate(full_filename), options.merge(
84-
syntax: syntax,
85-
filename: full_filename,
86-
importer: self
87-
))
88-
89-
if engine && (filename = engine.options[:filename])
90-
@context.depend_on(filename)
91-
end
92-
93-
engine
94-
end
95-
96-
def evaluate(filename)
97-
attributes = context.environment.attributes_for(filename)
98-
processors = context.environment.preprocessors(attributes.content_type) +
99-
attributes.engines.reverse - [Sprockets::ScssTemplate, Sprockets::SassTemplate]
100-
101-
context.evaluate(filename, processors: processors)
102-
end
10319
end
10420
end

lib/sass/rails/template.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ def evaluate(context, locals, &block)
1212
:line => line,
1313
:syntax => syntax,
1414
:cache_store => cache_store,
15-
:importer => Sprockets::SassImporter.new(context.pathname.to_s),
16-
:load_paths => context.environment.paths.map { |path| Sprockets::SassImporter.new(path.to_s) },
15+
:importer => SassImporter.new(context.pathname.to_s),
16+
:load_paths => context.environment.paths.map { |path| SassImporter.new(path.to_s) },
1717
:sprockets => {
1818
:context => context,
1919
:environment => context.environment

0 commit comments

Comments
 (0)