Mojolicious::Plugin::AssetPack::Manual::Cookbook - Code snippets
This example contains a preprocessors to expand the url() definitions in a css file downloaded from a CDN:
url()
$app->asset->preprocessors->add( css => sub { my ($assetpack, $text, $file) = @_; if ($file =~ /font.*awesome/) { $$text =~ s!url\(["']../([^']+)["']\)!url(https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/$1)!g; } }, ); $app->asset("app.css" => "https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css");
This example contains a preprocessors which fetch referred assets and serves them from the local webserver:
use File::Basename "basename"; my $cdn_base_url = "http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.1.0"; $t->app->asset->preprocessors->add( css => sub { my ($assetpack, $text, $file) = @_; my $fetch = sub { my $url = "$cdn_base_url/$1"; my $path = $assetpack->fetch($url); return sprintf "url('%s')", basename $path; }; $$text =~ s!url\('..([^']+)'\)!{ $fetch->() }!ge if $file =~ /awesome/; } ); $t->app->asset("app.css" => "$cdn_base_url/css/font-awesome.css");
$app->asset("app.css" => "https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css");
See also "Rewrite URLs" and http://fortaweso.me/font-awesome/.
$app->asset("app.css" => "http://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic");
See also http://www.google.com/fonts for a selection of fonts.
$app->asset("app.js" => "http://code.jquery.com/jquery-1.11.2.min.js");
See http://api.jquery.com/ for jQuery documentation.
Have a look at the unit tests.
Jan Henning Thorsen - jhthorsen@cpan.org
jhthorsen@cpan.org
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.