How to Compile JS & CSS to one file for best performance

To get the best performance on your website you need to reduce external call like js & css files.
I build little BASH script that compile all your files to one JS file and one CSS file.

We will use some tools like : Google Closure & dos2unix

1. Create “build” folder in your application.
2. Create the files “js_list.txt” and “css_list.txt” and write line by line all your js & css files.
3. Download Google Closure and copy the file “compiler.jar” to this folder.
4. Create the file “”

# (c) Copyright by Garry Lachman -
echo "" > full.js
echo "" > full.css
# JS
echo -e "\nBuild JS\n----------------"
while read line ; do
done < js_list.txt

for f in "${JS_FILES[@]}"
        cat "../js/$f" >> full.js
        echo ";" >> full.js
        echo -e "* $f"

echo -e "\nBuild CSS\n----------------"
while read line ; do
done < css_list.txt

for f in "${CSS_FILES[@]}"
        cat "../css/$f" >> full.css
        echo -e "* $f"

echo -e "\n"
echo -n "Compile JS"
java -jar compiler.jar --language_in ECMASCRIPT5 --compilation_level WHITESPACE_ONLY --js full.js --js_output_file full-compiled.js
echo " - Done"
echo -n "CSS Dos2Unix Format"
dos2unix full.css full.css
echo -e "Moving Compiled Files"
cp full.js ../js/full.js
cp full-compiled.js ../js/full-compiled.js
cp full.css ../css/full.css

5. I use the parent folders “js” and “css” but you can change it to your folder, just replace “../js/” & “../css/” to your path
6. Executing permission:

chmod +x

7. Run the code


You will see the output like:

[root@web-serv build]# ./

Build JS
* flash_wrapper/swfobject.js
* jquery.dropkick-1.0.0.js

Build CSS
* style.css
* components.css

Compile JS - Done
CSS Dos2Unix Formatdos2unix: converting file full.css to UNIX format ...
dos2unix: converting file full.css to UNIX format ...
Moving Compiled Files

Now check the files: full.css & full-compiled.js

Have fun :)

One thought on “How to Compile JS & CSS to one file for best performance

Leave a Reply

Your email address will not be published. Required fields are marked *


Protected with Antivirus