Disable Asset Minification in Rails Production

后端 未结 9 1890
Happy的楠姐
Happy的楠姐 2020-12-04 17:52

In order to debug javascript in my heroku production environment, I need to disable asset compression (or at least compression of javascript). I tried config.assets.c

相关标签:
9条回答
  • 2020-12-04 18:15

    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

    0 讨论(0)
  • 2020-12-04 18:21

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

    config.assets.js_compressor = ...
    config.assets.css_compressor = ...
    
    0 讨论(0)
  • 2020-12-04 18:22

    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
    
    0 讨论(0)
  • 2020-12-04 18:24

    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
    
    0 讨论(0)
  • 2020-12-04 18:25

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

    0 讨论(0)
  • 2020-12-04 18:31

    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.

    0 讨论(0)
提交回复
热议问题