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