| 1 | package Apache2::Filter::Minifier::JavaScript; | ||
| 2 | |||
| 3 | ############################################################################### | ||
| 4 | # Required inclusions. | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use Apache2::Filter qw(); # $f | ||
| 8 | use Apache2::RequestRec qw(); # $r | ||
| 9 | use Apache2::RequestUtil qw(); # $r->dir_config | ||
| 10 | use Apache2::Log qw(); # $log->*() | ||
| 11 | use APR::Table qw(); # dir_config->get() and headers_out->unset() | ||
| 12 | use Apache2::Const -compile => qw(OK DECLINED); | ||
| 13 | use Time::HiRes qw(gettimeofday tv_interval); | ||
| 14 | |||
| 15 | ############################################################################### | ||
| 16 | # Load up the JS minifier modules. | ||
| 17 | use JavaScript::Minifier qw(minify); | ||
| 18 | eval { require JavaScript::Minifier::XS; }; | ||
| 19 | |||
| 20 | ############################################################################### | ||
| 21 | # Keep our namespace clean. | ||
| 22 | use namespace::clean; | ||
| 23 | |||
| 24 | ############################################################################### | ||
| 25 | # Version number. | ||
| 26 | our $VERSION = '1.07'; | ||
| 27 | |||
| 28 | ############################################################################### | ||
| 29 | # MIME-Types we're willing to minify. | ||
| 30 | my %mime_types = ( | ||
| 31 | 'text/javascript' => 1, | ||
| 32 | 'text/ecmascript' => 1, | ||
| 33 | 'application/javascript' => 1, | ||
| 34 | 'application/ecmascript' => 1, | ||
| 35 | 'application/x-javascript' => 1, | ||
| 36 | ); | ||
| 37 | |||
| 38 | ############################################################################### | ||
| 39 | # Subroutine: handler($filter) | ||
| 40 | ############################################################################### | ||
| 41 | # JavaScript minification output filter. | ||
| 42 | sub handler { | ||
| 43 | my $f = shift; | ||
| 44 | my $r = $f->r; | ||
| 45 | my $log = $r->log; | ||
| 46 | |||
| 47 | # assemble list of acceptable MIME-Types | ||
| 48 | my %types = ( | ||
| 49 | %mime_types, | ||
| 50 | map { $_=>1 } $r->dir_config->get('JsMimeType'), | ||
| 51 | ); | ||
| 52 | |||
| 53 | # determine Content-Type of document | ||
| 54 | my ($ctype) = ($r->content_type =~ /^(.+?)(?:;.*)?$/); | ||
| 55 | unless ($ctype) { | ||
| 56 | $log->info( "unable to determine content type; skipping : URL ", $r->uri ); | ||
| 57 | return Apache2::Const::DECLINED; | ||
| 58 | } | ||
| 59 | |||
| 60 | # only process JS documents | ||
| 61 | unless (exists $types{$ctype}) { | ||
| 62 | $log->info( "skipping request to ", $r->uri, " (not a JS document)" ); | ||
| 63 | return Apache2::Const::DECLINED; | ||
| 64 | } | ||
| 65 | |||
| 66 | # figure out which minifier module/function we're supposed to be using; | ||
| 67 | # either an explicit minifier function/package, or our list of acceptable | ||
| 68 | # minifiers | ||
| 69 | my $minifier; | ||
| 70 | my @possible = $r->dir_config->get('JsMinifier') || ( | ||
| 71 | 'JavaScript::Minifier::XS', | ||
| 72 | 'JavaScript::Minifier', | ||
| 73 | ); | ||
| 74 | foreach my $maybe (@possible) { | ||
| 75 | no strict 'refs'; | ||
| 76 | # explicit function name | ||
| 77 | if (defined &{"$maybe"}) { | ||
| 78 | $minifier = sub { $maybe->(shift) }; | ||
| 79 | last; | ||
| 80 | } | ||
| 81 | # package name; look for "minify()" function | ||
| 82 | if (defined &{"${maybe}::minify"}) { | ||
| 83 | my $func = \&{"${maybe}::minify"}; | ||
| 84 | $minifier = ($maybe eq 'JavaScript::Minifier') | ||
| 85 | ? sub { $func->(input=>shift) } | ||
| 86 | : sub { $func->(shift) }; | ||
| 87 | last; | ||
| 88 | } | ||
| 89 | } | ||
| 90 | unless ($minifier) { | ||
| 91 | $log->info( "no JavaScript minifier available; declining" ); | ||
| 92 | return Apache2::Const::DECLINED; | ||
| 93 | } | ||
| 94 | |||
| 95 | # gather up entire document | ||
| 96 | my $ctx = $f->ctx; | ||
| 97 | while ($f->read(my $buffer, 4096)) { | ||
| 98 | $ctx .= $buffer; | ||
| 99 | } | ||
| 100 | |||
| 101 | # unless we're at the end, store the JS for our next invocation | ||
| 102 | unless ($f->seen_eos) { | ||
| 103 | $f->ctx( $ctx ); | ||
| 104 | return Apache2::Const::OK; | ||
| 105 | } | ||
| 106 | |||
| 107 | # if we've got JS to minify, minify it | ||
| 108 | if ($ctx) { | ||
| 109 | my $t_st = [gettimeofday()]; | ||
| 110 | my $min = eval { $minifier->($ctx) }; | ||
| 111 | if ($@) { | ||
| 112 | # minification failed; log error and send original JS | ||
| 113 | $log->error( "error minifying: $@" ); | ||
| 114 | $f->print( $ctx ); | ||
| 115 | } | ||
| 116 | else { | ||
| 117 | # minification ok; log results and send minified JS | ||
| 118 | my $t_dif = tv_interval($t_st); | ||
| 119 | my $l_min = length($min); | ||
| 120 | my $l_js = length($ctx); | ||
| 121 | $log->debug( "JS minified $l_js to $l_min : t:$t_dif : URL ", $r->uri ); | ||
| 122 | $r->headers_out->unset( 'Content-Length' ); | ||
| 123 | $f->print( $min ); | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | return Apache2::Const::OK; | ||
| 128 | } | ||
| 129 | |||
| 130 | 1; | ||
| 131 | |||
| 132 | =for stopwords PerlVar minified minifier minification minifies minifying | ||
| 133 | |||
| 134 | =head1 NAME | ||
| 135 | |||
| 136 | Apache2::Filter::Minifier::JavaScript - JavaScript minifying output filter for mod_perl | ||
| 137 | |||
| 138 | =head1 SYNOPSIS | ||
| 139 | |||
| 140 | <LocationMatch "\.js$"> | ||
| 141 | PerlOutputFilterHandler Apache2::Filter::Minifier::JavaScript | ||
| 142 | |||
| 143 | # if you need to supplement MIME-Type list | ||
| 144 | PerlSetVar JsMimeType text/json | ||
| 145 | |||
| 146 | # if you want to explicitly specify the minifier to use | ||
| 147 | #PerlSetVar JsMinifier JavaScript::Minifier::XS | ||
| 148 | #PerlSetVar JsMinifier JavaScript::Minifier | ||
| 149 | #PerlSetVar JsMinifier MY::Minifier::function | ||
| 150 | </LocationMatch> | ||
| 151 | |||
| 152 | =head1 DESCRIPTION | ||
| 153 | |||
| 154 | C<Apache2::Filter::Minifier::JavaScript> is a Mod_perl2 output filter which | ||
| 155 | minifies JavaScript using C<JavaScript::Minifier> or | ||
| 156 | C<JavaScript::Minifier::XS>. | ||
| 157 | |||
| 158 | Only JavaScript documents are minified, all others are passed through | ||
| 159 | unaltered. C<Apache2::Filter::Minifier::JavaScript> comes with a list of | ||
| 160 | several known acceptable MIME-Types for JavaScript documents, but you can | ||
| 161 | supplement that list yourself by setting the C<JsMimeType> PerlVar | ||
| 162 | appropriately (use C<PerlSetVar> for a single new MIME-Type, or C<PerlAddVar> | ||
| 163 | when you want to add multiple MIME-Types). | ||
| 164 | |||
| 165 | Given a choice, using C<JavaScript::Minifier::XS> is preferred over | ||
| 166 | C<JavaScript::Minifier>, but we'll use whichever one you've got available. If | ||
| 167 | you want to explicitly specify which minifier you want to use, set the | ||
| 168 | C<JsMinifier> PerlVar to the name of the package/function that implements the | ||
| 169 | minifier. Minification functions are expected to accept a single parameter | ||
| 170 | (the JavaScript to be minified) and to return the minified JavaScript on | ||
| 171 | completion. If you specify a package name, we look for a C<minify()> function | ||
| 172 | in that package. | ||
| 173 | |||
| 174 | =head2 Caching | ||
| 175 | |||
| 176 | Minification does require additional CPU resources, and it is recommended that | ||
| 177 | you use some sort of cache in order to keep this to a minimum. | ||
| 178 | |||
| 179 | Being that you're already running Apache2, though, here's some examples of a | ||
| 180 | mod_cache setup: | ||
| 181 | |||
| 182 | Disk Cache | ||
| 183 | |||
| 184 | # Cache root directory | ||
| 185 | CacheRoot /path/to/your/disk/cache | ||
| 186 | # Enable cache for "/js/" location | ||
| 187 | CacheEnable disk /js/ | ||
| 188 | |||
| 189 | Memory Cache | ||
| 190 | |||
| 191 | # Cache size: 4 MBytes | ||
| 192 | MCacheSize 4096 | ||
| 193 | # Min object size: 128 Bytes | ||
| 194 | MCacheMinObjectSize 128 | ||
| 195 | # Max object size: 512 KBytes | ||
| 196 | MCacheMaxObjectSize 524288 | ||
| 197 | # Enable cache for "/js/" location | ||
| 198 | CacheEnable mem /js/ | ||
| 199 | |||
| 200 | =head1 METHODS | ||
| 201 | |||
| 202 | =over | ||
| 203 | |||
| 204 | =item handler($filter) | ||
| 205 | |||
| 206 | JavaScript minification output filter. | ||
| 207 | |||
| 208 | =back | ||
| 209 | |||
| 210 | =head1 AUTHOR | ||
| 211 | |||
| 212 | Graham TerMarsch (cpan@howlingfrog.com) | ||
| 213 | |||
| 214 | Many thanks to Geoffrey Young for writing C<Apache::Clean>, from which several | ||
| 215 | things were lifted. :) | ||
| 216 | |||
| 217 | =head1 COPYRIGHT | ||
| 218 | |||
| 219 | Copyright (C) 2007, Graham TerMarsch. All Rights Reserved. | ||
| 220 | |||
| 221 | This is free software; you can redistribute it and/or modify it under the same | ||
| 222 | license as Perl itself. | ||
| 223 | |||
| 224 | =head1 SEE ALSO | ||
| 225 | |||
| 226 | =over | ||
| 227 | |||
| 228 | =item L<JavaScript::Minifier> | ||
| 229 | |||
| 230 | =item L<JavaScript::Minifier::XS> | ||
| 231 | |||
| 232 | =item L<Apache2::Filter::Minifier::CSS> | ||
| 233 | |||
| 234 | =item L<Apache::Clean> | ||
| 235 | |||
| 236 | =back | ||
| 237 | |||
| 238 | =cut |