移至生产环境时预编译资产时出错

error precompiling assets when moving to production

我正在尝试将我的项目投入生产,
正在努力

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 它也没有告诉我。

在研究时发现了这个问题: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 文件,包括子目录。
  • 脚本不会输出压缩文件。这只是一个 in-memory 测试。