【问题标题】:upgrade sass-rails gem to 5.0 gives deprecation warning将 sass-rails gem 升级到 5.0 会给出弃用警告
【发布时间】:2015-02-16 16:48:57
【问题描述】:

我们升级到 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)

有人知道这是怎么回事吗? gem 是否真的希望我从以下位置重命名我的所有样式表资产:

app/assets/stylesheets/foo.css.scss

到:

app/assets/stylesheets/foo.scss

?

在我看来,这与多年来的 Rails 惯例背道而驰。也许有办法抑制弃用警告?

【问题讨论】:

标签: ruby-on-rails sass asset-pipeline


【解决方案1】:

是的,您需要将 .css.scss 重命名为 .scss,因为 sprockets 4 不支持 .css.scss

如果您想暂时禁止弃用,可以将以下内容发送至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

或者你可以通过猴子补丁不生成这样的消息:

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

【讨论】:

    【解决方案2】:

    这为我处理了:

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

    【讨论】:

    • 额外帮助:将其保存到文件(类似于remove_css_extension.sh)、$ chmod a+x remove_css_extension.sh$ ./remove_css_extension.sh,完成。
    • 只使用基本名称而不是 sed:git mv "$file" $(basename "$file" .scss)
    • 不知道基本名称。不错!
    • 我使用你的脚本和这个 find 命令循环遍历所有子文件夹中的 sass 和 scss 文件(我也有一些在 ./vendor/assets 中):find ./ -name "*.css.scss" -o -name "*.css.sass"
    【解决方案3】:

    这个命令帮助我重命名了很多 .css.sass 文件:

    find ./app/assets/stylesheets -type f | sed 'p;s/\.css\.scss/.scss/' | xargs -n2 git mv
    

    【讨论】:

      猜你喜欢
      • 2020-04-07
      • 2019-04-28
      • 2023-03-06
      • 1970-01-01
      • 2019-03-05
      • 2020-06-09
      • 2021-07-09
      • 2012-04-15
      • 1970-01-01
      相关资源
      最近更新 更多