Recently someone asked me about it, so we have today’s small article.
To accomplish the task, we can use one of the compressors. I recommend YUI Compressor:
Download and unpack:
Run the command line, and we can compress. Attention: as the tool is a .jar file, we need Java environment in our system.
Call the .jar file with parameters input_file.js -o output_file.min.js.
For example (note the version in the name of compressor):
c:\yuicompressor-2.4.8>java -jar yuicompressor-2.4.8.jar gameFramework.js -o gf.min.js –charset utf-8
We get the output file.
To avoid any problems with the encoding, I added the flag charset.
Sample code – source and minified version to download:
CSS files are compressed in the same way.
Compressor recognizes the file type by extension; we can also use the –type flag:
extension of the input file name (.js or .css) This option is required
if no input file has been specified. Otherwise, this option is only
required if the input file extension is neither ‘js’ nor ‘css’.
There are also on-line tools:
Everyone has their own preferences. The main thing is to complete the task quickly and efficiently.
Couple words about file versions. If we include .js / .css files to the project (not necessarily compressed), a good trick is appending version to its name. Versions of .js and .css files can be set in application configuration.
– CSS: … href=”my.css?v=123″ …
– JS: … src=”myjs.min.js?v=110 …
This will help to avoid caching of the files by browser, and thus when the user of our application runs the latest version, he will get also the latest version of .js / .css files for sure.
Open-source libraries usually are available both in source and compressed version. In case of our code we can easily do the same. Files will get less weight, and moreover no one will look at our code – at least not so easy.
> java -jar yuicompressor.jar thank_you.js -o thank_you.min.js