Juicy lil' bytes

Compress and Obfuscate Your JavaScript With This YUI Compressor Script

The YUI Compressor is a tool that was developed by Yahoo for JavaScript obfuscation and compression (and CSS compression). This shell script makes it simple to minify one or more JS files.

What it Does

According to the documentation:

It starts by analyzing the source JavaScript file to understand how it is structured. It then prints out the token stream, omitting as many white space characters as possible, and replacing all local symbols by a 1 (or 2, or 3) letter symbol wherever such a substitution is appropriate (in the face of evil features such as eval or with, the YUI Compressor takes a defensive approach by not obfuscating any of the scopes containing the evil statement).

Dependencies

The YUI Compressor is a Java .jar file and Java version 1.4+ is necessary to run it on your system.

Download YUI Compressor

Release 2.4.8 is available on the GitHub repository. Download and decompress the yuicompressor-2.4.8.jar file.

Set Up Your Shell Script

You can use the yuijs.sh script below (also available on my GitHub repository):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Modify YUI_PATH with the path to the yuicompressor jar file
YUI_PATH="path/to/yuicompressor-2.4.8.jar"

if [ $# -eq 0 ]; then
  echo "Please include the file path(s) for the file(s) that you would like to compress." 1>&2
  exit 1
fi

for file in "$@";
do
if [ -f "$file" ]; then
      java -jar "$YUI_PATH" -o "${file%%.*}-min.js" "$file"
      if (( $? )); then
          echo "$file was not able to be minified"
          exit 1
      else
          echo "$file was minified to ${file%%.*}-min.js"
      fi
  else
      echo "Unable to find the javascript file '$file'."
fi
done;
exit 0

Change the YUI_PATH variable to the actual path to your yuicompressor-2.4.8.jar file. Save the file and use the command chmod 744 yuijs.sh to make it executable.

Make a Bash Alias

Create an alias for your shell script so that you don’t have to type the full path to the script each time that you use it. Let’s say that you saved the yuijs.sh file on the path ~/scripts/yuijs.sh. Open your bash startup file (either .bash_profile or .bashrc) and enter the following line to create the alias minijs:

1
alias minijs="~/scripts/yuijs.sh"

To use this alias in your current shell process, use the source command on the bash startup file. Assuming that you placed this in the .bash_profile file and your working directory contains that file, your command is:

1
source .bash_profile

It is not necessary to run the source command again when you close and then reopen your terminal.

Compress Your JS Files with YUI Compressor

You’re all set. Now you can use the alias in any directory. Navigate to a directory that contains one or more JS files and go to town. It works with as many files as you’d like to throw at it. The file is compressed and obfuscated, and then saved as the original file name with a -min suffix. The original file is not modified and remains in the same directory. Hang on to the original because the obfuscated file is no longer human readable (intentionally so as to minimize file size).

Single File Example

1
minijs example.js

creates the file example-min.js.

Multiple Files Example

1
minijs example.js example2.js example3.js

creates the files example-min.js, example2-min.js, and example3-min.js.

All .js Files in Working Directory

1
minijs *.js