Skip to content
This repository was archived by the owner on Jul 24, 2023. It is now read-only.
Open
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 Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ source "http://rubygems.org"
gemspec

group :development do
gem "pry-debugger"
gem "byebug"
end
gem "sass-rails"
gem "jquery-rails"
Expand Down
20 changes: 4 additions & 16 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,11 @@ GEM
multi_json (~> 1.0)
arel (3.0.2)
builder (3.0.4)
coderay (1.0.9)
byebug (2.7.0)
columnize (~> 0.3)
debugger-linecache (~> 1.2)
columnize (0.3.6)
debugger (1.5.0)
columnize (>= 0.3.1)
debugger-linecache (~> 1.2.0)
debugger-ruby_core_source (~> 1.2.0)
debugger-linecache (1.2.0)
debugger-ruby_core_source (1.2.0)
erubis (2.7.0)
execjs (1.4.0)
multi_json (~> 1.0)
Expand All @@ -57,17 +54,9 @@ GEM
mail (2.5.4)
mime-types (~> 1.16)
treetop (~> 1.4.8)
method_source (0.8.1)
mime-types (1.25)
multi_json (1.7.1)
polyglot (0.3.3)
pry (0.9.12)
coderay (~> 1.0.5)
method_source (~> 0.8)
slop (~> 3.4)
pry-debugger (0.2.2)
debugger (~> 1.3)
pry (~> 0.9.10)
rack (1.4.5)
rack-cache (1.2)
rack (>= 0.4)
Expand Down Expand Up @@ -98,7 +87,6 @@ GEM
railties (~> 3.2.0)
sass (>= 3.1.10)
tilt (~> 1.3)
slop (3.4.4)
sprockets (2.2.2)
hike (~> 1.2)
multi_json (~> 1.0)
Expand All @@ -118,8 +106,8 @@ PLATFORMS
ruby

DEPENDENCIES
byebug
css_splitter!
jquery-rails
pry-debugger
sass-rails
uglifier
8 changes: 6 additions & 2 deletions app/helpers/css_splitter/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ def split_stylesheet_link_tag(*sources)
original_sources = sources.dup

options = sources.extract_options!
sources.collect!{ |source| "#{source}_split2" }
sources = sources.each_with_object([]) do |source, collection|
for i in 2..CssSplitter.config.number_of_splits
collection << "#{source}_split#{i}"
end
end
sources << options

[
Expand All @@ -15,4 +19,4 @@ def split_stylesheet_link_tag(*sources)
].join("\n").html_safe
end
end
end
end
12 changes: 12 additions & 0 deletions lib/css_splitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,16 @@
require "css_splitter/splitter"

module CssSplitter
def self.config(&block)
@@config ||= begin
engine = Engine::Configuration.new
engine.number_of_splits = 2
engine.max_selectors = 4095
engine
end

yield @@config if block

return @@config
end
end
4 changes: 3 additions & 1 deletion lib/css_splitter/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ class Engine < ::Rails::Engine
isolate_namespace CssSplitter

initializer 'css_splitter.sprockets_engine', after: 'sprockets.environment', group: :all do |app|
app.assets.register_engine '.split2', CssSplitter::SprocketsEngine
for i in 2..CssSplitter.config.number_of_splits
app.assets.register_engine ".split#{i}", CssSplitter::SprocketsEngine
end
end

initializer 'css_splitter.action_controller' do |app|
Expand Down
10 changes: 6 additions & 4 deletions lib/css_splitter/sprockets_engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ def prepare
end

def evaluate(scope, locals, &block)
split = scope.pathname.extname =~ /(\d+)$/ && $1 || 0 # determine which is the current split (e.g. split2, split3)
CssSplitter::Splitter.split_string data, split.to_i
split = if scope.pathname.extname =~ /(\d+)$/; $1
elsif scope.pathname.basename.to_s =~ /_split(\d+)\.css/; $1
else 2
end
CssSplitter::Splitter.split_string data, split.to_i, CssSplitter.config.max_selectors
end
end

end
end
15 changes: 15 additions & 0 deletions test/css_splitter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,19 @@ class CssSplitterTest < ActiveSupport::TestCase
test "truth" do
assert_kind_of Module, CssSplitter
end

test "config" do
assert_kind_of CssSplitter::Engine::Configuration, CssSplitter.config
end

test "config_set_options" do
CssSplitter.config.max_selectors = 10

assert_equal 10, CssSplitter.config.max_selectors
end

test "config_default_options" do
assert_equal 4095, CssSplitter.config.max_selectors
assert_equal 2, CssSplitter.config.number_of_splits
end
end
11 changes: 11 additions & 0 deletions test/unit/helpers/css_splitter/application_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,16 @@ class ApplicationHelperTest < ActionView::TestCase
assert_equal "<link href=\"/stylesheets/too_big_stylesheet.css\" media=\"print\" rel=\"stylesheet\" type=\"text/css\" />\n<link href=\"/stylesheets/foo.css\" media=\"print\" rel=\"stylesheet\" type=\"text/css\" />\n<!--[if lte IE 9]>\n<link href=\"/stylesheets/too_big_stylesheet_split2.css\" media=\"print\" rel=\"stylesheet\" type=\"text/css\" />\n<link href=\"/stylesheets/foo_split2.css\" media=\"print\" rel=\"stylesheet\" type=\"text/css\" />\n<![endif]-->", output
end

test "should work with multiple split files" do
CssSplitter.config.number_of_splits = 3
CssSplitter.config.max_selectors = 2000

output = split_stylesheet_link_tag("too_big_stylesheet")

CssSplitter.config.number_of_splits = 2
CssSplitter.config.max_selectors = 4095

assert_equal "<link href=\"/stylesheets/too_big_stylesheet.css\" media=\"screen\" rel=\"stylesheet\" type=\"text/css\" />\n<!--[if lte IE 9]>\n<link href=\"/stylesheets/too_big_stylesheet_split2.css\" media=\"screen\" rel=\"stylesheet\" type=\"text/css\" />\n<link href=\"/stylesheets/too_big_stylesheet_split3.css\" media=\"screen\" rel=\"stylesheet\" type=\"text/css\" />\n<![endif]-->", output
end
end
end