diff --git a/.travis.yml b/.travis.yml index f21f396..f09ded4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,9 @@ +sudo: false language: ruby bundler_args: --without development rvm: - "1.9.3" - - "2.1.1" \ No newline at end of file + - "2.1" + - "2.2" +gemfile: + - "Gemfile" \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 9aff500..cd411e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,20 @@ +# 0.4.6 + +* [fix] Implement new engine interface for future sprockets versions #70 +* [bugfix] Fix issue where helper is not a defined method on controller (e.g. ActionController::API:Class) #65 + +# 0.4.5 + +* [bugfix] fix sprockets engine registering for older sprocket-rails versions #68 + +# 0.4.4 + +* [bugfix] fix compatibility with with sprockts-rails version 3.x, see #62 + +# 0.4.3 + +* [bugfix] for media queries with whitespace in front of them #57 + # 0.4.2 * [bugfix] correctly split stylesheets even if @keyframes are directly on the rule limit #55 by [@rubenswieringa](https://github.com/rubenswieringa) diff --git a/Gemfile b/Gemfile index 8a91985..74fab5c 100644 --- a/Gemfile +++ b/Gemfile @@ -13,11 +13,3 @@ gem "rails" gem "sass-rails" gem "jquery-rails" gem "uglifier" - -# Declare any dependencies that are still in development here instead of in -# your gemspec. These might include edge Rails or gems from your path or -# Git. Remember to move these dependencies to your gemspec before releasing -# your gem to rubygems.org. - -# To use debugger -# gem 'debugger' diff --git a/README.md b/README.md index 0a86631..d1b3805 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,10 @@ Gem for splitting up stylesheets that go beyond the IE limit of 4096 selectors, for Rails 3.1+ apps using the Asset Pipeline. You can read this [blogpost](http://railslove.com/blog/2013/03/08/overcoming-ies-4096-selector-limit-using-the-css-splitter-gem) for an explanation of this gem's background story. +### Development status + +Fortunately, the problem of too large CSS files is long gone. This repo is unmaintained. +It remains as an artefact of dark times in the history of web browsers. ## Installation @@ -95,14 +99,13 @@ Since 0.4.1 in development split stylesheets have `debug: false` option by defau ## Credits & License -This is a joint project by the two German Rails shops [Zweitag](http://zweitag.de) and [Railslove](http://railslove.com), therefore the GitHub name "Zweilove". +This is a joint project by the two German Rails shops [Zweitag](https://zweitag.de) and [Railslove](https://railslove.com), therefore the GitHub name "Zweilove". -The original code was written by [Christian Peters](mailto:christian.peters@zweitag.de) and [Thomas Hollstegge](mailto:thomas.hollstegge@zweitag.de) (see this [Gist](https://gist.github.com/2398394)) and turned into a gem by [Jakob Hilden](mailto:jakobhilden@gmail.com). +The original code was written by Christian Peters and Thomas Hollstegge (see this [Gist](https://gist.github.com/2398394)) and turned into a gem by Jakob Hilden. **Major Contributors** * [@Umofomia](https://github.com/Umofomia) * [@kruszczynski](https://github.com/kruszczynski) -This project rocks and uses MIT-LICENSE. - +This project uses MIT-LICENSE. diff --git a/lib/css_splitter/engine.rb b/lib/css_splitter/engine.rb index c51574e..05a1fae 100644 --- a/lib/css_splitter/engine.rb +++ b/lib/css_splitter/engine.rb @@ -1,14 +1,19 @@ module CssSplitter class Engine < ::Rails::Engine - isolate_namespace CssSplitter - initializer 'css_splitter.sprockets_engine', after: 'sprockets.environment', group: :all do |app| - app.assets.register_bundle_processor 'text/css', CssSplitter::SprocketsEngine + if app.config.assets.public_methods.include? :configure + app.config.assets.configure do |assets| + assets.register_bundle_processor 'text/css', CssSplitter::SprocketsEngine + end + else + app.assets.register_bundle_processor 'text/css', CssSplitter::SprocketsEngine + end end initializer 'css_splitter.action_controller' do |app| ActiveSupport.on_load :action_controller do - helper CssSplitter::ApplicationHelper + # Not all controllers use helpers (such as API based controllers) + helper CssSplitter::ApplicationHelper if respond_to?(:helper) end end end diff --git a/lib/css_splitter/splitter.rb b/lib/css_splitter/splitter.rb index 1cf7a10..f5f6a25 100644 --- a/lib/css_splitter/splitter.rb +++ b/lib/css_splitter/splitter.rb @@ -18,7 +18,7 @@ def self.split_string_into_rules(css_string) in_media_query = false partial_rules.each do |rule| - if rule =~ /^@media/ + if rule =~ /^\s*@media/ in_media_query = true elsif bracket_balance == 0 in_media_query = false diff --git a/lib/css_splitter/sprockets_engine.rb b/lib/css_splitter/sprockets_engine.rb index f6ae2a8..551af8c 100644 --- a/lib/css_splitter/sprockets_engine.rb +++ b/lib/css_splitter/sprockets_engine.rb @@ -10,6 +10,22 @@ def self.engine_initialized? def prepare end + def self.call(input) + data_in = input[:data] + + # Instantiate Sprockets::Context to pass along helper methods for Tilt + # processors + context = input[:environment].context_class.new(input) + + # Pass the asset file contents as a block to the template engine, + # then get the results of the engine rendering + engine = self.new { data_in } + rendered_data = engine.render(context, {}) + + # Return the data and any metadata (ie file dependencies, etc) + context.metadata.merge(data: rendered_data.to_str) + end + def evaluate(scope, locals, &block) # Evaluate the split if the asset is named with a trailing _split2, _split3, etc. if scope.logical_path =~ /_split(\d+)$/ diff --git a/lib/css_splitter/version.rb b/lib/css_splitter/version.rb index 22c192c..57859d1 100644 --- a/lib/css_splitter/version.rb +++ b/lib/css_splitter/version.rb @@ -1,3 +1,3 @@ module CssSplitter - VERSION = "0.4.2" + VERSION = "0.4.6" end diff --git a/test/css_splitter_test.rb b/test/css_splitter_test.rb index 5090a3d..012cf89 100644 --- a/test/css_splitter_test.rb +++ b/test/css_splitter_test.rb @@ -13,18 +13,17 @@ class CssSplitterTest < ActiveSupport::TestCase part2 = "#test{background-color:green}" * CssSplitter::Splitter::MAX_SELECTORS_DEFAULT part3 = "#test{background-color:blue}" - assert_equal "#{part1}#{part2}#{part3}\n", assets["erb_stylesheet"].to_s - assert_equal "#{part2}\n", assets["erb_stylesheet_split2"].to_s - assert_equal "#{part3}\n", assets["erb_stylesheet_split3"].to_s + assert_equal "#{part1}#{part2}#{part3}", assets["erb_stylesheet"].to_s.gsub(/\s/, '') + assert_equal "#{part2}", assets["erb_stylesheet_split2"].to_s.gsub(/\s/, '') + assert_equal "#{part3}", assets["erb_stylesheet_split3"].to_s.gsub(/\s/, '') end test "asset pipeline stylesheet splitting on stylesheet combined using requires" do red = "#test{background-color:red}" * 100 green = "#test{background-color:green}" * CssSplitter::Splitter::MAX_SELECTORS_DEFAULT blue = "#test{background-color:blue}" - - assert_equal "#{red}#{green}#{blue}\n", assets["combined"].to_s - assert_equal "#{"#test{background-color:green}" * 100}#{blue}\n", assets["combined_split2"].to_s + assert_equal "#{red}#{green}#{blue}", assets["combined"].to_s.gsub(/\s/, '') + assert_equal "#{"#test{background-color:green}" * 100}#{blue}", assets["combined_split2"].to_s.gsub(/\s/, '') end private diff --git a/test/dummy/config/application.rb b/test/dummy/config/application.rb index 6a0af91..7a5b60e 100644 --- a/test/dummy/config/application.rb +++ b/test/dummy/config/application.rb @@ -38,7 +38,8 @@ class Application < Rails::Application # Configure sensitive parameters which will be filtered from the log file. config.filter_parameters += [:password] - + config.sass.line_comments = false + config.assets.compress = true # Enable escaping HTML in JSON. config.active_support.escape_html_entities_in_json = true @@ -55,9 +56,6 @@ class Application < Rails::Application # Enable the asset pipeline config.assets.enabled = true - - # Version of your assets, change this if you want to expire all your assets - config.assets.version = '1.0' end end diff --git a/test/dummy/config/environments/production.rb b/test/dummy/config/environments/production.rb index 7ad11fe..d92c150 100644 --- a/test/dummy/config/environments/production.rb +++ b/test/dummy/config/environments/production.rb @@ -45,9 +45,6 @@ # Enable serving of images, stylesheets, and JavaScripts from an asset server # config.action_controller.asset_host = "http://assets.example.com" - # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added) - config.assets.precompile += %w( too_big_stylesheet.css too_big_stylesheet_split2.css test_stylesheet_with_media_queries.css test_stylesheet_with_media_queries_split2.css ) - # Disable delivery errors, bad email addresses will be ignored # config.action_mailer.raise_delivery_errors = false diff --git a/test/dummy/config/initializers/assets.rb b/test/dummy/config/initializers/assets.rb new file mode 100644 index 0000000..91073da --- /dev/null +++ b/test/dummy/config/initializers/assets.rb @@ -0,0 +1,8 @@ +# Be sure to restart your server when you modify this file. + +# Version of your assets, change this if you want to expire all your assets. +Rails.application.config.assets.version = '1.0' + +# Precompile additional assets. +# application.js, application.css, and all non-JS/CSS in app/assets folder are already added. +Rails.application.config.assets.precompile += %w( too_big_stylesheet.css too_big_stylesheet_split2.css test_stylesheet_with_media_queries.css test_stylesheet_with_media_queries_split2.css ) \ No newline at end of file diff --git a/test/dummy/config/secrets.yml b/test/dummy/config/secrets.yml new file mode 100644 index 0000000..5dc368e --- /dev/null +++ b/test/dummy/config/secrets.yml @@ -0,0 +1,4 @@ +test: + secret_key_base: c2a98602cc1537b2e38d4f279f20d24db66ff86fe523ab5197529f2ea907b6d70e2dca2fa3da50dcc9cad7bb1b861f0c45b6920fc576cc3e170d8c1ded61887f +development: + secret_key_base: c2a98602cc1537b2e38d4f279f20d24db66ff86fe523ab5197529f2ea907b6d70e2dca2fa3da50dcc9cad7bb1b861f0c45b6920fc576cc3e170d8c1ded61887f \ No newline at end of file diff --git a/test/unit/splitter_test.rb b/test/unit/splitter_test.rb index bcbb8e7..f9713ea 100644 --- a/test/unit/splitter_test.rb +++ b/test/unit/splitter_test.rb @@ -40,6 +40,11 @@ class CssSplitterTest < ActiveSupport::TestCase assert_equal ["a{foo:bar;}", "@media print{b{baz:qux;}", "c{quux:corge;}", "}", "d{grault:garply;}"], CssSplitter::Splitter.split_string_into_rules(has_media) end + test '#split_string_into_rules containing media queries with leading whitespace' do + has_media = "a{foo:bar;}\n @media print{b{baz:qux;}c{quux:corge;}}d{grault:garply;}" + assert_equal ["a{foo:bar;}", "\n @media print{b{baz:qux;}", "c{quux:corge;}", "}", "d{grault:garply;}"], CssSplitter::Splitter.split_string_into_rules(has_media) + end + test "#split_string_into_rules containing keyframes" do has_keyframes = "a{foo:bar;}@keyframes rubes{from{baz:qux;}50%{quux:corge;}}d{grault:garply;}" assert_equal ["a{foo:bar;}", "@keyframes rubes{from{baz:qux;}50%{quux:corge;}}", "d{grault:garply;}"], CssSplitter::Splitter.split_string_into_rules(has_keyframes)