移动到生产时错误预编译资产

时间:2017-01-14 11:34:16

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-3.2 ruby-on-rails-3.1

我正在尝试将我的项目转移到生产中 试图

RAILS_ENV=production bundle exec rake assets:precompile

给了我一个错误,没有指定它在哪个文件中。

rake aborted!
Sass::SyntaxError: Invalid CSS after "}": expected selector or at-rule, was "}"
(sass):89
/Users/mac/.rvm/gems/ruby-2.2.3/gems/sass-3.4.23/lib/sass/scss/parser.rb:1207:in `expected'
/Users/mac/.rvm/gems/ruby-2.2.3/gems/sass-3.4.23/lib/sass/scss/parser.rb:1137:in `expected'
/Users/mac/.rvm/gems/ruby-2.2.3/gems/sass-3.4.23/lib/sass/scss/parser.rb:42:in `parse'
/Users/mac/.rvm/gems/ruby-2.2.3/gems/sass-3.4.23/lib/sass/engine.rb:406:in `_to_tree'
/Users/mac/.rvm/gems/ruby-2.2.3/gems/sass-3.4.23/lib/sass/engine.rb:281:in `render'
/Users/mac/.rvm/gems/ruby-2.2.3/gems/sprockets-3.7.1/lib/sprockets/sass_compressor.rb:48:in `call'
/Users/mac/.rvm/gems/ruby-2.2.3/gems/sprockets-3.7.1/lib/sprockets/sass_compressor.rb:28:in `call'
/Users/mac/.rvm/gems/ruby-2.2.3/gems/sprockets-3.7.1/lib/sprockets/processor_utils.rb:75:in `call_processor'
/Users/mac/.rvm/gems/ruby-2.2.3/gems/sprockets-3.7.1/lib/sprockets/processor_utils.rb:57:in `block in call_processors'
/Users/mac/.rvm/gems/ruby-2.2.3/gems/sprockets-3.7.1/lib/sprockets/processor_utils.rb:56:in `reverse_each'

如何找出错误发生在哪个文件中? 即使用--trace也没告诉我。

1 个答案:

答案 0 :(得分:11)

在研究时发现此问题:https://github.com/rails/sass-rails/issues/368。显然,您必须检查每个文件以找出哪个文件无法编译。

但是你可以创建一个rake任务来帮助完成这个过程:

只需将以下代码添加到lib/tasks/assets.rake文件

即可
namespace :assets do

  desc "Find Sass::SyntaxError files..."
  task find_scss_with_error: :environment do
    files = Dir.glob( Rails.root.join("app", "assets", "stylesheets", "**/*")).grep(/.*\.[css|scss]/)
    files.each do |file|
        print "Trying to compile #{file}..."
        template = File.read(file)
        sass_engine = Sass::Engine.new(template)
        begin
            sass_engine.render
            print "[OK]"
        rescue
            print "[ERROR]"
        end
        puts
    end
  end

end

并运行

rake assets:find_scss_with_error

结果:

Trying to compile /data/ruby/scss-comments-failure/app/assets/stylesheets/example.css...[ERROR]
Trying to compile /data/ruby/scss-comments-failure/app/assets/stylesheets/application.css...[OK]

一些注意事项:

  • 它会检查*.css*.scss个文件,包括子目录。
  • 脚本不会将压缩输出到文件。它只是一个内存测试。