Skip to content

Commit a62bdf8

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

File tree

4 files changed

+45
-23
lines changed

4 files changed

+45
-23
lines changed

lib/sass/rails/railtie.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ module SassContext
5656
end
5757

5858
if app.assets
59+
app.assets.register_engine '.sass', Sass::Rails::SassTemplate
60+
app.assets.register_engine '.scss', Sass::Rails::ScssTemplate
61+
5962
app.assets.context_class.extend(SassContext)
6063
app.assets.context_class.sass_config = app.config.sass
6164
end

lib/sass/rails/template.rb

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,47 @@
11
require "sprockets/sass_template"
22

3-
module Sprockets
4-
class SassTemplate
5-
def evaluate(context, locals, &block)
6-
cache_store = SassCacheStore.new(context.environment)
7-
8-
options = {
9-
:filename => eval_file,
10-
:line => line,
11-
:syntax => syntax,
12-
:cache_store => cache_store,
13-
:importer => SassImporter.new(context, context.pathname),
14-
:load_paths => context.environment.paths.map { |path| SassImporter.new(context, path) },
15-
:sprockets => {
16-
:context => context,
17-
:environment => context.environment
3+
module Sass
4+
module Rails
5+
class SassTemplate < Sprockets::SassTemplate
6+
7+
def evaluate(context, locals, &block)
8+
cache_store = Sprockets::SassCacheStore.new(context.environment)
9+
10+
options = {
11+
:filename => eval_file,
12+
:line => line,
13+
:syntax => syntax,
14+
: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) },
17+
:sprockets => {
18+
:context => context,
19+
:environment => context.environment
20+
}
1821
}
19-
}
2022

21-
sass_config = context.environment.context_class.sass_config.merge(options)
22-
::Sass::Engine.new(data, sass_config).render
23-
rescue ::Sass::SyntaxError => e
24-
context.__LINE__ = e.sass_backtrace.first[:line]
25-
raise e
23+
sass_config = context.environment.context_class.sass_config.merge(options)
24+
25+
result = ::Sass::Engine.new(data, sass_config).render
26+
27+
filenames = ([options[:importer].imported_filenames] + options[:load_path].map(&:imported_filenames)).flatten.uniq
28+
filenames.each { |filename| context.depend_on(filename) }
29+
30+
result
31+
rescue ::Sass::SyntaxError => e
32+
context.__LINE__ = e.sass_backtrace.first[:line]
33+
raise e
34+
end
35+
end
36+
37+
class ScssTemplate < SassTemplate
38+
def self.default_mime_type
39+
'text/css'
40+
end
41+
42+
def syntax
43+
:scss
44+
end
2645
end
2746
end
2847
end

0 commit comments

Comments
 (0)