Skip to content

Instantly share code, notes, and snippets.

@hernan604
Last active August 29, 2015 14:00
Show Gist options
  • Select an option

  • Save hernan604/11057601 to your computer and use it in GitHub Desktop.

Select an option

Save hernan604/11057601 to your computer and use it in GitHub Desktop.
js minifier for vim (using Vim::X + perl)
package JsMinifier;
use strict;
use warnings;
use Vim::X;
use JavaScript::Packer;
=head2 .vimrc
perl push @INC, '/home/hernan/mnt/desktop/home/hernan/mnt/sda4/files/perl/vimx/lib/';
perl use JsMinifier;
map ,jsmini :call Vjs_minifier( line("'<"), line("'>") );
=head2 usage in vim
:call Vjs_minifier( line("'<"), line("'>") , "best" );
:call Vjs_minifier( line("'<"), line("'>") , "obfuscate" );
=cut
sub Vjs_minifier :Vim(args) {
my $range_init = shift;
my $range_end = shift;
my $compress_option = shift||"best";
my @range = ( $range_init .. $range_end );
my @lines = map { vim_line($_)->content } @range;
my $js_src = join "", @lines;
vim_delete( @range );
my $packer = JavaScript::Packer->init();
vim_prepend(
$packer->minify( \$js_src , { compress => $compress_option } )
);
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment