use strict; use warnings; package Jifty::Plugin::CompressedCSSandJS; use base 'Jifty::Plugin'; use IPC::Run3 'run3'; use IO::Handle (); =head1 NAME Jifty::Plugin::CompressedCSSandJS - Compression of CSS and javascript files =head1 SYNOPSIS # In your jifty config.yml under the framework section: Plugins: - CompressedCSSandJS: js: 1 css: 1 jsmin: /path/to/jsmin cdn: 'http://yourcdn.for.static.prefix/' gzip: 1 skipped_js: - complex.js =head1 DESCRIPTION This plugin provides auto-compilation and on-wire compression of your application's CSS and Javascript. It is enabled by default, unless your C is greater or equal than 2. It also supports js minifier, you will need to specify the full path. The jsmin can be obtained from L. Note that you will need to use C 2 to be able to configure jsmin feature. The gzip configuration directive, which defaults to enabled, instructs Jifty to transparently gzip css and js files as they're served if the client indicates it supports that feature. skipped_js is a list of js that you don't want to compress for some reason. =cut __PACKAGE__->mk_accessors(qw(css js jsmin cdn gzip_enabled skipped_js)); =head2 init Initializes the compression object. Takes a paramhash containing keys 'css' and 'js' which can be used to disable compression on files of that type. =cut sub init { my $self = shift; return if $self->_pre_init; my %opt = @_; $self->css( $opt{css} ); $self->gzip_enabled( exists $opt{gzip} ? $opt{gzip} : 1); $self->js( $opt{js} ); $self->jsmin( $opt{jsmin} ); $self->cdn( $opt{cdn} || ''); Jifty::Web->add_trigger( name => 'include_javascript', callback => sub { $self->_include_javascript(@_) }, abortable => 1, ) if $self->js_enabled; Jifty::Web->add_trigger( name => 'include_css', callback => sub { $self->_include_css(@_) }, abortable => 1, ) if $self->css_enabled; } =head2 js_enabled Returns whether JS compression is enabled (which it is by default) =cut sub js_enabled { my $self = shift; defined $self->js ? $self->js : 1; } =head2 css_enabled Returns whether CSS compression is enabled (which it is by default) =cut sub css_enabled { my $self = shift; defined $self->css ? $self->css : 1; } =head2 gzip_enabled Returns whether gzipping is enabled (which it is by default) =cut sub _include_javascript { my $self = shift; $self->_generate_javascript; Jifty->web->out( qq[] ); my $skipped_js = $self->skipped_js; if ( $self->skipped_js ) { for my $file ( @{ $self->skipped_js } ) { Jifty->web->out( qq{