Last updated on August 6, 2020 by Dan Nanni
For web designers, publishers and developers, one cannot stress enough the importance of keeping web pages or web applications as lightweight as possible. There are various design strategies and client/server-side techniques that can be exploited to achieve this goal.
The YUI Compressor is written in Java, and its pre-built JAR file (as well as its source code) is available for download. Here we are going to use the JAR executable.
First, install Java run-time, which is required to run YUI Compressor.
Download and install YUI Compressor as follows.
$ sudo mkdir -p /usr/share/java $ sudo wget https://github.com/yui/yuicompressor/releases/download/v2.4.8/yuicompressor-2.4.8.jar -P /usr/share/java
Create a start-up script named
yc, with the following content.
$ sudo vi /usr/bin/yc
#!/bin/sh java -jar /usr/share/java/yuicompressor-2.4.8.jar "[email protected]"
$ sudo chmod +x /usr/bin/yc
Now you can use
yc command to launch YUI Compressor.
YUICompressor Version: 2.4.8 Usage: java -jar yuicompressor-2.4.8.jar [options] [input file] Global Options -V, --version Print version information -h, --help Displays this information --type
To compress or minify a CSS file, use the following command:
$ yc default.css -o default-min.css
Check how much size is reduced before and after CSS compression:
$ wc default.css default-min.css
756 1097 14007 default.css 0 163 5745 default-min.css
In this example, the total byte count for the original CSS file is reduced from 14KB to 5.7KB after compression.
A compressed/minified CSS file looks like the following:
$ yc sample.js -o sample-min.js
Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.