天天看点

Vim 中调用 YUICompressor 压缩 js, css

Vim 函数, 就是调用 YUICompressor 压缩 js 或 css

//

把 下面这个函数放到 _vimrc(.vimrc ) 中, 修改 yuicompressor_program 为 jar 文件所在位置;

如果不需要打印详细信息, 可以去掉 --verbose 选项

//

使用:

打开要压缩的文件, 执行

:call YUICompressor()

 

//

也可以定义自己的  map, 使用快捷键来调用函数. 或者 autocommand 绑定 vim 写入事件

 

function! YUICompressor()

let yuicompressor_program = 'java -jar D:/tools/ourtools/yui-compressor/yuicompressor.jar --verbose'

"

let yui_option_charset = '--charset ' . &fileencoding

"

let current_file = expand('%:p')

if match(current_file, "/.js$") != -1

let yui_option_type = '--type js'

elseif match(current_file, "/.css$") != -1

let yui_option_type = '--type css'

else

echoerr 'Error: File "' . current_file . '" is not js or css!'

return

endif

"

let yui_option_outputfile = substitute(current_file, '/./(js/|css/)$', '-min/./1', "")

if yui_option_outputfile == current_file

echoerr 'Error: Cannot substitute filename to "-min" file!'

return

endif

"

let yui_option_outputfile = '-o ' . shellescape(yui_option_outputfile)

let current_file = shellescape(current_file)

"

"

let yui_options = yui_option_charset . ' ' . yui_option_type . ' ' . yui_option_outputfile . ' ' . current_file

let cmd = yuicompressor_program . ' ' . yui_options

"

"" echo cmd

"" return

"

let cmd_output = system(cmd)

"

echo cmd_output

endfunction