2012-02-07 77 views

回答

23

不幸的是asset_cache_buster :none選項不會禁用添加散列到文件名末尾。

就像我幾年時間前寫(法語),指南針也沒有辦法禁用緩存哈希剋星,但我建議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文件。在樣式表中,所有對精靈的引用現在指向沒有散列的版本。

一定要保持文件中有提供優化的指南針編譯時間的哈希值。

+0

感謝你們,我個人選擇使用FileUtils.mv代替FileUtils。cp,這樣hold文件不會掛起 – isNaN1247 2012-03-15 21:23:33

+1

在grunt-contrib-sass中使用它似乎不會觸發on_stylesheet_saved塊,從而使css背景屬性引用帶有散列的文件而不是未散列的文件。使用它與grunt-contrib-compass確實觸發了它。 – yuvilio 2013-06-22 20:52:34

+1

@yuvilio這個鉤子是由Compass提供的,並不存在於Sass中。 Thx提醒! – piouPiouM 2013-06-22 23:27:18

22

asset_cache_buster :none在config.rb爲documented in their configuration reference

+1

這需要更多upvotes – 2013-03-27 22:54:02

+3

應該是被接受的答案。 – 2013-09-16 21:30:58

+4

此解決方案不會阻止Compass將哈希作爲後綴添加到精靈文件名中給定問題中的問題是什麼。 – s3m3n 2013-11-05 16:06:54

1

我還沒有與精靈測試,但是這一點也適用replace-text-with-dimensions,例如:

config.rb:

# disable asset cache buster 
asset_cache_buster do |http_path, real_path| 
    nil 
end 

The compass configuration file at caring.com

4

更好的發現的解決方案可以在另一個發現similar question

這是更好,因爲:

  1. 腳本生成精靈之前改名 - 而不是之後。
  2. 因爲點1沒有必要幾乎不變.css自動生成的文件。它從頭開始以正確的名字生成。
  3. 接受的解決方案,使cp與哈希生成精靈(複印件)和它停留在文件系統/回購爲重複這是相當糟糕的。此外,它仍然被視爲與本地回購更改,所以你犯兩個相同的文件。解決方案可以通過mv更改生成的散列文件名來清除,但在這種情況下,每次在.scss文件中使用它時都會生成精靈,因此更糟糕。