Skip to content

Commit

Permalink
Sourcemaps support, refs #25
Browse files Browse the repository at this point in the history
  • Loading branch information
Vasfed committed Mar 15, 2016
1 parent 4edb489 commit 94818fe
Show file tree
Hide file tree
Showing 16 changed files with 104 additions and 22 deletions.
1 change: 1 addition & 0 deletions Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ end

appraise "sprockets-4" do
gem "sprockets", "~>4.0.0.beta2"
gem 'sass', '>=3.3'
end

appraise "rails-4" do
Expand Down
4 changes: 2 additions & 2 deletions gemfiles/rails_4.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ../
specs:
csso-rails (0.4.0)
csso-rails (0.4.1)
execjs (>= 1)

GEM
Expand Down Expand Up @@ -115,4 +115,4 @@ DEPENDENCIES
rake

BUNDLED WITH
1.10.6
1.11.2
2 changes: 1 addition & 1 deletion gemfiles/sprockets_2.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ../
specs:
csso-rails (0.4.0)
csso-rails (0.4.1)
execjs (>= 1)

GEM
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/sprockets_3.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ../
specs:
csso-rails (0.4.0)
csso-rails (0.4.1)
execjs (>= 1)

GEM
Expand Down
1 change: 1 addition & 0 deletions gemfiles/sprockets_4.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
source "https://rubygems.org"

gem "sprockets", "~>4.0.0.beta2"
gem "sass", ">=3.3"

gemspec :path => "../"
6 changes: 4 additions & 2 deletions gemfiles/sprockets_4.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ../
specs:
csso-rails (0.4.0)
csso-rails (0.4.1)
execjs (>= 1)

GEM
Expand All @@ -16,6 +16,7 @@ GEM
minitest (5.8.4)
rack (1.6.4)
rake (11.1.0)
sass (3.4.21)
sprockets (4.0.0.beta2)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
Expand All @@ -29,7 +30,8 @@ DEPENDENCIES
csso-rails!
minitest
rake
sass (>= 3.3)
sprockets (~> 4.0.0.beta2)

BUNDLED WITH
1.10.6
1.11.2
2 changes: 1 addition & 1 deletion gemfiles/therubyracer.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ../
specs:
csso-rails (0.4.0)
csso-rails (0.4.1)
execjs (>= 1)

GEM
Expand Down
6 changes: 6 additions & 0 deletions lib/csso.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ def self.optimize(css, maniac_mode=false, structural_optimization=true)
end
end

def self.optimize_with_sourcemap css, filename, structural_optimization=true
return nil unless css.is_a?(String)
return css if css.size <= 3
Csso.js_api.compress_with_sourcemap(css, filename, structural_optimization)
end


class Optimizer
def optimize(css, structural_optimization=true)
Expand Down
18 changes: 14 additions & 4 deletions lib/csso/compressor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@ class Compressor
def self.call(input)
require 'csso'
#TODO: settings?
#TODO: handle metadata somehow?
{
data: Csso.optimize(input[:data], true)
}
if input[:metadata] && input[:metadata][:map]
css, map = Csso.optimize_with_sourcemap(input[:data],
# Sprockets::PathUtils.split_subpath(input[:load_path], input[:filename])
# sprockets seems to ignore filenames here, so we may save some mem:
'uri'
)
map = Sprockets::SourceMapUtils.combine_source_maps(
input[:metadata][:map],
Sprockets::SourceMapUtils.decode_json_source_map(map)["mappings"]
)
{ data: css, map: map }
else
{ data: Csso.optimize(input[:data], true) }
end
end

# sprockets 2:
Expand Down
9 changes: 9 additions & 0 deletions lib/csso/csso.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,12 @@ do_compression = function(css, disable_structural){
restructure: !disable_structural
});
};

do_compression_with_map = function(css, filename, structural){
var result = csso.minify(css, {
restructure: structural,
filename: filename,
sourceMap: true
});
return [result.css, result.map.toString()];
};
6 changes: 5 additions & 1 deletion lib/csso/js_lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ def initialize
end

def compress css, structural_optimization=true
@csso.call("do_compression", css, !structural_optimization)
@csso.call("do_compression", css, structural_optimization)
end

def compress_with_sourcemap css, filename, structural_optimization=true
@csso.call("do_compression_with_map", css, filename, structural_optimization)
end

end
Expand Down
37 changes: 29 additions & 8 deletions spec/csso/sprockets_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@
describe Csso do

subject { Csso }
let(:sprockets_env){
let(:sprockets_env_without_csso){
begin
require 'sprockets'
rescue LoadError
skip "Skipping sprockets integration, as there's no sprockets in this env"
end
e = Sprockets::Environment.new(File.expand_path('../', File.dirname(__FILE__)))
e.append_path 'fixtures'
e.config = e.config.merge(gzip_enabled: false).freeze if e.respond_to? :config
# e.logger = Logger.new STDOUT
e
}
let(:result_dir){
Expand All @@ -24,17 +30,15 @@
File.expand_path('manifest.json', result_dir)
}
let(:manifest){
sprockets_env
Sprockets::Manifest.new(sprockets_env, result_dir, manifest_file)
}
let(:sprockets_env){
subject.install(sprockets_env_without_csso)
sprockets_env_without_csso
}

it "installs" do
begin
require 'sprockets'
rescue LoadError
skip "Skipping sprockets integration, as there's no sprockets in this env"
end

subject.install(sprockets_env)
sprockets_env.css_compressor.must_equal Csso::Compressor
manifest.environment.must_equal(sprockets_env)
manifest.clobber
Expand All @@ -46,6 +50,23 @@
manifest.clobber
end

it "compiles with sourcemap" do
manifest.clobber
begin
require 'sass'
rescue LoadError
skip 'No sass in this env, skipping'
end
manifest.compile('test2.css')
manifest.compile('test2.css.map')
json = JSON.load File.read(manifest_file)
json["assets"]["test2.css"].must_match /\.css$/
sprockets_env['test2.css'].source.must_equal '.class,.class .other_class{color:red}.something{color:#000}.test2{color:#00f}'
map = JSON.load(sprockets_env['test2.css.map'].source)
map["sources"].size.must_equal 4
manifest.clobber
end

it "loads into rails" do
begin
require "rails"
Expand Down
7 changes: 7 additions & 0 deletions spec/fixtures/test2.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* LaLaLa */
//= require ./test.css
//= require ./test4.css

.test2{
color: #0000ff;
}
3 changes: 3 additions & 0 deletions spec/fixtures/test3.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.something{
color: #000000;
}
10 changes: 10 additions & 0 deletions spec/fixtures/test4.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@import 'test3';

$color: #ff0000;

.class {
.other_class{
// comment blablabla
color: $color;
}
}
12 changes: 10 additions & 2 deletions vendor/csso/csso.js

Large diffs are not rendered by default.

0 comments on commit 94818fe

Please sign in to comment.