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