Moritz Onken > Plack-Middleware-Assets-1.0.0 > Plack::Middleware::Assets

Download:
Plack-Middleware-Assets-1.0.0.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: v1.0.0   Source  

NAME ^

Plack::Middleware::Assets - Concatenate and minify JavaScript and CSS files

VERSION ^

version 1.0.0

SYNOPSIS ^

  # 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.

DESCRIPTION ^

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.

The Last-Modified header is set to the mtime of the most recently changed file.

The Expires header is set to one month in advance. Set "expires" to change the time of expiry.

The concatented and minified content is cached in memory.

DEVELOPMENT MODE ^

 $ 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.

CONFIGURATIONS ^

separator

By default files are prepended with /* filename */\n before being concatenated.

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.

    separator => "# %s\n"
files

Files to concatenate.

filter

A coderef that can process/transform the content.

The current content will be passed in as $_[0] and also available via $_ for convenience.

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".

type

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.

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.

expires

Time in seconds from now (i.e. time) until the resource expires.

extension

File extension that is appended to the asset's URI.

TODO ^

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.

SEE ALSO ^

Catalyst::Plugin::Assets

Inspired by Plack::Middleware::JSConcat

AUTHOR ^

Moritz Onken

COPYRIGHT AND LICENSE ^

This software is Copyright (c) 2013 by Moritz Onken.

This is free software, licensed under:

  The (three-clause) BSD License
syntax highlighting: