将sass-rails gem升级到5.0会给出弃用警告

ipd*_*ipd 16 ruby-on-rails sass asset-pipeline

我们升级到sass-rails版本5.0.0并获得此弃用警告:

DEPRECATION WARNING: Extra .css in SCSS file is unnecessary. Rename /Users/foo/Projects/foo/app/assets/stylesheets/foo.css.scss to /Users/foo/Projects/foo/app/assets/stylesheets/foo.scss. (called from _app_views_layouts_application_html_erb__1560597815210891605_70190441246060 at /Users/foo/Projects/foo/app/views/layouts/application.html.erb:13)
Run Code Online (Sandbox Code Playgroud)

有谁知道这是怎么回事?gem是否真的希望我重命名我的所有样式表资源:

app/assets/stylesheets/foo.css.scss
Run Code Online (Sandbox Code Playgroud)

至:

app/assets/stylesheets/foo.scss
Run Code Online (Sandbox Code Playgroud)

似乎与我一起反对多年的Rails惯例.也许有办法抑制弃用警告?

jma*_*xyz 30

这为我处理了它:

#!/bin/sh
for file in $(find ./app/assets/stylesheets/ -name "*.css.scss")
do
    git mv $file `echo $file | sed s/\.css//`
done
Run Code Online (Sandbox Code Playgroud)

  • 额外帮助:将其保存到文件(类似`remove_css_extension.sh`),`$ chmod a + x remove_css_extension.sh`,`$./ remove_css_extension.sh`,完成. (3认同)
  • 而不是sed只使用basename:`git mv"$ file"$(basename"$ file".scss)` (2认同)

mfa*_*kas 13

是的,你需要重命名你的.css.scss只是.scss,因为.css.scss不会在链轮4支持.

如果要暂时禁止弃用,可以执行以下操作 config/initializer/deprecations.rb

Rails.application.config.after_initialize do
  old_behaviour = ActiveSupport::Deprecation.behavior
  ActiveSupport::Deprecation.behavior = ->(message, callstack) {
    unless message.starts_with?('DEPRECATION WARNING: Extra .css in SCSS file is unnecessary.',
                                'DEPRECATION WARNING: Extra .css in SASS file is unnecessary.')
      old_behaviour.each { |behavior| behavior[message,callstack] }
    end
  }
end
Run Code Online (Sandbox Code Playgroud)

或者你可以修补补丁,不要生成这样的消息:

module DisableCssDeprecation
  def deprecate_extra_css_extension(engine)
    if engine && filename = engine.options[:filename]
      if filename.end_with?('.css.scss','.css.sass')
        engine
      else
        super
      end 
    end
  end
end

module Sass ; module Rails ; class SassImporter
  prepend DisableCssDeprecation
end ; end ; end
Run Code Online (Sandbox Code Playgroud)