Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Rails 4 asset pipeline doesn't minify CSS #10084

Closed
NARKOZ opened this Issue · 31 comments

7 participants

@NARKOZ

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

@rafaelfranca
Owner

How are you running the assets precompile?

@NARKOZ

RAILS_ENV=production bundle exec rake assets:precompile

@NARKOZ

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

@NARKOZ

Setting config.sass.style doesn't make any changes.

@rafaelfranca
Owner

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

@NARKOZ

4.0.0.beta1

@rubys

I did some investigation. Setting config.assets.css_compressor = :sass does cause the following to occur in https://github.com/sstephenson/sprockets/blob/master/lib/sprockets/compressing.rb#L40 :

register_bundle_processor 'text/css', Sprockets::SassCompressor

It appears that the evaluation of such bundle processors is done in the initializer of https://github.com/sstephenson/sprockets/blob/master/lib/sprockets/bundled_asset.rb#L27

However, this initializer is never called. Instead allocate and init_with is called, which doesn't evaluate the bundle_processors. This is done by https://github.com/sstephenson/sprockets/blob/master/lib/sprockets/asset.rb#L22

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?

@rubys

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)
@rubys

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
       end

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

     # Serialize custom attributes in `BundledAsset`.

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

@rubys

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

https://github.com/rails/sprockets-rails/blob/master/lib/sprockets/railtie.rb#L26

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
    @manifest.compile('gallery.css')

    gallery = Dir["#{@dir}/gallery-*.css"].first
    assert gallery
    assert_equal ".gallery{color:red}", File.read(gallery).chomp
  end
@rubys

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
Owner
dhh commented

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

@rubys

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
Owner
dhh commented
@rubys

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

rails/sass-rails@339529f

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

@dhh
Owner
dhh commented
@dmathieu
Collaborator

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

@rubys

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).

@rubys

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

@rafaelfranca
Owner

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

@rafaelfranca
Owner

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

@NARKOZ

same thing

@rubys

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
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
@guilleiguaran

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

@rubys

@guilleiguaran beta2 won't :)

@rafaelfranca
Owner

So this issue can be closed. Thank you guys

@NARKOZ

@rubys thank you.

@leondewey

Its still happing to me.

Css is not minified

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

Css is minified

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

Im using:

rails 4.0.0
sass-rails 4.0.0

@guilleiguaran

@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.