Skip to content

[WIP] SassC support #349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ rvm:

gemfile:
- gemfiles/Gemfile-rails-4-2
- gemfiles/Gemfile-sassc

sudo: false

Expand Down
9 changes: 9 additions & 0 deletions gemfiles/Gemfile-sassc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
source "https://rubygems.org"

# Specify your gem"s dependencies in sass-rails.gemspec
gemspec path: ".."

gem "sassc", "~> 1.7.1"
gem "rails", "~> 4.2.0"
gem "sprockets", github: "rails/sprockets", branch: "master"
gem "sprockets-rails", github: "rails/sprockets-rails", branch: "master"
7 changes: 6 additions & 1 deletion lib/sass/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@ module Rails
end

require 'sass/rails/version'
require 'sass/rails/importer'
require 'sass/rails/sass_importer'
require 'sass/rails/railtie'

begin
require 'sass/rails/sassc_importer'
rescue LoadError
end
22 changes: 17 additions & 5 deletions lib/sass/rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
require 'sprockets/railtie'
require 'sprockets/sass_processor'

begin
require "sassc"
require "sprockets/sassc_processor"
rescue LoadError
end

module Sass::Rails
class Railtie < ::Rails::Railtie
config.sass = ActiveSupport::OrderedOptions.new
Expand Down Expand Up @@ -55,11 +61,17 @@ class Railtie < ::Rails::Railtie
end

config.assets.configure do |env|

env.register_transformer 'text/sass', 'text/css',
Sprockets::SassProcessor.new(importer: SassImporter)
env.register_transformer 'text/scss', 'text/css',
Sprockets::ScssProcessor.new(importer: SassImporter)
if defined?(SassC)
env.register_transformer 'text/sass', 'text/css',
Sprockets::SasscProcessor.new(importer: SasscImporter)
env.register_transformer 'text/scss', 'text/css',
Sprockets::ScsscProcessor.new(importer: SasscImporter)
else
env.register_transformer 'text/sass', 'text/css',
Sprockets::SassProcessor.new(importer: SassImporter)
env.register_transformer 'text/scss', 'text/css',
Sprockets::ScssProcessor.new(importer: SassImporter)
end

env.context_class.class_eval do
class_attribute :sass_config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def glob_imports(base, glob, options)
contents << "@import #{filename.inspect};\n"
end
return nil if contents == ""
Sass::Engine.new(contents, options.merge(
sass_engine(contents, options.merge(
:filename => base,
:importer => self,
:syntax => :scss
Expand All @@ -58,6 +58,10 @@ def each_globbed_file(base, glob, context)
end
end
end

def sass_engine(contents, options)
Sass::Engine.new(contents, options)
end
end

module ERB
Expand Down Expand Up @@ -100,7 +104,7 @@ def process_erb_engine(engine)

result = Sprockets::ProcessorUtils.call_processors(processors, input)

Sass::Engine.new(result[:data], engine.options.merge(:syntax => syntax))
sass_engine(result[:data], engine.options.merge(:syntax => syntax))
else
engine
end
Expand Down
46 changes: 46 additions & 0 deletions lib/sass/rails/sassc_importer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require "sassc"

module Sass
module Rails
class SasscImporter < SassC::Importer
def imports(path, parent_path)
context = options[:sprockets][:context]
root = Pathname.new(context.filename).to_s
lookup = FileLookup.new(root)

engine = lookup.find_relative(path, parent_path, options)
template = engine.template

if engine.options[:syntax] == :sass
template = SassC::Sass2Scss.convert(template)
end

context.depend_on(engine.filename)

SassC::Importer::Import.new(engine.filename, source: template)
end

class FileLookup < SassImporter
private
def _find(dir, name, options)
# this is copied from Sass, save for the last line.

full_filename, syntax = Sass::Util.destructure(find_real_file(dir, name, options))
return unless full_filename && File.readable?(full_filename)

full_filename = full_filename.tr("\\", "/") if Sass::Util.windows?

options[:syntax] = syntax
options[:filename] = full_filename
options[:importer] = self

SassC::Engine.new(File.read(full_filename), options)
end

def sass_engine(*args)
SassC::Engine.new(*args)
end
end
end
end
end
4 changes: 3 additions & 1 deletion test/support/sass_rails_test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ def sprockets_render(project, filename)
end

def asset_output(filename)
runcmd "ruby script/rails runner 'puts Rails.application.assets[#{filename.inspect}]'"
runner "development" do
"puts Rails.application.assets[#{filename.inspect}]"
end
end

def assert_file_exists(filename)
Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# If developing against local dependencies, this code will ensure they get picked up
# in the project fixtures that have their own bundle environment
$gem_options = {}
possible_dev_dependencies = %w(sass-rails sass rails arel actionpack rack railties sprockets journey sprockets-rails activerecord-deprecated_finders)
possible_dev_dependencies = %w(sass-rails sass sassc rails arel actionpack rack railties sprockets journey sprockets-rails activerecord-deprecated_finders)
Bundler.load.specs.each do |s|
if possible_dev_dependencies.include?(s.name)
gem_path = s.full_gem_path
Expand Down