Skip to content

Instantly share code, notes, and snippets.

@marians
Created July 16, 2010 11:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marians/478245 to your computer and use it in GitHub Desktop.
Save marians/478245 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
# This script concatenates and minifies a given set of CSS and JavaScript files
# so that only one JS and one CSS file are the result.
#
# In order to make it work for your project, configure the path settings and the
# file names both for input and output files.
#
# by Marian Steinbach <marian@sendung.de>
# Root path to act on (for JavaScript)
$js_root = '/var/www/s.sendung.de/js';
# Root path to act on (for CSS)
$css_root = '/var/www/s.sendung.de/css';
# Path to javascript source files.
# The order is important! Files are concatenated in the given order.
@js_files = (
'jquery-1.2.js',
'soundmanager2-jsmin.js',
'jquery_interface.js',
'player2.js',
'urchin.js',
'jquery.hotkeys.js'
);
# Path to CSS soruce files.
# Files are concatenated in the given order.
@css_files = (
'browser_screen.css',
'browser_screen_rating.css',
'player2.css'
);
# Absolute path to your output JS file.
$js_outputfile = $js_root.'/compact.js';
# Absolute path to your output CSS file.
$css_outputfile = $css_root.'/compact.css';
######## END OF CONFIGURATION #################################################
################################## JS Code concatenation ######################
use JavaScript::Minifier qw(minify);
unlink($js_outputfile);
open (OUTFILE, ">>$js_outputfile") or die($!);
foreach (@js_files) {
open(INFILE, $js_root.'/'.$_) or die($!);
my $out = minify(input => *INFILE);
close INFILE;
print OUTFILE $out."\n\n";
}
close OUTFILE;
################################## CSS Code concatenation #####################
unlink($css_outfile);
foreach (@css_files) {
system("cat ".$css_root.'/'.$_." >> ".$css_outfile);
}
open(CSS, "<$css_outputfile") || die ("Kann ".$css_outputfile." nicht lesen. $!");
$css = "";
while (<CSS>) {
$css .= $_;
}
close(CSS);
print 'CSS length before minification: '.length($css)." Bytes\n";
$css =~ s/^\/\*.*\*\/$//g;
$css =~ s/\n\n/\n/g;
$css =~ s/;\n/;/g;
$css =~ s/{\n/{/g;
$css =~ s/[[:blank:]]+{/{/g;
$css =~ s/{[[:blank:]]+/{/g;
$css =~ s/:[[:blank:]]+/:/g;
$css =~ s/;[[:blank:]]+/;/g;
$css =~ s/,[[:blank:]]+/,/g;
$css =~ s/^[[:blank:]]+//g;
$css =~ s/;}/}/g;
print 'CSS length after minification: '.length($css)." Bytes\n";
open(CSS, ">$css_outputfile") || die ("Kann ".$css_outputfile." nicht schreiben. $!");
print CSS $css;
close(CSS);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment