如何在 Rails 4 中缩小 CSS? [英] How do I minify CSS in Rails 4?

查看:30
本文介绍了如何在 Rails 4 中缩小 CSS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了以下方法,但是我查看了 CSS 源代码并没有缩小!我重新启动了服务器几十次.我在浏览器中关闭了缓存.我还尝试了 'yui-compressor' gem.

I tried the following, however I look at the CSS source and it does not minify! I restarted the server several dozen times. I turned off cache in the browser. I also tried the 'yui-compressor' gem.

  config.assets.debug = false
  config.assets.css_compressor = :sass
  config.assets.compile = true

Gemfile

group :assets do
  # Add any compass extensions here
  # Use SCSS for stylesheets
  gem 'sass-rails', '~> 4.0.0'

参考

http://edgeguides.rubyonrails.org/asset_pipeline.html#customizing-the-pipeline

WEBrick 1.3.1,红宝石 2.0.0 (2013-06-27) [i386-mingw32],Rails 4.0.3

WEBrick 1.3.1, ruby 2.0.0 (2013-06-27) [i386-mingw32], Rails 4.0.3

推荐答案

我在生产环境中遇到了同样的问题,在部署到 Heroku 时我无法让 CSS 最小化.使用以下命令打开压缩后:

I was having the same problem in my production environment, where I couldn't get the CSS to minify upon deploying to Heroku. After turning on compression with the following:

production.rb

config.assets.css_compressor = :sass

Gemfile

gem 'sass-rails', '~> 4.0.0'    

我设法通过更新资产版本使其缩小:

I managed to get it to minify by updating the assets version:

production.rb

config.assets.version = '1.1' # was '1.0'

之后做了一些测试,我发现更新源 CSS/SASS 也有同样的效果.因此,尝试更新您的样式表(而不是仅配置),当 Heroku 在您推送后预编译您的资产时,这应该启动"缩小过程,而无需更新资产版本.

Doing a few tests afterwards, I found that updating the source CSS/SASS had the same effect. So try updating your stylesheets (as opposed to only the config), which should "kickstart" the minification process when Heroku precompiles your assets after you push, without needing to update the assets version.

这篇关于如何在 Rails 4 中缩小 CSS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆