Skip to content
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

Move to sassc #16

Merged
merged 4 commits into from
Mar 21, 2016
Merged
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: ruby
rvm:
- 2.2
- 2.1
- 2.0.0
- 1.9.3
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ gem "listen", "~> 2.4"

# compilation support gems
gem "haml", "~> 3.1"
gem "sass", "~> 3.3"
gem "sassc", "~> 1.8"
gem "coffee-script", "~> 2.2"

group :development do
gem "rspec", "~> 2.10"
gem "yard", "~> 0.8"
gem "bundler", "~> 1.5"
gem "jeweler", "~> 1.8"
gem "fakefs", '0.5.3', :require => "fakefs/safe"
gem "fakefs", '0.6.7', :require => "fakefs/safe"
gem "em-http-request", '~> 1.0'
gem 'simplecov'
# optional compilation gems
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.8.2
0.8.3
10 changes: 5 additions & 5 deletions lib/slinky/compilers/sass-compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ module SassCompiler
Compilers.register_compiler self,
:inputs => ["sass", "scss"],
:outputs => ["css"],
:dependencies => [["sass", ">= 3.1.1"]],
:requires => ["sass"]
:dependencies => [["sassc", "~> 1.8"]],
:requires => ["sassc"]

def SassCompiler::compile s, file
syntax = file.end_with?(".sass") ? :sass : :scss
if Pathname.new(file).basename.to_s.start_with?("_")
# This is a partial, don't render it
""
else
sass_engine = Sass::Engine.new(s,
:syntax => syntax,
:load_paths => [File.dirname(file)])
sass_engine = SassC::Engine.new(s,
:syntax => syntax,
:load_paths => [File.dirname(file)])
sass_engine.render
end
end
Expand Down
15 changes: 9 additions & 6 deletions lib/slinky/manifest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,16 @@ def find_by_pattern pattern
# Finds all the matching manifest files for a particular product.
# This does not take into account dependencies.
def files_for_product product
if !p = @config.produce[product]
SlinkyError.raise NoSuchProductError,
"Product '#{product}' has not been configured"
p = @config.produce[product]

if p.nil?
raise NoSuchProductError.new(
"Product '#{product}' has not been configured")
end

type = type_for_product product
if type != ".js" && type != ".css"
SlinkyError.raise InvalidConfigError, "Only .js and .css products are supported"
raise InvalidConfigError.new("Only .js and .css products are supported")
end

g = dependency_graph.transitive_closure
Expand Down Expand Up @@ -308,14 +310,15 @@ def files_rec md
end

def compressor_for_product product
require 'sassc'
case type_for_product(product)
when ".js"
# Use UglifyJS
lambda{|s| Uglifier.compile(s.force_encoding("UTF-8"),
mangle: false, output: {ascii_only: false})}
when ".css"
# Use SASS's compressed output
lambda{|s| Sass::Engine.new(s, :syntax => :scss, :style => :compressed).render}
lambda{|s| SassC::Engine.new(s, :syntax => :scss, :style => :compressed).render}
end
end

Expand Down Expand Up @@ -752,7 +755,7 @@ def build_to
# to the build path
def build
return nil unless should_build

if !File.exists? @build_path
FileUtils.mkdir_p(@build_path)
end
Expand Down
52 changes: 28 additions & 24 deletions spec/compilers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,40 @@
require 'em-http'

def compiler_test file, ext, src, &test
File.open(file, "w+"){|f| f.write(src)}
cf = Slinky::Compilers.cfile_for_file(file)
called_back = false
FakeFS.without {
dir = Dir.mktmpdir

$stdout.should_receive(:puts).with("Compiled #{file}".foreground(:green))
begin
path = "#{dir}/#{file}"

File.open(path, "w+"){|f| f.write(src)}
cf = Slinky::Compilers.cfile_for_file(path)
called_back = false

cf.compile{|cpath, _, _, error|
error.should == nil
cpath.nil?.should == false
cpath.end_with?(ext).should == true
test.call(File.open(cpath).read).should == true
called_back = true
}
$stdout.should_receive(:puts).with("Compiled #{path}".foreground(:green))

called_back.should == true
cf.compile{|cpath, _, _, error|
error.should == nil
cpath.nil?.should == false
cpath.end_with?(ext).should == true
test.call(File.open(cpath).read).should == true
called_back = true
}
called_back.should == true
ensure
FileUtils.rm_rf(dir)
end
}
end

describe "Compilers" do
before :each do
FileUtils.rm_rf("/compilers") rescue nil
FileUtils.mkdir("/compilers")
end

context "SassCompiler" do
it "should be able to compile SASS files" do
src = <<eos
h1
color: red
eos
compiler_test("/compilers/test.sass", ".css", src){|s|
compiler_test("test.sass", ".css", src){|s|
s.include?("color: red;")
}
end
Expand All @@ -44,14 +48,14 @@ def compiler_test file, ext, src, &test
}
}
eos
compiler_test("/compilers/test.scss", ".css", src){|s|
compiler_test("test.scss", ".css", src){|s|
s.include?("color: red;") && s.include?("h1 a")
}
end

it "should not compile partials" do
src = "body { color: $something; }"
compiler_test("/compilers/_partial.scss", ".css", src){|s|
compiler_test("_partial.scss", ".css", src){|s|
s == ""
}
end
Expand All @@ -66,7 +70,7 @@ def compiler_test file, ext, src, &test
width: percentage(@width);
}
eos
compiler_test("/compilers/test.less", ".css", src){|s|
compiler_test("test.less", ".css", src){|s|
s.include?("width: 50%;")
}
end
Expand All @@ -78,7 +82,7 @@ def compiler_test file, ext, src, &test
test = {do: (x) -> console.log(x)}
test.do("Hello, world")
eos
compiler_test("/compilers/test.coffee", ".js", src){|s|
compiler_test("test.coffee", ".js", src){|s|
s.include?("function(x) {")
}
end
Expand All @@ -92,7 +96,7 @@ def compiler_test file, ext, src, &test
%title Hello!
%body
eos
compiler_test("/compilers/test.haml", ".html", src){|s|
compiler_test("test.haml", ".html", src){|s|
s.include?("<title>Hello!</title>")
}
end
Expand All @@ -107,7 +111,7 @@ def compiler_test file, ext, src, &test
document.getElementById('example')
);
EOF
compiler_test("/compilers/test.jsx", ".js", src){|s|
compiler_test("test.jsx", ".js", src){|s|
s.include?("Hello, world!")
}
end
Expand Down
58 changes: 34 additions & 24 deletions spec/manifest_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
build_path = mf.process
end

it "should report errors for bad files" do
it "should report errors for bad files" do
File.open("/src/l1/l2/bad.sass", "w+"){|f|
f.write "color: red;"
}
Expand Down Expand Up @@ -479,29 +479,39 @@
end

it "should recompile files when files they depend on change" do
$stdout.should_receive(:puts).with(/Compiled \/src\/.+/).exactly(4).times

File.open("/src/l1/depender.scss", "w+"){|f|
f.write("// slinky_depends('/l1/_dependee.scss')\n")
f.write("@import \"dependee\";\n")
f.write("body { color: $bodycolor }\n")
}
File.open("/src/l1/_dependee.scss", "w+"){|f|
f.write("$bodycolor: red;\n")
}
mdevel = Slinky::Manifest.new("/src", @config)

mf = mdevel.find_by_path("/l1/depender.scss").first
path = mf.process
File.read(path).should match /color: red/

File.open("/src/l1/_dependee.scss", "w+"){|f|
f.write("$bodycolor: green;\n")
}

mf = mdevel.find_by_path("/l1/depender.scss").first
path = mf.process
File.read(path).should match /color: green/
$stdout.should_receive(:puts).with(/Compiled .+/).exactly(4).times

# We can't use FakeFS with SassC
FakeFS.without do
dir = Dir.mktmpdir
begin
FileUtils.mkdir("#{dir}/l1")
File.open("#{dir}/l1/depender.scss", "w+"){|f|
f.write("// slinky_depends('/l1/_dependee.scss')\n")
f.write("@import \"dependee\";\n")
f.write("body { color: $bodycolor }\n")
}
File.open("#{dir}/l1/_dependee.scss", "w+"){|f|
f.write("$bodycolor: red;\n")
}

mdevel = Slinky::Manifest.new(dir, @config)

mf = mdevel.find_by_path("/l1/depender.scss").first
path = mf.process
File.read(path).should match /color: red/

File.open("#{dir}/l1/_dependee.scss", "w+"){|f|
f.write("$bodycolor: green;\n")
}

mf = mdevel.find_by_path("/l1/depender.scss").first
path = mf.process
File.read(path).should match /color: green/
ensure
FileUtils.rm_rf(dir)
end
end
end
end

Expand Down
Loading