Skip to content

Instantly share code, notes, and snippets.

@waghanza
Created March 20, 2012 16:57
Show Gist options
  • Save waghanza/2138184 to your computer and use it in GitHub Desktop.
Save waghanza/2138184 to your computer and use it in GitHub Desktop.
Minify your CSS file
package Minify;
use nginx;
use CSS::Minifier::XS;
sub css_handler {
my $r = shift;
my $cache_dir="/tmp";
my $cache_file=$r->uri;
$cache_file=~s!/!_!g;
$cache_file=join("/", $cache_dir, $cache_file);
my $uri=$r->uri;
my $filename=$r->filename;
local $/=undef;
return DECLINED unless -f $filename;
open(INFILE, $filename) or die "Error reading file: $!";
my $css = <INFILE>;
close(INFILE);
open(OUTFILE, '>' . $cache_file) or die "Error writing file: $!";
print OUTFILE CSS::Minifier::XS::minify($css);
close(OUTFILE);
$r->send_http_header('text/css');
$r->sendfile($cache_file);
return OK;
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment