Yui Compressor for CSS and JS

Magento merged CSS compression

To reduce the size of merged Magento css files, you can setup scheduled script to do that for your magento project.

You will this library to get it working. Go to this page and download yuicompressor-2--.jar file.

Then create a directory outside your web root. Containing your file and bash script with the content below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env bash
readonly DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )";
cd $DIR;
set -e
set -u
set -o pipefail
standardIFS="$IFS"
IFS=$'\n\t'

readonly cssDir="${DIR}/../public/media/css_secure";

function optimiseImages() {
    for f in $(ls $cssDir -I "*original" -I "*minified")
    do
        if [[ ! -h $cssDir/$f ]];
        then
            # optimizes only if file is not a symbolic link
            echo "Optimising $f";
            cd $cssDir;
            cp $f $f.original
            java -Xss4m -jar ../../../merged_css_optimisation/yuicompressor-2.4.8.jar --type css $f > $f.minified && rm -f $f && ln -s $f.minified $f;
            echo "Optimisation done for - $f";
        fi
    done

}

function cleanOldOriginals() {
    for f in $(ls $cssDir | grep 'original\|minified')
    do
        minified=$(echo $f | sed 's/.original//g' | sed 's/.minified//g');

        if [[ ! -f $cssDir/$minified  ]];
        then
            echo "Minified file $minified not found, therefore removing original $f"
            # remove .original file
            rm -f $cssDir/$f;
            # remove .minified file
            rm -f $cssDir/$minified.minified
        fi
    done
}

optimiseImages
cleanOldOriginals

Then put the record in crontab and your script is good to do it's job which is compress and reduce the size of merged css files which are located in /media/css_secure/ directory.

The script will also keep original file copy. And do the cleanup of no longer used merged css files.