如何从罗盘生成的精灵图像文件名中删除哈希值? [英] How to remove the hash from Compass's generated sprite image filenames?

查看:200
本文介绍了如何从罗盘生成的精灵图像文件名中删除哈希值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

指南针使用chunky_png渲染精灵。它在文件的末尾添加一个哈希,以强制缓存下载新的图像精灵。

解决方案

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



像我之前写过的几次(法语),Compass没有办法禁用缓存哈希破坏,但我建议解决方案

您的配置文件(例如 config.rb )添加以下行:

 #制作一个名称没有散列唯一性的精灵的副本。 
on_sprite_saved do | filename |
if File.exists?(filename)
FileUtils.cp filename,filename.gsub(%r {-s [a-z0-9] {10} \.png $},'.png ')
end
end

#在样式表中替换生成的对sprites
#的引用,没有hash唯一性。
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 删除生成的文件并重新启动编译与罗盘编译

例如,您获得一个 images / icons-scb1e5456d5.png 文件一个 images / icons.png 文件。在样式表中,对sprite的所有引用现在指向没有散列的版本。



确保保留文件有一个散列,以优化Compass的编译时间。 / p>

Compass uses chunky_png to render the sprites. It adds a hash to the end of the file to force caches to download the new image sprites. Is there a way to turn this cache busting off?

解决方案

Unfortunately asset_cache_buster :none option does not disable adding the hash to the end of the filename.

Like I wrote few time ago (in french), Compass has no way to disable the cache hash buster, but I propose a solution.
In your configuration file (eg config.rb) add the following lines:

# 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

Now, uses compass cleanto remove generated files and restarts a compilation with compass compile.
You obtain, for example, a images/icons-scb1e5456d5.png file and a images/icons.png file. In the stylesheets, all references to the sprites now point to the version without hash.

Be sure to keep the file has a hash provided to optimize compile times by Compass.

这篇关于如何从罗盘生成的精灵图像文件名中删除哈希值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆