Plack-Middleware-Assets-RailsLike

 view release on metacpan or  search on metacpan

lib/Plack/Middleware/Assets/RailsLike.pm  view on Meta::CPAN

    my $self = shift;
    $self->_response( 404, 'Not Found' );
}

sub _500 {
    my $self = shift;
    $self->_response( 500, 'Internal Server Error' );
}

sub _response {
    my $self = shift;
    my ( $code, $content ) = @_;
    return [
        $code,
        [   'Content-Type'   => 'text/plain',
            'Content-Length' => length($content)
        ],
        [$content]
    ];
}

1;
__END__

=encoding utf-8

=head1 NAME

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

=head1 SYNOPSIS

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

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

=head1 WARNING

B<This module is under development and considered BETA quality.>

=head1 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
C<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 C<$root>/assets). This middleware return
HTTP response with C<Cache-Control>, C<Expires> and C<Etag>. C<Cache-Control>
and C<Expires> are computed from the C<expires> option. C<Etag> is computed
from bundled content.

=head1 CONFIGURATIONS

=over 4

=item root

Document root to find manifest files to serve.

Default value is current directory('.').

=item path

The URL pattern (regular expression) for matching.

Default value is C<qr{^/assets}>.

=item search_path

Paths to find javascript and css files.

Default value is C<[qw($root/assets)]>.

=item minify

Minify javascript and css files if true.

Default value is C<1>.

=item cache

Store concatenated data in memory by default using L<Cache::MemoryCache>. The
C<cache> option must be a object implemented C<get> and C<set> methods. For
example, L<Cache::Memcached::Fast>. If C<$ENV{PLACK_ENV} eq "development"> and
you didn't pass a cache object, cache is disabled.

Default is a L<Cache::MemoryCache> Object.

    Cache::MemoryCache->new({
        namespace           => "Plack::Middleware::Assets::RailsLike",
        default_expires_in  => $expires
        auto_purge_interval => '1 day',
        auto_purge_on_set   => 1,
        auto_purge_on_get   => 1
    })



( run in 2.922 seconds using v1.01-cache-2.11-cpan-524268b4103 )