Permalink
Browse files

Implement CSS, SCSS, and ERB imports

  • Loading branch information...
bwr committed Mar 16, 2015
1 parent 5d3655d commit e85de874f91594f08dba580f28a5c8f9b720e980
@@ -1,6 +1,55 @@
require 'tilt'
module SassC
module Rails
class Importer < SassC::Importer
class Extension
attr_reader :postfix
def initialize(postfix)
@postfix = postfix
end
def import_for(original_path, parent_path, full_path)
SassC::Importer::Import.new(full_path)
end
end
class CSSExtension
def postfix
".css"
end
def import_for(original_path, parent_path, full_path)
import_path = full_path.gsub(/\.css$/,"")
SassC::Importer::Import.new(import_path)
end
end
class ERBExtension < Extension
def initialize(postfix)
super
end
def import_for(original_path, parent_path, full_path)
template = Tilt::ERBTemplate.new(full_path)
parsed_erb = template.render
SassC::Importer::Import.new(full_path, source: parsed_erb)
end
end
EXTENSIONS = [
Extension.new(".scss"),
Extension.new(".sass"),
CSSExtension.new,
ERBExtension.new(".scss.erb"),
ERBExtension.new(".sass.erb"),
ERBExtension.new(".css.erb"),
]
PREFIXS = [ "", "_" ]
def imports(path, parent_path)
# should return an Import, or array of Imports.
# Import.new(path)
@@ -31,11 +80,44 @@ def imports(path, parent_path)
# https://github.com/sass/sass/blob/stable/lib/sass/importers/filesystem.rb
# for reference
parent_dir, _ = File.split(parent_path)
specified_dir, specified_file = File.split(path)
search_paths = ([parent_dir] + load_paths).uniq
if specified_dir != "."
search_paths.map! do |path|
File.join(path, specified_dir)
end
end
search_paths.each do |search_path|
PREFIXS.each do |prefix|
file_name = prefix + specified_file
EXTENSIONS.each do |extension|
try_path = File.join(search_path, file_name + extension.postfix)
if File.exists?(try_path)
record_import_as_dependency try_path
return extension.import_for(path, parent_path, try_path)
end
end
end
end
Import.new(path)
end
private
def record_import_as_dependency(path)
context.depend_on path
end
def context
options[:sprockets][:context]
end
def load_paths
options[:load_paths]
end
@@ -25,11 +25,6 @@ def call(input)
engine.render
end
# Track all imported files
engine.dependencies.map do |dependency|
context.metadata[:dependency_paths] << dependency.options[:filename]
end
context.metadata.merge(data: css)
end
@@ -28,7 +28,9 @@ Gem::Specification.new do |spec|
# reuse portions of the sprockets template
spec.add_dependency 'sass'
spec.add_dependency "sassc", "0.0.9"
spec.add_dependency 'tilt' # For ERB
spec.add_dependency "sassc", "0.0.10"
spec.add_dependency 'railties'
spec.add_dependency 'sprockets', '3.0.0.beta.6'
end
@@ -4,16 +4,16 @@
// //@import "globbed/**/*";
@import "subfolder/plain";
@import "subfolder/second_level";
// @import "partials/without_css_ext";
// @import "css_erb_handler";
// @import "scss_erb_handler";
@import "partials/without_css_ext";
@import "css_erb_handler";
@import "scss_erb_handler";
// @import "sass_erb_handler";
// @import "css_scss_erb_handler";
@import "css_scss_erb_handler";
// @import "css_sass_erb_handler";
.main {
color: yellow;
// @include background-from-partial(red);
}
// @include without-css-ext;
@include without-css-ext;
@@ -89,8 +89,8 @@ def test_sass_imports_work_correctly
assert_match /without-css-ext/, css_output
assert_match /css-erb-handler/, css_output
assert_match /scss-erb-handler/, css_output
assert_match /sass-erb-handler/, css_output
assert_match /css-sass-erb-handler/, css_output
# assert_match /sass-erb-handler/, css_output
# assert_match /css-sass-erb-handler/, css_output
assert_match /css-scss-erb-handler/, css_output
assert_match /default-old-css/, css_output

0 comments on commit e85de87

Please sign in to comment.