Skip to content

Instantly share code, notes, and snippets.

@hjue
Last active April 11, 2016 17:50
Show Gist options
  • Save hjue/5820687 to your computer and use it in GitHub Desktop.
Save hjue/5820687 to your computer and use it in GitHub Desktop.
EmbeddedPerlMinifyJS
package Minify;
use nginx;
use JavaScript::Minifier qw(minify);
sub handler {
my $r=shift;
my $cache_dir="/tmp";
my $cache_file=$r->uri;
$cache_file=~s!/!_!g;
$cache_file=$r->header_in("Host").$cache_file;
$cache_file=join("/", $cache_dir, $cache_file);
my $uri=$r->uri;
my $filename=$r->filename;
return DECLINED unless -f $filename;
if (! -f $cache_file) {
open(INFILE, $filename) or die "Error reading file: $!";
open(OUTFILE, '>' . $cache_file ) or die "Error writting file: $!";
minify(input => *INFILE, outfile => *OUTFILE);
chmod 0777 , $cache_file;
close(INFILE);
close(OUTFILE);
}
$r->send_http_header("application/javascript");
$r->sendfile($cache_file);
return OK;
}
1;
__END__
http://wiki.nginx.org/EmbeddedPerlMinifyJS
compile nginx:
./configure --with-http_perl_module
make && make instll
nginx: nginx.conf
http {
...
perl_modules perl;
perl_require JavaScript/Minifier.pm;
perl_require Minify.pm;
server {
...
location ~ \.js$ {
perl Minify::handler;
}
On the fly javascript compression/optimization on nginx using Google Closure Compiler
http://openspot.antithesis.gr/archives/187
@ansjsun
Copy link

ansjsun commented Oct 30, 2013

还是不能

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment