Skip to content

Commit 0404d62

Browse files
author
Francesco Rodríguez
committed
Merge pull request #154 from rails/erb_support
Add ERB support
2 parents 44e8d0d + 575c73d commit 0404d62

File tree

8 files changed

+61
-11
lines changed

8 files changed

+61
-11
lines changed

lib/sass/rails/importer.rb

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,32 @@ class SassImporter < Sass::Importers::Filesystem
88

99
def extensions
1010
{
11-
"css" => :scss,
12-
"css.sass" => :sass,
13-
"css.scss" => :scss
11+
'css' => :scss,
12+
'css.scss' => :scss,
13+
'css.sass' => :sass,
14+
'css.erb' => :scss,
15+
'scss.erb' => :scss,
16+
'sass.erb' => :sass,
17+
'css.scss.erb' => :scss,
18+
'css.sass.erb' => :sass
1419
}.merge!(super)
1520
end
1621

17-
def find_relative_with_glob(name, base, options)
22+
def find_relative(name, base, options)
1823
if name =~ GLOB
1924
glob_imports(name, Pathname.new(base), options)
2025
else
21-
find_relative_without_glob(name, base, options)
26+
engine_from_path(name, File.dirname(base), options)
2227
end
2328
end
24-
alias_method :find_relative_without_glob, :find_relative
25-
alias_method :find_relative, :find_relative_with_glob
2629

27-
def find_with_glob(name, options)
30+
def find(name, options)
2831
if name =~ GLOB
2932
nil # globs must be relative
3033
else
31-
find_without_glob(name, options)
34+
engine_from_path(name, root, options)
3235
end
3336
end
34-
alias_method :find_without_glob, :find
35-
alias_method :find, :find_with_glob
3637

3738
def each_globbed_file(glob, base_pathname, options)
3839
Dir["#{base_pathname}/#{glob}"].sort.each do |filename|
@@ -58,5 +59,31 @@ def glob_imports(glob, base_pathname, options)
5859
:syntax => :scss
5960
))
6061
end
62+
63+
private
64+
65+
def engine_from_path(name, dir, options)
66+
full_filename, syntax = Sass::Util.destructure(find_real_file(dir, name, options))
67+
return unless full_filename && File.readable?(full_filename)
68+
69+
engine = Sass::Engine.new(evaluate(full_filename), options.merge(
70+
syntax: syntax,
71+
filename: full_filename,
72+
importer: self
73+
))
74+
75+
if engine && (filename = engine.options[:filename])
76+
@context.depend_on(filename)
77+
end
78+
79+
engine
80+
end
81+
82+
def evaluate(filename)
83+
processors = context.environment.attributes_for(filename).processors.reject { |processor|
84+
processor.in? [Sprockets::ScssTemplate, Sprockets::SassTemplate]
85+
}
86+
context.evaluate(filename, processors: processors)
87+
end
6188
end
6289
end

test/fixtures/scss_project/app/assets/stylesheets/application.css.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
@import "subfolder/plain";
55
@import "subfolder/second_level";
66
@import "partials/without_css_ext";
7+
@import "css_erb_handler";
8+
@import "scss_erb_handler";
9+
@import "sass_erb_handler";
10+
@import "css_scss_erb_handler";
11+
@import "css_sass_erb_handler";
712

813
.main {
914
color: yellow;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.css-erb-handler {
2+
margin: <%= 0 %>;
3+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.css-sass-erb-handler
2+
margin: <%= 0 %>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.css-scss-erb-handler {
2+
margin: <%= 0 %>;
3+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.sass-erb-handler
2+
margin: <%= 0 %>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.scss-erb-handler {
2+
margin: <%= 0 %>;
3+
}

test/sass_rails_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ class SassRailsTest < Sass::Rails::TestCase
8181
assert_match /plain-old-css/, css_output
8282
assert_match /another-plain-old-css/, css_output
8383
assert_match /without-css-ext/, css_output
84+
assert_match /css-erb-handler/, css_output
85+
assert_match /scss-erb-handler/, css_output
86+
assert_match /sass-erb-handler/, css_output
87+
assert_match /css-sass-erb-handler/, css_output
88+
assert_match /css-scss-erb-handler/, css_output
8489
end
8590

8691
test 'sass asset paths work' do

0 commit comments

Comments
 (0)