如何从Compass生成的精灵图像文件名中删除哈希?

Tho*_*ltz 14 css sass compass-sass

指南针使用chunky_png渲染精灵.它在文件末尾添加一个哈希,以强制缓存下载新的图像精灵.有没有办法让这个缓存破坏?

pio*_*ouM 23

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

就像我不久前写的那样(法语),Compass无法禁用缓存哈希破坏程序,但我提出了一个解决方案.
在您的配置文件(例如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
Run Code Online (Sandbox Code Playgroud)

现在,用于compass clean删除生成的文件并重新启动编译compass compile.
你获得,例如images/icons-scb1e5456d5.png文件一个images/icons.png文件.在样式表中,对精灵的所有引用现在都指向没有哈希的版本.

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


max*_*tty 22

按照配置参考中的说明在asset_cache_buster :none config.rb中进行设置

  • 此解决方案不会阻止Compass将散列作为后缀放入sprite文件名,这在给定问题中是一个问题. (4认同)