跳過導覽列

O3noBLOG

A Happy(?)Designer ~~

單篇彙整

Vim 儲存完 JavaScript 檔案後自動用 yuicompressor

因為每次存完 js 檔案還要 Ctrl+Z ,然後手動跑 yuicompressor 才能去看效果,有問題還要 fg 回來繼續改,所以我剛剛終於受不了去研究 vim script 了

function Js_css_compress ()
    let cwd = expand('<afile>:p:h')
    let nam = expand('<afile>:t:r')
    let ext = expand('<afile>:e')
    if -1 == match(nam, "[\._]src$")
        let minfname = nam.".min.".ext
    else
        let minfname = substitute(nam, "[\._]src$", "", "g").".".ext
    endif
    if ext == 'less'
        if executable('lessc')
            cal system( 'lessc '.cwd.'/'.nam.'.'.ext.' &')
        endif
    else
        if filewritable(cwd.'/'.minfname)
            if ext == 'js' && executable('closure-compiler')
                cal system( 'closure-compiler --js '.cwd.'/'.nam.'.'.ext.' > '.cwd.'/'.minfname.' &')
            elseif executable('yuicompressor')
                cal system( 'yuicompressor '.cwd.'/'.nam.'.'.ext.' > '.cwd.'/'.minfname.' &')
            endif
        endif
    endif
endfunction
autocmd FileWritePost,BufWritePost *.js :call Js_css_compress()
autocmd FileWritePost,BufWritePost *.css :call Js_css_compress()
autocmd FileWritePost,BufWritePost *.less :call Js_css_compress()

把上面的指令加到 vimrc 檔裡面就可以了,前兩個分別是會把 blah.src.js, blah_src.js 轉成 blah.js 用的,第三行則是會把 blah.js 檔轉成 blah.min.js ,慎用之~~

加上第四行,會先檢查 blah.min.js 存不存在,存在的話才用 yuicompressor ,另外 code 在 gist 也貼了一份

改版成 function 形式了~~

更新:支援 less,有 Google Closure 的話會先用它壓縮 js 檔。

引用(http://othree.net/cgi-bin/mt/mt-tb.cgi/589)

目前無人引用。

迴響(發表你的迴響)

  1. 1網頁設計在 2009-09-16 15:00:53 發表:

    PageRank的值很高耶,表示你的內容對google的搜尋來說,是具知識性的~~~

    你有表示說你是非營利,純個人空間,所以就不可能提供text link的欄位就是了?! 如果有其他想法請跟我聯繫。

  2. 2由 c9s 在 2009-11-25 16:17:23 發表:

    如果改用 system() 會好很多

    而且可以加上 '&' 號來丟到背景執行 ^^

    cal system(cmd . ' &')

    像這樣。 :-)

    minifilename 可以用 matchstr 來取得,會比 substitute 方便的多 :-)

  3. 3Anonymous在 2009-11-25 17:06:37 發表:

    剛剛改用 system 丟背景果然舒服很多XD,感謝感謝

  4. 4由 c9s 在 2009-12-07 23:17:01 發表:

    我在想說你要不要順便弄成 plugin .. :p

  5. 5othree.net在 2009-12-23 00:04:47 發表:

    我想說才這幾行好像也不用弄成 plugin 吧:P

發表迴響

如果欲使用OpenID,請開啟瀏覽器的JavaScript功能。

accesskey:P