【问题标题】:Why does rake assets:precompile produce an empty css stylesheet?为什么 rake assets:precompile 会生成一个空的 CSS 样式表?
【发布时间】:2013-03-11 10:42:29
【问题描述】:

我正在使用 sass-railshaml-rails gems,这样我就可以用 HAML-sass 风格编写 css 文件。

问题是当我想进行生产并预编译我的样式表时,命令 rake assets:precompile 运行没有错误,但生成的 application.css 为空。

这是我的 Gemfile:

source 'https://rubygems.org'

gem 'rails', '~>3.2'
gem 'pg', '>= 0.14'
gem 'haml-rails' #, '~> 0.3'

group :development, :test do
  gem 'factory_girl_rails'
end

group :developpement do
  gem 'rspec-rails', '>= 2.11'
  gem 'capistrano', '>= 2.12'
  gem 'faker', '>= 1.0'
  gem 'rvm-capistrano'
end

group :test do
  gem 'rspec', '>= 2.11'
  gem 'webrat', '>= 0.7'
  gem 'spork-rails', '>= 3.2'
end

group :assets do
  gem 'sass-rails',   '>= 3.2.3'
  gem 'coffee-rails', '>= 3.2.1'
  gem 'compass-rails', '>= 1.0'
  gem 'execjs'
  gem 'therubyracer', :platforms => :ruby

  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'
gem 'annotate'
gem 'excel_rails' #, '~> 0.3'
gem 'spreadsheet' #, '~> 0.7'
gem "schema_plus", :git => "git://github.com/lomba/schema_plus.git"
gem 'ar-octopus' #, '~> 0.3'
gem 'squeel' #, '~> 1.0'
gem 'devise' #, '~> 2.1'
gem 'role_model'
gem 'declarative_authorization'
gem 'rails-translate-routes'  #, '~> 0.1'
gem 'validates_timeliness'  #, '~> 3.0'

这是我的(空)app/assets/stylesheets/application.css.sass

/*
 * This is a manifest file that'll be compiled into application.css, which will include all the files
 * listed below.
 *
 * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
 * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
 *
 * You're free to add application-wide styles to this file and they'll appear at the top of the
 * compiled file, but it's generally better to create a new file per style scope.
 *
 */

我根据https://github.com/rails/sass-rails 的建议删除了以下注释行:

 *= require_self
 *= require_tree .

我的 css.sass 文件存储在这里:

  • app/assets/stylesheets/screen.css.sass
  • app/assets/stylesheets/partials/(screen.css.sass 引用的许多部分文件 _*.css.sass)

=== 更新 ===

请注意,我使用的是 COMPASS gem。所以样式表之间需要共享 mixin 和变量。

以防万一,这是我的 app/assets/stylsheets/screen.css.sass 文件:

// This import applies a global reset to any page that imports this stylesheet.
@import blueprint/reset

// To configure blueprint, edit the partials/base.sass file.
@import partials/base

@import blueprint/interaction
@import blueprint/typography
@import blueprint/grid
@import blueprint/buttons
@import blueprint/utilities

@import compass/layout/grid-background
@import compass/typography/lists/inline-block-list
@import compass/typography/lists/horizontal-list
@import compass/utilities/general/float
@import compass/css3/border-radius
@import compass/css3/box-shadow
@import compass/css3/text-shadow
@import compass/css3/transition

@import partials/opf_mixins_et_functions
@import partials/link_icons
@import partials/typo
@import partials/application
@import partials/affichage_donnees
@import partials/tableau_edition
@import partials/page
@import partials/form
@import partials/feedback

=== 更新 2 ===

我意识到我的 Gemfile 会导致我在生产中遇到麻烦,因为我将以下 Gems 放在 assets 组中:

group :assets do
  ...
  gem 'execjs'
  gem 'therubyracer', :platforms => :ruby
  ...
end

因为生产环境也需要一个javascript解释器... 所以我会将它们移到我的 Gemfile 中的任何组之外。

欢迎任何建议

【问题讨论】:

  • 您是否在特定页面上包含样式表?您是否设置了环境文件来编译嵌套在您的部分文件夹中的样式表?
  • 我不明白你的问题。我得到了一个导轨的标准样式表:screen.css.sass,它在 app/assets/stylesheets 部分目录中引用了部分样式表。我没有修改任何环境文件。

标签: css ruby-on-rails-3.2 sass haml asset-pipeline


【解决方案1】:

我的预编译资产(application.js 和 application.css)是空的 ...

什么解决了我的问题:

  • 在 Gemfile 中设置最后一个 rails 版本(目前为 3.2.14)

  • bundle update

  • bundle install

  • 重启应用touch tmp/restart.txt

【讨论】:

    【解决方案2】:

    好的,我用以下解决方案解决了它:

    按照最后一个答案的精神,似乎在 /app/assets/styelsheets/application.css.sass 的末尾添加以下行解决了我的问题:

    @import screen
    

    screen.css.sass 引用的每个部分样式表都会被自动导入。

    【讨论】:

      【解决方案3】:

      建议您删除这两行,但您仍然需要包含自己的文件:

      *= require screen
      *= require_dir partials/
      

      但是请注意:这不像@import 那样工作 - 你不会让你的 mixins 和变量共享。为此使用真正的@import

      【讨论】:

      • 我认为 sass-rail 遵循 CoC(Convention over Configuration)rail 的理念。你是说我必须分别@import每个:screen.css.sass,以及所有部分样式表screen.css.sass引用(在:app/assets/stylsheets/partials/)?当我使用 COMPASS 时,我需要共享 mixins 和变量。所以 sprocket "require screen" 和 "require_dir partials/" 对我来说是禁止的。
      • 如果你不想使用你删除的两个指令,是的,你有。或者自己使用 application.css.scss 和 @import :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-08
      • 1970-01-01
      • 2012-04-30
      • 1970-01-01
      • 2019-07-03
      • 2014-11-01
      相关资源
      最近更新 更多