Last active
April 11, 2016 17:50
-
-
Save hjue/5820687 to your computer and use it in GitHub Desktop.
EmbeddedPerlMinifyJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
还是不能