NAME

Plack::Middleware::Assets::RailsLike - Bundle and minify JavaScript and CSS files

SYNOPSIS

use strict;
use warnings;
use MyApp;
use Plack::Builder;

my $app = MyApp->new->to_app;
builder {
    enable 'Assets::RailsLike', root => './htdocs';
    $app;
};

WARNING

This module is under development and considered BETA quality.

DESCRIPTION

Plack::Middleware::Assets::RailsLike is a middleware to bundle and minify JavaScript and CSS (included Sass and LESS) files like Ruby on Rails Asset Pipeline.

At first, you create a manifest file. The Manifest file is a list of JavaScript and CSS files you want to bundle. You can also use Sass and LESS as css files. The Manifest syntax is same as Rails Asset Pipeline, but only support require command.

> vim ./htdocs/assets/main-page.js
> cat ./htdocs/assets/main-page.js
//= require jquery
//= require myapp

Next, write URLs of manifest file to your html. This middleware supports versioning. So you can add version string in between its file basename and suffix.

<- $basename-$version.$suffix ->
<script type="text/javascript" src="/assets/main-page-v2013060701.js">

If manifest files were requested, bundle files in manifest file and serve it or serve bundled data from cache. In this case, find jquery.js and myapp.js from search path (default search path is $root/assets). This middleware return HTTP response with Cache-Control, Expires and Etag. Cache-Control and Expires are computed from the expires option. Etag is computed from bundled content.

CONFIGURATIONS

PRECOMPILE

This distribution includes assets-railslike-precompiler.pl. This script can pre-compile manifest files. See perldoc assets-railslike-precompiler.pl for more details.

I strongly recommend using pre-compiled files with Plack::Middleware::Static in the production environment.

MOTIVATION

I want a middleware has futures below.

1. Concat JavaScript and CSS
2. Minify contents
3. Cache a compiled data
4. Version string in filename
5. Support Sass and LESS files
6. Less configuration
7. Pre-compile

Plack::Middleware::StaticShared is good choice for me. But it needs to write definitions for each resource types. And its URL format is a bit strange.

SEE ALSO

Plack::Middleware::StaticShared

Plack::Middleware::Assets

Plack::App::MCCS

Plack::Middleware::Static::Combine

Plack::Middleware::Static::Minifier

Plack::Middleware::Compile

Plack::Middleware::JSConcat

Catalyst::Plugin::Assets

DEPENDENCIES

Plack

Cache::Cache

File::Slurp

JavaScript::Minifier::XS

CSS::Minifier::XS

Digest::SHA1

HTTP::Date

Text::Sass

CSS::LESSp

LICENSE

Copyright (C) 2013 Yoshihiro Sasaki

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHOR

Yoshihiro Sasaki ysasaki@cpan.org