Plack::Middleware::Assets - Concatenate and minify JavaScript and CSS files
version 1.0.0
# in app.psgi use Plack::Builder; builder { enable Assets => ( files => [<static/js/*.js>] ); enable Assets => ( files => [<static/css/*.css>], minify => 0 ); $app; }; # or customize your assets as desired: builder { # concatenate sass files and transform them into css enable Assets => ( files => [<static/sass/*.sass>], type => 'css', filter => sub { Text::Sass->new->sass2css(shift) }, minify => 0 ); # pass a coderef for a custom minifier enable Assets => ( files => [<static/any/*.txt>], filter => sub {uc}, minify => sub { s/ +/\t/g; $_ } ); # concatenate any arbitrary content type enable Assets => ( files => [<static/less/*.less>], type => 'less' ); $app; }; # since this module ships only with the types css and js, # you have to implement the less type yourself: package Plack::Middleware::Assets::Type::less; use base 'Plack::Middleware::Assets::Type::css'; use CSS::Minifier::XS qw(minify); use CSS::LESSp (); sub filter { CSS::LESSp->parse(@_) } # $env->{'psgix.assets'}->[0] points at the first asset.
Plack::Middleware::Assets concatenates JavaScript and CSS files and minifies them.
A md5 digest is generated and used as the unique url to the asset. For instance, if the first psgix.assets is static/js/*.js, then the unique md5 url can be used in a single HTML script element for all js files.
md5
psgix.assets
static/js/*.js
script
The Last-Modified header is set to the mtime of the most recently changed file.
Last-Modified
mtime
The Expires header is set to one month in advance. Set "expires" to change the time of expiry.
Expires
The concatented and minified content is cached in memory.
$ plackup app.psgi $ starman -E development app.psgi
In development mode the minification is disabled and the concatenated content is regenerated if there were any changes to the files.
By default files are prepended with /* filename */\n before being concatenated.
/* filename */\n
Set this to false to disable these comments.
If set to a string containing a %s it will be passed to sprintf with the file name.
%s
sprintf
separator => "# %s\n"
Files to concatenate.
A coderef that can process/transform the content.
The current content will be passed in as $_[0] and also available via $_ for convenience.
$_[0]
$_
This will be called before it is minified (if minify is enabled).
minify
Value to indicate whether to minify or not. Defaults to 1. This can also be a coderef which works the same as "filter".
1
Type of the asset. Predefined types include css and js. Additional types can be implemented by creating a new class in the Plack::Middleware::Assets::Type namespace. See the "SYNOPSIS" for an example.
css
js
Plack::Middleware::Assets::Type
An attempt to guess the correct value is made from the file extensions but this can be set explicitly if you are using non-standard file extensions.
Time in seconds from now (i.e. time) until the resource expires.
time
File extension that is appended to the asset's URI.
Allow to concatenate documents from URLs, such that you can have a Plack::Middleware::File::Sass that converts SASS files to CSS and concatenate those with other CSS files. Also concatenate content from CDNs that host common JavaScript libraries.
Catalyst::Plugin::Assets
Inspired by Plack::Middleware::JSConcat
Moritz Onken
This software is Copyright (c) 2013 by Moritz Onken.
This is free software, licensed under:
The (three-clause) BSD License
To install Plack::Middleware::Assets, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Plack::Middleware::Assets
CPAN shell
perl -MCPAN -e shell install Plack::Middleware::Assets
For more information on module installation, please visit the detailed CPAN module installation guide.