Vim Tips Wiki
(Assign tip id + convert to TipNew template + minor clean)
(Add instructions for closure-based complier plugin)
Line 51: Line 51:
 
*Could be expanded to other file types.
 
*Could be expanded to other file types.
 
*Might be nice to define a closure compiler plugin. I.e. this would be best accomplished using the {{help|prefix=no|id='makeprg'}} option, which is normally set using the {{help|prefix=no|:compiler}} command.
 
*Might be nice to define a closure compiler plugin. I.e. this would be best accomplished using the {{help|prefix=no|id='makeprg'}} option, which is normally set using the {{help|prefix=no|:compiler}} command.
  +
  +
==Alternative using {{help|prefix=no|:compiler}}==
  +
  +
This procedure defines a closure compiler plugin that may be used rather than the above.
  +
  +
Put the following in <tt>.vim/compiler/closure.vim</tt>:
  +
  +
<source lang="vim">
  +
if exists("current_compiler")
  +
finish
  +
endif
  +
  +
let current_compiler = "closure"
  +
  +
if exists(":CompilerSet") != 2
  +
" older Vim always used :setlocal
  +
command -nargs=* CompilerSet setlocal <args>
  +
endif
  +
  +
CompilerSet makeprg=java\ -jar\ /home/username/closure/compiler.jar\ --js_output_file=%<.min.js\ --js\ %
  +
CompilerSet errorformat=%E%f:%l:\ %m,%-Z%p^,%-C%.%#,%-G%.%#
  +
</source>
  +
  +
=== Using this tip ===
  +
*Edit .js file
  +
*Set compiler: <tt>:compiler closure</tt>
  +
*Save .js file
  +
*<tt>:make</tt>
  +
*Fix bugs (<tt>:cope</tt>, <tt>:cn</tt>, etc), repeat.
  +
*Link compiled version in .html
  +
*Test (because it's automatically compiled!)

Revision as of 14:50, 6 September 2011

Tip 1657 Printable Monobook Previous Next

created June 28, 2010 · complexity basic · author Sternebrau · version 7.0


This tip describes a simple way to automatically compile Javascript files using Google Closure. The compiled results are saved to a different file, for example, filename.js saves as both filename.js and filename.min.js.

The regular way

  • Edit .js file
  • Save .js file
  • Link it in .html file
  • Compile .js file
  • Link compiled version in .html file
  • Edit .js file
  • Save .js file
  • Forget to compile it
  • Become frustrated because the compiled version is still linked in .html file

Using this tip

  • Edit .js file
  • Save .js file
  • Link compiled version in .html
  • Edit .js file
  • Save .js file
  • Test (because it's automatically compiled!)

Auto compile

Add this to your vimrc, while changing the path in "cpa" to suit your system:

autocmd BufWriteCmd *.js :call CompileJS()
function! CompileJS()
  if &modified
    write
    let fn = expand('%:p')
    let pn = expand('%:p:h')
    let fnm = expand('%:r.js')
    let cpa = '/home/username/closure/compiler.jar'
    execute "! java -jar " . cpa . " --js=" . fn . " --js_output_file=" . pn . "/" . fnm . ".min.js"
  endif
endfunction

Comments

  • Could be expanded to other file types.
  • Might be nice to define a closure compiler plugin. I.e. this would be best accomplished using the 'makeprg' option, which is normally set using the :compiler command.

Alternative using :compiler

This procedure defines a closure compiler plugin that may be used rather than the above.

Put the following in .vim/compiler/closure.vim:

if exists("current_compiler")
  finish
endif

let current_compiler = "closure"

if exists(":CompilerSet") != 2
  " older Vim always used :setlocal
  command -nargs=* CompilerSet setlocal <args>
endif

CompilerSet makeprg=java\ -jar\ /home/username/closure/compiler.jar\ --js_output_file=%<.min.js\ --js\ %
CompilerSet errorformat=%E%f:%l:\ %m,%-Z%p^,%-C%.%#,%-G%.%#

Using this tip

  • Edit .js file
  • Set compiler: :compiler closure
  • Save .js file
  • :make
  • Fix bugs (:cope, :cn, etc), repeat.
  • Link compiled version in .html
  • Test (because it's automatically compiled!)