Disable Asset Minification in Rails Production

China☆狼群 提交于 2019-11-28 16:59:52

I came up with this workaround after reading the docs:

create a module that does nothing to compress js / css here: lib/modules/no_compression.rb

class NoCompression
  def compress(string)
    # do nothing
    string
  end
end

configure your assets to (not) be compressed with your do-nothing compressor

config.assets.compress = true
config.assets.js_compressor = NoCompression.new
config.assets.css_compressor = NoCompression.new

Under Rails 4 just commenting out the line

# config.assets.js_compressor = :uglifier

in config/environments/production.rb worked for me. Looks like default is no compresson.

I also need to debug my js so I tried ncherro's solution. The problem was that it would still throw

rake aborted! uninitialized constant NoCompression

So I just put the NoCompression class in the production.rb file

    # Compress JavaScripts and CSS
    class NoCompression
         def compress(string)
             # do nothing
             string
         end
     end

     config.assets.compress = true
     config.assets.js_compressor = NoCompression.new
     config.assets.css_compressor = NoCompression.new

Also worth noting... In addition to ncherro solution you will need to do the following:

  1. make sure to put your new module somewhere where it will be loaded by default. Was lib/extras in my case.
  2. run rake assets:clean to clean your existing assets.
  3. run rake assets:precompile to compile your assets using the new compressor.
  4. restart your app... i use touch tmp/restart.txt

Happy debugging ;)

user3630729

Comment out the uglifier and add config.assets.debug = true. This worked for me.

  • Compress JavaScripts and CSS:

    config.assets.js_compressor = :uglifier

  • Debug mode disables concatenation and preprocessing of assets. But this option may cause significant delays in view rendering with a large number of complex assets:

    config.assets.debug = true

With Rails 4 on Heroku you need to do two things. First as @geekQ mentioned, comment out the js_compressor line in config/environments/production.rb

# config.assets.js_compressor = :uglifier

Second, you need to consider Heroku's asset pipeline cache for Rails 4. Any file with the same MD5 as the version in the cache will not be recompiled. The previous (possibly compressed) version will be served. Any file you edit will have a new MD5 and be recompiled.

You can also purge the entire asset cache with the Heroku Repo plugin to the Heroku toolbelt. Install that, then use the command

heroku repo:purge_cache

Deploy a new version after purging the cache and all your assets will be recompiled.

I had to update Rails.application.config.assets.version in config/initializers/assets.rb for the production.rb changes to take effect.

Manish Singh

Find and comment out these line in environments/production.rb:

config.assets.js_compressor = ...
config.assets.css_compressor = ...

Looks like this MAY have been a bug in Rails. From the changelog for upcoming rails 3.2.9, is this what you were running into?

Respect config.digest = false for asset_path

Previously, the asset_path internals only respected the :digest option, but ignored the global config setting. This meant that config.digest = false could not be used in conjunction with config.compile = false this corrects the behavior.

http://weblog.rubyonrails.org/2012/10/29/ann-rails-3-2-9-rc1-has-been-released/

Do you think that could be related?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!