3
3
module SassC
4
4
module Rails
5
5
class Importer < SassC ::Importer
6
-
7
6
class Extension
8
7
attr_reader :postfix
9
8
10
9
def initialize ( postfix )
11
10
@postfix = postfix
12
11
end
13
12
14
- def import_for ( original_path , parent_path , full_path )
13
+ def import_for ( original_path , parent_path , full_path , options )
15
14
SassC ::Importer ::Import . new ( full_path )
16
15
end
17
16
end
@@ -21,20 +20,25 @@ def postfix
21
20
".css"
22
21
end
23
22
24
- def import_for ( original_path , parent_path , full_path )
23
+ def import_for ( original_path , parent_path , full_path , options )
25
24
import_path = full_path . gsub ( /\. css$/ , "" )
26
25
SassC ::Importer ::Import . new ( import_path )
27
26
end
28
27
end
29
28
30
- class ERBExtension < Extension
31
- def initialize ( postfix )
32
- super
29
+ class SassERBExtension < Extension
30
+ def import_for ( original_path , parent_path , full_path , options )
31
+ template = Tilt ::ERBTemplate . new ( full_path )
32
+ parsed_erb = template . render ( options [ :sprockets ] [ :context ] , { } )
33
+ parsed_scss = SassC ::Sass2Scss . convert ( parsed_erb )
34
+ SassC ::Importer ::Import . new ( full_path , source : parsed_scss )
33
35
end
36
+ end
34
37
35
- def import_for ( original_path , parent_path , full_path )
38
+ class ERBExtension < Extension
39
+ def import_for ( original_path , parent_path , full_path , options )
36
40
template = Tilt ::ERBTemplate . new ( full_path )
37
- parsed_erb = template . render
41
+ parsed_erb = template . render ( options [ :sprockets ] [ :context ] , { } )
38
42
SassC ::Importer ::Import . new ( full_path , source : parsed_erb )
39
43
end
40
44
end
@@ -44,42 +48,13 @@ def import_for(original_path, parent_path, full_path)
44
48
Extension . new ( ".sass" ) ,
45
49
CSSExtension . new ,
46
50
ERBExtension . new ( ".scss.erb" ) ,
47
- ERBExtension . new ( ".sass.erb" ) ,
51
+ SassERBExtension . new ( ".sass.erb" ) ,
48
52
ERBExtension . new ( ".css.erb" ) ,
49
53
]
50
54
51
55
PREFIXS = [ "" , "_" ]
52
56
53
57
def imports ( path , parent_path )
54
- # should return an Import, or array of Imports.
55
- # Import.new(path)
56
-
57
- # Imports can usually be passed with only a path. However,
58
- # when parsing ERB we need to return the import like this:
59
- # Import.new(path, source: parsed_erb)
60
- # in this case, the path is just for reference, i don't believe it
61
- # gets used.
62
-
63
- # the parent path is basically the file that's calling the
64
- # @import function. This necessary when we need to do an import
65
- # relative to the folder that the import function is being called from.
66
-
67
- # for the filename, it looks like libsass can properly resolve
68
- # filenames without extensions provided that the actual file has
69
- # a .sass or .scss filename.
70
-
71
- # it CANNOT resolve a .css file unless you explicitly pass the
72
- # filename with the .css extension.
73
-
74
- # lets ignore globbing for now.
75
-
76
- # for ERB parsing, check out the sass-rails importer below, it looks pretty
77
- # simple
78
-
79
- # check out
80
- # https://github.com/sass/sass/blob/stable/lib/sass/importers/filesystem.rb
81
- # for reference
82
-
83
58
parent_dir , _ = File . split ( parent_path )
84
59
specified_dir , specified_file = File . split ( path )
85
60
@@ -99,7 +74,7 @@ def imports(path, parent_path)
99
74
try_path = File . join ( search_path , file_name + extension . postfix )
100
75
if File . exists? ( try_path )
101
76
record_import_as_dependency try_path
102
- return extension . import_for ( path , parent_path , try_path )
77
+ return extension . import_for ( path , parent_path , try_path , options )
103
78
end
104
79
end
105
80
end
@@ -124,151 +99,3 @@ def load_paths
124
99
end
125
100
end
126
101
end
127
-
128
- #require 'active_support/deprecation/reporting'
129
- #require 'sass'
130
- #require 'sprockets/sass_importer'
131
- #require 'tilt'
132
-
133
- # module Sass
134
- # module Rails
135
- # class SassImporter < Sass::Importers::Filesystem
136
- # module Globbing
137
- # GLOB = /(\A|\/)(\*|\*\*\/\*)\z/
138
- #
139
- # def find_relative(name, base, options)
140
- # if options[:sprockets] && m = name.match(GLOB)
141
- # path = name.sub(m[0], "")
142
- # base = File.expand_path(path, File.dirname(base))
143
- # glob_imports(base, m[2], options)
144
- # else
145
- # super
146
- # end
147
- # end
148
- #
149
- # def find(name, options)
150
- # # globs must be relative
151
- # return if name =~ GLOB
152
- # super
153
- # end
154
- #
155
- # private
156
- # def glob_imports(base, glob, options)
157
- # contents = ""
158
- # context = options[:sprockets][:context]
159
- # each_globbed_file(base, glob, context) do |filename|
160
- # next if filename == options[:filename]
161
- # contents << "@import #{filename.inspect};\n"
162
- # end
163
- # return nil if contents == ""
164
- # Sass::Engine.new(contents, options.merge(
165
- # :filename => base,
166
- # :importer => self,
167
- # :syntax => :scss
168
- # ))
169
- # end
170
- #
171
- # def each_globbed_file(base, glob, context)
172
- # raise ArgumentError unless glob == "*" || glob == "**/*"
173
- #
174
- # exts = extensions.keys.map { |ext| Regexp.escape(".#{ext}") }.join("|")
175
- # sass_re = Regexp.compile("(#{exts})$")
176
- #
177
- # context.depend_on(base)
178
- #
179
- # Dir["#{base}/#{glob}"].sort.each do |path|
180
- # if File.directory?(path)
181
- # context.depend_on(path)
182
- # elsif sass_re =~ path
183
- # yield path
184
- # end
185
- # end
186
- # end
187
- # end
188
- #
189
- # module ERB
190
- # def extensions
191
- # {
192
- # 'css.erb' => :scss_erb,
193
- # 'scss.erb' => :scss_erb,
194
- # 'sass.erb' => :sass_erb
195
- # }.merge(super)
196
- # end
197
- #
198
- # def erb_extensions
199
- # {
200
- # :scss_erb => :scss,
201
- # :sass_erb => :sass
202
- # }
203
- # end
204
- #
205
- # def find_relative(*args)
206
- # process_erb_engine(super)
207
- # end
208
- #
209
- # def find(*args)
210
- # process_erb_engine(super)
211
- # end
212
- #
213
- # private
214
- # def process_erb_engine(engine)
215
- # if engine && engine.options[:sprockets] && syntax = erb_extensions[engine.options[:syntax]]
216
- # template = Tilt::ERBTemplate.new(engine.options[:filename])
217
- # contents = template.render(engine.options[:sprockets][:context], {})
218
- #
219
- # Sass::Engine.new(contents, engine.options.merge(:syntax => syntax))
220
- # else
221
- # engine
222
- # end
223
- # end
224
- # end
225
- #
226
- # module Deprecated
227
- # def extensions
228
- # {
229
- # 'css.scss' => :scss,
230
- # 'css.sass' => :sass,
231
- # 'css.scss.erb' => :scss_erb,
232
- # 'css.sass.erb' => :sass_erb
233
- # }.merge(super)
234
- # end
235
- #
236
- # def find_relative(*args)
237
- # deprecate_extra_css_extension(super)
238
- # end
239
- #
240
- # def find(*args)
241
- # deprecate_extra_css_extension(super)
242
- # end
243
- #
244
- # private
245
- # def deprecate_extra_css_extension(engine)
246
- # if engine && filename = engine.options[:filename]
247
- # if filename.end_with?('.css.scss')
248
- # msg = "Extra .css in SCSS file is unnecessary. Rename #{filename} to #{filename.sub('.css.scss', '.scss')}."
249
- # elsif filename.end_with?('.css.sass')
250
- # msg = "Extra .css in SASS file is unnecessary. Rename #{filename} to #{filename.sub('.css.sass', '.sass')}."
251
- # elsif filename.end_with?('.css.scss.erb')
252
- # msg = "Extra .css in SCSS/ERB file is unnecessary. Rename #{filename} to #{filename.sub('.css.scss.erb', '.scss.erb')}."
253
- # elsif filename.end_with?('.css.sass.erb')
254
- # msg = "Extra .css in SASS/ERB file is unnecessary. Rename #{filename} to #{filename.sub('.css.sass.erb', '.sass.erb')}."
255
- # end
256
- #
257
- # ActiveSupport::Deprecation.warn(msg) if msg
258
- # end
259
- #
260
- # engine
261
- # end
262
- # end
263
- #
264
- # include Deprecated
265
- # include ERB
266
- # include Globbing
267
- #
268
- # # Allow .css files to be @import'd
269
- # def extensions
270
- # { 'css' => :scss }.merge(super)
271
- # end
272
- # end
273
- # end
274
- # end
0 commit comments