Skip to content

Instantly share code, notes, and snippets.

@waghanza
Created March 20, 2012 16:55
Show Gist options
  • Save waghanza/2138178 to your computer and use it in GitHub Desktop.
Save waghanza/2138178 to your computer and use it in GitHub Desktop.
Minify your JS file
package JSClosureCompiler;
use nginx;
use WebService::Google::Closure;
#to make sure we write UTF-8 files
use open OUT => ':utf8';
sub handler {
my $r=shift;
my $cache_dir="/tmp"; # Cache directory where optimized files will be kept
my $cache_file=$r->uri;
$cache_file=~s!/!_!g;
$cache_file=join("/", $cache_dir, $cache_file);
my $uri=$r->uri;
my $filename=$r->filename;
return DECLINED unless -f $filename;
if (! -f $cache_file) {
my $minjs = WebService::Google::Closure->new(file => $filename)->compile;
open(OUTFILE, '>' . $cache_file ) or die "Error writting file: $!";
print OUTFILE $minjs->code;
close(OUTFILE);
}
$r->send_http_header("application/javascript");
$r->sendfile($cache_file);
return OK;
}
1;
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment