| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Jifty::Plugin::CompressedCSSandJS; | ||
| 5 | use base 'Jifty::Plugin'; | ||
| 6 | |||
| 7 | use IPC::Run3 'run3'; | ||
| 8 | use IO::Handle (); | ||
| 9 | use Plack::Util; | ||
| 10 | use HTTP::Message::PSGI; | ||
| 11 | use HTTP::Request; | ||
| 12 | use Plack::Response; | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | Jifty::Plugin::CompressedCSSandJS - Compression of CSS and javascript files | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | # In your jifty config.yml under the framework section: | ||
| 21 | |||
| 22 | Plugins: | ||
| 23 | - CompressedCSSandJS: | ||
| 24 | js: 1 | ||
| 25 | css: 1 | ||
| 26 | jsmin: /path/to/jsmin | ||
| 27 | skipped_js: | ||
| 28 | - complex.js | ||
| 29 | |||
| 30 | |||
| 31 | =head1 DESCRIPTION | ||
| 32 | |||
| 33 | This plugin provides auto-compilation and on-wire compression of your | ||
| 34 | application's CSS and Javascript. It is enabled by default, unless | ||
| 35 | your C<ConfigFileVersion> is greater or equal than 2. | ||
| 36 | |||
| 37 | It also supports js minifier, you will need to specify the full path. | ||
| 38 | The jsmin can be obtained from | ||
| 39 | L<http://www.crockford.com/javascript/jsmin.html>. | ||
| 40 | |||
| 41 | Note that you will need to use C<ConfigFileVersion> 2 to be able to | ||
| 42 | configure jsmin feature. | ||
| 43 | |||
| 44 | skipped_js is a list of js that you don't want to compress for some reason. | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | __PACKAGE__->mk_accessors(qw(css js jsmin skipped_js external_publish)); | ||
| 49 | |||
| 50 | =head2 init | ||
| 51 | |||
| 52 | Initializes the compression object. Takes a paramhash containing keys | ||
| 53 | 'css' and 'js' which can be used to disable compression on files of | ||
| 54 | that type. | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | sub init { | ||
| 59 | my $self = shift; | ||
| 60 | return if $self->_pre_init; | ||
| 61 | |||
| 62 | my %opt = @_; | ||
| 63 | $self->css( $opt{css} ); | ||
| 64 | $self->js( $opt{js} ); | ||
| 65 | $self->jsmin( $opt{jsmin} ); | ||
| 66 | $self->external_publish( $opt{external_publish} ); | ||
| 67 | if ($self->external_publish and not Jifty::CAS->backend("ccjs")->durable) { | ||
| 68 | $self->log->warn("External publishing does not work with non-durable CAS stores; disabling"); | ||
| 69 | $self->external_publish(0); | ||
| 70 | } | ||
| 71 | |||
| 72 | if ( $self->js_enabled ) { | ||
| 73 | Jifty::Web->add_trigger( | ||
| 74 | name => 'include_javascript', | ||
| 75 | callback => sub { $self->_include_javascript(@_) }, | ||
| 76 | abortable => 1, | ||
| 77 | ); | ||
| 78 | } | ||
| 79 | |||
| 80 | if ( $self->css_enabled ) { | ||
| 81 | Jifty::Web->add_trigger( | ||
| 82 | name => 'include_css', | ||
| 83 | callback => sub { $self->_include_css(@_) }, | ||
| 84 | abortable => 1, | ||
| 85 | ); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | =head2 js_enabled | ||
| 90 | |||
| 91 | Returns whether JS compression is enabled (which it is by default) | ||
| 92 | |||
| 93 | =cut | ||
| 94 | |||
| 95 | sub js_enabled { | ||
| 96 | my $self = shift; | ||
| 97 | defined $self->js ? $self->js : 1; | ||
| 98 | } | ||
| 99 | |||
| 100 | =head2 css_enabled | ||
| 101 | |||
| 102 | Returns whether CSS compression is enabled (which it is by default) | ||
| 103 | |||
| 104 | =cut | ||
| 105 | |||
| 106 | sub css_enabled { | ||
| 107 | my $self = shift; | ||
| 108 | defined $self->css ? $self->css : 1; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub _include_javascript { | ||
| 112 | my $self = shift; | ||
| 113 | |||
| 114 | $self->generate_javascript unless $self->external_publish; | ||
| 115 | Jifty->web->out( | ||
| 116 | qq[<script type="text/javascript" src="] | ||
| 117 | . Jifty::CAS->uri("ccjs","js-all") | ||
| 118 | . qq[.js"></script>] ); | ||
| 119 | |||
| 120 | my $skipped_js = $self->skipped_js; | ||
| 121 | if ( $self->skipped_js ) { | ||
| 122 | for my $file ( @{ $self->skipped_js } ) { | ||
| 123 | my $url = Jifty->web->static("js/$file"); | ||
| 124 | Jifty->web->out( | ||
| 125 | qq{<script type="text/javascript" src="$url" /> }); | ||
| 126 | } | ||
| 127 | } | ||
| 128 | return 0; | ||
| 129 | } | ||
| 130 | |||
| 131 | sub _include_css { | ||
| 132 | my $self = shift; | ||
| 133 | $self->generate_css unless $self->external_publish; | ||
| 134 | Jifty->web->out( | ||
| 135 | qq{<link rel="stylesheet" type="text/css" href="} | ||
| 136 | . Jifty::CAS->uri('ccjs', 'css-all').'.css" />'); | ||
| 137 | return 0; | ||
| 138 | } | ||
| 139 | |||
| 140 | =head3 generate_css | ||
| 141 | |||
| 142 | |||
| 143 | Checks if the compressed CSS is generated, and if it isn't, generates | ||
| 144 | and caches it. (In devel mode, it always regenerates it) | ||
| 145 | |||
| 146 | =cut | ||
| 147 | |||
| 148 | sub generate_css { | ||
| 149 | my $self = shift; | ||
| 150 | |||
| 151 | return if !$self->external_publish | ||
| 152 | && Jifty::CAS->key('ccjs', 'css-all') | ||
| 153 | && !Jifty->config->framework('DevelMode'); | ||
| 154 | |||
| 155 | $self->log->debug("Generating CSS..."); | ||
| 156 | |||
| 157 | my @roots = map { Jifty::Util->absolute_path( $_ ) } | ||
| 158 | Jifty->handler->view('Jifty::View::Static::Handler')->roots; | ||
| 159 | |||
| 160 | my $css = CSS::Squish->new( roots => \@roots )->concatenate( | ||
| 161 | map { File::Spec->catfile('css', $_ ) } @{ Jifty->web->css_files } | ||
| 162 | ); | ||
| 163 | |||
| 164 | Jifty::CAS->publish( 'ccjs', 'css-all', $css, | ||
| 165 | { content_type => 'text/css', time => time() } ); | ||
| 166 | } | ||
| 167 | |||
| 168 | |||
| 169 | |||
| 170 | =head3 generate_javascript | ||
| 171 | |||
| 172 | Checks if the compressed JS is generated, and if it isn't, generates | ||
| 173 | and caches it. | ||
| 174 | |||
| 175 | =cut | ||
| 176 | |||
| 177 | sub generate_javascript { | ||
| 178 | my $self = shift; | ||
| 179 | |||
| 180 | return if !$self->external_publish | ||
| 181 | && Jifty::CAS->key('ccjs', 'js-all') | ||
| 182 | && !Jifty->config->framework('DevelMode'); | ||
| 183 | |||
| 184 | my $js = $self->_generate_javascript_nocache; | ||
| 185 | |||
| 186 | Jifty::CAS->publish( 'ccjs', 'js-all', $js, | ||
| 187 | { content_type => 'application/x-javascript', time => time() } ); | ||
| 188 | } | ||
| 189 | |||
| 190 | =head3 _generate_javascript_nocache | ||
| 191 | |||
| 192 | Generates compressed javascript, ignoring the cache completely. | ||
| 193 | |||
| 194 | =cut | ||
| 195 | |||
| 196 | sub _generate_javascript_nocache { | ||
| 197 | my $self = shift; | ||
| 198 | $self->log->debug("Generating JS..."); | ||
| 199 | |||
| 200 | # for the file cascading logic | ||
| 201 | my $js = ""; | ||
| 202 | |||
| 203 | my $static_app = Jifty->handler->psgi_app_static; | ||
| 204 | |||
| 205 | for my $file ( @{ Jifty::Web->javascript_libs } ) { | ||
| 206 | next if $self->_js_is_skipped($file); | ||
| 207 | |||
| 208 | my $res = Plack::Util::run_app | ||
| 209 | ( $static_app, | ||
| 210 | HTTP::Request->new(GET => "/js/$file")->to_psgi ); | ||
| 211 | if ($res->[0] == 200) { | ||
| 212 | Plack::Util::foreach($res->[2], sub { $js .= $_[0] } ); | ||
| 213 | } | ||
| 214 | else { | ||
| 215 | $self->log->error("Unable to include '$file': $res->[0]"); | ||
| 216 | $js .= "\n/* Unable to include '$file': $res->[0] */\n"; | ||
| 217 | } | ||
| 218 | } | ||
| 219 | |||
| 220 | if ($self->jsmin) { | ||
| 221 | eval { $self->minify_js(\$js) }; | ||
| 222 | $self->log->error("Unable to run jsmin: $@") if $@; | ||
| 223 | } | ||
| 224 | |||
| 225 | return $js; | ||
| 226 | } | ||
| 227 | |||
| 228 | =head2 minify_js \$js | ||
| 229 | |||
| 230 | Runs the given JS through jsmin | ||
| 231 | |||
| 232 | =cut | ||
| 233 | |||
| 234 | sub minify_js { | ||
| 235 | my $self = shift; | ||
| 236 | my $input = shift; | ||
| 237 | my ($output, $err); | ||
| 238 | |||
| 239 | $self->log->debug("Minifying JS..."); | ||
| 240 | |||
| 241 | # We need to reopen stdout temporarily, because in FCGI | ||
| 242 | # environment, stdout is tied to FCGI::Stream, and the child | ||
| 243 | # of the run3 wouldn't be able to reopen STDOUT properly. | ||
| 244 | my $stdout = IO::Handle->new; | ||
| 245 | $stdout->fdopen( 1, 'w' ); | ||
| 246 | local *STDOUT = $stdout; | ||
| 247 | |||
| 248 | my $stderr = IO::Handle->new; | ||
| 249 | $stderr->fdopen( 2, 'w' ); | ||
| 250 | local *STDERR = $stderr; | ||
| 251 | |||
| 252 | local $SIG{'CHLD'} = 'DEFAULT'; | ||
| 253 | run3 [$self->jsmin], $input, \$output, \$err; | ||
| 254 | |||
| 255 | my $ret = $? >> 8; | ||
| 256 | $self->log->warn("Javascript minify @{[$self->jsmin]} returned $ret:\n$err") if $ret; | ||
| 257 | |||
| 258 | $$input = $output; | ||
| 259 | } | ||
| 260 | |||
| 261 | sub _js_is_skipped { | ||
| 262 | my $self = shift; | ||
| 263 | my $file = shift; | ||
| 264 | my $skipped_js = $self->skipped_js; | ||
| 265 | return unless $self->skipped_js; | ||
| 266 | return grep { $file eq $_ } @{ $self->skipped_js }; | ||
| 267 | } | ||
| 268 | |||
| 269 | =head2 wrap | ||
| 270 | |||
| 271 | If a JS or CSS file references another by a relative path, it will most | ||
| 272 | likely end up being mis-requested under the CAS directory. Catch those | ||
| 273 | requests, warn about them, and re-dispatch them internally to what is | ||
| 274 | likely the right place. | ||
| 275 | |||
| 276 | =cut | ||
| 277 | |||
| 278 | sub wrap { | ||
| 279 | my ($self, $app) = @_; | ||
| 280 | return sub { | ||
| 281 | my $env = shift; | ||
| 282 | if (my ($arg, $type) = $env->{PATH_INFO} =~ m{/__jifty/cas/ccjs/(.*?)\.(js|css)$}) { | ||
| 283 | if ($arg !~ /^[0-9a-fA-F]{32}$/ and not Jifty::CAS->key("ccjs",$arg)) { | ||
| 284 | $self->log->warn("Request for relative path $type file: $arg.$type"); | ||
| 285 | $env->{PATH_INFO} = "/static/$type/$arg.$type"; | ||
| 286 | } | ||
| 287 | } | ||
| 288 | $app->($env); | ||
| 289 | }; | ||
| 290 | } | ||
| 291 | |||
| 292 | 1; |