Skip to content

Commit a60eaaf

Browse files
committed
Added support for {{{JavaScript::Minifier::XS}}}, if available.
1 parent 448ec43 commit a60eaaf

File tree

8 files changed

+115
-3
lines changed

8 files changed

+115
-3
lines changed

Build.PL

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ $build_pkg->new(
5353
'mod_perl2' => 2.0,
5454
'Time::HiRes' => 0,
5555
},
56+
'recommends' => {
57+
'JavaScript::Minifier::XS' => 0,
58+
},
5659
# add configuration requirements both to build_requires and to META.yml,
5760
# until M::B supports "configure_requires" directly.
5861
'build_requires' => { %configure_requires },

Changes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ Revision history for Perl extension Apache2::Filter::Minifier::JavaScript.
22

33
1.04_01 ???
44
- added timing info to the debug logs
5+
- use JavaScript::Minifier::XS if available; its -MUCH- faster
6+
- new "JsMinifier" PerlVar, to let you specify which minifier package or
7+
function to use (otherwise we try J:M:XS first, falling back to J:M)
58

69
1.03 Fri Sep 28 11:09 PDT 2007
710
- release; verified that changes in 1.03_01 for configuration requirements

MANIFEST

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ t/content-length.t
1111
t/decline.t
1212
t/dynamic.t
1313
t/mime-types.t
14+
t/minifiers.t
1415
t/mod-cgi.t
1516
t/non-js.t
1617
t/registry.t
@@ -20,6 +21,7 @@ t/conf/extra.conf.in
2021
t/htdocs/test.js
2122
t/htdocs/test.txt
2223
t/htdocs/minified.txt
24+
t/htdocs/minified-xs.txt
2325
t/MY/slurp.pm
2426
t/MY/CharsetHandler.pm
2527
t/MY/JSHandler.pm

META.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ requires:
1313
mod_perl2: 2
1414
build_requires:
1515
Apache::Test: 1.12
16+
recommends:
17+
JavaScript::Minifier::XS: 0
1618
provides:
1719
Apache2::Filter::Minifier::JavaScript:
1820
file: lib/Apache2/Filter/Minifier/JavaScript.pm

lib/Apache2/Filter/Minifier/JavaScript.pm

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ use Apache2::Log qw(); # $log->*()
1111
use APR::Table qw(); # dir_config->get() and headers_out->unset()
1212
use Apache2::Const -compile => qw(OK DECLINED);
1313
use Time::HiRes qw(gettimeofday tv_interval);
14-
use JavaScript::Minifier qw(minify);
14+
15+
###############################################################################
16+
# Load up the JS minifier modules.
17+
use JavaScript::Minifier;
18+
eval { require JavaScript::Minifier::XS; };
1519

1620
###############################################################################
1721
# Version number.
@@ -52,6 +56,35 @@ sub handler {
5256
return Apache2::Const::DECLINED;
5357
}
5458

59+
# figure out which minifier module/function we're supposed to be using;
60+
# either an explicit minifier function/package, or our list of acceptable
61+
# minifiers
62+
my $minifier;
63+
my @possible = $r->dir_config->get('JsMinifier') || (
64+
'JavaScript::Minifier::XS',
65+
'JavaScript::Minifier',
66+
);
67+
foreach my $maybe (@possible) {
68+
no strict 'refs';
69+
# explicit function name
70+
if (defined &{"$maybe"}) {
71+
$minifier = sub { $maybe->(shift) };
72+
last;
73+
}
74+
# package name; look for "minify()" function
75+
if (defined &{"${maybe}::minify"}) {
76+
my $func = \&{"${maybe}::minify"};
77+
$minifier = ($maybe eq 'JavaScript::Minifier')
78+
? sub { $func->(input=>shift) }
79+
: sub { $func->(shift) };
80+
last;
81+
}
82+
}
83+
unless ($minifier) {
84+
$log->info( "no JavaScript minifier available; declining" );
85+
return Apache2::Const::DECLINED;
86+
}
87+
5588
# gather up entire document
5689
my $ctx = $f->ctx;
5790
while ($f->read(my $buffer, 4096)) {
@@ -67,7 +100,7 @@ sub handler {
67100
# if we've got JS to minify, minify it
68101
if ($ctx) {
69102
my $t_st = [gettimeofday()];
70-
my $min = eval { minify( input=>$ctx ) };
103+
my $min = eval { $minifier->($ctx) };
71104
if ($@) {
72105
# minification failed; log error and send original JS
73106
$log->error( "error minifying: $@" );
@@ -100,12 +133,18 @@ Apache2::Filter::Minifier::JavaScript - JS minifying output filter
100133
101134
# if you need to supplement MIME-Type list
102135
PerlSetVar JsMimeType text/json
136+
137+
# if you want to explicitly specify the minifier to use
138+
#PerlSetVar JsMinifier JavaScript::Minifier::XS
139+
#PerlSetVar JsMinifier JavaScript::Minifier
140+
#PerlSetVar JsMinifier MY::Minifier::function
103141
</LocationMatch>
104142
105143
=head1 DESCRIPTION
106144
107145
C<Apache2::Filter::Minifier::JavaScript> is a Mod_perl2 output filter which
108-
minifies JavaScript using C<JavaScript::Minifier>.
146+
minifies JavaScript using C<JavaScript::Minifier> or
147+
C<JavaScript::Minifier::XS>.
109148
110149
Only JavaScript documents are minified, all others are passed through
111150
unaltered. C<Apache2::Filter::Minifier::JavaScript> comes with a list of
@@ -114,6 +153,15 @@ supplement that list yourself by setting the C<JsMimeType> PerlVar
114153
appropriately (use C<PerlSetVar> for a single new MIME-Type, or C<PerlAddVar>
115154
when you want to add multiple MIME-Types).
116155
156+
Given a choice, using C<JavaScript::Minifier::XS> is preferred over
157+
C<JavaScript::Minifier>, but we'll use whichever one you've got available. If
158+
you want to explicitly specify which minifier you want to use, set the
159+
C<JsMinifier> PerlVar to the name of the package/function that implements the
160+
minifier. Minification functions are expected to accept a single parameter
161+
(the JavaScript to be minified) and to return the minified JavaScript on
162+
completion. If you specify a package name, we look for a C<minify()> function
163+
in that package.
164+
117165
=head2 Caching
118166
119167
Minification does require additional CPU resources, and it is recommended that
@@ -167,6 +215,7 @@ license as Perl itself.
167215
=head1 SEE ALSO
168216
169217
L<JavaScript::Minifier>,
218+
L<JavaScript::Minifier::XS>,
170219
L<Apache2::Filter::Minifier::CSS>,
171220
L<Apache::Clean>.
172221

t/conf/extra.conf.in

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ PerlSwitches -w
22
LogLevel debug
33
DirectoryIndex test.js
44

5+
<Location />
6+
PerlSetVar JsMinifier JavaScript::Minifier
7+
</Location>
8+
59
Alias /raw @DocumentRoot@
610
<Location /raw>
711
PerlOutputFilterHandler Apache2::Filter::Minifier::JavaScript
@@ -61,3 +65,17 @@ Alias /perl-bin @ServerRoot@/perl-bin
6165
PerlOutputFilterHandler Apache2::Filter::Minifier::JavaScript
6266
Options +ExecCGI
6367
</Location>
68+
69+
<Location /explicit/pp>
70+
SetHandler modperl
71+
PerlResponseHandler MY::JSHandler
72+
PerlOutputFilterHandler Apache2::Filter::Minifier::JavaScript
73+
PerlSetVar JsMinifier JavaScript::Minifier
74+
</Location>
75+
76+
<Location /explicit/xs>
77+
SetHandler modperl
78+
PerlResponseHandler MY::JSHandler
79+
PerlOutputFilterHandler Apache2::Filter::Minifier::JavaScript
80+
PerlSetVar JsMinifier JavaScript::Minifier::XS
81+
</Location>

t/htdocs/minified-xs.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
function test(){alert("minify me!");}

t/minifiers.t

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use strict;
2+
use warnings FATAL => 'all';
3+
use Apache::Test;
4+
use Apache::TestRequest;
5+
use Apache::TestUtil qw(t_cmp);
6+
use lib 't';
7+
use MY::slurp;
8+
9+
# Test minification with an explicitly specified minifier.
10+
plan tests => 2, need_lwp;
11+
12+
# JavaScript::Minifier
13+
js_minifier: {
14+
my $body = GET_BODY '/explicit/pp';
15+
my $min = slurp('t/htdocs/minified.txt');
16+
chomp($min);
17+
18+
ok( t_cmp($body, $min) );
19+
}
20+
21+
# JavaScript::Minifier::XS
22+
js_minifier_xs: {
23+
eval { require JavaScript::Minifier::XS };
24+
if ($@) {
25+
skip "JavaScript::Minifier::XS not installed";
26+
}
27+
else {
28+
my $body = GET_BODY '/explicit/xs';
29+
my $min = slurp('t/htdocs/minified-xs.txt');
30+
chomp($min);
31+
32+
ok( t_cmp($body, $min) );
33+
}
34+
}

0 commit comments

Comments
 (0)