Return to Snippet

Revision: 15612
at July 11, 2009 22:13 by iloveitaly


Initial Code
#!/bin/bash

development=0

localAddress=`ifconfig  | grep -E 'inet.[0-9]' | grep -v '127.0.0.1' | awk '{print $2; exit 1}'`
localAddress='your.localhost'

coreCSSOutputFile='css/core.css'
coreJSOutputFile='js/core.js'

if [ $development == 1 ]; then
	cat > $coreJSOutputFile <<EOL
document.write('<script src="/includes/js/mootools.js" type="text/javascript" charset="utf-8"></script>');
document.write('<script src="/includes/js/validator.js" type="text/javascript" charset="utf-8"></script>');
document.write('<script src="/includes/js/datepicker.js" type="text/javascript" charset="utf-8"></script>');
EOL
	
	cat > $coreCSSOutputFile <<EOL
@import "lib/reset.css";
@import "lib/typography.css";
@import "lib/forms.css";
@import "lib/plugins/tabs.css";
@import "components/datepicker_dashboard.css";
@import "admin.css";
EOL
	
	# we dont want the server trying to serve the wrong data
	if [[ -f "$coreCSSOutputFile.gz" ]]; then
		rm "$coreCSSOutputFile.gz"
	fi
	
	if [[ -f "$coreJSOutputFile.gz" ]]; then
		rm "$coreJSOutputFile.gz"
	fi
	
	exit 0
fi

tempFile=`mktemp -t webcompress.xxx`

# Compress CSS

coreCSSFiles=(
	'css/lib/reset.css'
	'css/lib/forms.css'
	'css/lib/ie.css'
	'css/lib/typography.css'
	'css/screen.css'
	'css/components/datepicker_dashboard.css'
)

for file in ${coreCSSFiles[@]}; do
	# count /'s.. if we are father down in the directory replace ../../../ with ../
	if [[ `echo "$file" | perl -ne 'while(/\//g){++$count}; print "$count\n"'` > 2 ]]; then
		cat $file | sed 's/\.\.\/\.\.\/\.\.\//..\/..\//g' >> $tempFile
	else
		cat $file >> $tempFile
	fi
done

cat $tempFile > /out.css
cssoptimizer -lo $tempFile > $coreCSSOutputFile

# Compress JS

javascriptFiles=(
	js/mootools.js
	js/validator.js
	js/datepicker.js
)

# clear the tmp file
> $tempFile

for file in ${javascriptFiles[@]}; do
	cat $file >> $tempFile
done

java -jar /usr/bin/yuicompressor.jar --type js "$tempFile" > "$coreJSOutputFile"

# gzip compress!

gzip -f "$coreJSOutputFile"
gzip -f "$coreCSSOutputFile"

exit 0

Initial URL

                                

Initial Description
A custom compression script I use for auto compression and gzip compression of my css & files. Also allows for 'debug' mode (uncompressed). You simply include the core.css & core.js in your <head>

Initial Title
Compression Script for CSS & JS

Initial Tags
css, javascript, js, debug

Initial Language
Bash