css - 如何从 Compass 生成的 Sprite 图像文件名中删除散列?

标签 css sass compass-sass

Compass 使用 chunky_png 渲染 Sprite 。它在文件末尾添加一个哈希值以强制缓存下载新的图像 Sprite 。有没有办法关闭此缓存破坏?

最佳答案

不幸的是,asset_cache_buster :none 选项不会禁用将哈希添加到文件名的末尾。

就像我之前写的(法语),Compass 没有办法禁用缓存哈希破坏器,但我建议使用 a solution
在您的配置文件(例如 config.rb)中添加以下行:

# Make a copy of sprites with a name that has no uniqueness of the hash.
on_sprite_saved do |filename|
  if File.exists?(filename)
    FileUtils.cp filename, filename.gsub(%r{-s[a-z0-9]{10}\.png$}, '.png')
  end
end

# Replace in stylesheets generated references to sprites
# by their counterparts without the hash uniqueness.
on_stylesheet_saved do |filename|
  if File.exists?(filename)
    css = File.read filename
    File.open(filename, 'w+') do |f|
      f << css.gsub(%r{-s[a-z0-9]{10}\.png}, '.png')
    end
  end
end

现在,使用 compass clean 删除生成的文件并使用 compass compile 重新开始编译。
例如,您获得了一个images/icons-scb1e5456d5.png 文件一个images/icons.png 文件。在样式表中,所有对 Sprite 的引用现在都指向没有散列的版本。

确保文件具有提供的哈希值,以优化 Compass 的编译时间。

关于css - 如何从 Compass 生成的 Sprite 图像文件名中删除散列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9183133/

相关文章:

node.js - Node-sass 找不到 Compass 导入

css - compass 样式 - 内联 block 列表和水平列表之间的区别

SASS、COMPASS、SUSY——它们之间的关系是什么?

html - CSS 选择不适用于图像

css - CSS 网格中的 Chrome/Firefox 百分比高度差异

javascript - 打印网页时删除页面标题和日期(使用 CSS?)

css - 我正在尝试编辑这个 wordpress 主题

ruby-on-rails - 如何在Rails应用程序中导入SCSS mixins?

css - 在父 SASS 上设置样式

css - SASS 没有更新我的 Ionic 应用程序样式