Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@andreas-marschke
Last active December 17, 2015 16:49
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreas-marschke/5642075 to your computer and use it in GitHub Desktop.
Save andreas-marschke/5642075 to your computer and use it in GitHub Desktop.
Mojolicious Command to minify all CSS/JS Scripts in a directory and save minified versions as .min.{js|css}
package Mojolicious::Command::minify;
use Mojo::Base 'Mojolicious::Commands';
use File::Find;
use File::Slurp;
has description => "Minify scripts in public/ directory.\n";
has hint => <<"EOF";
Minifies all Javascript and CSS files with the .js instead of the .min.js extension.
EOF
has message => <<"EOF";
usage: $0 minify DIRECTORY
EOF
has namespace => sub {[qw(Mojolicious::Command::minify)]};
has usage => "usage: $0 minify DIRECTORY";
sub help { shift->run(@_) }
sub run {
my $self = shift;
my $dir = shift;
find( {follow => 1,
wanted => sub{
/(\.min\.css$|\.min\.js$)/ && return 1;
if (/\.js$/) {
use JavaScript::Minifier qw(minify);
my $outfile = $File::Find::fullname;
$outfile =~ s/\.js$/\.min\.js/;
print "Saving minified version to $outfile... ";
my $input = read_file($File::Find::fullname);
write_file($outfile,minify(input => $input));
print "DONE!\n"
} elsif (/\.css/) {
use CSS::Minifier qw(minify);
my $outfile = $File::Find::fullname;
$outfile =~ s/\.css$/\.min\.css/;
print "Saving minified version to $outfile... ";
my $input = read_file($File::Find::fullname);
write_file($outfile,minify(input => $input));
print "DONE!\n"
}
}},$dir);
print "Finished minifying files.\n";
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment