-
Notifications
You must be signed in to change notification settings - Fork 333
/
importer.rb
147 lines (125 loc) · 4.29 KB
/
importer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
require 'active_support/deprecation/reporting'
require 'sass'
require 'sprockets/sass_importer'
require 'tilt'
module Sass
module Rails
class SassImporter < Sass::Importers::Filesystem
module Globbing
GLOB = /(\A|\/)(\*|\*\*\/\*)\z/
def find_relative(name, base, options)
if options[:sprockets] && m = name.match(GLOB)
path = name.sub(m[0], "")
base = File.expand_path(path, File.dirname(base))
glob_imports(base, m[2], options)
else
super
end
end
def find(name, options)
# globs must be relative
return if name =~ GLOB
super
end
private
def glob_imports(base, glob, options)
contents = ""
context = options[:sprockets][:context]
each_globbed_file(base, glob, context) do |filename|
next if filename == options[:filename]
contents << "@import #{filename.inspect};\n"
end
return nil if contents == ""
Sass::Engine.new(contents, options.merge(
:filename => base,
:importer => self,
:syntax => :scss
))
end
def each_globbed_file(base, glob, context)
raise ArgumentError unless glob == "*" || glob == "**/*"
exts = extensions.keys.map { |ext| Regexp.escape(".#{ext}") }.join("|")
sass_re = Regexp.compile("(#{exts})$")
context.depend_on(base)
Dir["#{base}/#{glob}"].sort.each do |path|
if File.directory?(path)
context.depend_on(path)
elsif sass_re =~ path
yield path
end
end
end
end
module ERB
def extensions
{
'css.erb' => :scss_erb,
'scss.erb' => :scss_erb,
'sass.erb' => :sass_erb
}.merge(super)
end
def erb_extensions
{
:scss_erb => :scss,
:sass_erb => :sass
}
end
def find_relative(*args)
process_erb_engine(super)
end
def find(*args)
process_erb_engine(super)
end
private
def process_erb_engine(engine)
if engine && engine.options[:sprockets] && syntax = erb_extensions[engine.options[:syntax]]
template = Tilt::ERBTemplate.new(engine.options[:filename])
contents = template.render(engine.options[:sprockets][:context], {})
Sass::Engine.new(contents, engine.options.merge(:syntax => syntax))
else
engine
end
end
end
module Deprecated
def extensions
{
'css.scss' => :scss,
'css.sass' => :sass,
'css.scss.erb' => :scss_erb,
'css.sass.erb' => :sass_erb
}.merge(super)
end
def find_relative(*args)
deprecate_extra_css_extension(super)
end
def find(*args)
deprecate_extra_css_extension(super)
end
private
def deprecate_extra_css_extension(engine)
if engine && filename = engine.options[:filename]
if filename.end_with?('.css.scss')
msg = "Extra .css in SCSS file is unnecessary. Rename #{filename} to #{filename.sub('.css.scss', '.scss')}."
elsif filename.end_with?('.css.sass')
msg = "Extra .css in SASS file is unnecessary. Rename #{filename} to #{filename.sub('.css.sass', '.sass')}."
elsif filename.end_with?('.css.scss.erb')
msg = "Extra .css in SCSS/ERB file is unnecessary. Rename #{filename} to #{filename.sub('.css.scss.erb', '.scss.erb')}."
elsif filename.end_with?('.css.sass.erb')
msg = "Extra .css in SASS/ERB file is unnecessary. Rename #{filename} to #{filename.sub('.css.sass.erb', '.sass.erb')}."
end
ActiveSupport::Deprecation.warn(msg) if msg
end
engine
end
end
include Deprecated
include ERB
include Globbing
# Allow .css files to be @import'd
def extensions
{ 'css' => :scss }.merge(super)
end
end
end
end