Skip to content

Commit fce9c77

Browse files
committed
Merge pull request #4 from bolandrm/rmb/fixes/render_erb_with_context
pass context to erb render
2 parents e65638b + 9ffb825 commit fce9c77

File tree

10 files changed

+1959
-197
lines changed

10 files changed

+1959
-197
lines changed

lib/sassc/rails.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
require_relative "rails/version"
2+
3+
require "sassc"
24
require_relative "rails/functions"
35
require_relative "rails/importer"
46
require_relative "rails/template"

lib/sassc/rails/functions.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require "sprockets/sass_template"
2-
require "sassc"
32

43
module Sprockets
54
class SassTemplate

lib/sassc/rails/importer.rb

Lines changed: 14 additions & 187 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
module SassC
44
module Rails
55
class Importer < SassC::Importer
6-
76
class Extension
87
attr_reader :postfix
98

109
def initialize(postfix)
1110
@postfix = postfix
1211
end
1312

14-
def import_for(original_path, parent_path, full_path)
13+
def import_for(original_path, parent_path, full_path, options)
1514
SassC::Importer::Import.new(full_path)
1615
end
1716
end
@@ -21,20 +20,25 @@ def postfix
2120
".css"
2221
end
2322

24-
def import_for(original_path, parent_path, full_path)
23+
def import_for(original_path, parent_path, full_path, options)
2524
import_path = full_path.gsub(/\.css$/,"")
2625
SassC::Importer::Import.new(import_path)
2726
end
2827
end
2928

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)
3335
end
36+
end
3437

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)
3640
template = Tilt::ERBTemplate.new(full_path)
37-
parsed_erb = template.render
41+
parsed_erb = template.render(options[:sprockets][:context], {})
3842
SassC::Importer::Import.new(full_path, source: parsed_erb)
3943
end
4044
end
@@ -44,42 +48,13 @@ def import_for(original_path, parent_path, full_path)
4448
Extension.new(".sass"),
4549
CSSExtension.new,
4650
ERBExtension.new(".scss.erb"),
47-
ERBExtension.new(".sass.erb"),
51+
SassERBExtension.new(".sass.erb"),
4852
ERBExtension.new(".css.erb"),
4953
]
5054

5155
PREFIXS = [ "", "_" ]
5256

5357
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-
8358
parent_dir, _ = File.split(parent_path)
8459
specified_dir, specified_file = File.split(path)
8560

@@ -99,7 +74,7 @@ def imports(path, parent_path)
9974
try_path = File.join(search_path, file_name + extension.postfix)
10075
if File.exists?(try_path)
10176
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)
10378
end
10479
end
10580
end
@@ -124,151 +99,3 @@ def load_paths
12499
end
125100
end
126101
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

lib/sassc/rails/railtie.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ class Railtie < ::Rails::Railtie
5353
end
5454

5555
initializer :setup_compression, group: :all do |app|
56+
app.config.assets.css_compressor = nil
57+
5658
if !Rails.env.development?
57-
app.config.assets.css_compressor ||= :sass
59+
app.config.sass.style = :compressed
5860
else
5961
# Use expanded output instead of the sass default of :nested unless specified
6062
app.config.sass.style ||= :expanded

sassc-rails.gemspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ Gem::Specification.new do |spec|
2323
spec.add_development_dependency "rake", "~> 10.0"
2424
spec.add_development_dependency "minitest", "~> 5.5.1"
2525
spec.add_development_dependency 'rails'
26+
spec.add_development_dependency 'mocha'
2627

2728
# unfortunately we require sass for now, so that we can
2829
# reuse portions of the sprockets template
2930
spec.add_dependency 'sass'
3031

3132
spec.add_dependency 'tilt' # For ERB
3233

33-
spec.add_dependency "sassc", "0.0.10"
34+
spec.add_dependency "sassc", "1.0.0"
3435
spec.add_dependency 'railties'
3536
spec.add_dependency 'sprockets', '3.0.0.beta.6'
3637
end

test/dummy/app/assets/fonts/fake-font.ttf

Whitespace-only changes.

0 commit comments

Comments
 (0)