Last active
December 17, 2015 16:49
-
-
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}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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