Rails 4 asset pipeline doesn't minify CSS #10084

Closed
NARKOZ opened this Issue Apr 4, 2013 · 32 comments

Comments

Projects
None yet
7 participants
Contributor

NARKOZ commented Apr 4, 2013

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

Owner

rafaelfranca commented Apr 4, 2013

How are you running the assets precompile?

Contributor

NARKOZ commented Apr 4, 2013

RAILS_ENV=production bundle exec rake assets:precompile

Contributor

NARKOZ commented Apr 5, 2013

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

Contributor

NARKOZ commented Apr 5, 2013

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

Owner

rafaelfranca commented Apr 5, 2013

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

Contributor

NARKOZ commented Apr 5, 2013

4.0.0.beta1

Contributor

rubys commented Apr 5, 2013

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?

Contributor

rubys commented Apr 5, 2013

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

rubys commented Apr 5, 2013

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.

Contributor

rubys commented Apr 5, 2013

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
Contributor

rubys commented Apr 5, 2013

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.

Owner

dhh commented Apr 5, 2013

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

Contributor

rubys commented Apr 9, 2013

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.

Owner

dhh commented Apr 9, 2013

Sounds like we should just remove that line then.
On Apr 8, 2013 5:45 PM, "Sam Ruby" notifications@github.com wrote:

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.


Reply to this email directly or view it on GitHubhttps://github.com/rails/rails/issues/10084#issuecomment-16088025
.

Contributor

rubys commented Apr 9, 2013

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.

Owner

dhh commented Apr 9, 2013

Why would you ever not want to compress in production?

On Apr 9, 2013, at 8:08, Sam Ruby notifications@github.com wrote:

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.


Reply to this email directly or view it on GitHub.

Contributor

dmathieu commented Apr 9, 2013

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

Contributor

rubys commented Apr 9, 2013

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

Contributor

rubys commented Apr 9, 2013

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

Owner

rafaelfranca commented Apr 9, 2013

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

Owner

rafaelfranca commented Apr 9, 2013

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

Contributor

NARKOZ commented Apr 9, 2013

same thing

Contributor

rubys commented Apr 9, 2013

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
Owner

guilleiguaran commented Apr 9, 2013

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

Contributor

rubys commented Apr 9, 2013

@guilleiguaran beta2 won't :)

Owner

rafaelfranca commented Apr 9, 2013

So this issue can be closed. Thank you guys

Contributor

NARKOZ commented Apr 9, 2013

@rubys thank you.

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

Owner

guilleiguaran commented Jul 12, 2013

@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