2012-02-17 108 views
2

有沒有辦法在SASS中執行文件rev緩存破壞?它似乎有可能使用羅盤每個這個答案在這裏: SASS Image CSS Cache Busting (via Compass)sass緩存破壞

但我還沒有找到任何方式來做這個只是使用SASS。有沒有辦法讓SASS從文件系統獲取文件修改信息,並追加到圖像路徑?

我寧願不追加查詢字符串 - 相反,我認爲這是一個更好的方法。

+0

它看起來確實可以通過定義一個自定義sass函數來實現:http://www.seancolombo.com/2010/07/28/how-to-make-and-use-a-custom-sass-function /但我並不熟悉w/Ruby。我寧願不必通過命令行指定自定義函數,而是繼續使用SASS監視命令。 – elleeott 2012-02-20 15:08:25

+0

你說的避免查詢字符串是正確的 - 它們可以被忽略(http://www.stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring/)。如果你使用ruby和大概的rails,你使用的是v3嗎? Rails資產管道爲你做的一切,如果不是,那麼鏈輪可能會有所幫助。放置圖像引用時,使用image-url()而不是url(),以便計算路徑 – Dawn 2012-09-28 15:50:22

回答

1

非常感謝Dawn在這段時間裏一直在鳴叫!我從那以後就知道了,但忘了我在這裏發佈的。

我有我引用當我通過命令行運行SASS一個自定義的RB文件 - 像這樣:

sass --update sass:css -r file_mod.rb 

在file_mod.rb,我有以下的紅寶石函數的伎倆:

require 'sass' 

module GETMODINT 
def file_url(staticFilePath,staticHost,filePath) 
    assert_type filePath, :String 
    filePath = filePath.value #get string value of literal 
    staticFilePath = staticFilePath.value 
    staticHost = staticHost.value 
    modtime = File.mtime(filePath).to_i 
    #Sass::Script::Number.new(modtime) 

    fileBaseName = File.basename filePath, '.*' 
    fileDir = File.dirname(filePath).sub(staticFilePath,'') 
    fileExt = File.extname(filePath) 
    path = "url('#{staticHost}#{fileDir}/#{fileBaseName}.#{modtime}#{fileExt}')" 
    return Sass::Script::String.new(path) 
end 
Sass::Script::Functions.declare :modInt, [:filePath] 
end 

module Sass::Script::Functions 
    include GETMODINT 
end 

然後,在SASS混入,我簡單引用FILE_URL函數,傳遞它需要建立的結果的參數:

@mixin backgroundImage($path, $position:0 0, $repeat:no-repeat) { 
background: { 
    image:file_url($staticFilePath,$staticHost,$path); 
    position:$position; 
    repeat:$repeat; 
} 
} 

在我的情況下,我使用它來構建一個CSS背景圖像路徑。應該容易修改以適應其他目的。