Mojolicious::Plugin::AssetPack - Compress and convert css, less, sass and javascript files
0.0602
In your application:
use Mojolicious::Lite;
plugin 'AssetPack';
# add a preprocessor
app->asset->preprocessors->add(js => sub {
my($assetpack, $text, $file) = @_;
$$text = "// yikes!\n" if 5 < rand 10;
});
# define assets: $moniker => @real_assets
app->asset('app.js' => '/js/foo.js', '/js/bar.js');
app->asset('app.css' => '/css/foo.less', '/css/bar.scss', '/css/main.css');
app->start;
In your template:
%= asset 'app.js'
%= asset 'app.css'
Or if you need to add the tags manually:
% for my $asset (asset->get('app.js')) {
%= javascript $asset
% }
See also "register".
This plugin will compress scss, less, css and javascript with the help of external applications on startup. The result will be one file with all the sources combined. This file is stored in "Packed directory".
The files in the packed directory will have a checksum added to the filename which will ensure broken browsers request a new version once the file is changed. Example:
<script src="/packed/app-ed6d968e39843a556dbe6dad8981e3e0.js">
This is done using "process".
This plugin will expand the input files to multiple script or link tags which makes debugging and development easier.
This is done using "expand".
TIP! Make morbo watch your less/sass files as well:
$ morbo -w lib -w templates -w public/sass
The output directory where all the compressed files are stored will be "public/packed", relative to the application home:
$app->home->rel_dir('public/packed');
If you use several instances of servers with same code and want recive static files from separate domain name (e.g. http://static.exsample.com) use url_prefix attribute. Then links to scripts and stylesheets will look like:
<script src="http://static.exsample.com/packed/app-ed6d968e39843a556dbe6dad8981e3e0.js">
<link href="http://static.exsample.com/packed/app-8ff3e5f1c5cab970a3f597572f18d367.css" rel="stylesheet" />
This library tries to find default preprocessors for less, scss, js and css.
NOTE! The preprocessors require optional dependencies to function properly. Check out "detect" in Mojolicious::Plugin::AssetPack::Preprocessors for more details.
Set this to true if the assets should be minified.
Holds a Mojolicious::Plugin::AssetPack::Preprocessors object.
Deprecated.
$self->add($moniker => @rel_files);
Used to define new assets aliases. This method is called when the asset()
helper is called on the app.
$bytestream = $self->expand($c, $moniker);
This method will return one tag for each asset defined by the "$moniker".
Will also run "less" or "sass" on the files to convert them to css, which the browser understand.
The returning bytestream will contain style or script tags.
@files = $self->get($moniker);
Returns a list of files which the moniker point to. The list will only contain one file if the $moniker
is minified.
$self->process($moniker => @files);
This method use "process" in Mojolicious::Plugin::AssetPack::Preprocessors to convert and/or minify the sources pointed at by $moniker
.
The result file will be stored in "Packed directory".
plugin 'AssetPack', {
cleanup => $bool, # default is true
minify => $bool, # compress assets
no_autodetect => $bool, # disable preprocessor autodetection
url_prefix => $string, # prefix for links to another domain
};
Will register the compress
helper. All arguments are optional.
"cleanup" will remove any old processed files. You want to disable this if you have other web sites that need to access an old version of the minified files.
"minify" will default to true if "mode" in Mojolicious is "production".
Jan Henning Thorsen - [email protected]