Mojolicious::Plugin::AssetPack - Compress and convert css, less, sass, javascript and coffeescript files
0.68
use Mojolicious::Lite; # load plugin plugin "AssetPack"; # Define assets: $moniker => @real_assets app->asset('app.js' => '/js/foo.js', '/js/bar.js', '/js/baz.coffee'); # Add custom response headers for assets app->asset->headers({"Cache-Control" => "max-age=31536000"}); # Remove old assets app->asset->purge; # Start the application app->start;
See also Mojolicious::Plugin::AssetPack::Manual::Assets for more details on how to define assets.
%= asset 'app.js' %= asset 'app.css'
See also Mojolicious::Plugin::AssetPack::Manual::Include for more details on how to include assets.
Mojolicious::Plugin::AssetPack is a Mojolicious plugin which can be used to cram multiple assets of the same type into one file. This means that if you have a lot of CSS files (.css, .less, .sass, ...) as input, the AssetPack can make one big CSS file as output. This is good, since it will often speed up the rendering of your page. The output file can even be minified, meaning you can save bandwidth and browser parsing time.
The core preprocessors that are bundled with this module can handle CSS and JavaScript files, written in many languages.
The documentation is split up in different manuals, for more in-depth information:
See Mojolicious::Plugin::AssetPack::Manual::Assets for how to define assets in your application.
See Mojolicious::Plugin::AssetPack::Manual::Include for how to include the assets in the template.
See Mojolicious::Plugin::AssetPack::Manual::Modes for how AssetPack behaves in different modes.
See Mojolicious::Plugin::AssetPack::Manual::CustomDomain for how to serve your assets from a custom host.
See Mojolicious::Plugin::AssetPack::Preprocessors for details on the different (official) preprocessors.
Set this to get extra debug information to STDERR from AssetPack internals.
If true, convert the assets each time they're expanded, instead of once at application start (useful for development).
This plugin defined the helper asset(). This helper can be called in different ways:
asset()
$self = $c->asset;
This will return the plugin instance, that you can call methods on.
$c->asset($moniker => @real_files);
See "add".
$bytestream = $c->asset($moniker, \%args, @attr);
Used to include an asset in a template.
$app->plugin("AssetPack" => {base_url => "/packed/"}); $str = $self->base_url;
This attribute can be used to control where to serve static assets from.
Defaults value is "/packed/".
See Mojolicious::Plugin::AssetPack::Manual::CustomDomain for more details.
NOTE! You need to have a trailing "/" at the end of the string.
$app->plugin("AssetPack" => {minify => $bool}); $bool = $self->minify;
Set this to true if the assets should be minified.
Default is false in "development" mode and true otherwise.
See also Mojolicious::Plugin::AssetPack::Manual::Modes.
$obj = $self->preprocessors;
Holds a Mojolicious::Plugin::AssetPack::Preprocessors object.
$self->add($moniker => @real_files);
Used to define assets.
See Mojolicious::Plugin::AssetPack::Manual::Assets for mode details.
$path = $self->fetch($url);
This method can be used to fetch an asset and store the content to a local file. The download will be skipped if the file already exists. The return value is the absolute path to the downloaded file.
@files = $self->get($moniker);
Returns a list of files which the moniker point to. The list will only contain one file if "minify" is true.
See "Full control" in Mojolicious::Plugin::AssetPack::Manual::Include for mode details.
$app->plugin("AssetPack" => {headers => {"Cache-Control" => "max-age=31536000"}}); $app->asset->headers({"Cache-Control" => "max-age=31536000"});
Calling this method will add a after_static hook which will set additional response headers when an asset is served.
$app->plugin("AssetPack" => {out_dir => $str}); $str = $self->out_dir;
Returns the path to the directory where generated packed files are located.
Changing this from the default will probably lead to inconsistency. Please report back if you are using this feature with success.
$self = $self->purge({always => $bool});
Used to purge old packed files. This is useful if you want to avoid filling up "out_dir" with many versions of the packed file.
always default to true if in "development" mode and false otherwise.
always
This method is EXPERIMENTAL.
plugin AssetPack => { base_url => $str, # default to "/packed" headers => {"Cache-Control" => "max-age=31536000"}, minify => $bool, # compress assets proxy => "detect", # autodetect proxy settings out_dir => "/path/to/some/directory", source_paths => [...], };
Will register the asset helper. All arguments are optional.
asset
$self = $self->source_paths($array_ref); $array_ref = $self->source_paths;
This method returns a list of paths to source files. The default is to return "paths" in Mojolicious::Static from the current application object, but you can specify your own paths.
See also "Custom source directories" in Mojolicious::Plugin::AssetPack::Manual::Assets for more information.
Mojolicious::Plugin::AssetPack->test_app("MyApp");
This method will create two Mojo::Test instances of "MyApp" and create assets with "minify" set to 0 and 1. "SHIPPING" in Mojolicious::Plugin::AssetPack::Manual::Cookbook for more details.
Copyright (C) 2014, Jan Henning Thorsen.
This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.
Jan Henning Thorsen - jhthorsen@cpan.org
jhthorsen@cpan.org
Alexander Rymasheusky
Per Edin - info@peredin.com
info@peredin.com
Viktor Turskyi
To install Mojolicious::Plugin::AssetPack, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojolicious::Plugin::AssetPack
CPAN shell
perl -MCPAN -e shell install Mojolicious::Plugin::AssetPack
For more information on module installation, please visit the detailed CPAN module installation guide.