Skip to content

Instantly share code, notes, and snippets.

@mlampret
Created January 11, 2015 13:04
Show Gist options
  • Save mlampret/b8de70ad27fe850f8298 to your computer and use it in GitHub Desktop.
Save mlampret/b8de70ad27fe850f8298 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use Getopt::Std;
sub main {
my $rh_args={};
getopts('o:i:',$rh_args);
unless (-f $rh_args->{i}) {
die "Input file $rh_args->{i} not found";
}
my $css=`cat $rh_args->{i}`;
my $length_before=length($css);
$css=~s!\r?\n! !isg;
$css=~s!\s+! !isg;
$css=~s!\s{\s*!{!isg;
$css=~s!}\s+!}!isg;
$css=~s!:\s+!:!isg;
$css=~s!;\s+!;!isg;
$css=~s!/\*.+?\*/!!isg;
my $length_after=length($css);
if ($rh_args->{o}) {
print "Writing compacted css to $rh_args->{o}\n";
open(FILE,">$rh_args->{o}") or die "can't open file $rh_args->{o}";
print FILE $css;
close(FILE);
} else {
print "No output file specified.. comparison only\n";
}
print "Size before: $length_before\n";
print "Size after: $length_after\n";
}
main;
@mlampret
Copy link
Author

Usage: ./css_compressor -i input.css -o output.css

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment