Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


Rails 4 asset pipeline doesn't minify CSS #10084

NARKOZ opened this Issue · 31 comments

7 participants


Setting config.assets.css_compressor = :sass in production.rb doesn't help.
It only concatenates css files.


How are you running the assets precompile?


RAILS_ENV=production bundle exec rake assets:precompile


Output style of css in development not expanded but nested,
and I don't see sass generated line comments as in Rails 3.


Setting doesn't make any changes.


Which sass-rails version? This is a issue on the sass-rails project, not Rails




I did some investigation. Setting config.assets.css_compressor = :sass does cause the following to occur in :

register_bundle_processor 'text/css', Sprockets::SassCompressor

It appears that the evaluation of such bundle processors is done in the initializer of

However, this initializer is never called. Instead allocate and init_with is called, which doesn't evaluate the bundle_processors. This is done by

At this point, I don't know enough about the intent. Should Sprockets invoke bundle_processors when the asset is loaded from a serialized hash, or should rails (or sprocket_rails) not be loading assets from a serialized hash?


Looking further, it appears that caching is an internal decision made by sprockets, so my tentative conclusion is that this is a sprockets problem. Stack trace:

/home/rubys/git/sprockets/lib/sprockets/asset.rb:14:in `from_hash'
/home/rubys/git/sprockets/lib/sprockets/caching.rb:54:in `cache_asset'
/home/rubys/git/sprockets/lib/sprockets/index.rb:93:in `build_asset'
/home/rubys/git/sprockets/lib/sprockets/base.rb:287:in `find_asset'
/home/rubys/git/sprockets/lib/sprockets/index.rb:61:in `find_asset'
/home/rubys/git/sprockets/lib/sprockets/manifest.rb:211:in `block in find_asset'
/home/rubys/git/sprockets/lib/sprockets/manifest.rb:257:in `benchmark'
/home/rubys/git/sprockets/lib/sprockets/manifest.rb:210:in `find_asset'
/home/rubys/git/sprockets/lib/sprockets/manifest.rb:119:in `block in compile'
/home/rubys/git/sprockets/lib/sprockets/manifest.rb:118:in `each'
/home/rubys/git/sprockets/lib/sprockets/manifest.rb:118:in `compile'
/home/rubys/git/sprockets-rails/lib/sprockets/rails/task.rb:22:in `block (3 levels) in define'
/home/rubys/git/sprockets/lib/rake/sprocketstask.rb:146:in `with_logger'
/home/rubys/git/sprockets-rails/lib/sprockets/rails/task.rb:21:in `block (2 levels) in define'
/home/rubys/.rvm/gems/ruby-2.0.0-p0/gems/rake-10.0.4/lib/rake/task.rb:246:in `call'
/home/rubys/.rvm/gems/ruby-2.0.0-p0/gems/rake-10.0.4/lib/rake/task.rb:246:in `block in execute'
/home/rubys/.rvm/gems/ruby-2.0.0-p0/gems/rake-10.0.4/lib/rake/task.rb:241:in `each'
/home/rubys/.rvm/gems/ruby-2.0.0-p0/gems/rake-10.0.4/lib/rake/task.rb:241:in `execute'
/home/rubys/.rvm/gems/ruby-2.0.0-p0/gems/rake-10.0.4/lib/rake/task.rb:184:in `block in invoke_with_call_chain'
/home/rubys/.rvm/gems/ruby-2.0.0-p0/gems/rake-10.0.4/lib/rake/task.rb:177:in `invoke_with_call_chain'
/home/rubys/.rvm/gems/ruby-2.0.0-p0/gems/rake-10.0.4/lib/rake/task.rb:170:in `invoke'
/home/rubys/.rvm/gems/ruby-2.0.0-p0/gems/rake-10.0.4/lib/rake/application.rb:143:in `invoke_task'
/home/rubys/.rvm/gems/ruby-2.0.0-p0/gems/rake-10.0.4/lib/rake/application.rb:101:in `block (2 levels) in top_level'
/home/rubys/.rvm/gems/ruby-2.0.0-p0/gems/rake-10.0.4/lib/rake/application.rb:101:in `each'
/home/rubys/.rvm/gems/ruby-2.0.0-p0/gems/rake-10.0.4/lib/rake/application.rb:101:in `block in top_level'
/home/rubys/.rvm/gems/ruby-2.0.0-p0/gems/rake-10.0.4/lib/rake/application.rb:110:in `run_with_threads'
/home/rubys/.rvm/gems/ruby-2.0.0-p0/gems/rake-10.0.4/lib/rake/application.rb:95:in `top_level'
/home/rubys/.rvm/gems/ruby-2.0.0-p0/gems/rake-10.0.4/lib/rake/application.rb:73:in `block in run'
/home/rubys/.rvm/gems/ruby-2.0.0-p0/gems/rake-10.0.4/lib/rake/application.rb:160:in `standard_exception_handling'
/home/rubys/.rvm/gems/ruby-2.0.0-p0/gems/rake-10.0.4/lib/rake/application.rb:70:in `run'
/home/rubys/.rvm/gems/ruby-2.0.0-p0/bin/ruby_noexec_wrapper:14:in `eval'
/home/rubys/.rvm/gems/ruby-2.0.0-p0/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => assets:precompile
(See full trace by running task with --trace)

Copying a few lines from initialize to init_with does indeed result in minified css:

diff --git a/lib/sprockets/bundled_asset.rb b/lib/sprockets/bundled_asset.rb
index 231ad81..7b05e49 100644
--- a/lib/sprockets/bundled_asset.rb
+++ b/lib/sprockets/bundled_asset.rb
@@ -44,6 +44,11 @@ module Sprockets

       @source = coder['source']
+      # Run bundle processors on concatenated source
+      context =, logical_path, pathname)
+      @source = context.evaluate(pathname, :data => @source,
+                  :processors => environment.bundle_processors(content_type))

     # Serialize custom attributes in `BundledAsset`.

I don't know if this causes other problems. At a minimum, the duplicated code should be factored out.


Additional investigation, commenting out the line setting env.cache in sprocket-rails causes minify to work:

That being said, I have not yet been able to reproduce this failure yet using as a sprockets test:

  test "minify assets" do
    @env.cache = {}
    @env.css_compressor = :sass

    gallery = Dir["#{@dir}/gallery-*.css"].first
    assert gallery
    assert_equal ".gallery{color:red}",

Personal opinion: new rails projects have the following line in config/environments/production.rb:

  # config.assets.css_compressor = :sass

I would suggest that that line be uncommented by the generator, and be made to work.

dhh commented

@josh, I remember we talked about this issue. What was the hold up?


I just tried it again, and I can no longer reproduce the problem. What's even odder is that the assets is compressed whether or not config.assets.css_compressor = :sass is commented out.

dhh commented

It indeed looks like it is no longer configurable, but instead is hardcoded to be :compressed for all environments except development:


This seems like a rather... ahem, curious... choice.

dhh commented

Or why can't you compress in development -for debugging purposes- ?


I agree with @dmathieu . The default should indeed to be compress in production, and not in development. But the question I would like to raise is whether this should be hardcoded or configurable. As it stands now, there appears to be much dead code in sprockets-rails to process a configuration parameter. This confused me for a while as I attempted to detangle how rails involves sass (using three intermediaries, not just sprockets, but sprockets-rails and even sass-rails).


Upon further reading, those lines (in sass-rails) all are of the form app.config.* ||= ..., so they merely establish reasonable defaults.


@NARKOZ could you provide a sample application since we could not reproduce?


Could you try with rails master and sass-rails master?


same thing


I cloned this repository, and saw the failure. I moved one line in Gemfile outside of the group :assets block:

gem 'sass-rails',   '~> 4.0.0.beta1'

I cleaned up the cache and output:

rm -rf tmp/cache/assets/production/sprockets/
rm -f public/assets/*

One that was done, I reran the tests:

bundle exec rake assets:precompile RAILS_ENV=production

When done, it worked as expected:

$ wc -l public/assets/application-*.css
1 public/assets/application-61b84f79c77ea5fc40b02685db46091e.css

@rubys is right, we shouldn't put assets gems anymore in assets group :)


@guilleiguaran beta2 won't :)


So this issue can be closed. Thank you guys


@rubys thank you.


Its still happing to me.

Css is not minified

group :development, :test do
  gem 'sass-rails', '~> 4.0.0'

Css is minified

gem 'sass-rails', '~> 4.0.0'
group :development, :test do

Im using:

rails 4.0.0
sass-rails 4.0.0


@leondewey assets are compiled in production environment then sass-rails can't be in :development group

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.