javascript 在 Rails 生产中禁用资产缩小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9674714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable Asset Minification in Rails Production
提问by Hyman R-G
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.compress = false
along with config.assets.debug = true
, and the compressed assets were still used. I then deleted the compressed assets, at which point no assets were rendered at all. I added config.assets.enabled = false
, which did not help. I tried copying the uncompressed assets into various directories, including the application root, public, and public/assets (the latter two using both the folders "images, "javascripts", and "stylesheets", and putting the assets directly into the folders without the three subfolders). I was eventually able to get the javascripts to work by changing the html to directly reference all of the javascript files. But the CSS and images still are not working.
为了在我的 heroku 生产环境中调试 javascript,我需要禁用资产压缩(或至少是 javascript 的压缩)。我和config.assets.compress = false
一起尝试过config.assets.debug = true
,仍然使用压缩的资产。然后我删除了压缩的资产,此时根本没有渲染任何资产。我补充说config.assets.enabled = false
,这没有帮助。我尝试将未压缩的资产复制到各种目录中,包括应用程序根目录、公共和公共/资产(后两者使用文件夹“图像”、“javascripts”和“样式表”,并将资产直接放入文件夹中,而无需三个子文件夹)。我最终能够通过将 html 更改为直接引用所有 javascript 文件来使 javascript 工作。但是 CSS 和图像仍然无法正常工作。
I would have thought that my original config.assets.compress = false
should have worked. Any ideas what I did wrong?
我会认为我的原件config.assets.compress = false
应该有效。任何想法我做错了什么?
回答by ncherro
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
在此处创建一个不压缩 js / css 的模块: 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
回答by geekQ
Under Rails 4 just commenting out the line
在 Rails 4 下只是注释掉这一行
# config.assets.js_compressor = :uglifier
in config/environments/production.rb
worked for me. Looks like default is no compresson.
在config/environments/production.rb
对我来说有效。看起来默认是没有压缩。
回答by user2576663
I also need to debug my js so I tried ncherro's solution. The problem was that it would still throw
我还需要调试我的 js,所以我尝试了 ncherro 的解决方案。问题是它仍然会抛出
rake aborted! uninitialized constant NoCompression
耙子中止!未初始化的常量 NoCompression
So I just put the NoCompression class in the production.rb file
所以我只是把 NoCompression 类放在 production.rb 文件中
# 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
回答by user3630729
Comment out the uglifier and add config.assets.debug = true
. This worked for me.
注释掉 uglifier 并添加config.assets.debug = true
. 这对我有用。
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
压缩 JavaScript 和 CSS:
config.assets.js_compressor = :uglifier
调试模式禁用资产的串联和预处理。但是此选项可能会导致具有大量复杂资产的视图渲染显着延迟:
config.assets.debug = true
回答by whyvez
Also worth noting... In addition to ncherro solution you will need to do the following:
另外值得注意的是......除了ncherro解决方案之外,您还需要执行以下操作:
- make sure to put your new module somewhere where it will be loaded by default. Was lib/extras in my case.
- run
rake assets:clean
to clean your existing assets. - run
rake assets:precompile
to compile your assets using the new compressor. - restart your app... i use
touch tmp/restart.txt
- 确保将您的新模块放在默认情况下会加载的地方。在我的情况下是 lib/extras。
- 运行
rake assets:clean
以清理您现有的资产。 - 运行
rake assets:precompile
以使用新压缩器编译您的资产。 - 重新启动您的应用程序...我使用
touch tmp/restart.txt
Happy debugging ;)
调试愉快;)
回答by dodgio
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
使用 Heroku 上的 Rails 4,您需要做两件事。首先正如@geekQ 提到的,注释掉 js_compressor 行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.
其次,您需要考虑Heroku 的 Rails 4 资产管道缓存。任何与缓存中的版本具有相同 MD5 的文件都不会被重新编译。将提供以前的(可能是压缩的)版本。您编辑的任何文件都会有一个新的 MD5 并被重新编译。
You can also purge the entire asset cache with the Heroku Repo pluginto the Heroku toolbelt. Install that, then use the command
您还可以使用Heroku Repo 插件将整个资产缓存清除到 Heroku 工具带。安装它,然后使用命令
heroku repo:purge_cache
Deploy a new version after purging the cache and all your assets will be recompiled.
清除缓存后部署新版本,您的所有资产都将重新编译。
回答by averageradical
I had to update Rails.application.config.assets.version
in config/initializers/assets.rb
for the production.rb
changes to take effect.
我不得不更新Rails.application.config.assets.version
中config/initializers/assets.rb
的production.rb
更改生效。
回答by Manish Singh
Find and comment out these line in environments/production.rb
:
在 中查找并注释掉这些行environments/production.rb
:
config.assets.js_compressor = ...
config.assets.css_compressor = ...
回答by jrochkind
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?
看起来这可能是 Rails 中的一个错误。从即将发布的 rails 3.2.9 的变更日志来看,这是您遇到的问题吗?
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.
尊重 config.digest = false 为 asset_path
以前,asset_path 内部只考虑 :digest 选项,但忽略了全局配置设置。这意味着 config.digest = false 不能与 config.compile = false 结合使用,这纠正了行为。
http://weblog.rubyonrails.org/2012/10/29/ann-rails-3-2-9-rc1-has-been-released/
http://weblog.rubyonrails.org/2012/10/29/ann-rails-3-2-9-rc1-has-been-released/
Do you think that could be related?
你认为这可能有关系吗?