43 lines
1.1 KiB
Plaintext
43 lines
1.1 KiB
Plaintext
|
#!/usr/bin/env perl
|
||
|
# Minifies CSS files, that is removes all whitespace characters.
|
||
|
# Usage: `ls /path/to/css/files | /path/to/this/script`
|
||
|
|
||
|
use v5.14;
|
||
|
use CSS::Packer;
|
||
|
use warnings;
|
||
|
use strict;
|
||
|
|
||
|
# It leaves the copyright in place if it encounters the word 'copyright', this
|
||
|
# adds 'license' word as a trigger.
|
||
|
#
|
||
|
# I have already sent a pull request for that: https://github.com/leejo/css-packer-perl/pull/12.
|
||
|
$CSS::Packer::COPYRIGHT_COMMENT = '\/\*((?>[^*]|\*[^/])*(?:license|copyright)(?>[^*]|\*[^/])*)\*\/';
|
||
|
|
||
|
# There is no matching cleanup function to call afterwards.
|
||
|
my $packer = CSS::Packer->init();
|
||
|
|
||
|
my $fh;
|
||
|
my $mincss_path;
|
||
|
my $content;
|
||
|
my $min;
|
||
|
|
||
|
while (my $css_path = <>) {
|
||
|
chomp $css_path;
|
||
|
$mincss_path = $css_path =~ s/.css$/.min.css/r;
|
||
|
|
||
|
# Read CSS file.
|
||
|
open($fh, '<', $css_path) or die "Cannot open $css_path: $!";
|
||
|
$content = do { local $/; <$fh> };
|
||
|
close $fh;
|
||
|
|
||
|
# Minify CSS.
|
||
|
$min = $packer->minify(\$content, {compress => 'minify'});
|
||
|
|
||
|
# Write minified CSS down.
|
||
|
open($fh, '>', $mincss_path) or die "Cannot open $mincss_path: $!";
|
||
|
print $fh $min;
|
||
|
close $fh;
|
||
|
|
||
|
say "$css_path -> $mincss_path";
|
||
|
}
|